aboutsummaryrefslogtreecommitdiff
path: root/tests/iketests/src/java/com/android/ike/ikev2/message/IkeConfigPayloadTest.java
blob: 78493247bb21f9d3806869b574d080d2e8095e15 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
 * Copyright (C) 2019 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 com.android.ike.ikev2.message;

import static com.android.ike.ikev2.message.IkeConfigPayload.CONFIG_ATTR_INTERNAL_IP4_ADDRESS;
import static com.android.ike.ikev2.message.IkeConfigPayload.CONFIG_ATTR_INTERNAL_IP4_DNS;
import static com.android.ike.ikev2.message.IkeConfigPayload.CONFIG_ATTR_INTERNAL_IP4_NETMASK;
import static com.android.ike.ikev2.message.IkeConfigPayload.CONFIG_ATTR_INTERNAL_IP4_SUBNET;
import static com.android.ike.ikev2.message.IkeConfigPayload.CONFIG_ATTR_INTERNAL_IP6_ADDRESS;
import static com.android.ike.ikev2.message.IkeConfigPayload.CONFIG_ATTR_INTERNAL_IP6_DNS;
import static com.android.ike.ikev2.message.IkeConfigPayload.CONFIG_ATTR_INTERNAL_IP6_SUBNET;
import static com.android.ike.ikev2.message.IkeConfigPayload.CONFIG_TYPE_REPLY;
import static com.android.ike.ikev2.message.IkeConfigPayload.CONFIG_TYPE_REQUEST;
import static com.android.ike.ikev2.message.IkePayload.PAYLOAD_TYPE_CP;
import static com.android.ike.ikev2.message.IkePayload.PAYLOAD_TYPE_NOTIFY;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.net.LinkAddress;

import com.android.ike.TestUtils;
import com.android.ike.ikev2.exceptions.InvalidSyntaxException;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttrIpv4AddressBase;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttrIpv6AddrRangeBase;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttribute;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv4Address;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv4Dns;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv4Netmask;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv4Subnet;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv6Address;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv6Dns;
import com.android.ike.ikev2.message.IkeConfigPayload.ConfigAttributeIpv6Subnet;

import libcore.net.InetAddressUtils;

import org.junit.Before;
import org.junit.Test;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.List;

public final class IkeConfigPayloadTest {
    private static final String CONFIG_REQ_PAYLOAD_HEX =
            "2900001801000000000100000008000000030000000a0000";
    private static final String CONFIG_RESP_PAYLOAD_HEX =
            "210000200200000000010004c000026400030004080808080003000408080404";
    private static final String CONFIG_RESP_PAYLOAD_INVALID_ONE_HEX =
            "210000200200000000010004c000026400020004fffffffe00020004fffffffe";
    private static final String CONFIG_RESP_PAYLOAD_INVALID_TWO_HEX =
            "210000100200000000020004fffffffe";

    private static final byte[] CONFIG_REQ_PAYLOAD =
            TestUtils.hexStringToByteArray(CONFIG_REQ_PAYLOAD_HEX);
    private static final byte[] CONFIG_RESP_PAYLOAD =
            TestUtils.hexStringToByteArray(CONFIG_RESP_PAYLOAD_HEX);

    private static final Inet4Address IPV4_ADDRESS =
            (Inet4Address) (InetAddressUtils.parseNumericAddress("192.0.2.100"));
    private static final Inet4Address IPV4_NETMASK =
            (Inet4Address) (InetAddressUtils.parseNumericAddress("255.255.255.240"));
    private static final int IP4_PREFIX_LEN = 28;
    private static final LinkAddress IPV4_LINK_ADDRESS =
            new LinkAddress(IPV4_ADDRESS, IP4_PREFIX_LEN);

    private static final byte[] IPV4_ADDRESS_ATTRIBUTE_WITH_VALUE =
            TestUtils.hexStringToByteArray("00010004c0000264");
    private static final byte[] IPV4_ADDRESS_ATTRIBUTE_WITHOUT_VALUE =
            TestUtils.hexStringToByteArray("00010000");

