2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.protocol.util;
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;
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.Ipv4AddressNoZone;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
20 public class Ipv4UtilTest {
21 private static final byte[] FOUR_BYTE_ZEROS = { 0, 0, 0, 0 };
24 public void testWriteIpv4Address() {
25 final byte[] result = { 127, 0, 0, 1 };
26 final ByteBuf output = Unpooled.buffer(Ipv4Util.IP4_LENGTH);
27 writeIpv4Address(new Ipv4AddressNoZone("127.0.0.1"), output);
28 assertArrayEquals(result, output.array());
31 writeIpv4Address(null, output);
32 assertArrayEquals(FOUR_BYTE_ZEROS, output.array());
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());
43 final byte[] zeroResult = { 0, 0, 0, 0, 0 };
44 writeIpv4Prefix(null, output);
45 assertArrayEquals(zeroResult, output.array());