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.type.lisp.address;
10 import static junit.framework.Assert.assertEquals;
12 import java.nio.ByteBuffer;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
17 import junitx.framework.ArrayAssert;
18 import junitx.framework.Assert;
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;
26 public class LispListLCAFAddressTest extends BaseTestCase {
29 public void deserialize__Simple() throws Exception {
30 LispAddress address = LispAddress.valueOf(hexToByteBuffer("40 03 00 00 " + //
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
35 assertEquals(AddressFamilyNumberEnum.LCAF, address.getAfi());
36 LispListLCAFAddress lcafList = (LispListLCAFAddress) address;
38 assertEquals(LispCanonicalAddressFormatEnum.LIST, lcafList.getType());
40 List<LispAddress> addressList = lcafList.getAddresses();
41 assertEquals(2, addressList.size());
43 assertEquals(new LispIpv4Address(0xAABBCCDD), addressList.get(0));
44 assertEquals(new LispIpv6Address("1122:3344:1122:3344:1122:3344:1122:3344"), addressList.get(1));
48 public void deserialize__NoAddresses() throws Exception {
49 LispAddress address = LispAddress.valueOf(hexToByteBuffer("40 03 00 00 " + //
52 assertEquals(AddressFamilyNumberEnum.LCAF, address.getAfi());
53 LispListLCAFAddress lcafList = (LispListLCAFAddress) address;
55 assertEquals(LispCanonicalAddressFormatEnum.LIST, lcafList.getType());
57 List<LispAddress> addressList = lcafList.getAddresses();
58 assertEquals(0, addressList.size());
61 @Test(expected = LispMalformedPacketException.class)
62 public void deserialize__ShorterBuffer() throws Exception {
63 LispAddress.valueOf(hexToByteBuffer("40 03 00 00 " + //
65 "00 01 AA BB CC DD " + // IPv4
66 "00 02 11 22 33 44 11 22 33 44 11 22 33 44"));
69 @Test(expected = LispMalformedPacketException.class)
70 public void deserialize__ShorterBuffer2() throws Exception {
71 LispAddress.valueOf(hexToByteBuffer("40 03 00 00 " + //
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);
82 ByteBuffer buf = ByteBuffer.allocate(address.getAddressSize());
83 address.serialize(buf);
84 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
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());
92 public void serialize__NoAddresses() throws Exception {
93 LispListLCAFAddress address = new LispListLCAFAddress((byte) 0, new ArrayList<LispAddress>());
95 ByteBuffer buf = ByteBuffer.allocate(address.getAddressSize());
96 address.serialize(buf);
97 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
99 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());
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>());
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);