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