added authentication to map notify and map reply, and moved the serialization process...
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / serializer / address / LispSourceDestLCAFAddressTest.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.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;
25
26 public class LispSourceDestLCAFAddressTest extends BaseTestCase {
27
28     @Test
29     public void deserialize__Simple() throws Exception {
30         LispAddress address = LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
31                 "0C 20 00 10 " + //
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
35
36         assertEquals(AddressFamilyNumberEnum.LCAF, address.getAfi());
37         LispSourceDestLCAFAddress srcDestAddress = (LispSourceDestLCAFAddress) address;
38         
39         assertEquals(0, srcDestAddress.getReserved());
40         assertEquals((byte)0x10, srcDestAddress.getSrcMaskLength());
41         assertEquals((byte)0x18, srcDestAddress.getDstMaskLength());
42
43         assertEquals(new LispIpv4Address(0x11223344), srcDestAddress.getSrcAddress());
44         assertEquals(new LispIpv4Address(0x22334455), srcDestAddress.getDstAddress());
45         assertEquals(LispCanonicalAddressFormatEnum.SOURCE_DEST, srcDestAddress.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                 "02 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                 "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
62     }
63
64     @Test
65     public void deserialize__Ipv6() throws Exception {
66         LispSourceDestLCAFAddress srcAddress = (LispSourceDestLCAFAddress) LispAddressSerializer.getInstance().deserialize(hexToByteBuffer("40 03 00 00 " + //
67                         "0C 20 00 28 " + //
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,
71         // IPv6
72
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());
81
82     }
83
84     @Test
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 " + //
90                          "0C 20 00 10 " + //
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());
95     }
96 }