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.serializer.address;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
13 import java.nio.ByteBuffer;
15 import junitx.framework.ArrayAssert;
17 import org.junit.Test;
18 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
19 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializerContext;
20 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
21 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
22 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6Binary;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
28 public class InstanceIdSerializerTest extends BaseTestCase {
31 public void deserialize__Simple() throws Exception {
32 Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
34 "00 BB CC DD " + // instance ID
35 "00 01 11 22 33 44"), // AFI=1, IP=0x11223344
36 new LispAddressSerializerContext(null));
38 assertEquals(Ipv4BinaryAfi.class, address.getAddressType());
39 Ipv4Binary ipv4 = (Ipv4Binary) address.getAddress();
41 assertArrayEquals(new byte[] {0x11, 0x22, 0x33, 0x44}, ipv4.getIpv4Binary().getValue());
42 assertEquals(0x00BBCCDD, address.getVirtualNetworkId().getValue().longValue());
45 @Test(expected = LispSerializationException.class)
46 public void deserialize__ShorterBuffer() throws Exception {
47 LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
50 new LispAddressSerializerContext(null));
53 @Test(expected = LispSerializationException.class)
54 public void deserialize__UnknownLCAFType() throws Exception {
55 LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
56 "AA 20 00 0A " + // Type AA is unknown
57 "00 BB CC DD " + // instance ID
58 "00 01 11 22 33 44"), // AFI=1, IP=0x11223344
59 new LispAddressSerializerContext(null));
62 @Test(expected = LispSerializationException.class)
63 public void deserialize__LongInstanceID() throws Exception {
64 LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
65 "02 20 00 0A " + // Type AA is unknown
66 "AA BB CC DD " + // instance ID
67 "00 01 11 22 33 44"), // AFI=1, IP=0x11223344
68 new LispAddressSerializerContext(null));
72 public void deserialize__Ipv6() throws Exception {
73 Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 " + //
75 "00 BB CC DD " + // instance ID
76 "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD"), // AFI=2,
77 new LispAddressSerializerContext(null));
80 assertArrayEquals(new byte[] {(byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
81 (byte) 0x55, (byte) 0x66, (byte) 0x77, (byte) 0x88,
82 (byte) 0x99, (byte) 0xAA, (byte) 0xBB, (byte) 0xCC,
83 (byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD},
84 ((Ipv6Binary) address.getAddress()).getIpv6Binary().getValue());
89 public void serialize__Simple() throws Exception {
90 Eid eid = LispAddressUtil.asIpv4Eid("17.34.51.68", (long) 0x00020304);
92 ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eid));
93 LispAddressSerializer.getInstance().serialize(buf, eid);
94 ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 " + //
96 "00 02 03 04 " + // instance ID
98 ArrayAssert.assertEquals(expectedBuf.array(), buf.array());