summaryrefslogtreecommitdiff
path: root/core/tests/coretests/src/android/net/LinkAddressTest.java
blob: 17423be65febe34281f1e1ded1d952d4afb05ac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.net.LinkAddress;
import android.os.Parcel;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import static libcore.io.OsConstants.IFA_F_DEPRECATED;
import static libcore.io.OsConstants.IFA_F_PERMANENT;
import static libcore.io.OsConstants.IFA_F_TENTATIVE;
import static libcore.io.OsConstants.RT_SCOPE_HOST;
import static libcore.io.OsConstants.RT_SCOPE_LINK;
import static libcore.io.OsConstants.RT_SCOPE_SITE;
import static libcore.io.OsConstants.RT_SCOPE_UNIVERSE;

/**
 * Tests for {@link LinkAddress}.
 */
public class LinkAddressTest extends AndroidTestCase {

    private static final String V4 = "192.0.2.1";
    private static final String V6 = "2001:db8::1";
    private static final InetAddress V4_ADDRESS = NetworkUtils.numericToInetAddress(V4);
    private static final InetAddress V6_ADDRESS = NetworkUtils.numericToInetAddress(V6);

    public void testConstructors() throws SocketException {
        LinkAddress address;

        // Valid addresses work as expected.
        address = new LinkAddress(V4_ADDRESS, 25);
        assertEquals(V4_ADDRESS, address.getAddress());
        assertEquals(25, address.getNetworkPrefixLength());
        assertEquals(0, address.getFlags());
        assertEquals(RT_SCOPE_UNIVERSE, address.getScope());

        address = new LinkAddress(V6_ADDRESS, 127);
        assertEquals(V6_ADDRESS, address.getAddress());
        assertEquals(127, address.getNetworkPrefixLength());
        assertEquals(0, address.getFlags());
        assertEquals(RT_SCOPE_UNIVERSE, address.getScope());

        // Nonsensical flags/scopes or combinations thereof are acceptable.
        address = new LinkAddress(V6 + "/64", IFA_F_DEPRECATED | IFA_F_PERMANENT, RT_SCOPE_LINK);
        assertEquals(V6_ADDRESS, address.getAddress());
        assertEquals(64, address.getNetworkPrefixLength());
        assertEquals(IFA_F_DEPRECATED | IFA_F_PERMANENT, address.getFlags());
        assertEquals(RT_SCOPE_LINK, address.getScope());

        address = new LinkAddress(V4 + "/23", 123, 456);
        assertEquals(V4_ADDRESS, address.getAddress());
        assertEquals(23, address.getNetworkPrefixLength());
        assertEquals(123, address.getFlags());
        assertEquals(456, address.getScope());

        // InterfaceAddress doesn't have a constructor. Fetch some from an interface.
        List<InterfaceAddress> addrs = NetworkInterface.getByName("lo").getInterfaceAddresses();

        // We expect to find 127.0.0.1/8 and ::1/128, in any order.
        LinkAddress ipv4Loopback, ipv6Loopback;
        assertEquals(2, addrs.size());
        if (addrs.get(0).getAddress() instanceof Inet4Address) {
            ipv4Loopback = new LinkAddress(addrs.get(0));
            ipv6Loopback = new LinkAddress(addrs.get(1));
        } else {
            ipv4Loopback = new LinkAddress(addrs.get(1));
            ipv6Loopback = new LinkAddress(addrs.get(0));
        }

        assertEquals(NetworkUtils.numericToInetAddress("127.0.0.1"), ipv4Loopback.getAddress());
        assertEquals(8, ipv4Loopback.getNetworkPrefixLength());

        assertEquals(NetworkUtils.numericToInetAddress("::1"), ipv6Loopback.getAddress());
        assertEquals(128, ipv6Loopback.getNetworkPrefixLength());

        // Null addresses are rejected.
        try {
            address = new LinkAddress(null, 24);
            fail("Null InetAddress should cause IllegalArgumentException");
        } catch(IllegalArgumentException expected) {}

        try {
            address = new LinkAddress((String) null, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
            fail("Null string should cause IllegalArgumentException");
        } catch(IllegalArgumentException expected) {}

        try {
            address = new LinkAddress((InterfaceAddress) null);
            fail("Null string should cause NullPointerException");
        } catch(NullPointerException expected) {}

        // Invalid prefix lengths are rejected.
        try {
            address = new LinkAddress(V4_ADDRESS, -1);
            fail("Negative IPv4 prefix length should cause IllegalArgumentException");
        } catch(IllegalArgumentException expected) {}

        try {
            address = new LinkAddress(V6_ADDRESS, -1);
            fail("Negative IPv6 prefix length should cause IllegalArgumentException");
        } catch(IllegalArgumentException expected) {}

        try {
            address = new LinkAddress(V4_ADDRESS, 33);
            fail("/33 IPv4 prefix length should cause IllegalArgumentException");
        } catch(IllegalArgumentException expected) {}

        try {
            address = new LinkAddress(V4 + "/33", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
            fail("/33 IPv4 prefix length should cause IllegalArgumentException");
        } catch(IllegalArgumentException expected) {}


        try {
            address = new LinkAddress(V6_ADDRESS, 129, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
            fail("/129 IPv6 prefix length should cause IllegalArgumentException");
        } catch(IllegalArgumentException expected) {}

        try {
            address = new LinkAddress(V6 + "/129", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
            fail("/129 IPv6 prefix length should cause IllegalArgumentException");
        } catch(IllegalArgumentException expected) {}

        // Multicast addresses are rejected.
        try {
            address = new LinkAddress("224.0.0.2/32");
            fail("IPv4 multicast address should cause IllegalArgumentException");
        } catch(IllegalArgumentException expected) {}

        try {
            address = new LinkAddress("ff02::1/128");
            fail("IPv6 multicast address should cause IllegalArgumentException");
        } catch(IllegalArgumentException expected) {}
    }

    public void testAddressScopes() {
        assertEquals(RT_SCOPE_HOST, new LinkAddress("::/128").getScope());
        assertEquals(RT_SCOPE_HOST, new LinkAddress("0.0.0.0/32").getScope());

        assertEquals(RT_SCOPE_LINK, new LinkAddress("::1/128").getScope());
        assertEquals(RT_SCOPE_LINK, new LinkAddress("127.0.0.5/8").getScope());
        assertEquals(RT_SCOPE_LINK, new LinkAddress("fe80::ace:d00d/64").getScope());
        assertEquals(RT_SCOPE_LINK, new LinkAddress("169.254.5.12/16").getScope());

        assertEquals(RT_SCOPE_SITE, new LinkAddress("fec0::dead/64").getScope());

        assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("10.1.2.3/21").getScope());
        assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("192.0.2.1/25").getScope());
        assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("2001:db8::/64").getScope());
        assertEquals(RT_SCOPE_UNIVERSE, new LinkAddress("5000::/127").getScope());
    }

    private void assertIsSameAddressAs(LinkAddress l1, LinkAddress l2) {
        assertTrue(l1 + " unexpectedly does not have same address as " + l2,
                l1.isSameAddressAs(l2));
        assertTrue(l2 + " unexpectedly does not have same address as " + l1,
                l2.isSameAddressAs(l1));
    }

    private void assertIsNotSameAddressAs(LinkAddress l1, LinkAddress l2) {
        assertFalse(l1 + " unexpectedly has same address as " + l2,
                l1.isSameAddressAs(l2));
        assertFalse(l2 + " unexpectedly has same address as " + l1,
                l1.isSameAddressAs(l2));
    }

    private void assertLinkAddressesEqual(LinkAddress l1, LinkAddress l2) {
        assertTrue(l1 + " unexpectedly not equal to " + l2, l1.equals(l2));
        assertTrue(l2 + " unexpectedly not equal to " + l1, l2.equals(l1));
        assertEquals(l1.hashCode(), l2.hashCode());
    }

    private void assertLinkAddressesNotEqual(LinkAddress l1, LinkAddress l2) {
        assertFalse(l1 + " unexpectedly equal to " + l2, l1.equals(l2));
        assertFalse(l2 + " unexpectedly equal to " + l1, l2.equals(l1));
    }

    public void testEqualsAndSameAddressAs() {
        LinkAddress l1, l2, l3;

        l1 = new LinkAddress("2001:db8::1/64");
        l2 = new LinkAddress("2001:db8::1/64");
        assertLinkAddressesEqual(l1, l2);
        assertIsSameAddressAs(l1, l2);

        l2 = new LinkAddress("2001:db8::1/65");
        assertLinkAddressesNotEqual(l1, l2);
        assertIsNotSameAddressAs(l1, l2);

        l2 = new LinkAddress("2001:db8::2/64");
        assertLinkAddressesNotEqual(l1, l2);
        assertIsNotSameAddressAs(l1, l2);


        l1 = new LinkAddress("192.0.2.1/24");
        l2 = new LinkAddress("192.0.2.1/24");
        assertLinkAddressesEqual(l1, l2);
        assertIsSameAddressAs(l1, l2);

        l2 = new LinkAddress("192.0.2.1/23");
        assertLinkAddressesNotEqual(l1, l2);
        assertIsNotSameAddressAs(l1, l2);

        l2 = new LinkAddress("192.0.2.2/24");
        assertLinkAddressesNotEqual(l1, l2);
        assertIsNotSameAddressAs(l1, l2);


        // Check equals() and isSameAddressAs() on identical addresses with different flags.
        l1 = new LinkAddress(V6_ADDRESS, 64);
        l2 = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_UNIVERSE);
        assertLinkAddressesEqual(l1, l2);
        assertIsSameAddressAs(l1, l2);

        l2 = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_UNIVERSE);
        assertLinkAddressesNotEqual(l1, l2);
        assertIsSameAddressAs(l1, l2);

