added source/dest lookup lcaf TELSDN-176: #close
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispApplicationDataLCAFAddressTest.java
1 /*
2  * Copyright (c) 2013 Contextream, Inc. 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.lispflowmapping.implementation.serializer.address;
9
10 import static junit.framework.Assert.assertEquals;
11
12 import java.nio.ByteBuffer;
13
14 import junitx.framework.ArrayAssert;
15
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.tools.junit.BaseTestCase;
20 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
21 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
22 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
23 import org.opendaylight.lispflowmapping.type.lisp.address.LispApplicationDataLCAFAddress;
24 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
25 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
26
27 public class LispApplicationDataLCAFAddressTest extends BaseTestCase {
28
29     @Test
30     public void deserialize__Simple() throws Exception {
31         LispAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
32                 "04 20 00 0A " + //
33                 "AA BB CC DD " + // IPTOS & protocol
34                 "A6 A1 FF DD " + // local port & remote port
35                 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
36
37         assertEquals(AddressFamilyNumberEnum.LCAF, address.getAfi());
38         LispApplicationDataLCAFAddress appAddress = (LispApplicationDataLCAFAddress) address;
39
40         assertEquals(new LispIpv4Address(0x11223344), appAddress.getAddress());
41         assertEquals(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }), appAddress.getIPTos());
42         assertEquals((byte) 0xDD, appAddress.getProtocol());
43         assertEquals((short) 0xA6A1, appAddress.getLocalPort());
44         assertEquals((short) 0xFFDD, appAddress.getRemotePort());
45         assertEquals(LispCanonicalAddressFormatEnum.APPLICATION_DATA, appAddress.getType());
46     }
47
48     @Test(expected = LispSerializationException.class)
49     public void deserialize__ShorterBuffer() throws Exception {
50         LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
51                 "04 20 00 0A " + //
52                 "AA BB "));
53     }
54
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                 "AA BB CC DD " + // IPTOS & protocol
60                 "A6 A1 FF DD " + // local port & remote port
61                 "00 01 11 22 33 44")); // AFI=1, IP=0x11223344
62     }
63
64     @Test
65     public void deserialize__Ipv6() throws Exception {
66         LispApplicationDataLCAFAddress appAddress = (LispApplicationDataLCAFAddress) LispAddressSerializer.getInstance().deserialize(
67                 hexToByteBuffer("40 03 00 00 " + //
68                         "04 20 00 0A " + //
69                         "AA BB CC DD " + // IPTOS & protocol
70                         "A6 A1 FF DD " + // local port & remote port
71                         "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD")); // AFI=2,
72         // IPv6
73
74         assertEquals(new LispIpv6Address(new byte[] { 0x11, 0x22, 0x33, 0x44, //
75                 0x55, 0x66, 0x77, (byte) 0x88, //
76                 (byte) 0x99, (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, //
77                 (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD }), appAddress.getAddress());
78
79     }
80
81     @Test
82     public void serialize__Simple() throws Exception {
83         LispApplicationDataLCAFAddress address = new LispApplicationDataLCAFAddress((byte) 0x20);
84         address.setIPTos(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }));
85         address.setProtocol((byte) 0xDD);
86         address.setLocalPort((short) 0xA6A1);
87         address.setRemotePort((short) 0xFFDD);
88         address.setAddress(new LispIpv4Address(0x11223344));
89         ByteBuffer buf = ByteBuffer.allocate(LispApplicationDataLCAFAddressSerializer.getInstance().getAddressSize(address));
90         LispApplicationDataLCAFAddressSerializer.getInstance().serialize(buf, address);
91         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
92                 "04 20 00 0E " + //
93                 "AA BB CC DD " + // IPTOS & protocol
94                 "A6 A1 FF DD " + // local port & remote port
95                 "00 01 11 22 33 44"); // AFI=1, IP=0x11223344
96         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
97     }
98 }