Remove use of junit-addons
[lispflowmapping.git] / mappingservice / lisp-proto / src / test / java / org / opendaylight / lispflowmapping / serializer / address / ApplicationDataSerializerTest.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.exception.LispSerializationException;
17 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
18 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.ApplicationDataLcaf;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ApplicationData;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.application.data.ApplicationDataBuilder;
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 import org.opendaylight.yangtools.yang.common.Uint16;
29 import org.opendaylight.yangtools.yang.common.Uint8;
30
31 public class ApplicationDataSerializerTest extends BaseTestCase {
32
33     @Test
34     public void deserialize__Simple() throws Exception {
35         Eid address = LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("40 03 00 00 "
36                 + "04 20 00 0E "
37                 + "AA BB CC DD " // IPTOS & protocol
38                 + "A6 A1 A6 A2 " // local port range
39                 + "FF DD FF DE " // remote port range
40                 + "00 01 11 22 33 44"), null); // AFI=1, IP=0x11223344
41
42         assertEquals(ApplicationDataLcaf.VALUE, address.getAddressType());
43         ApplicationData appAddress = (ApplicationData) address.getAddress();
44
45         assertEquals("17.34.51.68", appAddress.getApplicationData().getAddress().stringValue());
46         assertEquals(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }),
47                 appAddress.getApplicationData().getIpTos().intValue());
48         assertEquals((byte) 0xDD, appAddress.getApplicationData().getProtocol().byteValue());
49         assertEquals((short) 0xA6A1, appAddress.getApplicationData().getLocalPortLow().getValue().shortValue());
50         assertEquals((short) 0xA6A2, appAddress.getApplicationData().getLocalPortHigh().getValue().shortValue());
51         assertEquals((short) 0xFFDD, appAddress.getApplicationData().getRemotePortLow().getValue().shortValue());
52         assertEquals((short) 0xFFDE, appAddress.getApplicationData().getRemotePortHigh().getValue().shortValue());
53     }
54
55     @Test(expected = LispSerializationException.class)
56     public void deserialize__ShorterBuffer() throws Exception {
57         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("""
58             40 03 00 00 \
59             04 20 00 0A \
60             AA BB \
61             """), null);
62     }
63
64     @Test(expected = LispSerializationException.class)
65     public void deserialize__UnknownLCAFType() throws Exception {
66         LispAddressSerializer.getInstance().deserializeEid(hexToByteBuffer("""
67             40 03 00 00 \
68             AA 20 00 12 \
69             AA BB CC DD \
70             A6 A1 A6 A2 \
71             FF DD FF DE \
72             00 01 11 22 33 44"""), null); // AFI=1, IP=0x11223344
73     }
74
75     @Test
76     public void deserialize__Ipv6() throws Exception {
77         Eid appAddress = LispAddressSerializer.getInstance().deserializeEid(
78                 hexToByteBuffer("40 03 00 00 "
79                         + "04 20 00 1E "
80                         + "AA BB CC DD " // IPTOS & protocol
81                         + "A6 A1 A6 A2 " // local port range
82                         + "FF DD FF DE " // remote port range
83                         + "00 02 11 22 33 44 55 66 77 88 99 AA BB CC AA BB CC DD"), null); // AFI=2,
84         // IPv6
85
86         assertEquals("1122:3344:5566:7788:99aa:bbcc:aabb:ccdd",
87                 ((ApplicationData) appAddress.getAddress()).getApplicationData().getAddress().stringValue());
88     }
89
90     @Test
91     public void serialize__Simple() throws Exception {
92         ApplicationDataBuilder addressBuilder = new ApplicationDataBuilder()
93             .setIpTos(ByteUtil.getPartialInt(new byte[] { (byte) 0xAA, (byte) 0xBB, (byte) 0xCC }))
94             .setProtocol(Uint8.valueOf(0xDD))
95             .setLocalPortLow(new PortNumber(Uint16.valueOf(0xA6A1)))
96             .setLocalPortHigh(new PortNumber(Uint16.valueOf(0xA6A2)))
97             .setRemotePortLow(new PortNumber(Uint16.valueOf(0xFFDD)))
98             .setRemotePortHigh(new PortNumber(Uint16.valueOf(0xFFDE)))
99             .setAddress(new SimpleAddress(new IpAddress(new Ipv4Address("17.34.51.68"))));
100
101         EidBuilder eb = new EidBuilder();
102         eb.setAddressType(ApplicationDataLcaf.VALUE);
103         eb.setVirtualNetworkId(null);
104         eb.setAddress(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105
105             .lisp.address.address.ApplicationDataBuilder()
106             .setApplicationData(addressBuilder.build()).build());
107
108         ByteBuffer buf = ByteBuffer.allocate(LispAddressSerializer.getInstance().getAddressSize(eb.build()));
109         LispAddressSerializer.getInstance().serialize(buf, eb.build());
110         ByteBuffer expectedBuf = hexToByteBuffer("40 03 00 00 "
111                 + "04 00 00 12 " //
112                 + "AA BB CC DD " // IPTOS & protocol
113                 + "A6 A1 A6 A2 " // local port range
114                 + "FF DD FF DE " // remote port range
115                 + "00 01 11 22 33 44"); // AFI=1, IP=0x11223344
116         assertArrayEquals(expectedBuf.array(), buf.array());
117     }
118 }