Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[bgpcep.git] / util / src / test / java / org / opendaylight / protocol / util / Ipv4UtilTest.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.Ipv4Util.writeIpv4Address;
12 import static org.opendaylight.protocol.util.Ipv4Util.writeIpv4Prefix;
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.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
19
20 public class Ipv4UtilTest {
21     private static final byte[] FOUR_BYTE_ZEROS = { 0, 0, 0, 0 };
22
23     @Test
24     public void testWriteIpv4Address() {
25         final byte[] result = { 127, 0, 0, 1 };
26         final ByteBuf output = Unpooled.buffer(Ipv4Util.IP4_LENGTH);
27         writeIpv4Address(new Ipv4Address("127.0.0.1"), output);
28         assertArrayEquals(result, output.array());
29
30         output.clear();
31         writeIpv4Address(null, output);
32         assertArrayEquals(FOUR_BYTE_ZEROS, output.array());
33     }
34
35     @Test
36     public void testWriteIpv4Prefix() {
37         final byte[] result = { 123, 122, 4, 5, 8 };
38         final ByteBuf output = Unpooled.buffer(Ipv4Util.PREFIX_BYTE_LENGTH);
39         writeIpv4Prefix(new Ipv4Prefix("123.122.4.5/8"), output);
40         assertArrayEquals(result, output.array());
41
42         output.clear();
43         final byte[] zeroResult = { 0, 0, 0, 0, 0 };
44         writeIpv4Prefix(null, output);
45         assertArrayEquals(zeroResult, output.array());
46     }
47 }