2 * Copyright (c) 2013 Contextream, Inc. 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.lispflowmapping.implementation.serializer.address;
10 import static junit.framework.Assert.assertEquals;
12 import java.nio.ByteBuffer;
14 import junitx.framework.ArrayAssert;
16 import org.junit.Test;
17 import org.opendaylight.lispflowmapping.implementation.lisp.exception.LispSerializationException;
18 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
19 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
20 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
21 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
22 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
23 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
24 import org.opendaylight.lispflowmapping.type.lisp.address.LispSourceDestLCAFAddress;
26 public class LispSourceDestLCAFAddressTest extends BaseTestCase {
29 public void deserialize__Simple() throws Exception {
30 LispAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
32 "00 00 10 18 " + // reserved + masks
33 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
34 "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
36 assertEquals(AddressFamilyNumberEnum.LCAF, address.getAfi());
37 LispSourceDestLCAFAddress srcDestAddress = (LispSourceDestLCAFAddress) address;
39 assertEquals(0, srcDestAddress.getReserved());
40 assertEquals((byte)0x10, srcDestAddress.getSrcMaskLength());
41 assertEquals((byte)0x18, srcDestAddress.getDstMaskLength());
43 assertEquals(new LispIpv4Address(0x11223344), srcDestAddress.getSrcAddress());
44 assertEquals(new LispIpv4Address(0x22334455), srcDestAddress.getDstAddress());
45 assertEquals(LispCanonicalAddressFormatEnum.SOURCE_DEST, srcDestAddress.getType());
48 @Test(expected = LispSerializationException.class)
49 public void deserialize__ShorterBuffer() throws Exception {
50 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
55 @Test(expected = LispSerializationException.class)
56 public void deserialize__UnknownLCAFType() throws Exception {
57 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
58 "AA 20 00 0A " + // Type AA is unknown
59 "00 00 CC DD " + // reserved + masks
60 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
61 "00 01 22 33 44 55")); // AFI=1, IP=0x22334455
65 public void deserialize__Ipv6() throws Exception {
66 LispSourceDestLCAFAddress srcAddress = (LispSourceDestLCAFAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
68 "00 00 CC DD " + // reserved + masks
69 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD " + // AFI=2,
70 "00 02 44 33 22 11 88 77 66 55 99 AA BB CC AA BB CC DD")); // AFI=2,
73 assertEquals(new LispIpv6Address(new byte[] { 0x11, 0x22, 0x33, 0x44, //
74 0x55, 0x66, 0x77, (byte) 0x88, //
75 (byte) 0x99, (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, //
76 (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD }), srcAddress.getSrcAddress());
77 assertEquals(new LispIpv6Address(new byte[] { 0x44, 0x33, 0x22, 0x11, //
78 (byte) 0x88, 0x77, 0x66, 0x55, //
79 (byte) 0x99, (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, //
80 (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD }), srcAddress.getDstAddress());
85 public void serialize__Simple() throws Exception {
86 LispSourceDestLCAFAddress address = new LispSourceDestLCAFAddress((byte) 0x20, (short)0x0000, (byte) 0xCC, (byte) 0xDD, new LispIpv4Address(0x11223344), new LispIpv4Address(0x22334455));
87 ByteBuffer buf = ByteBuffer.allocate(LispSourceDestLCAFAddressSerializer.getInstance().getAddressSize(address));
88 LispSourceDestLCAFAddressSerializer.getInstance().serialize(buf,address);
89 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
91 "00 00 CC DD " + // reserved + masks
92 "00 01 11 22 33 44 " + // AFI=1, IP=0x11223344
93 "00 01 22 33 44 55"); // AFI=1, IP=0x22334455
94 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());