blob: 2cf3cf9c11da43072e5aaa48f8a5426f85cc9619 [file] [log] [blame]
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +09001/*
2 * Copyright (C) 2013 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
Hugo Benichi3054e102017-08-08 13:06:04 +090019import static android.system.OsConstants.IFA_F_DADFAILED;
20import static android.system.OsConstants.IFA_F_DEPRECATED;
21import static android.system.OsConstants.IFA_F_OPTIMISTIC;
22import static android.system.OsConstants.IFA_F_PERMANENT;
23import static android.system.OsConstants.IFA_F_TEMPORARY;
24import static android.system.OsConstants.IFA_F_TENTATIVE;
25import static android.system.OsConstants.RT_SCOPE_HOST;
26import static android.system.OsConstants.RT_SCOPE_LINK;
27import static android.system.OsConstants.RT_SCOPE_SITE;
28import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
Brett Chabotab11bf12019-03-04 14:14:56 -080029
Chalard Jeane47850d2020-06-26 00:41:26 +090030import static com.android.testutils.MiscAsserts.assertEqualBothWays;
31import static com.android.testutils.MiscAsserts.assertFieldCountEquals;
32import static com.android.testutils.MiscAsserts.assertNotEqualEitherWay;
33import static com.android.testutils.ParcelUtils.assertParcelingIsLossless;
Chalard Jean64802872019-05-30 17:11:14 +090034
Hugo Benichi3054e102017-08-08 13:06:04 +090035import static org.junit.Assert.assertEquals;
36import static org.junit.Assert.assertFalse;
37import static org.junit.Assert.assertNotEquals;
Hugo Benichi3054e102017-08-08 13:06:04 +090038import static org.junit.Assert.assertTrue;
39import static org.junit.Assert.fail;
40
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +090041import android.os.Build;
Jack Yu1fe4b302020-01-17 15:28:50 -080042import android.os.SystemClock;
43
Brett Chabotab11bf12019-03-04 14:14:56 -080044import androidx.test.filters.SmallTest;
45import androidx.test.runner.AndroidJUnit4;
46
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +090047import com.android.testutils.DevSdkIgnoreRule;
48import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter;
49import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
50
51import org.junit.Rule;
Brett Chabotab11bf12019-03-04 14:14:56 -080052import org.junit.Test;
53import org.junit.runner.RunWith;
54
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090055import java.net.Inet4Address;
paulhucbe73812021-03-03 22:15:11 +080056import java.net.Inet6Address;
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090057import java.net.InetAddress;
58import java.net.InterfaceAddress;
59import java.net.NetworkInterface;
60import java.net.SocketException;
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +090061import java.util.Arrays;
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090062import java.util.List;
63
Hugo Benichi3054e102017-08-08 13:06:04 +090064@RunWith(AndroidJUnit4.class)
65@SmallTest
66public class LinkAddressTest {
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +090067 @Rule
68 public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule();
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090069
70 private static final String V4 = "192.0.2.1";
71 private static final String V6 = "2001:db8::1";
Serik Beketayev88bf9842020-12-06 22:31:23 -080072 private static final InetAddress V4_ADDRESS = InetAddresses.parseNumericAddress(V4);
73 private static final InetAddress V6_ADDRESS = InetAddresses.parseNumericAddress(V6);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090074
Hugo Benichi3054e102017-08-08 13:06:04 +090075 @Test
Lorenzo Colittif3e0e302014-06-13 16:55:54 +090076 public void testConstants() {
77 // RT_SCOPE_UNIVERSE = 0, but all the other constants should be nonzero.
Hugo Benichi3054e102017-08-08 13:06:04 +090078 assertNotEquals(0, RT_SCOPE_HOST);
79 assertNotEquals(0, RT_SCOPE_LINK);
80 assertNotEquals(0, RT_SCOPE_SITE);
Lorenzo Colittif3e0e302014-06-13 16:55:54 +090081
Hugo Benichi3054e102017-08-08 13:06:04 +090082 assertNotEquals(0, IFA_F_DEPRECATED);
83 assertNotEquals(0, IFA_F_PERMANENT);
84 assertNotEquals(0, IFA_F_TENTATIVE);
Lorenzo Colittif3e0e302014-06-13 16:55:54 +090085 }
86
Hugo Benichi3054e102017-08-08 13:06:04 +090087 @Test
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090088 public void testConstructors() throws SocketException {
89 LinkAddress address;
90
91 // Valid addresses work as expected.
92 address = new LinkAddress(V4_ADDRESS, 25);
93 assertEquals(V4_ADDRESS, address.getAddress());
Lorenzo Colitti48a7da02014-06-09 22:58:46 +090094 assertEquals(25, address.getPrefixLength());
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +090095 assertEquals(0, address.getFlags());
96 assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
paulhu9bb04802019-03-08 16:35:20 +080097 assertTrue(address.isIpv4());
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +090098
99 address = new LinkAddress(V6_ADDRESS, 127);
100 assertEquals(V6_ADDRESS, address.getAddress());
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900101 assertEquals(127, address.getPrefixLength());
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900102 assertEquals(0, address.getFlags());
103 assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
paulhu9bb04802019-03-08 16:35:20 +0800104 assertTrue(address.isIpv6());
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900105
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900106 // Nonsensical flags/scopes or combinations thereof are acceptable.
107 address = new LinkAddress(V6 + "/64", IFA_F_DEPRECATED | IFA_F_PERMANENT, RT_SCOPE_LINK);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900108 assertEquals(V6_ADDRESS, address.getAddress());
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900109 assertEquals(64, address.getPrefixLength());
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900110 assertEquals(IFA_F_DEPRECATED | IFA_F_PERMANENT, address.getFlags());
111 assertEquals(RT_SCOPE_LINK, address.getScope());
paulhu9bb04802019-03-08 16:35:20 +0800112 assertTrue(address.isIpv6());
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900113
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900114 address = new LinkAddress(V4 + "/23", 123, 456);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900115 assertEquals(V4_ADDRESS, address.getAddress());
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900116 assertEquals(23, address.getPrefixLength());
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900117 assertEquals(123, address.getFlags());
118 assertEquals(456, address.getScope());
paulhu9bb04802019-03-08 16:35:20 +0800119 assertTrue(address.isIpv4());
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900120
paulhucbe73812021-03-03 22:15:11 +0800121 address = new LinkAddress("/64", 1 /* flags */, 2 /* scope */);
122 assertEquals(Inet6Address.LOOPBACK, address.getAddress());
123 assertEquals(64, address.getPrefixLength());
124 assertEquals(1, address.getFlags());
125 assertEquals(2, address.getScope());
126 assertTrue(address.isIpv6());
127
128 address = new LinkAddress("[2001:db8::123]/64", 3 /* flags */, 4 /* scope */);
129 assertEquals(InetAddresses.parseNumericAddress("2001:db8::123"), address.getAddress());
130 assertEquals(64, address.getPrefixLength());
131 assertEquals(3, address.getFlags());
132 assertEquals(4, address.getScope());
133 assertTrue(address.isIpv6());
134
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900135 // InterfaceAddress doesn't have a constructor. Fetch some from an interface.
136 List<InterfaceAddress> addrs = NetworkInterface.getByName("lo").getInterfaceAddresses();
137
138 // We expect to find 127.0.0.1/8 and ::1/128, in any order.
139 LinkAddress ipv4Loopback, ipv6Loopback;
140 assertEquals(2, addrs.size());
141 if (addrs.get(0).getAddress() instanceof Inet4Address) {
142 ipv4Loopback = new LinkAddress(addrs.get(0));
143 ipv6Loopback = new LinkAddress(addrs.get(1));
144 } else {
145 ipv4Loopback = new LinkAddress(addrs.get(1));
146 ipv6Loopback = new LinkAddress(addrs.get(0));
147 }
148
Serik Beketayev88bf9842020-12-06 22:31:23 -0800149 assertEquals(InetAddresses.parseNumericAddress("127.0.0.1"), ipv4Loopback.getAddress());
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900150 assertEquals(8, ipv4Loopback.getPrefixLength());
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900151
Serik Beketayev88bf9842020-12-06 22:31:23 -0800152 assertEquals(InetAddresses.parseNumericAddress("::1"), ipv6Loopback.getAddress());
Lorenzo Colitti48a7da02014-06-09 22:58:46 +0900153 assertEquals(128, ipv6Loopback.getPrefixLength());
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900154
155 // Null addresses are rejected.
156 try {
157 address = new LinkAddress(null, 24);
158 fail("Null InetAddress should cause IllegalArgumentException");
159 } catch(IllegalArgumentException expected) {}
160
161 try {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900162 address = new LinkAddress((String) null, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900163 fail("Null string should cause IllegalArgumentException");
164 } catch(IllegalArgumentException expected) {}
165
166 try {
167 address = new LinkAddress((InterfaceAddress) null);
168 fail("Null string should cause NullPointerException");
169 } catch(NullPointerException expected) {}
170
171 // Invalid prefix lengths are rejected.
172 try {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900173 address = new LinkAddress(V4_ADDRESS, -1);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900174 fail("Negative IPv4 prefix length should cause IllegalArgumentException");
175 } catch(IllegalArgumentException expected) {}
176
177 try {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900178 address = new LinkAddress(V6_ADDRESS, -1);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900179 fail("Negative IPv6 prefix length should cause IllegalArgumentException");
180 } catch(IllegalArgumentException expected) {}
181
182 try {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900183 address = new LinkAddress(V4_ADDRESS, 33);
184 fail("/33 IPv4 prefix length should cause IllegalArgumentException");
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900185 } catch(IllegalArgumentException expected) {}
186
187 try {
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900188 address = new LinkAddress(V4 + "/33", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
189 fail("/33 IPv4 prefix length should cause IllegalArgumentException");
190 } catch(IllegalArgumentException expected) {}
191
192
193 try {
194 address = new LinkAddress(V6_ADDRESS, 129, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900195 fail("/129 IPv6 prefix length should cause IllegalArgumentException");
196 } catch(IllegalArgumentException expected) {}
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900197
198 try {
199 address = new LinkAddress(V6 + "/129", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
200 fail("/129 IPv6 prefix length should cause IllegalArgumentException");
201 } catch(IllegalArgumentException expected) {}
202
203 // Multicast addresses are rejected.
204 try {
205 address = new LinkAddress("224.0.0.2/32");
206 fail("IPv4 multicast address should cause IllegalArgumentException");
207 } catch(IllegalArgumentException expected) {}
208
209 try {
210 address = new LinkAddress("ff02::1/128");
211 fail("IPv6 multicast address should cause IllegalArgumentException");
212 } catch(IllegalArgumentException expected) {}
213 }
214
Hugo Benichi3054e102017-08-08 13:06:04 +0900215 @Test
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900216 public void testAddressScopes() {
217 assertEquals(RT_SCOPE_HOST, new LinkAddress("::/128").getScope());
218 assertEquals(RT_SCOPE_HOST, new LinkAddress("0.0.0.0/32").getScope());
219
220 assertEquals(RT_SCOPE_LINK, new LinkAddress("::1/128").getScope());
221 assertEquals(RT_SCOPE_LINK, new LinkAddress("127.0.0.5/8").getScope());
222 assertEquals(RT_SCOPE_LINK, new LinkAddress("fe80::ace:d00d/64").getScope());
223 assertEquals(RT_SCOPE_LINK, new LinkAddress("169.254.5.12/16").getScope());
224
225 assertEquals(RT_SCOPE_SITE, new LinkAddress("fec0::dead/64").getScope());
226
227 assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("10.1.2.3/21").getScope());
228 assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("192.0.2.1/25").getScope());
229 assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("2001:db8::/64").getScope());
230 assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("5000::/127").getScope());
231 }
232
233 private void assertIsSameAddressAs(LinkAddress l1, LinkAddress l2) {
234 assertTrue(l1 + " unexpectedly does not have same address as " + l2,
235 l1.isSameAddressAs(l2));
236 assertTrue(l2 + " unexpectedly does not have same address as " + l1,
237 l2.isSameAddressAs(l1));
238 }
239
240 private void assertIsNotSameAddressAs(LinkAddress l1, LinkAddress l2) {
241 assertFalse(l1 + " unexpectedly has same address as " + l2,
242 l1.isSameAddressAs(l2));
243 assertFalse(l2 + " unexpectedly has same address as " + l1,
244 l1.isSameAddressAs(l2));
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900245 }
246
Hugo Benichi3054e102017-08-08 13:06:04 +0900247 @Test
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900248 public void testEqualsAndSameAddressAs() {
249 LinkAddress l1, l2, l3;
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900250
251 l1 = new LinkAddress("2001:db8::1/64");
252 l2 = new LinkAddress("2001:db8::1/64");
Chalard Jean64802872019-05-30 17:11:14 +0900253 assertEqualBothWays(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900254 assertIsSameAddressAs(l1, l2);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900255
256 l2 = new LinkAddress("2001:db8::1/65");
Chalard Jean64802872019-05-30 17:11:14 +0900257 assertNotEqualEitherWay(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900258 assertIsNotSameAddressAs(l1, l2);
259
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900260 l2 = new LinkAddress("2001:db8::2/64");
Chalard Jean64802872019-05-30 17:11:14 +0900261 assertNotEqualEitherWay(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900262 assertIsNotSameAddressAs(l1, l2);
263
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900264
265 l1 = new LinkAddress("192.0.2.1/24");
266 l2 = new LinkAddress("192.0.2.1/24");
Chalard Jean64802872019-05-30 17:11:14 +0900267 assertEqualBothWays(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900268 assertIsSameAddressAs(l1, l2);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900269
270 l2 = new LinkAddress("192.0.2.1/23");
Chalard Jean64802872019-05-30 17:11:14 +0900271 assertNotEqualEitherWay(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900272 assertIsNotSameAddressAs(l1, l2);
273
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900274 l2 = new LinkAddress("192.0.2.2/24");
Chalard Jean64802872019-05-30 17:11:14 +0900275 assertNotEqualEitherWay(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900276 assertIsNotSameAddressAs(l1, l2);
277
278
279 // Check equals() and isSameAddressAs() on identical addresses with different flags.
280 l1 = new LinkAddress(V6_ADDRESS, 64);
281 l2 = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_UNIVERSE);
Chalard Jean64802872019-05-30 17:11:14 +0900282 assertEqualBothWays(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900283 assertIsSameAddressAs(l1, l2);
284
285 l2 = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_UNIVERSE);
Chalard Jean64802872019-05-30 17:11:14 +0900286 assertNotEqualEitherWay(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900287 assertIsSameAddressAs(l1, l2);
288
289 // Check equals() and isSameAddressAs() on identical addresses with different scope.
290 l1 = new LinkAddress(V4_ADDRESS, 24);
291 l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_UNIVERSE);
Chalard Jean64802872019-05-30 17:11:14 +0900292 assertEqualBothWays(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900293 assertIsSameAddressAs(l1, l2);
294
295 l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_HOST);
Chalard Jean64802872019-05-30 17:11:14 +0900296 assertNotEqualEitherWay(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900297 assertIsSameAddressAs(l1, l2);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900298
299 // Addresses with the same start or end bytes aren't equal between families.
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900300 l1 = new LinkAddress("32.1.13.184/24");
301 l2 = new LinkAddress("2001:db8::1/24");
302 l3 = new LinkAddress("::2001:db8/24");
303
304 byte[] ipv4Bytes = l1.getAddress().getAddress();
305 byte[] l2FirstIPv6Bytes = Arrays.copyOf(l2.getAddress().getAddress(), 4);
306 byte[] l3LastIPv6Bytes = Arrays.copyOfRange(l3.getAddress().getAddress(), 12, 16);
307 assertTrue(Arrays.equals(ipv4Bytes, l2FirstIPv6Bytes));
308 assertTrue(Arrays.equals(ipv4Bytes, l3LastIPv6Bytes));
309
Chalard Jean64802872019-05-30 17:11:14 +0900310 assertNotEqualEitherWay(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900311 assertIsNotSameAddressAs(l1, l2);
312
Chalard Jean64802872019-05-30 17:11:14 +0900313 assertNotEqualEitherWay(l1, l3);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900314 assertIsNotSameAddressAs(l1, l3);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900315
316 // Because we use InetAddress, an IPv4 address is equal to its IPv4-mapped address.
317 // TODO: Investigate fixing this.
318 String addressString = V4 + "/24";
319 l1 = new LinkAddress(addressString);
320 l2 = new LinkAddress("::ffff:" + addressString);
Chalard Jean64802872019-05-30 17:11:14 +0900321 assertEqualBothWays(l1, l2);
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900322 assertIsSameAddressAs(l1, l2);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900323 }
324
Hugo Benichi3054e102017-08-08 13:06:04 +0900325 @Test
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900326 public void testHashCode() {
Hugo Benichi3054e102017-08-08 13:06:04 +0900327 LinkAddress l1, l2;
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900328
Hugo Benichi3054e102017-08-08 13:06:04 +0900329 l1 = new LinkAddress(V4_ADDRESS, 23);
330 l2 = new LinkAddress(V4_ADDRESS, 23, 0, RT_SCOPE_HOST);
331 assertNotEquals(l1.hashCode(), l2.hashCode());
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900332
Hugo Benichi3054e102017-08-08 13:06:04 +0900333 l1 = new LinkAddress(V6_ADDRESS, 128);
334 l2 = new LinkAddress(V6_ADDRESS, 128, IFA_F_TENTATIVE, RT_SCOPE_UNIVERSE);
335 assertNotEquals(l1.hashCode(), l2.hashCode());
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900336 }
337
Hugo Benichi3054e102017-08-08 13:06:04 +0900338 @Test
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900339 public void testParceling() {
340 LinkAddress l;
341
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900342 l = new LinkAddress(V6_ADDRESS, 64, 123, 456);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900343 assertParcelingIsLossless(l);
344
Lorenzo Colitti4ea70b72013-11-15 18:43:52 +0900345 l = new LinkAddress(V4 + "/28", IFA_F_PERMANENT, RT_SCOPE_LINK);
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +0900346 assertParcelingIsLossless(l);
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900347 }
Erik Klinec17b5282014-10-20 19:46:56 +0900348
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +0900349 @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
350 public void testLifetimeParceling() {
351 final LinkAddress l = new LinkAddress(V6_ADDRESS, 64, 123, 456, 1L, 3600000L);
352 assertParcelingIsLossless(l);
353 }
354
355 @Test @IgnoreAfter(Build.VERSION_CODES.Q)
356 public void testFieldCount_Q() {
357 assertFieldCountEquals(4, LinkAddress.class);
358 }
359
360 @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
361 public void testFieldCount() {
362 // Make sure any new field is covered by the above parceling tests when changing this number
363 assertFieldCountEquals(6, LinkAddress.class);
364 }
365
366 @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
Jack Yu1fe4b302020-01-17 15:28:50 -0800367 public void testDeprecationTime() {
368 try {
369 new LinkAddress(V6_ADDRESS, 64, 0, 456,
Jack Yu7782a0d2020-01-26 15:52:11 -0800370 LinkAddress.LIFETIME_UNKNOWN, 100000L);
Jack Yu1fe4b302020-01-17 15:28:50 -0800371 fail("Only one time provided should cause exception");
372 } catch (IllegalArgumentException expected) { }
373
374 try {
375 new LinkAddress(V6_ADDRESS, 64, 0, 456,
Jack Yu7782a0d2020-01-26 15:52:11 -0800376 200000L, 100000L);
Jack Yu1fe4b302020-01-17 15:28:50 -0800377 fail("deprecation time later than expiration time should cause exception");
378 } catch (IllegalArgumentException expected) { }
379
380 try {
381 new LinkAddress(V6_ADDRESS, 64, 0, 456,
Jack Yu7782a0d2020-01-26 15:52:11 -0800382 -2, 100000L);
Jack Yu1fe4b302020-01-17 15:28:50 -0800383 fail("negative deprecation time should cause exception");
384 } catch (IllegalArgumentException expected) { }
Chiachang Wangaae21602020-03-09 14:35:39 +0800385
386 LinkAddress addr = new LinkAddress(V6_ADDRESS, 64, 0, 456, 100000L, 200000L);
387 assertEquals(100000L, addr.getDeprecationTime());
Jack Yu1fe4b302020-01-17 15:28:50 -0800388 }
389
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +0900390 @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
Jack Yu1fe4b302020-01-17 15:28:50 -0800391 public void testExpirationTime() {
392 try {
393 new LinkAddress(V6_ADDRESS, 64, 0, 456,
Jack Yu7782a0d2020-01-26 15:52:11 -0800394 200000L, LinkAddress.LIFETIME_UNKNOWN);
Jack Yu1fe4b302020-01-17 15:28:50 -0800395 fail("Only one time provided should cause exception");
396 } catch (IllegalArgumentException expected) { }
397
398 try {
399 new LinkAddress(V6_ADDRESS, 64, 0, 456,
Jack Yu7782a0d2020-01-26 15:52:11 -0800400 100000L, -2);
Jack Yu1fe4b302020-01-17 15:28:50 -0800401 fail("negative expiration time should cause exception");
402 } catch (IllegalArgumentException expected) { }
Chiachang Wangaae21602020-03-09 14:35:39 +0800403
404 LinkAddress addr = new LinkAddress(V6_ADDRESS, 64, 0, 456, 100000L, 200000L);
405 assertEquals(200000L, addr.getExpirationTime());
Jack Yu1fe4b302020-01-17 15:28:50 -0800406 }
407
408 @Test
409 public void testGetFlags() {
410 LinkAddress l = new LinkAddress(V6_ADDRESS, 64, 123, RT_SCOPE_HOST);
411 assertEquals(123, l.getFlags());
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +0900412 }
Jack Yu1fe4b302020-01-17 15:28:50 -0800413
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +0900414 @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
415 public void testGetFlags_Deprecation() {
Jack Yu1fe4b302020-01-17 15:28:50 -0800416 // Test if deprecated bit was added/remove automatically based on the provided deprecation
417 // time
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +0900418 LinkAddress l = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_HOST,
Jack Yu7782a0d2020-01-26 15:52:11 -0800419 1L, LinkAddress.LIFETIME_PERMANENT);
Jack Yu1fe4b302020-01-17 15:28:50 -0800420 // Check if the flag is added automatically.
421 assertTrue((l.getFlags() & IFA_F_DEPRECATED) != 0);
422
423 l = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_HOST,
Jack Yu7782a0d2020-01-26 15:52:11 -0800424 SystemClock.elapsedRealtime() + 100000L, LinkAddress.LIFETIME_PERMANENT);
Jack Yu1fe4b302020-01-17 15:28:50 -0800425 // Check if the flag is removed automatically.
426 assertTrue((l.getFlags() & IFA_F_DEPRECATED) == 0);
427
428 l = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_HOST,
429 LinkAddress.LIFETIME_PERMANENT, LinkAddress.LIFETIME_PERMANENT);
430 // Check if the permanent flag is added.
431 assertTrue((l.getFlags() & IFA_F_PERMANENT) != 0);
432
433 l = new LinkAddress(V6_ADDRESS, 64, IFA_F_PERMANENT, RT_SCOPE_HOST,
Jack Yu7782a0d2020-01-26 15:52:11 -0800434 1000L, SystemClock.elapsedRealtime() + 100000L);
Jack Yu1fe4b302020-01-17 15:28:50 -0800435 // Check if the permanent flag is removed
436 assertTrue((l.getFlags() & IFA_F_PERMANENT) == 0);
Jack Yu7782a0d2020-01-26 15:52:11 -0800437 }
Jack Yu1fe4b302020-01-17 15:28:50 -0800438
Erik Klinec17b5282014-10-20 19:46:56 +0900439 private void assertGlobalPreferred(LinkAddress l, String msg) {
440 assertTrue(msg, l.isGlobalPreferred());
441 }
442
443 private void assertNotGlobalPreferred(LinkAddress l, String msg) {
444 assertFalse(msg, l.isGlobalPreferred());
445 }
446
Hugo Benichi3054e102017-08-08 13:06:04 +0900447 @Test
Erik Klinec17b5282014-10-20 19:46:56 +0900448 public void testIsGlobalPreferred() {
449 LinkAddress l;
450
451 l = new LinkAddress(V4_ADDRESS, 32, 0, RT_SCOPE_UNIVERSE);
452 assertGlobalPreferred(l, "v4,global,noflags");
453
454 l = new LinkAddress("10.10.1.7/23", 0, RT_SCOPE_UNIVERSE);
455 assertGlobalPreferred(l, "v4-rfc1918,global,noflags");
456
457 l = new LinkAddress("10.10.1.7/23", 0, RT_SCOPE_SITE);
458 assertNotGlobalPreferred(l, "v4-rfc1918,site-local,noflags");
459
460 l = new LinkAddress("127.0.0.7/8", 0, RT_SCOPE_HOST);
461 assertNotGlobalPreferred(l, "v4-localhost,node-local,noflags");
462
463 l = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_UNIVERSE);
464 assertGlobalPreferred(l, "v6,global,noflags");
465
466 l = new LinkAddress(V6_ADDRESS, 64, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
467 assertGlobalPreferred(l, "v6,global,permanent");
468
469 // IPv6 ULAs are not acceptable "global preferred" addresses.
470 l = new LinkAddress("fc12::1/64", 0, RT_SCOPE_UNIVERSE);
471 assertNotGlobalPreferred(l, "v6,ula1,noflags");
472
473 l = new LinkAddress("fd34::1/64", 0, RT_SCOPE_UNIVERSE);
474 assertNotGlobalPreferred(l, "v6,ula2,noflags");
475
476 l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_UNIVERSE);
477 assertGlobalPreferred(l, "v6,global,tempaddr");
478
479 l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY|IFA_F_DADFAILED),
480 RT_SCOPE_UNIVERSE);
481 assertNotGlobalPreferred(l, "v6,global,tempaddr+dadfailed");
482
483 l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY|IFA_F_DEPRECATED),
484 RT_SCOPE_UNIVERSE);
485 assertNotGlobalPreferred(l, "v6,global,tempaddr+deprecated");
486
487 l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_SITE);
488 assertNotGlobalPreferred(l, "v6,site-local,tempaddr");
489
490 l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_LINK);
491 assertNotGlobalPreferred(l, "v6,link-local,tempaddr");
492
493 l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_HOST);
494 assertNotGlobalPreferred(l, "v6,node-local,tempaddr");
495
496 l = new LinkAddress("::1/128", IFA_F_PERMANENT, RT_SCOPE_HOST);
497 assertNotGlobalPreferred(l, "v6-localhost,node-local,permanent");
498
499 l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY|IFA_F_TENTATIVE),
500 RT_SCOPE_UNIVERSE);
501 assertNotGlobalPreferred(l, "v6,global,tempaddr+tentative");
502
503 l = new LinkAddress(V6_ADDRESS, 64,
504 (IFA_F_TEMPORARY|IFA_F_TENTATIVE|IFA_F_OPTIMISTIC),
505 RT_SCOPE_UNIVERSE);
506 assertGlobalPreferred(l, "v6,global,tempaddr+optimistic");
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +0900507 }
Jack Yu1fe4b302020-01-17 15:28:50 -0800508
Remi NGUYEN VAN6b543082020-03-06 18:50:01 +0900509 @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
510 public void testIsGlobalPreferred_DeprecatedInFuture() {
511 final LinkAddress l = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED,
Jack Yu1fe4b302020-01-17 15:28:50 -0800512 RT_SCOPE_UNIVERSE, SystemClock.elapsedRealtime() + 100000,
513 SystemClock.elapsedRealtime() + 200000);
514 // Although the deprecated bit is set, but the deprecation time is in the future, test
515 // if the flag is removed automatically.
516 assertGlobalPreferred(l, "v6,global,tempaddr+deprecated in the future");
Erik Klinec17b5282014-10-20 19:46:56 +0900517 }
Lorenzo Colitti64eb7fd2013-11-27 15:03:10 +0900518}