    private static final byte[] IPV4_NETMASK_ATTRIBUTE_WITHOUT_VALUE =
            TestUtils.hexStringToByteArray("00020000");

    private static final Inet4Address IPV4_DNS =
            (Inet4Address) (InetAddressUtils.parseNumericAddress("8.8.8.8"));
    private static final byte[] IPV4_DNS_ATTRIBUTE_VALUE =
            TestUtils.hexStringToByteArray("08080808");
    private static final byte[] IPV4_DNS_ATTRIBUTE_WITHOUT_VALUE =
            TestUtils.hexStringToByteArray("00030000");

    private static final byte[] IPV4_SUBNET_ATTRIBUTE_VALUE =
            TestUtils.hexStringToByteArray("c0000264fffffff0");
    private static final byte[] IPV4_SUBNET_ATTRIBUTE_WITH_VALUE =
            TestUtils.hexStringToByteArray("000d0008c0000264fffffff0");
    private static final byte[] IPV4_SUBNET_ATTRIBUTE_WITHOUT_VALUE =
            TestUtils.hexStringToByteArray("000d0000");

    private static final Inet6Address IPV6_ADDRESS =
            (Inet6Address) (InetAddressUtils.parseNumericAddress("2001:db8::1"));
    private static final int IP6_PREFIX_LEN = 64;
    private static final LinkAddress IPV6_LINK_ADDRESS =
            new LinkAddress(IPV6_ADDRESS, IP6_PREFIX_LEN);

    private static final byte[] IPV6_ADDRESS_ATTRIBUTE_VALUE =
            TestUtils.hexStringToByteArray("20010db800000000000000000000000140");
    private static final byte[] IPV6_ADDRESS_ATTRIBUTE_WITH_VALUE =
            TestUtils.hexStringToByteArray("0008001120010db800000000000000000000000140");
    private static final byte[] IPV6_ADDRESS_ATTRIBUTE_WITHOUT_VALUE =
            TestUtils.hexStringToByteArray("00080000");

    private static final byte[] IPV6_SUBNET_ATTRIBUTE_VALUE = IPV6_ADDRESS_ATTRIBUTE_VALUE;
    private static final byte[] IPV6_SUBNET_ATTRIBUTE_WITH_VALUE =
            TestUtils.hexStringToByteArray("000f001120010db800000000000000000000000140");
    private static final byte[] IPV6_SUBNET_ATTRIBUTE_WITHOUT_VALUE =
            TestUtils.hexStringToByteArray("000f0000");

    private static final Inet6Address IPV6_DNS =
            (Inet6Address) (InetAddressUtils.parseNumericAddress("2001:db8:100::1"));
    private static final byte[] IPV6_DNS_ATTRIBUTE_WITHOUT_VALUE =
            TestUtils.hexStringToByteArray("000a0000");

    private Inet4Address[] mNetMasks;
    private int[] mIpv4PrefixLens;

    @Before
    public void setUp() throws Exception {
        mNetMasks =
                new Inet4Address[] {
                    (Inet4Address) (InetAddressUtils.parseNumericAddress("0.0.0.0")),
                    (Inet4Address) (InetAddressUtils.parseNumericAddress("255.255.255.255")),
                    (Inet4Address) (InetAddressUtils.parseNumericAddress("255.255.255.240"))
                };
        mIpv4PrefixLens = new int[] {0, 32, 28};
    }

    private IkeConfigPayload verifyDecodeHeaderAndGetPayload(
            IkePayload payload, int expectedConfigType) {
        assertEquals(PAYLOAD_TYPE_CP, payload.payloadType);
        assertFalse(payload.isCritical);
        assertTrue(payload instanceof IkeConfigPayload);

        IkeConfigPayload configPayload = (IkeConfigPayload) payload;
        assertEquals(expectedConfigType, configPayload.configType);

        return configPayload;
    }

