4221b624f0a0c7792d174a9f3464228aecac0003
[lispflowmapping.git] / mappingservice / api / src / test / java / org / opendaylight / lispflowmapping / type / lisp / address / LispListLCAFAddressTest.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.type.lisp.address;
9
10 import static junit.framework.Assert.assertEquals;
11
12 import java.nio.ByteBuffer;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16
17 import junitx.framework.ArrayAssert;
18 import junitx.framework.Assert;
19
20 import org.junit.Test;
21 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
22 import org.opendaylight.lispflowmapping.type.LispCanonicalAddressFormatEnum;
23 import org.opendaylight.lispflowmapping.type.exception.LispMalformedPacketException;
24 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
25
26 public class LispListLCAFAddressTest extends BaseTestCase {
27
28     @Test
29     public void deserialize__Simple() throws Exception {
30         LispAddress address = LispAddress.valueOf(hexToByteBuffer("40 03 00 00 " + //
31                 "01 00 00 18 " + //
32                 "00 01 AA BB CC DD " + // IPv4
33                 "00 02 11 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44")); // IPv6
34
35         assertEquals(AddressFamilyNumberEnum.LCAF, address.getAfi());
36         LispListLCAFAddress lcafList = (LispListLCAFAddress) address;
37
38         assertEquals(LispCanonicalAddressFormatEnum.LIST, lcafList.getType());
39
40         List<LispAddress> addressList = lcafList.getAddresses();
41         assertEquals(2, addressList.size());
42
43         assertEquals(new LispIpv4Address(0xAABBCCDD), addressList.get(0));
44         assertEquals(new LispIpv6Address("1122:3344:1122:3344:1122:3344:1122:3344"), addressList.get(1));
45     }
46
47     @Test
48     public void deserialize__NoAddresses() throws Exception {
49         LispAddress address = LispAddress.valueOf(hexToByteBuffer("40 03 00 00 " + //
50                 "01 00 00 00 "));
51
52         assertEquals(AddressFamilyNumberEnum.LCAF, address.getAfi());
53         LispListLCAFAddress lcafList = (LispListLCAFAddress) address;
54
55         assertEquals(LispCanonicalAddressFormatEnum.LIST, lcafList.getType());
56
57         List<LispAddress> addressList = lcafList.getAddresses();
58         assertEquals(0, addressList.size());
59     }
60
61     @Test(expected = LispMalformedPacketException.class)
62     public void deserialize__ShorterBuffer() throws Exception {
63         LispAddress.valueOf(hexToByteBuffer("40 03 00 00 " + //
64                 "01 00 00 18 " + //
65                 "00 01 AA BB CC DD " + // IPv4
66                 "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
67     }
68
69     @Test(expected = LispMalformedPacketException.class)
70     public void deserialize__ShorterBuffer2() throws Exception {
71         LispAddress.valueOf(hexToByteBuffer("40 03 00 00 " + //
72                 "01 00 00 18 "));
73     }
74
75     @Test
76     public void serialize__Simple() throws Exception {
77         List<LispAddress> addressList = new ArrayList<LispAddress>();
78         addressList.add(new LispIpv4Address(0xAABBCCDD));
79         addressList.add(new LispIpv6Address("1222:3344:1122:3344:1122:3344:1122:3344"));
80         LispListLCAFAddress address = new LispListLCAFAddress((byte) 0, addressList);
81
82         ByteBuffer buf = ByteBuffer.allocate(address.getAddressSize());
83         address.serialize(buf);
84         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
85                 "01 00 00 18 " + //
86                 "00 01 AA BB CC DD " + // IPv4
87                 "00 02 12 22 33 44 11 22 33 44 11 22 33 44 11 22 33 44");
88         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
89     }
90
91     @Test
92     public void serialize__NoAddresses() throws Exception {
93         LispListLCAFAddress address = new LispListLCAFAddress((byte) 0, new ArrayList<LispAddress>());
94
95         ByteBuffer buf = ByteBuffer.allocate(address.getAddressSize());
96         address.serialize(buf);
97         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
98                 "01 00 00 00");
99         ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
100     }
101
102     @Test
103     public void equals__Simple() throws Exception {
104         LispListLCAFAddress address1 = new LispListLCAFAddress((byte) 0x06, Arrays.asList(new LispIpv4Address(0x11223344), //
105                 new LispIpv6Address("::1")));
106         LispListLCAFAddress address2 = new LispListLCAFAddress((byte) 0x06, Arrays.asList(new LispIpv4Address(0x11223344), //
107                 new LispIpv6Address("::1")));
108         LispListLCAFAddress address3 = new LispListLCAFAddress((byte) 0x06, Arrays.asList(new LispIpv4Address(0x11223344), //
109                 new LispIpv6Address("::2")));
110         LispListLCAFAddress address4 = new LispListLCAFAddress((byte) 0x05, Arrays.asList(new LispIpv4Address(0x11223344), //
111                 new LispIpv6Address("::1")));
112         LispListLCAFAddress address5 = new LispListLCAFAddress((byte) 0x06, new ArrayList<LispAddress>());
113
114         assertEquals(address1, address2);
115         assertEquals(address1, address1);
116         Assert.assertNotEquals(address1, address3);
117         Assert.assertNotEquals(address1, address4);
118         Assert.assertNotEquals(address1, address5);
119         Assert.assertNotEquals(address3, address4);
120         Assert.assertNotEquals(address3, address5);
121         Assert.assertNotEquals(address4, address5);
122     }
123 }