20347cd3823a48a3c2b86ed81431d1d4d24a1184
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPTlvParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, 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.protocol.pcep.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.junit.Test;
19
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.protocol.concepts.IPv4Address;
22 import org.opendaylight.protocol.concepts.IPv6Address;
23 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
24 import org.opendaylight.protocol.pcep.PCEPOFCodes;
25 import org.opendaylight.protocol.pcep.PCEPTlv;
26 import org.opendaylight.protocol.pcep.concepts.IPv4ExtendedTunnelIdentifier;
27 import org.opendaylight.protocol.pcep.concepts.IPv6ExtendedTunnelIdentifier;
28 import org.opendaylight.protocol.pcep.concepts.LSPIdentifier;
29 import org.opendaylight.protocol.pcep.concepts.LSPSymbolicName;
30 import org.opendaylight.protocol.pcep.concepts.TunnelIdentifier;
31 import org.opendaylight.protocol.pcep.impl.tlv.LSPIdentifierIPv4TlvParser;
32 import org.opendaylight.protocol.pcep.impl.tlv.LSPIdentifierIPv6TlvParser;
33 import org.opendaylight.protocol.pcep.impl.tlv.OFListTlvParser;
34 import org.opendaylight.protocol.pcep.impl.tlv.PCEStatefulCapabilityTlvParser;
35 import org.opendaylight.protocol.pcep.impl.tlv.RSVPErrorSpecIPv4TlvParser;
36 import org.opendaylight.protocol.pcep.impl.tlv.RSVPErrorSpecIPv6TlvParser;
37 import org.opendaylight.protocol.pcep.tlv.IPv4LSPIdentifiersTlv;
38 import org.opendaylight.protocol.pcep.tlv.IPv6LSPIdentifiersTlv;
39 import org.opendaylight.protocol.pcep.tlv.LSPStateDBVersionTlv;
40 import org.opendaylight.protocol.pcep.tlv.LSPSymbolicNameTlv;
41 import org.opendaylight.protocol.pcep.tlv.LSPUpdateErrorTlv;
42 import org.opendaylight.protocol.pcep.tlv.NoPathVectorTlv;
43 import org.opendaylight.protocol.pcep.tlv.NodeIdentifierTlv;
44 import org.opendaylight.protocol.pcep.tlv.OFListTlv;
45 import org.opendaylight.protocol.pcep.tlv.OrderTlv;
46 import org.opendaylight.protocol.pcep.tlv.OverloadedDurationTlv;
47 import org.opendaylight.protocol.pcep.tlv.P2MPCapabilityTlv;
48 import org.opendaylight.protocol.pcep.tlv.PCEStatefulCapabilityTlv;
49 import org.opendaylight.protocol.pcep.tlv.RSVPErrorSpecTlv;
50 import org.opendaylight.protocol.pcep.tlv.ReqMissingTlv;
51
52 /**
53  * Tests of PCEPTlvParser
54  */
55 public class PCEPTlvParserTest {
56
57     @Test
58     public void testDeserialization() throws PCEPDeserializerException, IOException {
59         final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PackOfTlvs.bin");
60         final List<PCEPTlv> tlvsToTest = PCEPTlvParser.parse(bytesFromFile);
61
62         assertEquals(17, tlvsToTest.size());
63         assertEquals(tlvsToTest.get(0), new PCEStatefulCapabilityTlv(false, false, true));
64         assertEquals(tlvsToTest.get(1), new LSPStateDBVersionTlv(0xFF00FFAAB2F5F2CFL));
65         assertEquals(tlvsToTest.get(2), new PCEStatefulCapabilityTlv(false, true, true));
66         assertEquals(tlvsToTest.get(3), new LSPStateDBVersionTlv(0xFFFFFFFFFFFFFFFFL));
67         assertEquals(tlvsToTest.get(4), new NoPathVectorTlv(true, true, true, false, true, true));
68         assertEquals(tlvsToTest.get(5), new OverloadedDurationTlv(0x7FFFFFFF));
69         assertEquals(tlvsToTest.get(6), new LSPSymbolicNameTlv(new LSPSymbolicName(new String("Med test of symbolic name").getBytes())));
70         final byte[] errorCode = { (byte) 0x25, (byte) 0x68, (byte) 0x95, (byte) 0x03 };
71         assertEquals(tlvsToTest.get(7), new LSPUpdateErrorTlv(errorCode));
72         final byte[] ipv4Address = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
73         final byte[] tunnelId1 = { (byte) 0x12, (byte) 0x34 };
74         final byte[] extendedTunnelID1 = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 };
75         final byte[] lspId1 = { (byte) 0xFF, (byte) 0xFF };
76         assertEquals(tlvsToTest.get(8), new IPv4LSPIdentifiersTlv(new IPv4Address(ipv4Address),
77                 new LSPIdentifier(lspId1), new TunnelIdentifier(tunnelId1), new IPv4ExtendedTunnelIdentifier(new IPv4Address(extendedTunnelID1))));
78         final byte[] ipv6Address = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0, (byte) 0x12,
79                 (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 };
80         final byte[] tunnelId2 = { (byte) 0xFF, (byte) 0xFF };
81         final byte[] extendedTunnelID2 = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x01,
82                 (byte) 0x23, (byte) 0x45, (byte) 0x67, (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67 };
83         final byte[] lspId2 = { (byte) 0x12, (byte) 0x34 };
84         assertEquals(tlvsToTest.get(9), new IPv6LSPIdentifiersTlv(new IPv6Address(ipv6Address),
85                 new LSPIdentifier(lspId2), new TunnelIdentifier(tunnelId2), new IPv6ExtendedTunnelIdentifier(new IPv6Address(extendedTunnelID2))));
86         assertEquals(tlvsToTest.get(10), new RSVPErrorSpecTlv<IPv4Address>(new IPv4Address(ipv4Address), false, true, 0x92, 0x1602));
87         assertEquals(tlvsToTest.get(11), new RSVPErrorSpecTlv<IPv6Address>(new IPv6Address(ipv6Address), true, false, 0xD5, 0xC5D9));
88         assertEquals(tlvsToTest.get(12), new ReqMissingTlv(0xF7823517L));
89         final byte[] valueBytes = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF };
90         assertEquals(tlvsToTest.get(13), new NodeIdentifierTlv(valueBytes));
91         assertEquals(tlvsToTest.get(14), new OrderTlv(0xFFFFFFFFL, 0x00000001L));
92         assertEquals(tlvsToTest.get(15), new OFListTlv(new ArrayList<PCEPOFCodes>() {
93             private static final long serialVersionUID = 1L;
94
95             {
96                 this.add(PCEPOFCodes.MCC);
97                 this.add(PCEPOFCodes.MCP);
98                 this.add(PCEPOFCodes.MLL);
99             }
100         }));
101         assertEquals(tlvsToTest.get(16), new P2MPCapabilityTlv(2));
102
103         assertArrayEquals(bytesFromFile, PCEPTlvParser.put(tlvsToTest));
104     }
105
106     @Test
107     public void testDifferentLengthExceptions() {
108         final byte[] bytes = { (byte) 0x00 }; // not empty but not enought data
109                                               // for parsing subobjects
110
111         try {
112             LSPIdentifierIPv4TlvParser.parse(bytes);
113             fail("");
114         } catch (final PCEPDeserializerException e) {
115         }
116
117         try {
118             LSPIdentifierIPv6TlvParser.parse(bytes);
119             fail("");
120         } catch (final PCEPDeserializerException e) {
121         }
122
123         try {
124             PCEStatefulCapabilityTlvParser.deserializeValueField(bytes);
125             fail("");
126         } catch (final PCEPDeserializerException e) {
127         }
128
129         try {
130             RSVPErrorSpecIPv4TlvParser.parse(bytes);
131             fail("");
132         } catch (final PCEPDeserializerException e) {
133         }
134
135         try {
136             RSVPErrorSpecIPv6TlvParser.parse(bytes);
137             fail("");
138         } catch (final PCEPDeserializerException e) {
139         }
140
141         try {
142             OFListTlvParser.parse(bytes);
143             fail("");
144         } catch (final PCEPDeserializerException e) {
145         }
146     }
147
148     @Test
149     public void testUnknownInstanceExceptions() {
150         try {
151             LSPIdentifierIPv4TlvParser.put(null);
152             fail("");
153         } catch (final IllegalArgumentException e) {
154         }
155
156         try {
157             LSPIdentifierIPv6TlvParser.put(null);
158             fail("");
159         } catch (final IllegalArgumentException e) {
160         }
161
162         try {
163             PCEStatefulCapabilityTlvParser.serializeValueField(null);
164             fail("");
165         } catch (final IllegalArgumentException e) {
166         }
167
168         try {
169             RSVPErrorSpecIPv4TlvParser.put(null);
170             fail("");
171         } catch (final IllegalArgumentException e) {
172         }
173
174         try {
175             RSVPErrorSpecIPv6TlvParser.put(null);
176             fail("");
177         } catch (final IllegalArgumentException e) {
178         }
179
180         try {
181             OFListTlvParser.put(null);
182             fail("");
183         } catch (final IllegalArgumentException e) {
184         }
185
186     }
187
188     @Test
189     public void testEmptyExceptions() throws PCEPDeserializerException {
190         final byte[] bytes = {}; // empty
191
192         try {
193             LSPIdentifierIPv4TlvParser.parse(bytes);
194             fail("");
195         } catch (final IllegalArgumentException e) {
196         }
197
198         try {
199             LSPIdentifierIPv6TlvParser.parse(bytes);
200             fail("");
201         } catch (final IllegalArgumentException e) {
202         }
203
204         try {
205             PCEStatefulCapabilityTlvParser.deserializeValueField(bytes);
206             fail("");
207         } catch (final IllegalArgumentException e) {
208         }
209
210         try {
211             RSVPErrorSpecIPv4TlvParser.parse(bytes);
212             fail("");
213         } catch (final IllegalArgumentException e) {
214         }
215
216         try {
217             RSVPErrorSpecIPv6TlvParser.parse(bytes);
218             fail("");
219         } catch (final IllegalArgumentException e) {
220         }
221
222         try {
223             OFListTlvParser.parse(bytes);
224             fail("");
225         } catch (final IllegalArgumentException e) {
226         }
227     }
228 }