    @Test
    public void testDecodeConfigRequest() throws Exception {
        IkePayload payload =
                IkePayloadFactory.getIkePayload(
                                PAYLOAD_TYPE_CP,
                                false /*isResp*/,
                                ByteBuffer.wrap(CONFIG_REQ_PAYLOAD))
                        .first;

        IkeConfigPayload configPayload =
                verifyDecodeHeaderAndGetPayload(payload, CONFIG_TYPE_REQUEST);

        List<ConfigAttribute> recognizedAttributeList = configPayload.recognizedAttributeList;
        assertEquals(4, recognizedAttributeList.size());

        ConfigAttribute att = recognizedAttributeList.get(0);
        assertEquals(CONFIG_ATTR_INTERNAL_IP4_ADDRESS, att.attributeType);
        assertNull(((ConfigAttributeIpv4Address) att).address);

        att = recognizedAttributeList.get(1);
        assertEquals(CONFIG_ATTR_INTERNAL_IP6_ADDRESS, att.attributeType);
        assertNull(((ConfigAttributeIpv6Address) att).linkAddress);

        att = recognizedAttributeList.get(2);
        assertEquals(CONFIG_ATTR_INTERNAL_IP4_DNS, att.attributeType);
        assertNull(((ConfigAttributeIpv4Dns) att).address);

        att = recognizedAttributeList.get(3);
        assertEquals(CONFIG_ATTR_INTERNAL_IP6_DNS, att.attributeType);
        assertNull(((ConfigAttributeIpv6Dns) att).address);
    }

    @Test
    public void testDecodeConfigResponse() throws Exception {
        IkePayload payload =
                IkePayloadFactory.getIkePayload(
                                PAYLOAD_TYPE_CP,
                                true /*isResp*/,
                                ByteBuffer.wrap(CONFIG_RESP_PAYLOAD))
                        .first;

        IkeConfigPayload configPayload =
                verifyDecodeHeaderAndGetPayload(payload, CONFIG_TYPE_REPLY);

        List<ConfigAttribute> recognizedAttributeList = configPayload.recognizedAttributeList;
        assertEquals(3, recognizedAttributeList.size());

        ConfigAttribute att = recognizedAttributeList.get(0);
        assertEquals(CONFIG_ATTR_INTERNAL_IP4_ADDRESS, att.attributeType);
        assertEquals(IPV4_ADDRESS, ((ConfigAttributeIpv4Address) att).address);

        att = recognizedAttributeList.get(1);
        assertEquals(CONFIG_ATTR_INTERNAL_IP4_DNS, att.attributeType);
        assertEquals(IPV4_DNS, ((ConfigAttributeIpv4Dns) att).address);

        att = recognizedAttributeList.get(2);
        InetAddress expectedDns = InetAddress.getByName("8.8.4.4");
        assertEquals(CONFIG_ATTR_INTERNAL_IP4_DNS, att.attributeType);
        assertEquals(expectedDns, ((ConfigAttributeIpv4Dns) att).address);
    }

    @Test
    public void testDecodeConfigRespWithTwoNetmask() throws Exception {
        byte[] configPayloadBytes =
                TestUtils.hexStringToByteArray(CONFIG_RESP_PAYLOAD_INVALID_ONE_HEX);
        try {
            IkePayloadFactory.getIkePayload(
                    PAYLOAD_TYPE_CP, true /*isResp*/, ByteBuffer.wrap(configPayloadBytes));
            fail("Expected to fail because more than on netmask found");
        } catch (InvalidSyntaxException expected) {
        }
    }

    @Test
    public void testDecodeConfigRespNetmaskFoundWithoutIpv4Addr() throws Exception {
        byte[] configPayloadBytes =
                TestUtils.hexStringToByteArray(CONFIG_RESP_PAYLOAD_INVALID_TWO_HEX);
        try {
            IkePayloadFactory.getIkePayload(
                    PAYLOAD_TYPE_CP, true /*isResp*/, ByteBuffer.wrap(configPayloadBytes));
            fail("Expected to fail because netmask is found without a IPv4 address");
        } catch (InvalidSyntaxException expected) {
        }
    }

