Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[bgpcep.git] / util / src / test / java / org / opendaylight / protocol / util / Ipv6UtilTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.util;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.opendaylight.protocol.util.Ipv6Util.writeIpv6Address;
12 import static org.opendaylight.protocol.util.Ipv6Util.writeIpv6Prefix;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.junit.Test;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
19
20 public class Ipv6UtilTest {
21     @Test
22     public void testWriteIpv6Address() {
23         final byte[] result = { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24             0x00, 0x00, 0x01 };
25         final ByteBuf output = Unpooled.buffer(Ipv6Util.IPV6_LENGTH);
26         writeIpv6Address(new Ipv6Address("2001::1"), output);
27         assertArrayEquals(result, output.array());
28
29         output.clear();
30         final byte[] zeroResult = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
31         writeIpv6Address(null, output);
32         assertArrayEquals(zeroResult, output.array());
33     }
34
35     @Test
36     public void testWriteIpv6Prefix() {
37         final byte[] result = { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
38             0x00, 0x00, 0x00, 0x00, 0x40 };
39         final ByteBuf output = Unpooled.buffer(Ipv6Util.PREFIX_BYTE_LENGTH);
40         writeIpv6Prefix(new Ipv6Prefix("2001:db8:1:2::/64"), output);
41         assertArrayEquals(result, output.array());
42
43         output.clear();
44         final byte[] zeroResult = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
45         writeIpv6Prefix(null, output);
46         assertArrayEquals(zeroResult, output.array());
47     }
48 }