        // Check equals() and isSameAddressAs() on identical addresses with different scope.
        l1 = new LinkAddress(V4_ADDRESS, 24);
        l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_UNIVERSE);
        assertLinkAddressesEqual(l1, l2);
        assertIsSameAddressAs(l1, l2);

        l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_HOST);
        assertLinkAddressesNotEqual(l1, l2);
        assertIsSameAddressAs(l1, l2);

        // Addresses with the same start or end bytes aren't equal between families.
        l1 = new LinkAddress("32.1.13.184/24");
        l2 = new LinkAddress("2001:db8::1/24");
        l3 = new LinkAddress("::2001:db8/24");

        byte[] ipv4Bytes = l1.getAddress().getAddress();
        byte[] l2FirstIPv6Bytes = Arrays.copyOf(l2.getAddress().getAddress(), 4);
        byte[] l3LastIPv6Bytes = Arrays.copyOfRange(l3.getAddress().getAddress(), 12, 16);
        assertTrue(Arrays.equals(ipv4Bytes, l2FirstIPv6Bytes));
        assertTrue(Arrays.equals(ipv4Bytes, l3LastIPv6Bytes));

        assertLinkAddressesNotEqual(l1, l2);
        assertIsNotSameAddressAs(l1, l2);

        assertLinkAddressesNotEqual(l1, l3);
        assertIsNotSameAddressAs(l1, l3);

        // Because we use InetAddress, an IPv4 address is equal to its IPv4-mapped address.
        // TODO: Investigate fixing this.
        String addressString = V4 + "/24";
        l1 = new LinkAddress(addressString);
        l2 = new LinkAddress("::ffff:" + addressString);
        assertLinkAddressesEqual(l1, l2);
        assertIsSameAddressAs(l1, l2);
    }

    public void testHashCode() {
        LinkAddress l;

        l = new LinkAddress(V4_ADDRESS, 23);
        assertEquals(-982787, l.hashCode());

        l = new LinkAddress(V4_ADDRESS, 23, 0, RT_SCOPE_HOST);
        assertEquals(-971865, l.hashCode());

        l = new LinkAddress(V4_ADDRESS, 27);
        assertEquals(-982743, l.hashCode());

        l = new LinkAddress(V6_ADDRESS, 64);
        assertEquals(1076522926, l.hashCode());

        l = new LinkAddress(V6_ADDRESS, 128);
        assertEquals(1076523630, l.hashCode());

        l = new LinkAddress(V6_ADDRESS, 128, IFA_F_TENTATIVE, RT_SCOPE_UNIVERSE);
        assertEquals(1076524846, l.hashCode());
    }

    private LinkAddress passThroughParcel(LinkAddress l) {
        Parcel p = Parcel.obtain();
        LinkAddress l2 = null;
        try {
            l.writeToParcel(p, 0);
            p.setDataPosition(0);
            l2 = LinkAddress.CREATOR.createFromParcel(p);
        } finally {
            p.recycle();
        }
        assertNotNull(l2);
        return l2;
    }

    private void assertParcelingIsLossless(LinkAddress l) {
      LinkAddress l2 = passThroughParcel(l);
      assertEquals(l, l2);
    }

    public void testParceling() {
        LinkAddress l;

        l = new LinkAddress(V6_ADDRESS, 64, 123, 456);
        assertParcelingIsLossless(l);

        l = new LinkAddress(V4 + "/28", IFA_F_PERMANENT, RT_SCOPE_LINK);
        assertParcelingIsLossless(l);
    }
}