    private ConfigAttribute makeMockAttribute(byte[] encodedAttribute) {
        ConfigAttribute mockAttribute = mock(ConfigAttribute.class);

        when(mockAttribute.getAttributeLen()).thenReturn(encodedAttribute.length);

        doAnswer(
                (invocation) -> {
                    ByteBuffer buffer = (ByteBuffer) invocation.getArguments()[0];
                    buffer.put(encodedAttribute);
                    return null;
                })
                .when(mockAttribute)
                .encodeAttributeToByteBuffer(any(ByteBuffer.class));

        return mockAttribute;
    }

    @Test
    public void testBuildAndEncodeOutboundConfig() throws Exception {
        List<ConfigAttribute> mockAttributeList = new LinkedList<>();
        mockAttributeList.add(makeMockAttribute(IPV4_ADDRESS_ATTRIBUTE_WITHOUT_VALUE));
        mockAttributeList.add(makeMockAttribute(IPV6_ADDRESS_ATTRIBUTE_WITHOUT_VALUE));
        mockAttributeList.add(makeMockAttribute(IPV4_DNS_ATTRIBUTE_WITHOUT_VALUE));
        mockAttributeList.add(makeMockAttribute(IPV6_DNS_ATTRIBUTE_WITHOUT_VALUE));
        IkeConfigPayload configPayload = new IkeConfigPayload(false /*isReply*/, mockAttributeList);

        assertEquals(PAYLOAD_TYPE_CP, configPayload.payloadType);
        assertFalse(configPayload.isCritical);
        assertEquals(CONFIG_TYPE_REQUEST, configPayload.configType);
        assertEquals(mockAttributeList, configPayload.recognizedAttributeList);

        ByteBuffer buffer = ByteBuffer.allocate(configPayload.getPayloadLength());
        configPayload.encodeToByteBuffer(PAYLOAD_TYPE_NOTIFY, buffer);
        assertArrayEquals(CONFIG_REQ_PAYLOAD, buffer.array());
    }

    private void verifyBuildAndEncodeAttributeCommon(
            ConfigAttribute attribute, int expectedAttributeType, byte[] expectedEncodedAttribute) {
        assertEquals(expectedAttributeType, attribute.attributeType);

        ByteBuffer buffer = ByteBuffer.allocate(attribute.getAttributeLen());
        attribute.encodeAttributeToByteBuffer(buffer);
        assertArrayEquals(expectedEncodedAttribute, buffer.array());
    }

    private void verifyEncodeIpv4AddresBaseAttribute(
            ConfigAttrIpv4AddressBase attribute,
            int expectedAttributeType,
            byte[] expectedEncodedAttribute,
            Inet4Address expectedAddress) {
        verifyBuildAndEncodeAttributeCommon(
                attribute, expectedAttributeType, expectedEncodedAttribute);
        assertEquals(expectedAddress, attribute.address);
    }

    private void verifyEncodeIpv6RangeBaseAttribute(
            ConfigAttrIpv6AddrRangeBase attribute,
            int expectedAttributeType,
            byte[] expectedEncodedAttribute,
            LinkAddress expectedLinkAddress) {
        verifyBuildAndEncodeAttributeCommon(
                attribute, expectedAttributeType, expectedEncodedAttribute);
        assertEquals(expectedLinkAddress, attribute.linkAddress);
    }

    @Test
    public void testDecodeIpv4AddressWithValue() throws Exception {
        ConfigAttributeIpv4Address attributeIp4Address =
                new ConfigAttributeIpv4Address(IPV4_ADDRESS.getAddress());

        assertEquals(CONFIG_ATTR_INTERNAL_IP4_ADDRESS, attributeIp4Address.attributeType);
        assertEquals(IPV4_ADDRESS, attributeIp4Address.address);
    }

