2 * Copyright (c) 2014 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.implementation.util.ByteUtil;
19 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
20 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
21 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
22 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
23 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LcafApplicationDataAddress;
24 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
25 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lcafapplicationdataaddress.AddressBuilder;
26 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafApplicationDataBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
29 public class LispApplicationDataLCAFAddressTest extends BaseTestCase {
32 public void deserialize__Simple() throws Exception {
33 LispAFIAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
35 "AA BB CC DD " + // IPTOS & protocol
36 "A6 A1 FF DD " + // local port & remote port
37 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
39 assertEquals(AddressFamilyNumberEnum.LCAF.getIanaCode(), address.getAfi().shortValue());
40 LcafApplicationDataAddress appAddress = (LcafApplicationDataAddress) address;
42 assertEquals(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68"), appAddress.getAddress().getPrimitiveAddress());
43 assertEquals(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }), appAddress.getIpTos().intValue());
44 assertEquals((byte) 0xDD, appAddress.getProtocol().byteValue());
45 assertEquals((short) 0xA6A1, appAddress.getLocalPort().getValue().shortValue());
46 assertEquals((short) 0xFFDD, appAddress.getRemotePort().getValue().shortValue());
47 assertEquals(LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode(), appAddress.getLcafType().byteValue());
50 @Test(expected = LispSerializationException.class)
51 public void deserialize__ShorterBuffer() throws Exception {
52 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
57 @Test(expected = LispSerializationException.class)
58 public void deserialize__UnknownLCAFType() throws Exception {
59 LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
60 "AA 20 00 0A " + // Type AA is unknown
61 "AA BB CC DD " + // IPTOS & protocol
62 "A6 A1 FF DD " + // local port & remote port
63 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
67 public void deserialize__Ipv6() throws Exception {
68 LcafApplicationDataAddress appAddress = (LcafApplicationDataAddress) LispAddressSerializer.getInstance().deserialize(
69 hexToByteBuffer("40 03 00 00 " + //
71 "AA BB CC DD " + // IPTOS & protocol
72 "A6 A1 FF DD " + // local port & remote port
73 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD")); // AFI=2,
76 assertEquals(LispAFIConvertor.toPrimitive(LispAFIConvertor.asIPv6AfiAddress("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd")), appAddress
77 .getAddress().getPrimitiveAddress());
82 public void serialize__Simple() throws Exception {
83 LcafApplicationDataBuilder addressBuilder = new LcafApplicationDataBuilder();
84 addressBuilder.setIpTos(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }));
85 addressBuilder.setAfi(AddressFamilyNumberEnum.LCAF.getIanaCode()).setLcafType(
86 (short) LispCanonicalAddressFormatEnum.APPLICATION_DATA.getLispCode());
87 addressBuilder.setProtocol((short) 0xDD);
88 addressBuilder.setLocalPort(new PortNumber(0xA6A1));
89 addressBuilder.setRemotePort(new PortNumber(0xFFDD));
90 addressBuilder.setAddress(new AddressBuilder().setPrimitiveAddress(LispAFIConvertor.asPrimitiveIPAfiAddress("17.34.51.68")).build());
91 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(addressBuilder.build()));
92 LispAddressSerializer.getInstance().serialize(buf, addressBuilder.build());
93 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
95 "AA BB CC DD " + // IPTOS & protocol
96 "A6 A1 FF DD " + // local port & remote port
97 "00 01 11 22 33 44"); // AFI=1, IP=0x11223344
98 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());