Remove use of junit-addons
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / SourceDestKeySerializerTest.java
1 /*
2  * Copyright (c) 2014 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.serializer.address;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12
13 import java.nio.ByteBuffer;
14 import org.junit.Test;
15 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializer;
16 import org.opendaylight.lispflowmapping.lisp.serializer.address.LispAddressSerializerContext;
17 import org.opendaylight.lispflowmapping.lisp.serializer.exception.LispSerializationException;
18 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
19 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SourceDestKeyLcaf;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.source.dest.key.SourceDestKeyBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
28
29 public class SourceDestKeySerializerTest extends BaseTestCase {
30
31     @Test
32     public void deserialize__Simple() throws Exception {
33         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 "
34                 + "0C 20 00 10 "
35                 + "00 00 10 18 " // reserved + masks
36                 + "00 01 11 22 33 44 "  // AFI=1, IP=0x11223344
37                 + "00 01 22 33 44 55"), // AFI=1, IP=0x22334455
38                 new LispAddressSerializerContext(null));
39
40         assertEquals(SourceDestKeyLcaf.VALUE, address.getAddressType());
41         SourceDestKey srcDestAddress = (SourceDestKey) address.getAddress();
42
43         assertEquals((byte) 0x10, MaskUtil.getMaskForAddress(srcDestAddress.getSourceDestKey().getSource()));
44         assertEquals((byte) 0x18, MaskUtil.getMaskForAddress(srcDestAddress.getSourceDestKey().getDest()));
45
46         assertEquals("17.34.51.68/16", srcDestAddress.getSourceDestKey().getSource().stringValue());
47         assertEquals("34.51.68.85/24", srcDestAddress.getSourceDestKey().getDest().stringValue());
48     }
49
50     @Test(expected = LispSerializationException.class)
51     public void deserialize__ShorterBuffer() throws Exception {
52         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("""
53             40 03 00 00 \
54             02 20 00 0A \
55             AA BB \
56             """), new LispAddressSerializerContext(null));
57     }
58
59     @Test(expected = LispSerializationException.class)
60     public void deserialize__UnknownLCAFType() throws Exception {
61         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("""
62             40 03 00 00 \
63             AA 20 00 0A \
64             00 00 CC DD \
65             00 01 11 22 33 44 \
66             00 01 22 33 44 55"""), // AFI=1, IP=0x22334455
67                 new LispAddressSerializerContext(null));
68     }
69
70     @Test
71     public void deserialize__Ipv6() throws Exception {
72         Eid srcAddress = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 "
73                 + "0C 20 00 28 "
74                 + "00 00 78 78 " // reserved + masks
75                 + "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD "  // AFI=2,
76                 + "00 02 44 33 22 11 88 77 66 55 99 AA BB CC AA BB CC DD"), // AFI=2,
77                 new LispAddressSerializerContext(null));
78         // IPv6
79
80         assertEquals("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd/120",
81                 ((SourceDestKey) srcAddress.getAddress()).getSourceDestKey().getSource().stringValue());
82         assertEquals("4433:2211:8877:6655:99aa:bbcc:aabb:ccdd/120",
83                 ((SourceDestKey) srcAddress.getAddress()).getSourceDestKey().getDest().stringValue());
84     }
85
86     @Test
87     public void serialize__Simple() throws Exception {
88         SourceDestKeyBuilder addressBuilder = new SourceDestKeyBuilder();
89         addressBuilder.setSource(new SimpleAddress(new IpPrefix(new Ipv4Prefix("17.34.51.68/8"))));
90         addressBuilder.setDest(new SimpleAddress(new IpPrefix(new Ipv4Prefix("34.51.68.85/16"))));
91
92         EidBuilder eb = new EidBuilder();
93         eb.setAddressType(SourceDestKeyLcaf.VALUE);
94         eb.setVirtualNetworkId(null);
95         eb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
96             .lisp.address.address.SourceDestKeyBuilder()
97             .setSourceDestKey(addressBuilder.build()).build());
98
99         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eb.build()));
100         LispAddressSerializer.getInstance().serialize(buf, eb.build());
101
102         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
103                 + "0C 00 00 10 " //
104                 + "00 00 08 10 " // reserved + masks
105                 + "00 01 11 22 33 44 "  // AFI=1, IP=0x11223344
106                 + "00 01 22 33 44 55"); // AFI=1, IP=0x22334455
107         assertArrayEquals(expectedBuf.array(), buf.array());
108     }
109 }