    @Test
    public void testDecodeIpv4AddressWithoutValue() throws Exception {
        ConfigAttributeIpv4Address attributeIp4Address =
                new ConfigAttributeIpv4Address(new byte[0]);

        assertEquals(CONFIG_ATTR_INTERNAL_IP4_ADDRESS, attributeIp4Address.attributeType);
        assertNull(attributeIp4Address.address);
    }

    @Test
    public void testDecodeIpv4AddressWithInvalidValue() throws Exception {
        byte[] invalidValue = new byte[] {1};

        try {
            ConfigAttributeIpv4Address attributeIp4Address =
                    new ConfigAttributeIpv4Address(invalidValue);
            fail("Expected to fail due to invalid attribute value");
        } catch (InvalidSyntaxException expected) {
        }
    }

    @Test
    public void testEncodeIpv4AddressWithValue() throws Exception {
        ConfigAttributeIpv4Address attributeIp4Address =
                new ConfigAttributeIpv4Address(IPV4_ADDRESS);

        verifyEncodeIpv4AddresBaseAttribute(
                attributeIp4Address,
                CONFIG_ATTR_INTERNAL_IP4_ADDRESS,
                IPV4_ADDRESS_ATTRIBUTE_WITH_VALUE,
                IPV4_ADDRESS);
    }

    @Test
    public void testEncodeIpv4AddressWithoutValue() throws Exception {
        ConfigAttributeIpv4Address attributeIp4Address = new ConfigAttributeIpv4Address();

        verifyEncodeIpv4AddresBaseAttribute(
                attributeIp4Address,
                CONFIG_ATTR_INTERNAL_IP4_ADDRESS,
                IPV4_ADDRESS_ATTRIBUTE_WITHOUT_VALUE,
                null /*expectedAddress*/);
    }

    @Test
    public void testDecodeIpv4NetmaskWithValue() throws Exception {
        ConfigAttributeIpv4Netmask attribute =
                new ConfigAttributeIpv4Netmask(IPV4_NETMASK.getAddress());

        assertEquals(CONFIG_ATTR_INTERNAL_IP4_NETMASK, attribute.attributeType);
        assertEquals(IPV4_NETMASK, attribute.address);
    }

    @Test
    public void testDecodeIpv4NetmaskWithoutValue() throws Exception {
        ConfigAttributeIpv4Netmask attribute = new ConfigAttributeIpv4Netmask(new byte[0]);

        assertEquals(CONFIG_ATTR_INTERNAL_IP4_NETMASK, attribute.attributeType);
        assertNull(attribute.address);
    }

    @Test
    public void testEncodeIpv4Netmask() throws Exception {
        ConfigAttributeIpv4Netmask attribute = new ConfigAttributeIpv4Netmask();

        verifyEncodeIpv4AddresBaseAttribute(
                attribute,
                CONFIG_ATTR_INTERNAL_IP4_NETMASK,
                IPV4_NETMASK_ATTRIBUTE_WITHOUT_VALUE,
                null /*expectedAddress*/);
    }

    @Test
    public void testDecodeIpv4DnsWithValue() throws Exception {
        ConfigAttributeIpv4Dns attribute = new ConfigAttributeIpv4Dns(IPV4_DNS.getAddress());

        assertEquals(CONFIG_ATTR_INTERNAL_IP4_DNS, attribute.attributeType);
        assertEquals(IPV4_DNS, attribute.address);
    }

    @Test
    public void testDecodeIpv4DnsWithoutValue() throws Exception {
        ConfigAttributeIpv4Dns attribute = new ConfigAttributeIpv4Dns(new byte[0]);

        assertEquals(CONFIG_ATTR_INTERNAL_IP4_DNS, attribute.attributeType);
        assertNull(attribute.address);
    }

