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.southbound.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.southbound.lisp.exception.LispMalformedPacketException;
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.LispSegmentLCAFAddress;
26 public class LispSegmentLCAFAddressTest extends BaseTestCase {
29 public void deserialize__Simple() throws Exception {
30 LispAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
32 "AA BB CC DD " + // instance ID
33 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
35 assertEquals(AddressFamilyNumberEnum.LCAF, address.getAfi());
36 LispSegmentLCAFAddress segAddress = (LispSegmentLCAFAddress) address;
38 assertEquals(new LispIpv4Address(0x11223344), segAddress.getAddress());
39 assertEquals(0xAABBCCDD, segAddress.getInstanceId());
40 assertEquals(0x20, segAddress.getIdMaskLen());
41 assertEquals(LispCanonicalAddressFormatEnum.SEGMENT, segAddress.getType());
44 @Test(expected = LispMalformedPacketException.class)
45 public void deserialize__ShorterBuffer() throws Exception {
46 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
51 @Test(expected = LispMalformedPacketException.class)
52 public void deserialize__UnknownLCAFType() throws Exception {
53 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
54 "AA 20 00 0A " + // Type AA is unknown
55 "AA BB CC DD " + // instance ID
56 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
60 public void deserialize__Ipv6() throws Exception {
61 LispSegmentLCAFAddress segAddress = (LispSegmentLCAFAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
63 "AA BB CC DD " + // instance ID
64 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD")); // AFI=2,
67 assertEquals(new LispIpv6Address(new byte[] { 0x11, 0x22, 0x33, 0x44, //
68 0x55, 0x66, 0x77, (byte) 0x88, //
69 (byte) 0x99, (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, //
70 (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD }), segAddress.getAddress());
75 public void serialize__Simple() throws Exception {
76 LispSegmentLCAFAddress address = new LispSegmentLCAFAddress((byte) 0x06, 0x01020304, new LispIpv4Address(0x11223344));
77 ByteBuffer buf = ByteBuffer.allocate(LispSegmentLCAFAddressSerializer.getInstance().getAddressSize(address));
78 LispSegmentLCAFAddressSerializer.getInstance().serialize(buf,address);
79 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
81 "01 02 03 04 " + // instance ID
83 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());