147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller/*
247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * Copyright (C) 2014 The Android Open Source Project
347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller *
447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * Licensed under the Apache License, Version 2.0 (the "License");
547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * you may not use this file except in compliance with the License.
647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * You may obtain a copy of the License at
747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller *
847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller *      http://www.apache.org/licenses/LICENSE-2.0
947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller *
1047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * Unless required by applicable law or agreed to in writing, software
1147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * distributed under the License is distributed on an "AS IS" BASIS,
1247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * See the License for the specific language governing permissions and
1447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller * limitations under the License.
1547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller */
1647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller
1747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fullerpackage libcore.java.net;
1847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller
1947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fullerimport junit.framework.TestCase;
2047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller
2147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fullerimport java.net.DatagramSocket;
2247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fullerimport java.net.InetSocketAddress;
2347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller
2447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fullerpublic class DatagramSocketTest extends TestCase {
2547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller
2647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller  public void testInitialState() throws Exception {
2747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    DatagramSocket ds = new DatagramSocket();
2847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    try {
2947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertTrue(ds.isBound());
3047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertTrue(ds.getBroadcast()); // The RI starts DatagramSocket in broadcast mode.
3147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertFalse(ds.isClosed());
3247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertFalse(ds.isConnected());
3347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertTrue(ds.getLocalPort() > 0);
3447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertTrue(ds.getLocalAddress().isAnyLocalAddress());
3547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      InetSocketAddress socketAddress = (InetSocketAddress) ds.getLocalSocketAddress();
3647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertEquals(ds.getLocalPort(), socketAddress.getPort());
3747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertEquals(ds.getLocalAddress(), socketAddress.getAddress());
3847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertNull(ds.getInetAddress());
3947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertEquals(-1, ds.getPort());
4047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertNull(ds.getRemoteSocketAddress());
4147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertFalse(ds.getReuseAddress());
4247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      assertNull(ds.getChannel());
4347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    } finally {
4447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller      ds.close();
4547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    }
4647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller  }
4747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller
4847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller  public void testStateAfterClose() throws Exception {
4947ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    DatagramSocket ds = new DatagramSocket();
5047ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    ds.close();
5147ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    assertTrue(ds.isBound());
5247ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    assertTrue(ds.isClosed());
5347ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    assertFalse(ds.isConnected());
5447ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    assertNull(ds.getLocalAddress());
5547ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    assertEquals(-1, ds.getLocalPort());
5647ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller    assertNull(ds.getLocalSocketAddress());
5747ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller  }
5847ae0b5a1d96c8030e0963ccc5b44c3ee66aaec3Neil Fuller}
59