1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.net; 18 19import static android.test.MoreAsserts.assertNotEqual; 20 21import android.net.LocalServerSocket; 22import android.net.LocalSocket; 23import android.net.LocalSocketAddress; 24import android.net.Network; 25import android.test.suitebuilder.annotation.SmallTest; 26 27import java.io.File; 28import java.io.FileDescriptor; 29import java.io.FileInputStream; 30import java.io.IOException; 31import java.net.DatagramSocket; 32import java.net.InetAddress; 33import java.net.Inet6Address; 34import java.net.SocketException; 35 36import junit.framework.TestCase; 37 38public class NetworkTest extends TestCase { 39 final Network mNetwork = new Network(99); 40 41 @SmallTest 42 public void testBindSocketOfInvalidFdThrows() throws Exception { 43 44 final FileDescriptor fd = new FileDescriptor(); 45 assertFalse(fd.valid()); 46 47 try { 48 mNetwork.bindSocket(fd); 49 fail("SocketException not thrown"); 50 } catch (SocketException expected) {} 51 } 52 53 @SmallTest 54 public void testBindSocketOfNonSocketFdThrows() throws Exception { 55 final File devNull = new File("/dev/null"); 56 assertTrue(devNull.canRead()); 57 58 final FileInputStream fis = new FileInputStream(devNull); 59 assertTrue(null != fis.getFD()); 60 assertTrue(fis.getFD().valid()); 61 62 try { 63 mNetwork.bindSocket(fis.getFD()); 64 fail("SocketException not thrown"); 65 } catch (SocketException expected) {} 66 } 67 68 @SmallTest 69 public void testBindSocketOfConnectedDatagramSocketThrows() throws Exception { 70 final DatagramSocket mDgramSocket = new DatagramSocket(0, (InetAddress) Inet6Address.ANY); 71 mDgramSocket.connect((InetAddress) Inet6Address.LOOPBACK, 53); 72 assertTrue(mDgramSocket.isConnected()); 73 74 try { 75 mNetwork.bindSocket(mDgramSocket); 76 fail("SocketException not thrown"); 77 } catch (SocketException expected) {} 78 } 79 80 @SmallTest 81 public void testBindSocketOfLocalSocketThrows() throws Exception { 82 final LocalSocket mLocalClient = new LocalSocket(); 83 mLocalClient.bind(new LocalSocketAddress("testClient")); 84 assertTrue(mLocalClient.getFileDescriptor().valid()); 85 86 try { 87 mNetwork.bindSocket(mLocalClient.getFileDescriptor()); 88 fail("SocketException not thrown"); 89 } catch (SocketException expected) {} 90 91 final LocalServerSocket mLocalServer = new LocalServerSocket("testServer"); 92 mLocalClient.connect(mLocalServer.getLocalSocketAddress()); 93 assertTrue(mLocalClient.isConnected()); 94 95 try { 96 mNetwork.bindSocket(mLocalClient.getFileDescriptor()); 97 fail("SocketException not thrown"); 98 } catch (SocketException expected) {} 99 } 100 101 @SmallTest 102 public void testZeroIsObviousForDebugging() { 103 Network zero = new Network(0); 104 assertEquals(0, zero.hashCode()); 105 assertEquals(0, zero.getNetworkHandle()); 106 assertEquals("0", zero.toString()); 107 } 108 109 @SmallTest 110 public void testGetNetworkHandle() { 111 Network one = new Network(1); 112 Network two = new Network(2); 113 Network three = new Network(3); 114 115 // None of the hashcodes are zero. 116 assertNotEqual(0, one.hashCode()); 117 assertNotEqual(0, two.hashCode()); 118 assertNotEqual(0, three.hashCode()); 119 120 // All the hashcodes are distinct. 121 assertNotEqual(one.hashCode(), two.hashCode()); 122 assertNotEqual(one.hashCode(), three.hashCode()); 123 assertNotEqual(two.hashCode(), three.hashCode()); 124 125 // None of the handles are zero. 126 assertNotEqual(0, one.getNetworkHandle()); 127 assertNotEqual(0, two.getNetworkHandle()); 128 assertNotEqual(0, three.getNetworkHandle()); 129 130 // All the handles are distinct. 131 assertNotEqual(one.getNetworkHandle(), two.getNetworkHandle()); 132 assertNotEqual(one.getNetworkHandle(), three.getNetworkHandle()); 133 assertNotEqual(two.getNetworkHandle(), three.getNetworkHandle()); 134 135 // The handles are not equal to the hashcodes. 136 assertNotEqual(one.hashCode(), one.getNetworkHandle()); 137 assertNotEqual(two.hashCode(), two.getNetworkHandle()); 138 assertNotEqual(three.hashCode(), three.getNetworkHandle()); 139 140 // Adjust as necessary to test an implementation's specific constants. 141 // When running with runtest, "adb logcat -s TestRunner" can be useful. 142 assertEquals(4311403230L, one.getNetworkHandle()); 143 assertEquals(8606370526L, two.getNetworkHandle()); 144 assertEquals(12901337822L, three.getNetworkHandle()); 145 } 146} 147