Initial code drop
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPXROSubobjectParserTest.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.List;
16
17 import org.junit.Test;
18
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.protocol.concepts.ASNumber;
21 import org.opendaylight.protocol.concepts.IPv4Address;
22 import org.opendaylight.protocol.concepts.IPv4Prefix;
23 import org.opendaylight.protocol.concepts.IPv6Address;
24 import org.opendaylight.protocol.concepts.IPv6Prefix;
25 import org.opendaylight.protocol.concepts.SharedRiskLinkGroup;
26 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
27 import org.opendaylight.protocol.pcep.concepts.UnnumberedInterfaceIdentifier;
28 import org.opendaylight.protocol.pcep.impl.subobject.XROAsNumberSubobjectParser;
29 import org.opendaylight.protocol.pcep.impl.subobject.XROIPv4PrefixSubobjectParser;
30 import org.opendaylight.protocol.pcep.impl.subobject.XROIPv6PrefixSubobjectParser;
31 import org.opendaylight.protocol.pcep.impl.subobject.XROUnnumberedInterfaceSubobjectParser;
32 import org.opendaylight.protocol.pcep.subobject.ExcludeRouteSubobject;
33 import org.opendaylight.protocol.pcep.subobject.XROAsNumberSubobject;
34 import org.opendaylight.protocol.pcep.subobject.XROIPPrefixSubobject;
35 import org.opendaylight.protocol.pcep.subobject.XROSRLGSubobject;
36 import org.opendaylight.protocol.pcep.subobject.XROSubobjectAttribute;
37 import org.opendaylight.protocol.pcep.subobject.XROUnnumberedInterfaceSubobject;
38
39 public class PCEPXROSubobjectParserTest {
40
41         @Test
42         public void testSerDeser() throws PCEPDeserializerException, IOException {
43                 final byte[] bytesFromFile = ByteArray.fileToBytes("src/test/resources/PackOfXROSubobjects.bin");
44                 final List<ExcludeRouteSubobject> objsToTest = PCEPXROSubobjectParser.parse(bytesFromFile);
45
46                 assertEquals(5, objsToTest.size());
47
48                 assertEquals(
49                                 objsToTest.get(0),
50                                 new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(
51                                                 new byte[] { (byte) 192, (byte) 168, (byte) 0, (byte) 0 }), 16), true, XROSubobjectAttribute.NODE));
52                 assertEquals(
53                                 objsToTest.get(1),
54                                 new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(
55                                                 new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x90, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
56                                                                 (byte) 0x90, (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0, (byte) 0 }), 112), true,
57                                                 XROSubobjectAttribute.INTERFACE));
58                 assertEquals(
59                                 objsToTest.get(2),
60                                 new XROUnnumberedInterfaceSubobject(new IPv4Address(
61                                                 new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0x20 }), new UnnumberedInterfaceIdentifier(0x1234L), false,
62                                                 XROSubobjectAttribute.SRLG));
63                 assertEquals(objsToTest.get(3), new XROAsNumberSubobject(new ASNumber(0x1234), false));
64                 assertEquals(objsToTest.get(4), new XROSRLGSubobject(new SharedRiskLinkGroup(0x12345678L), false));
65
66                 assertArrayEquals(bytesFromFile, PCEPXROSubobjectParser.put(objsToTest));
67
68         }
69
70         @Test
71         public void testDifferentLengthExceptions() {
72                 final byte[] bytes = { (byte) 0x00 }; //not empty but not enought data for parsing subobjects
73
74                 try {
75                         XROAsNumberSubobjectParser.parse(bytes, true);
76                         fail("");
77                 } catch (final PCEPDeserializerException e) {
78                 }
79
80                 try {
81                         XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
82                         fail("");
83                 } catch (final PCEPDeserializerException e) {
84                 }
85
86                 try {
87                         XROIPv4PrefixSubobjectParser.parse(bytes, true);
88                         fail("");
89                 } catch (final PCEPDeserializerException e) {
90                 }
91
92                 try {
93                         XROIPv6PrefixSubobjectParser.parse(bytes, true);
94                         fail("");
95                 } catch (final PCEPDeserializerException e) {
96                 }
97         }
98
99         @Test
100         public void testNullExceptions() throws PCEPDeserializerException {
101                 final byte[] bytes = null; //not empty but not enought data for parsing subobjects
102
103                 try {
104                         XROAsNumberSubobjectParser.parse(bytes, true);
105                         fail("");
106                 } catch (final IllegalArgumentException e) {
107                 }
108
109                 try {
110                         XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
111                         fail("");
112                 } catch (final IllegalArgumentException e) {
113                 }
114
115                 try {
116                         XROIPv4PrefixSubobjectParser.parse(bytes, true);
117                         fail("");
118                 } catch (final IllegalArgumentException e) {
119                 }
120
121                 try {
122                         XROIPv6PrefixSubobjectParser.parse(bytes, true);
123                         fail("");
124                 } catch (final IllegalArgumentException e) {
125                 }
126         }
127
128         @Test
129         public void testUnknownInstanceExceptions() {
130
131                 final ExcludeRouteSubobject instance = new ExcludeRouteSubobject(true) {
132                 };
133
134                 try {
135                         XROAsNumberSubobjectParser.put(instance);
136                         fail("");
137                 } catch (final IllegalArgumentException e) {
138                 }
139
140                 try {
141                         XROUnnumberedInterfaceSubobjectParser.put(instance);
142                         fail("");
143                 } catch (final IllegalArgumentException e) {
144                 }
145
146                 try {
147                         XROIPv4PrefixSubobjectParser.put(instance);
148                         fail("");
149                 } catch (final IllegalArgumentException e) {
150                 }
151
152                 try {
153                         final byte[] ipv6addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
154                                         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
155                         XROIPv4PrefixSubobjectParser.put(new XROIPPrefixSubobject<IPv6Prefix>(new IPv6Prefix(new IPv6Address(ipv6addr), 1), false, XROSubobjectAttribute.INTERFACE));
156                         fail("");
157                 } catch (final IllegalArgumentException e) {
158                 }
159
160                 try {
161                         XROIPv6PrefixSubobjectParser.put(instance);
162                         fail("");
163                 } catch (final IllegalArgumentException e) {
164                 }
165
166                 try {
167                         final byte[] ipv4addr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
168                         XROIPv6PrefixSubobjectParser.put(new XROIPPrefixSubobject<IPv4Prefix>(new IPv4Prefix(new IPv4Address(ipv4addr), 1), false, XROSubobjectAttribute.INTERFACE));
169                         fail("");
170                 } catch (final IllegalArgumentException e) {
171                 }
172
173         }
174
175         @Test
176         public void testEmptyExceptions() throws PCEPDeserializerException {
177                 final byte[] bytes = {}; //not empty but not enought data for parsing subobjects
178
179                 try {
180                         XROAsNumberSubobjectParser.parse(bytes, true);
181                         fail("");
182                 } catch (final IllegalArgumentException e) {
183                 }
184
185                 try {
186                         XROUnnumberedInterfaceSubobjectParser.parse(bytes, true);
187                         fail("");
188                 } catch (final IllegalArgumentException e) {
189                 }
190
191                 try {
192                         XROIPv4PrefixSubobjectParser.parse(bytes, true);
193                         fail("");
194                 } catch (final IllegalArgumentException e) {
195                 }
196
197                 try {
198                         XROIPv6PrefixSubobjectParser.parse(bytes, true);
199                         fail("");
200                 } catch (final IllegalArgumentException e) {
201                 }
202         }
203
204 }