    @Test
    public void testEncodeIpv4Dns() throws Exception {
        ConfigAttributeIpv4Dns attribute = new ConfigAttributeIpv4Dns();

        verifyEncodeIpv4AddresBaseAttribute(
                attribute,
                CONFIG_ATTR_INTERNAL_IP4_DNS,
                IPV4_DNS_ATTRIBUTE_WITHOUT_VALUE,
                null /*expectedAddress*/);
    }

    @Test
    public void testDecodeIpv4SubnetWithValue() throws Exception {
        ConfigAttributeIpv4Subnet attributeIp4Subnet =
                new ConfigAttributeIpv4Subnet(IPV4_SUBNET_ATTRIBUTE_VALUE);

        assertEquals(CONFIG_ATTR_INTERNAL_IP4_SUBNET, attributeIp4Subnet.attributeType);
        assertEquals(IPV4_LINK_ADDRESS, attributeIp4Subnet.linkAddress);
    }

    @Test
    public void testDecodeIpv4SubnetWithoutValue() throws Exception {
        ConfigAttributeIpv4Subnet attributeIp4Subnet = new ConfigAttributeIpv4Subnet(new byte[0]);

        assertEquals(CONFIG_ATTR_INTERNAL_IP4_SUBNET, attributeIp4Subnet.attributeType);
        assertNull(attributeIp4Subnet.linkAddress);
    }

    @Test
    public void testDecodeIpv4SubnetWithInvalidValue() throws Exception {
        byte[] ipAddress = IPV4_ADDRESS.getAddress();
        ByteBuffer buffer = ByteBuffer.allocate(ipAddress.length * 2);
        buffer.put(ipAddress).put(ipAddress);

        try {
            new ConfigAttributeIpv4Subnet(buffer.array());
            fail("Expected to fail due to invalid netmask.");
        } catch (InvalidSyntaxException expected) {
        }
    }

    @Test
    public void testEncodeIpv4SubnetWithoutValue() throws Exception {
        ConfigAttributeIpv4Subnet attributeIp4Subnet = new ConfigAttributeIpv4Subnet();

        verifyBuildAndEncodeAttributeCommon(
                attributeIp4Subnet,
                CONFIG_ATTR_INTERNAL_IP4_SUBNET,
                IPV4_SUBNET_ATTRIBUTE_WITHOUT_VALUE);
        assertNull(attributeIp4Subnet.linkAddress);
    }

    @Test
    public void testNetmaskToPrefixLen() throws Exception {
        for (int i = 0; i < mNetMasks.length; i++) {
            assertEquals(mIpv4PrefixLens[i], ConfigAttribute.netmaskToPrefixLen(mNetMasks[i]));
        }
    }

    @Test
    public void testPrefixToNetmaskBytes() throws Exception {
        for (int i = 0; i < mIpv4PrefixLens.length; i++) {
            assertArrayEquals(
                    mNetMasks[i].getAddress(),
                    ConfigAttribute.prefixToNetmaskBytes(mIpv4PrefixLens[i]));
        }
    }

    @Test
    public void testDecodeIpv6AddressWithValue() throws Exception {
        ConfigAttributeIpv6Address attributeIp6Address =
                new ConfigAttributeIpv6Address(IPV6_ADDRESS_ATTRIBUTE_VALUE);

        assertEquals(CONFIG_ATTR_INTERNAL_IP6_ADDRESS, attributeIp6Address.attributeType);
        assertEquals(IPV6_LINK_ADDRESS, attributeIp6Address.linkAddress);
    }

    @Test
    public void testDecodeIpv6AddressWithoutValue() throws Exception {
        ConfigAttributeIpv6Address attributeIp6Address =
                new ConfigAttributeIpv6Address(new byte[0]);

        assertEquals(CONFIG_ATTR_INTERNAL_IP6_ADDRESS, attributeIp6Address.attributeType);
        assertNull(attributeIp6Address.linkAddress);
    }

