blob: b5f23bf19a3c0fafbb7e848dc3df9a9994b86f71 [file] [log] [blame]
Remi NGUYEN VAN678277c2021-05-11 13:37:06 +00001/*
2 * Copyright (C) 2014 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 org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNotEquals;
22import static org.junit.Assert.assertNotNull;
23import static org.junit.Assert.assertNull;
24import static org.junit.Assert.assertTrue;
25
26import android.os.Parcel;
27
28import androidx.test.filters.SmallTest;
29import androidx.test.runner.AndroidJUnit4;
30
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import java.net.InetAddress;
35import java.util.ArrayList;
36import java.util.HashSet;
37import java.util.List;
38
39@RunWith(AndroidJUnit4.class)
40@SmallTest
41public class StaticIpConfigurationTest {
42
43 private static final String ADDRSTR = "192.0.2.2/25";
44 private static final LinkAddress ADDR = new LinkAddress(ADDRSTR);
45 private static final InetAddress GATEWAY = IpAddress("192.0.2.1");
46 private static final InetAddress OFFLINKGATEWAY = IpAddress("192.0.2.129");
47 private static final InetAddress DNS1 = IpAddress("8.8.8.8");
48 private static final InetAddress DNS2 = IpAddress("8.8.4.4");
49 private static final InetAddress DNS3 = IpAddress("4.2.2.2");
50 private static final String IFACE = "eth0";
51 private static final String FAKE_DOMAINS = "google.com";
52
53 private static InetAddress IpAddress(String addr) {
54 return InetAddress.parseNumericAddress(addr);
55 }
56
57 private void checkEmpty(StaticIpConfiguration s) {
58 assertNull(s.ipAddress);
59 assertNull(s.gateway);
60 assertNull(s.domains);
61 assertEquals(0, s.dnsServers.size());
62 }
63
64 private StaticIpConfiguration makeTestObject() {
65 StaticIpConfiguration s = new StaticIpConfiguration();
66 s.ipAddress = ADDR;
67 s.gateway = GATEWAY;
68 s.dnsServers.add(DNS1);
69 s.dnsServers.add(DNS2);
70 s.dnsServers.add(DNS3);
71 s.domains = FAKE_DOMAINS;
72 return s;
73 }
74
75 @Test
76 public void testConstructor() {
77 StaticIpConfiguration s = new StaticIpConfiguration();
78 checkEmpty(s);
79 }
80
81 @Test
82 public void testCopyAndClear() {
83 StaticIpConfiguration empty = new StaticIpConfiguration((StaticIpConfiguration) null);
84 checkEmpty(empty);
85
86 StaticIpConfiguration s1 = makeTestObject();
87 StaticIpConfiguration s2 = new StaticIpConfiguration(s1);
88 assertEquals(s1, s2);
89 s2.clear();
90 assertEquals(empty, s2);
91 }
92
93 @Test
94 public void testHashCodeAndEquals() {
95 HashSet<Integer> hashCodes = new HashSet();
96 hashCodes.add(0);
97
98 StaticIpConfiguration s = new StaticIpConfiguration();
99 // Check that this hash code is nonzero and different from all the ones seen so far.
100 assertTrue(hashCodes.add(s.hashCode()));
101
102 s.ipAddress = ADDR;
103 assertTrue(hashCodes.add(s.hashCode()));
104
105 s.gateway = GATEWAY;
106 assertTrue(hashCodes.add(s.hashCode()));
107
108 s.dnsServers.add(DNS1);
109 assertTrue(hashCodes.add(s.hashCode()));
110
111 s.dnsServers.add(DNS2);
112 assertTrue(hashCodes.add(s.hashCode()));
113
114 s.dnsServers.add(DNS3);
115 assertTrue(hashCodes.add(s.hashCode()));
116
117 s.domains = "example.com";
118 assertTrue(hashCodes.add(s.hashCode()));
119
120 assertFalse(s.equals(null));
121 assertEquals(s, s);
122
123 StaticIpConfiguration s2 = new StaticIpConfiguration(s);
124 assertEquals(s, s2);
125
126 s.ipAddress = new LinkAddress(DNS1, 32);
127 assertNotEquals(s, s2);
128
129 s2 = new StaticIpConfiguration(s);
130 s.domains = "foo";
131 assertNotEquals(s, s2);
132
133 s2 = new StaticIpConfiguration(s);
134 s.gateway = DNS2;
135 assertNotEquals(s, s2);
136
137 s2 = new StaticIpConfiguration(s);
138 s.dnsServers.add(DNS3);
139 assertNotEquals(s, s2);
140 }
141
142 @Test
143 public void testToLinkProperties() {
144 LinkProperties expected = new LinkProperties();
145 expected.setInterfaceName(IFACE);
146
147 StaticIpConfiguration s = new StaticIpConfiguration();
148 assertEquals(expected, s.toLinkProperties(IFACE));
149
150 final RouteInfo connectedRoute = new RouteInfo(new IpPrefix(ADDRSTR), null, IFACE);
151 s.ipAddress = ADDR;
152 expected.addLinkAddress(ADDR);
153 expected.addRoute(connectedRoute);
154 assertEquals(expected, s.toLinkProperties(IFACE));
155
156 s.gateway = GATEWAY;
157 RouteInfo defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), GATEWAY, IFACE);
158 expected.addRoute(defaultRoute);
159 assertEquals(expected, s.toLinkProperties(IFACE));
160
161 s.gateway = OFFLINKGATEWAY;
162 expected.removeRoute(defaultRoute);
163 defaultRoute = new RouteInfo(new IpPrefix("0.0.0.0/0"), OFFLINKGATEWAY, IFACE);
164 expected.addRoute(defaultRoute);
165
166 RouteInfo gatewayRoute = new RouteInfo(new IpPrefix("192.0.2.129/32"), null, IFACE);
167 expected.addRoute(gatewayRoute);
168 assertEquals(expected, s.toLinkProperties(IFACE));
169
170 s.dnsServers.add(DNS1);
171 expected.addDnsServer(DNS1);
172 assertEquals(expected, s.toLinkProperties(IFACE));
173
174 s.dnsServers.add(DNS2);
175 s.dnsServers.add(DNS3);
176 expected.addDnsServer(DNS2);
177 expected.addDnsServer(DNS3);
178 assertEquals(expected, s.toLinkProperties(IFACE));
179
180 s.domains = FAKE_DOMAINS;
181 expected.setDomains(FAKE_DOMAINS);
182 assertEquals(expected, s.toLinkProperties(IFACE));
183
184 s.gateway = null;
185 expected.removeRoute(defaultRoute);
186 expected.removeRoute(gatewayRoute);
187 assertEquals(expected, s.toLinkProperties(IFACE));
188
189 // Without knowing the IP address, we don't have a directly-connected route, so we can't
190 // tell if the gateway is off-link or not and we don't add a host route. This isn't a real
191 // configuration, but we should at least not crash.
192 s.gateway = OFFLINKGATEWAY;
193 s.ipAddress = null;
194 expected.removeLinkAddress(ADDR);
195 expected.removeRoute(connectedRoute);
196 expected.addRoute(defaultRoute);
197 assertEquals(expected, s.toLinkProperties(IFACE));
198 }
199
200 private StaticIpConfiguration passThroughParcel(StaticIpConfiguration s) {
201 Parcel p = Parcel.obtain();
202 StaticIpConfiguration s2 = null;
203 try {
204 s.writeToParcel(p, 0);
205 p.setDataPosition(0);
206 s2 = StaticIpConfiguration.readFromParcel(p);
207 } finally {
208 p.recycle();
209 }
210 assertNotNull(s2);
211 return s2;
212 }
213
214 @Test
215 public void testParceling() {
216 StaticIpConfiguration s = makeTestObject();
217 StaticIpConfiguration s2 = passThroughParcel(s);
218 assertEquals(s, s2);
219 }
220
221 @Test
222 public void testBuilder() {
223 final ArrayList<InetAddress> dnsServers = new ArrayList<>();
224 dnsServers.add(DNS1);
225
226 final StaticIpConfiguration s = new StaticIpConfiguration.Builder()
227 .setIpAddress(ADDR)
228 .setGateway(GATEWAY)
229 .setDomains(FAKE_DOMAINS)
230 .setDnsServers(dnsServers)
231 .build();
232
233 assertEquals(s.ipAddress, s.getIpAddress());
234 assertEquals(ADDR, s.getIpAddress());
235 assertEquals(s.gateway, s.getGateway());
236 assertEquals(GATEWAY, s.getGateway());
237 assertEquals(s.domains, s.getDomains());
238 assertEquals(FAKE_DOMAINS, s.getDomains());
239 assertTrue(s.dnsServers.equals(s.getDnsServers()));
240 assertEquals(1, s.getDnsServers().size());
241 assertEquals(DNS1, s.getDnsServers().get(0));
242 }
243
244 @Test
245 public void testAddDnsServers() {
246 final StaticIpConfiguration s = new StaticIpConfiguration((StaticIpConfiguration) null);
247 checkEmpty(s);
248
249 s.addDnsServer(DNS1);
250 assertEquals(1, s.getDnsServers().size());
251 assertEquals(DNS1, s.getDnsServers().get(0));
252
253 s.addDnsServer(DNS2);
254 s.addDnsServer(DNS3);
255 assertEquals(3, s.getDnsServers().size());
256 assertEquals(DNS2, s.getDnsServers().get(1));
257 assertEquals(DNS3, s.getDnsServers().get(2));
258 }
259
260 @Test
261 public void testGetRoutes() {
262 final StaticIpConfiguration s = makeTestObject();
263 final List<RouteInfo> routeInfoList = s.getRoutes(IFACE);
264
265 assertEquals(2, routeInfoList.size());
266 assertEquals(new RouteInfo(ADDR, (InetAddress) null, IFACE), routeInfoList.get(0));
267 assertEquals(new RouteInfo((IpPrefix) null, GATEWAY, IFACE), routeInfoList.get(1));
268 }
269}