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 17 package android.net; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotEquals; 22 import static org.junit.Assert.assertTrue; 23 import static org.junit.Assert.fail; 24 25 import android.os.Build; 26 import android.platform.test.annotations.AppModeFull; 27 28 import androidx.test.filters.SmallTest; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import com.android.testutils.DevSdkIgnoreRule; 32 import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter; 33 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; 34 35 import org.junit.Rule; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 import java.io.File; 40 import java.io.FileDescriptor; 41 import java.io.FileInputStream; 42 import java.net.DatagramSocket; 43 import java.net.Inet6Address; 44 import java.net.InetAddress; 45 import java.net.SocketException; 46 47 @RunWith(AndroidJUnit4.class) 48 @SmallTest 49 public class NetworkTest { 50 final Network mNetwork = new Network(99); 51 52 @Rule 53 public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule(); 54 55 @Test testBindSocketOfInvalidFdThrows()56 public void testBindSocketOfInvalidFdThrows() throws Exception { 57 58 final FileDescriptor fd = new FileDescriptor(); 59 assertFalse(fd.valid()); 60 61 try { 62 mNetwork.bindSocket(fd); 63 fail("SocketException not thrown"); 64 } catch (SocketException expected) {} 65 } 66 67 @Test testBindSocketOfNonSocketFdThrows()68 public void testBindSocketOfNonSocketFdThrows() throws Exception { 69 final File devNull = new File("/dev/null"); 70 assertTrue(devNull.canRead()); 71 72 final FileInputStream fis = new FileInputStream(devNull); 73 assertTrue(null != fis.getFD()); 74 assertTrue(fis.getFD().valid()); 75 76 try { 77 mNetwork.bindSocket(fis.getFD()); 78 fail("SocketException not thrown"); 79 } catch (SocketException expected) {} 80 } 81 82 @Test 83 @AppModeFull(reason = "Socket cannot bind in instant app mode") testBindSocketOfConnectedDatagramSocketThrows()84 public void testBindSocketOfConnectedDatagramSocketThrows() throws Exception { 85 final DatagramSocket mDgramSocket = new DatagramSocket(0, (InetAddress) Inet6Address.ANY); 86 mDgramSocket.connect((InetAddress) Inet6Address.LOOPBACK, 53); 87 assertTrue(mDgramSocket.isConnected()); 88 89 try { 90 mNetwork.bindSocket(mDgramSocket); 91 fail("SocketException not thrown"); 92 } catch (SocketException expected) {} 93 } 94 95 @Test testBindSocketOfLocalSocketThrows()96 public void testBindSocketOfLocalSocketThrows() throws Exception { 97 final LocalSocket mLocalClient = new LocalSocket(); 98 mLocalClient.bind(new LocalSocketAddress("testClient")); 99 assertTrue(mLocalClient.getFileDescriptor().valid()); 100 101 try { 102 mNetwork.bindSocket(mLocalClient.getFileDescriptor()); 103 fail("SocketException not thrown"); 104 } catch (SocketException expected) {} 105 106 final LocalServerSocket mLocalServer = new LocalServerSocket("testServer"); 107 mLocalClient.connect(mLocalServer.getLocalSocketAddress()); 108 assertTrue(mLocalClient.isConnected()); 109 110 try { 111 mNetwork.bindSocket(mLocalClient.getFileDescriptor()); 112 fail("SocketException not thrown"); 113 } catch (SocketException expected) {} 114 } 115 116 @Test testZeroIsObviousForDebugging()117 public void testZeroIsObviousForDebugging() { 118 Network zero = new Network(0); 119 assertEquals(0, zero.hashCode()); 120 assertEquals(0, zero.getNetworkHandle()); 121 assertEquals("0", zero.toString()); 122 } 123 124 @Test testGetNetworkHandle()125 public void testGetNetworkHandle() { 126 Network one = new Network(1); 127 Network two = new Network(2); 128 Network three = new Network(3); 129 130 // None of the hashcodes are zero. 131 assertNotEquals(0, one.hashCode()); 132 assertNotEquals(0, two.hashCode()); 133 assertNotEquals(0, three.hashCode()); 134 135 // All the hashcodes are distinct. 136 assertNotEquals(one.hashCode(), two.hashCode()); 137 assertNotEquals(one.hashCode(), three.hashCode()); 138 assertNotEquals(two.hashCode(), three.hashCode()); 139 140 // None of the handles are zero. 141 assertNotEquals(0, one.getNetworkHandle()); 142 assertNotEquals(0, two.getNetworkHandle()); 143 assertNotEquals(0, three.getNetworkHandle()); 144 145 // All the handles are distinct. 146 assertNotEquals(one.getNetworkHandle(), two.getNetworkHandle()); 147 assertNotEquals(one.getNetworkHandle(), three.getNetworkHandle()); 148 assertNotEquals(two.getNetworkHandle(), three.getNetworkHandle()); 149 150 // The handles are not equal to the hashcodes. 151 assertNotEquals(one.hashCode(), one.getNetworkHandle()); 152 assertNotEquals(two.hashCode(), two.getNetworkHandle()); 153 assertNotEquals(three.hashCode(), three.getNetworkHandle()); 154 155 // Adjust as necessary to test an implementation's specific constants. 156 // When running with runtest, "adb logcat -s TestRunner" can be useful. 157 assertEquals(7700664333L, one.getNetworkHandle()); 158 assertEquals(11995631629L, two.getNetworkHandle()); 159 assertEquals(16290598925L, three.getNetworkHandle()); 160 } 161 162 // getNetId() did not exist in Q 163 @Test @IgnoreUpTo(Build.VERSION_CODES.Q) testGetNetId()164 public void testGetNetId() { 165 assertEquals(1234, new Network(1234).getNetId()); 166 assertEquals(2345, new Network(2345, true).getNetId()); 167 } 168 169 @Test testFromNetworkHandle()170 public void testFromNetworkHandle() { 171 final Network network = new Network(1234); 172 assertEquals(network.netId, Network.fromNetworkHandle(network.getNetworkHandle()).netId); 173 } 174 175 // Parsing private DNS bypassing handle was not supported until S 176 @Test @IgnoreUpTo(Build.VERSION_CODES.R) testFromNetworkHandlePrivateDnsBypass_S()177 public void testFromNetworkHandlePrivateDnsBypass_S() { 178 final Network network = new Network(1234, true); 179 180 final Network recreatedNetwork = Network.fromNetworkHandle(network.getNetworkHandle()); 181 assertEquals(network.netId, recreatedNetwork.netId); 182 assertEquals(network.getNetIdForResolv(), recreatedNetwork.getNetIdForResolv()); 183 } 184 185 @Test @IgnoreAfter(Build.VERSION_CODES.R) testFromNetworkHandlePrivateDnsBypass_R()186 public void testFromNetworkHandlePrivateDnsBypass_R() { 187 final Network network = new Network(1234, true); 188 189 final Network recreatedNetwork = Network.fromNetworkHandle(network.getNetworkHandle()); 190 assertEquals(network.netId, recreatedNetwork.netId); 191 // Until R included, fromNetworkHandle would not parse the private DNS bypass flag 192 assertEquals(network.netId, recreatedNetwork.getNetIdForResolv()); 193 } 194 195 @Test testGetPrivateDnsBypassingCopy()196 public void testGetPrivateDnsBypassingCopy() { 197 final Network copy = mNetwork.getPrivateDnsBypassingCopy(); 198 assertEquals(mNetwork.netId, copy.netId); 199 assertNotEquals(copy.netId, copy.getNetIdForResolv()); 200 assertNotEquals(mNetwork.getNetIdForResolv(), copy.getNetIdForResolv()); 201 } 202 } 203