    @Test
    public void testDecodeIpv6AddressWithInvalidValue() throws Exception {
        byte[] invalidValue = new byte[] {1};

        try {
            ConfigAttributeIpv6Address attributeIp6Address =
                    new ConfigAttributeIpv6Address(invalidValue);
            fail("Expected to fail due to invalid attribute value");
        } catch (InvalidSyntaxException expected) {
        }
    }

    @Test
    public void testEncodeIpv6AddressWithValue() throws Exception {
        ConfigAttributeIpv6Address attributeIp6Address =
                new ConfigAttributeIpv6Address(IPV6_LINK_ADDRESS);

        verifyEncodeIpv6RangeBaseAttribute(
                attributeIp6Address,
                CONFIG_ATTR_INTERNAL_IP6_ADDRESS,
                IPV6_ADDRESS_ATTRIBUTE_WITH_VALUE,
                IPV6_LINK_ADDRESS);
    }

    @Test
    public void testEncodeIpv6AddressWithoutValue() throws Exception {
        ConfigAttributeIpv6Address attributeIp6Address = new ConfigAttributeIpv6Address();

        verifyEncodeIpv6RangeBaseAttribute(
                attributeIp6Address,
                CONFIG_ATTR_INTERNAL_IP6_ADDRESS,
                IPV6_ADDRESS_ATTRIBUTE_WITHOUT_VALUE,
                null /*expectedLinkAddress*/);
    }

    @Test
    public void testDecodeIpv6SubnetWithValue() throws Exception {
        ConfigAttributeIpv6Subnet attributeIp6Subnet =
                new ConfigAttributeIpv6Subnet(IPV6_SUBNET_ATTRIBUTE_VALUE);

        assertEquals(CONFIG_ATTR_INTERNAL_IP6_SUBNET, attributeIp6Subnet.attributeType);
        assertEquals(IPV6_LINK_ADDRESS, attributeIp6Subnet.linkAddress);
    }

    @Test
    public void testDecodeIpv6SubnetWithoutValue() throws Exception {
        ConfigAttributeIpv6Subnet attributeIp6Subnet = new ConfigAttributeIpv6Subnet(new byte[0]);

        assertEquals(CONFIG_ATTR_INTERNAL_IP6_SUBNET, attributeIp6Subnet.attributeType);
        assertNull(attributeIp6Subnet.linkAddress);
    }

    @Test
    public void testEncodeIpv6SubnetWithoutValue() throws Exception {
        ConfigAttributeIpv6Subnet attributeIp6Subnet = new ConfigAttributeIpv6Subnet();

        verifyEncodeIpv6RangeBaseAttribute(
                attributeIp6Subnet,
                CONFIG_ATTR_INTERNAL_IP6_SUBNET,
                IPV6_SUBNET_ATTRIBUTE_WITHOUT_VALUE,
                null /*expectedLinkAddress*/);
    }

    @Test
    public void testDecodeIpv6DnsWithValue() throws Exception {
        ConfigAttributeIpv6Dns attribute = new ConfigAttributeIpv6Dns(IPV6_DNS.getAddress());

        assertEquals(CONFIG_ATTR_INTERNAL_IP6_DNS, attribute.attributeType);
        assertEquals(IPV6_DNS, attribute.address);
    }

    @Test
    public void testDecodeIpv6DnsWithoutValue() throws Exception {
        ConfigAttributeIpv6Dns attribute = new ConfigAttributeIpv6Dns(new byte[0]);

        assertEquals(CONFIG_ATTR_INTERNAL_IP6_DNS, attribute.attributeType);
        assertNull(attribute.address);
    }

    @Test
    public void testEncodeIpv6Dns() throws Exception {
        ConfigAttributeIpv6Dns attribute = new ConfigAttributeIpv6Dns();

        verifyBuildAndEncodeAttributeCommon(
                attribute, CONFIG_ATTR_INTERNAL_IP6_DNS, IPV6_DNS_ATTRIBUTE_WITHOUT_VALUE);
        assertNull(attribute.address);
    }
}