blob: b45821b3217ab7487aeb3b6c2c848165c5c40656 [file] [log] [blame]
Dhruv Dhody43f3ce62016-02-16 22:44:21 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Dhruv Dhody43f3ce62016-02-16 22:44:21 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ospf.protocol.ospfpacket.types;
17
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.jboss.netty.buffer.ChannelBuffers;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.Ip4Address;
24import org.onosproject.ospf.controller.OspfLsa;
sunishvkf7c56552016-07-18 16:02:39 +053025import org.onosproject.ospf.controller.OspfPacketType;
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053026import org.onosproject.ospf.protocol.lsa.LsaHeader;
27import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
28import org.onosproject.ospf.protocol.lsa.types.AsbrSummaryLsa;
29import org.onosproject.ospf.protocol.lsa.types.ExternalLsa;
30import org.onosproject.ospf.protocol.lsa.types.NetworkLsa;
31import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa10;
32import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa11;
33import org.onosproject.ospf.protocol.lsa.types.OpaqueLsa9;
34import org.onosproject.ospf.protocol.lsa.types.RouterLsa;
35import org.onosproject.ospf.protocol.lsa.types.SummaryLsa;
36import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
Dhruv Dhody43f3ce62016-02-16 22:44:21 +053037
38import java.util.List;
39import java.util.Vector;
40
41import static org.hamcrest.CoreMatchers.*;
42import static org.hamcrest.MatcherAssert.assertThat;
43
44/**
45 * Unit test class for LsUpdate.
46 */
47public class LsUpdateTest {
48
49 private final byte[] packet1 = {0, 0, 0, 2, 0, 10, 2, 1, 7, 7, 7, 7, 7, 7, 7, 7,
50 -128, 0, 0, 2, 46, -126, 0, 48, 0, 0, 0, 2, 1, 1, 1, 1, 10, 10, 10, 7, 1,
51 0, 0, 10, 10, 10, 10, 0, -1, -1, -1, 0, 3, 0, 0, 10, 0, 10, 66, 10, 1, 0,
52 0, 1, 7, 7, 7, 7, -128, 0, 0, 1, -64, 79, 0, 116, 0, 1, 0, 4, 0, 0, 0, 0,
53 0, 2, 0, 84, 0, 1, 0, 1, 1, 0, 0, 0, 0, 2, 0, 4, 10, 10, 10, 0, 0, 5, 0,
54 4, 0, 0, 0, 0, 0, 6, 0, 4, 73, -104, -106, -128, 0, 7, 0, 4, 73, -104, -106,
55 -128, 0, 8, 0, 32, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106,
56 -128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128, 73,
57 -104, -106, -128, 73, -104, -106, -128, 0, 9, 0, 4, 0, 0, 0, 0};
58 private final byte[] packet3 = {0, 0, 0, 1, 0, 100, 2, 10, 1, 0, 0, 1, 9, 9, 9, 9,
59 -128, 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2,
60 0, 4, -64, -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0,
61 0, 0, 5, 0, 4, 0, 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0,
62 0, 8, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0,
64 0, 0, 1};
65 private final byte[] packet2 = {0, 0,
66 0, 1, 0, 1, 2, 2, -64, -88,
67 -86, 8, -64, -88, -86, 8, -128, 0, 0, 1, 55, -73, 0, 32, -1, -1, -1, 0, -64,
68 -88, -86, 3, -64, -88, -86, 8};
69 private final byte[] packet4 = {0, 0, 0, 1, 0, 100, 2, 9, 1, 0, 0, 1, 9, 9, 9, 9, -128,
70 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64,
71 -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0,
72 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
75
76 private final byte[] packet5 = {0, 0, 0, 1, 0, 100, 2, 11, 1, 0, 0, 1, 9, 9, 9, 9, -128, 0,
77 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64, -88,
78 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0,
79 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
82 private final byte[] packet6 = {0, 0, 0, 1, 0, 100, 2, 3, 1, 0, 0, 1, 9, 9, 9, 9, -128,
83 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64,
84 -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4,
85 0, 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
88 private final byte[] packet7 = {0, 0, 0, 1, 0, 100, 2, 4, 1, 0, 0, 1, 9, 9, 9, 9, -128,
89 0, 0, 1, -7, 62, 0, -124, 0, 2, 0, 108, 0, 1, 0, 1, 2, 0, 0, 0, 0, 2, 0, 4, -64,
90 -88, 7, -91, 0, 3, 0, 4, -64, -88, 7, -91, 0, 4, 0, 4, 0, 0, 0, 0, 0, 5, 0, 4, 0,
91 0, 0, 1, 0, 6, 0, 4, 0, 0, 0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 8, 0, 32, 0, 0, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 9, 0, 4, 0, 0, 0, 0, -128, 2, 0, 4, 0, 0, 0, 1};
94
95 private final byte[] packet8 = {0, 0, 0, 2, 1, 4, 2, 1, 5, 5, 5, 5, 5, 5, 5, 5, -128, 0, 0,
96 4, -39, -84, 0, 36, 1, 0, 0, 1, -64, -88, 7, 90, -64, -88, 7, 92, 2, 0, 0, 10, 1, 4,
97 2, 4, -34, -34, -34, -34, 5, 5, 5, 5, -128, 0, 0, 1, 31, -93, 0, 28, 0, 0, 0, 0, 0,
98 0, 0, 10};
99 private LsUpdate lsUpdate;
100 private RouterLsa ospflsa;
101 private NetworkLsa ospflsa1;
102 private SummaryLsa ospflsa2;
103 private AsbrSummaryLsa ospflsa3;
104 private ExternalLsa ospflsa4;
sunish vkaa48da82016-03-02 23:17:06 +0530105 private Vector<OspfLsa> listLsa = new Vector();
Dhruv Dhody43f3ce62016-02-16 22:44:21 +0530106 private List lsa;
107 private int result;
108 private OspfPacketType ospfMessageType;
109 private OspfPacketHeader ospfPacketHeader;
110 private byte[] result1;
111 private ChannelBuffer channelBuffer;
112 private OpaqueLsa10 opaqueLsa10;
113 private OpaqueLsa9 opaqueLsa9;
114 private OpaqueLsa11 opaqueLsa11;
115
116 @Before
117 public void setUp() throws Exception {
118 lsUpdate = new LsUpdate();
119 ospflsa = new RouterLsa();
120 lsUpdate.setAuthType(1);
121 lsUpdate.setOspftype(2);
122 lsUpdate.setRouterId(Ip4Address.valueOf("10.226.165.164"));
123 lsUpdate.setAreaId(Ip4Address.valueOf("10.226.165.100"));
124 lsUpdate.setChecksum(201);
125 lsUpdate.setAuthentication(2);
126 lsUpdate.setOspfPacLength(48);
127 lsUpdate.setOspfVer(2);
128 ospflsa.setLsType(1);
129 lsUpdate.addLsa(ospflsa);
130 ospflsa1 = new NetworkLsa();
131 ospflsa1.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
132 ospflsa1.setLsType(2);
133 lsUpdate.addLsa(ospflsa1);
134 ospflsa2 = new SummaryLsa(new LsaHeader());
135 ospflsa2.setLsType(3);
136 lsUpdate.addLsa(ospflsa2);
137 ospflsa3 = new AsbrSummaryLsa(new LsaHeader());
138 ospflsa3.setNetworkMask(Ip4Address.valueOf("10.226.165.164"));
139 ospflsa3.setLsType(4);
140 lsUpdate.addLsa(ospflsa3);
141 ospflsa4 = new ExternalLsa(new LsaHeader());
142 ospflsa4.setLsType(5);
143 lsUpdate.addLsa(ospflsa4);
144 }
145
146 @After
147 public void tearDown() throws Exception {
148 lsUpdate = null;
149 ospflsa = null;
150 ospflsa1 = null;
151 ospflsa2 = null;
152 ospflsa3 = null;
153 ospflsa4 = null;
sunish vkaa48da82016-03-02 23:17:06 +0530154 listLsa.clear();
Dhruv Dhody43f3ce62016-02-16 22:44:21 +0530155 lsa = null;
156 ospfMessageType = null;
157 ospfPacketHeader = null;
158 result1 = null;
159 channelBuffer = null;
160 opaqueLsa9 = null;
161 opaqueLsa10 = null;
162 opaqueLsa11 = null;
163 }
164
165 /**
166 * Tests getLsaList() getter method.
167 */
168 @Test
169 public void testGetListLsa() throws Exception {
170 lsUpdate.addLsa(ospflsa);
171 lsUpdate.addLsa(ospflsa);
172 lsa = lsUpdate.getLsaList();
173 assertThat(lsa, is(notNullValue()));
174 assertThat(lsa.size(), is(5));
175 }
176
177 /**
178 * Tests addLsa() method.
179 */
180 @Test
181 public void testAddLsa() throws Exception {
182 lsUpdate.addLsa(ospflsa);
183 assertThat(lsUpdate, is(notNullValue()));
184 }
185
186 /**
187 * Tests noLsa() getter method.
188 */
189 @Test
190 public void testGetNoLsa() throws Exception {
191 lsUpdate.setNumberOfLsa(5);
192 result = lsUpdate.noLsa();
193 assertThat(result, is(notNullValue()));
194 assertThat(result, is(5));
195 }
196
197 /**
198 * Tests noLsa() setter method.
199 */
200 @Test
201 public void testSetNoLsa() throws Exception {
202 lsUpdate.setNumberOfLsa(5);
203 result = lsUpdate.noLsa();
204 assertThat(result, is(notNullValue()));
205 assertThat(result, is(5));
206 }
207
208 /**
209 * Tests ospfMessageType() getter method.
210 */
211 @Test
212 public void testGetOspfMessageType() throws Exception {
213 ospfMessageType = lsUpdate.ospfMessageType();
214 assertThat(ospfMessageType, is(OspfPacketType.LSUPDATE));
215 }
216
217 /**
218 * Tests readFrom() method.
219 */
220 @Test
221 public void testReadFrom() throws Exception {
222 ospfPacketHeader = new OspfPacketHeader();
223 ospfPacketHeader.setAreaId(Ip4Address.valueOf("1.1.1.1"));
224 ospfPacketHeader.setAuthentication(0);
225 ospfPacketHeader.setAuthType(0);
226 ospfPacketHeader.setChecksum(12345);
227 ospfPacketHeader.setDestinationIp(Ip4Address.valueOf("10.10.10.10"));
228 ospfPacketHeader.setOspfPacLength(56);
229 ospfPacketHeader.setOspftype(4);
230 ospfPacketHeader.setOspfVer(2);
231 ospfPacketHeader.setRouterId(Ip4Address.valueOf("2.2.2.2"));
232 ospfPacketHeader.setSourceIp(Ip4Address.valueOf("3.3.3.3"));
233 lsUpdate = new LsUpdate(ospfPacketHeader);
234 result1 = createLsUpdatePacket();
235 channelBuffer = ChannelBuffers.copiedBuffer(result1);
236 lsUpdate.readFrom(channelBuffer);
237 channelBuffer = ChannelBuffers.copiedBuffer(packet1);
238 lsUpdate.readFrom(channelBuffer);
239 channelBuffer = ChannelBuffers.copiedBuffer(packet2);
240 lsUpdate.readFrom(channelBuffer);
241 channelBuffer = ChannelBuffers.copiedBuffer(packet3);
242 lsUpdate.readFrom(channelBuffer);
243 channelBuffer = ChannelBuffers.copiedBuffer(packet4);
244 lsUpdate.readFrom(channelBuffer);
245 channelBuffer = ChannelBuffers.copiedBuffer(packet5);
246 lsUpdate.readFrom(channelBuffer);
247 channelBuffer = ChannelBuffers.copiedBuffer(packet6);
248 lsUpdate.readFrom(channelBuffer);
249 channelBuffer = ChannelBuffers.copiedBuffer(packet7);
250 lsUpdate.readFrom(channelBuffer);
251 channelBuffer = ChannelBuffers.copiedBuffer(packet8);
252 lsUpdate.readFrom(channelBuffer);
253 assertThat(lsUpdate, is(notNullValue()));
254 assertThat(lsUpdate.ospfMessageType(), is(OspfPacketType.LSUPDATE));
255 }
256
257 /**
258 * Tests asBytes() method.
259 */
260 @Test
261 public void testAsBytes() throws Exception {
262 result1 = lsUpdate.asBytes();
263 assertThat(result1, is(notNullValue()));
264 }
265
266 /**
267 * Tests getLsuHeaderAsByteArray() method.
268 */
269 @Test
270 public void testGetLsuHeaderAsByteArray() throws Exception {
271 result1 = lsUpdate.getLsuHeaderAsByteArray();
272 assertThat(result1, is(notNullValue()));
273 }
274
275 /**
276 * Tests getLsuBodyAsByteArray() method.
277 */
278 @Test
279 public void testGetLsuBodyAsByteArray() throws Exception {
280 lsUpdate.setNumberOfLsa(8);
281 lsUpdate.addLsa(ospflsa3);
282 opaqueLsa9 = new OpaqueLsa9(new OpaqueLsaHeader());
283 opaqueLsa9.setLsType(9);
284 lsUpdate.addLsa(opaqueLsa9);
285 opaqueLsa10 = new OpaqueLsa10(new OpaqueLsaHeader());
286 opaqueLsa10.setLsType(10);
287 lsUpdate.addLsa(opaqueLsa10);
288 opaqueLsa11 = new OpaqueLsa11(new OpaqueLsaHeader());
289 opaqueLsa10.setLsType(11);
290 lsUpdate.addLsa(opaqueLsa11);
291 result1 = lsUpdate.getLsuBodyAsByteArray();
292 assertThat(result1, is(notNullValue()));
293 }
294
295 @Test
296 public void testGetLsuBodyAsByteArray1() throws Exception {
297 lsUpdate.setNumberOfLsa(8);
298 opaqueLsa10 = new OpaqueLsa10(new OpaqueLsaHeader());
299 opaqueLsa10.setLsType(10);
300 lsUpdate.addLsa(opaqueLsa10);
301 assertThat(result1, is(nullValue()));
302 }
303
304 @Test
305 public void testGetLsuBodyAsByteArray2() throws Exception {
306 opaqueLsa11 = new OpaqueLsa11(new OpaqueLsaHeader());
307 opaqueLsa11.setLsType(11);
308 lsUpdate.addLsa(opaqueLsa11);
309 result1 = lsUpdate.getLsuBodyAsByteArray();
310 assertThat(result1, is(notNullValue()));
311 }
312
313 /**
314 * Tests to string method.
315 */
316 @Test
317 public void testToString() throws Exception {
318 assertThat(lsUpdate.toString(), is(notNullValue()));
319 }
320
321 /**
322 * Utility method used by junit methods.
323 */
324 private byte[] createLsUpdatePacket() {
325 byte[] lsUpdatePacket = {0, 0, 0, 7, 0, 2, 2,
326 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0, 0, 1, 58,
327 -100, 0, 48, 2, 0, 0, 2, -64, -88, -86, 0, -1, -1, -1, 0,
sunish vkaa48da82016-03-02 23:17:06 +0530328 3, 0, 0, 10, -64, -88, -86, 0, -1, -1, -1, 0, 3, 0, 0, 10,
329 0, 3, 2, 5, 80, -44, 16, 0, -64, -88, -86, 2, -128, 0, 0,
330 1, 42, 73, 0, 36, -1, -1, -1, -1, -128, 0, 0, 20, 0, 0,
331 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -108, 121, -85, 0, -64, -88,
332 -86, 2, -128, 0, 0, 1, 52, -91, 0, 36, -1, -1, -1, 0,
333 -128, 0, 0, 20, -64, -88, -86, 1, 0, 0, 0, 0, 0, 3, 2, 5,
334 -64, -126, 120, 0, -64, -88, -86, 2, -128, 0, 0, 1, -45,
Dhruv Dhody43f3ce62016-02-16 22:44:21 +0530335 25, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, 0, 0, 0, 0, 0,
336 0, 0, 0, 0, 3, 2, 5, -64, -88, 0, 0, -64, -88, -86, 2,
337 -128, 0, 0, 1, 55, 8, 0, 36, -1, -1, -1, 0, -128, 0, 0,
338 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, 1, 0,
339 -64, -88, -86, 2, -128, 0, 0, 1, 44, 18, 0, 36, -1, -1, -1, 0, -128, 0,
340 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, -84, 0, -64, -88,
341 -86, 2, -128, 0, 0, 1, 51, 65, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, -64,
342 -88, -86, 10, 0, 0, 0, 0};
343 return lsUpdatePacket;
344 }
345}