blob: 2d01fe83ed49cea3d54150ccde0969f69ac0a230 [file] [log] [blame]
Vidyashree Ramac95ba002015-11-05 18:10:55 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Vidyashree Ramac95ba002015-11-05 18:10:55 +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 */
Ray Milkey753ad632015-11-06 08:46:33 -080016package org.onosproject.bgpio.protocol;
Vidyashree Ramac95ba002015-11-05 18:10:55 +053017
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.jboss.netty.buffer.ChannelBuffers;
20import org.junit.Test;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.types.BgpHeader;
Vidyashree Ramac95ba002015-11-05 18:10:55 +053023
Ray Milkey753ad632015-11-06 08:46:33 -080024import static org.hamcrest.MatcherAssert.assertThat;
25import static org.hamcrest.Matchers.instanceOf;
26import static org.hamcrest.core.Is.is;
27
Vidyashree Ramac95ba002015-11-05 18:10:55 +053028/**
29 * Test cases for BGP Open Message.
30 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053031public class BgpOpenMsgTest {
Vidyashree Ramac95ba002015-11-05 18:10:55 +053032
33 /**
34 * This test case checks open message without optional parameter.
35 */
36 @Test
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053037 public void openMessageTest1() throws BgpParseException {
Vidyashree Ramac95ba002015-11-05 18:10:55 +053038 //Open message without optional parameter
39 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
40 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
41 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
42 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
43 0x00, 0x1d, 0x01, 0X04, (byte) 0xfe, 0x09, 0x00,
44 (byte) 0xb4, (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f,
45 0x00};
46
47 byte[] testOpenMsg;
48 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
49 buffer.writeBytes(openMsg);
50
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053051 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
52 BgpMessage message;
53 BgpHeader bgpHeader = new BgpHeader();
Vidyashree Ramac95ba002015-11-05 18:10:55 +053054 message = reader.readFrom(buffer, bgpHeader);
55
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053056 assertThat(message, instanceOf(BgpOpenMsg.class));
Vidyashree Ramac95ba002015-11-05 18:10:55 +053057 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
58 message.writeTo(buf);
59
60 int readLen = buf.writerIndex();
61 testOpenMsg = new byte[readLen];
62 buf.readBytes(testOpenMsg, 0, readLen);
63
64 assertThat(testOpenMsg, is(openMsg));
65 }
66
67 /**
68 * This test case checks open message with Multiprotocol extension
69 * capability.
70 */
71 @Test
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053072 public void openMessageTest2() throws BgpParseException {
Vidyashree Ramac95ba002015-11-05 18:10:55 +053073
74 // OPEN Message (MultiProtocolExtension-CAPABILITY).
75 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
76 (byte) 0xff, (byte) 0xff, (byte) 0xff,
77 (byte) 0xff, (byte) 0xff, (byte) 0xff,
78 (byte) 0xff, (byte) 0xff, (byte) 0xff,
79 (byte) 0xff, (byte) 0xff, (byte) 0xff,
80 (byte) 0xff, 0x00, 0x25,
81 0x01, //BGP Header
82 0X04, //Version
83 (byte) 0x00, (byte) 0xc8, // AS Number
84 0x00, (byte) 0xb4, // Hold time
85 (byte) 0xb6, (byte) 0x02, 0x5d,
86 (byte) 0xc8, // BGP Identifier
87 0x08, 0x02, 0x06, // Opt Parameter length
88 0x01, 0x04, 0x00, 0x00, 0x00, (byte) 0xc8}; // Multiprotocol CAPABILITY
89
90 byte[] testOpenMsg;
91 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
92 buffer.writeBytes(openMsg);
93
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053094 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
95 BgpMessage message;
96 BgpHeader bgpHeader = new BgpHeader();
Vidyashree Ramac95ba002015-11-05 18:10:55 +053097
98 message = reader.readFrom(buffer, bgpHeader);
99
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530100 assertThat(message, instanceOf(BgpOpenMsg.class));
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530101 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
102 message.writeTo(buf);
103
104 int readLen = buf.writerIndex();
105 testOpenMsg = new byte[readLen];
106 buf.readBytes(testOpenMsg, 0, readLen);
107
108 assertThat(testOpenMsg, is(openMsg));
109 }
110
111 /**
112 * This test case checks open message with Four-octet AS number
113 * capability.
114 */
115 @Test
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530116 public void openMessageTest3() throws BgpParseException {
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530117
118 // OPEN Message (Four-Octet AS number capability).
119 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
120 (byte) 0xff, (byte) 0xff, (byte) 0xff,
121 (byte) 0xff, (byte) 0xff, (byte) 0xff,
122 (byte) 0xff, (byte) 0xff, (byte) 0xff,
123 (byte) 0xff, (byte) 0xff, (byte) 0xff,
124 (byte) 0xff, 0x00, 0x25,
125 0x01, //BGPHeader
126 0X04, //Version
127 (byte) 0x00, (byte) 0xc8, //AS Number
128 0x00, (byte) 0xb4, //Hold Time
129 (byte) 0xb6, (byte) 0x02, 0x5d,
130 (byte) 0xc8, //BGP Identifier
131 0x08, 0x02, 0x06, //Opt Parameter Length
132 0x41, 0x04, 0x00, 0x01, 0x00, 0x01}; //Four Octet AS Number-CAPABILITY-TLV
133
134 byte[] testOpenMsg;
135 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
136 buffer.writeBytes(openMsg);
137
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530138 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
139 BgpMessage message;
140 BgpHeader bgpHeader = new BgpHeader();
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530141
142 message = reader.readFrom(buffer, bgpHeader);
143
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530144 assertThat(message, instanceOf(BgpOpenMsg.class));
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530145 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
146 message.writeTo(buf);
147
148 int readLen = buf.writerIndex();
149 testOpenMsg = new byte[readLen];
150 buf.readBytes(testOpenMsg, 0, readLen);
151
152 assertThat(testOpenMsg, is(openMsg));
153 }
154
155 /**
156 * This test case checks open message with capabilities.
157 */
158 @Test
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530159 public void openMessageTest4() throws BgpParseException {
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530160
161 // OPEN Message with capabilities.
162 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
163 (byte) 0xff, (byte) 0xff, (byte) 0xff,
164 (byte) 0xff, (byte) 0xff, (byte) 0xff,
165 (byte) 0xff, (byte) 0xff, (byte) 0xff,
166 (byte) 0xff, (byte) 0xff, (byte) 0xff,
167 (byte) 0xff, 0x00, 0x2b,
168 0x01, //BGPHeader
169 0X04, //Version
170 (byte) 0x00, (byte) 0xc8, //AS Number
171 0x00, (byte) 0xb4, //Hold Time
172 (byte) 0xb6, (byte) 0x02, 0x5d, (byte) 0xc8, //BGP Identifier
173 0x0e, 0x02, 0x0c, //Opt Parameter Length
174 0x01, 0x04, 0x00, 0x00, 0x00, (byte) 0xc8, // Multiprotocol extension capability
175 0x41, 0x04, 0x00, 0x01, 0x00, 0x01}; //Four Octet AS Number-CAPABILITY-TLV
176
177 byte[] testOpenMsg;
178 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
179 buffer.writeBytes(openMsg);
180
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530181 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
182 BgpMessage message;
183 BgpHeader bgpHeader = new BgpHeader();
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530184
185 message = reader.readFrom(buffer, bgpHeader);
186
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530187 assertThat(message, instanceOf(BgpOpenMsg.class));
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530188
189 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
190 message.writeTo(buf);
191
192 int readLen = buf.writerIndex();
193 testOpenMsg = new byte[readLen];
194 buf.readBytes(testOpenMsg, 0, readLen);
195
196 assertThat(testOpenMsg, is(openMsg));
197 }
198
199 /**
200 * In this test case, Invalid version is given as input and expecting
201 * an exception.
202 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530203 @Test(expected = BgpParseException.class)
204 public void openMessageTest5() throws BgpParseException {
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530205
206 // OPEN Message with invalid version number.
207 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
208 (byte) 0xff, (byte) 0xff, (byte) 0xff,
209 (byte) 0xff, (byte) 0xff, (byte) 0xff,
210 (byte) 0xff, (byte) 0xff, (byte) 0xff,
211 (byte) 0xff, (byte) 0xff, (byte) 0xff,
212 (byte) 0xff, 0x00, 0x1d, 0x01, 0X05,
213 (byte) 0xfe, 0x09, 0x00, (byte) 0xb4,
214 (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f,
215 0x00};
216
217 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
218 buffer.writeBytes(openMsg);
219
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530220 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
221 BgpMessage message;
222 BgpHeader bgpHeader = new BgpHeader();
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530223 message = reader.readFrom(buffer, bgpHeader);
224
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530225 assertThat(message, instanceOf(BgpOpenMsg.class));
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530226 }
227
228 /**
229 * In this test case, Marker is set as 0 in input and expecting
230 * an exception.
231 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530232 @Test(expected = BgpParseException.class)
233 public void openMessageTest6() throws BgpParseException {
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530234
235 // OPEN Message with marker set to 0.
236 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
237 (byte) 0xff, (byte) 0xff, (byte) 0xff,
238 (byte) 0xff, (byte) 0xff, (byte) 0xff,
239 (byte) 0x00, (byte) 0xff, (byte) 0xff,
240 (byte) 0xff, (byte) 0xff, (byte) 0xff,
241 0x00, 0x00, 0x1d, 0x01, 0X04,
242 (byte) 0xfe, 0x09, 0x00, (byte) 0xb4,
243 (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f,
244 0x00};
245
246 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
247 buffer.writeBytes(openMsg);
248
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530249 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
250 BgpMessage message;
251 BgpHeader bgpHeader = new BgpHeader();
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530252 message = reader.readFrom(buffer, bgpHeader);
253
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530254 assertThat(message, instanceOf(BgpOpenMsg.class));
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530255 }
256
257 /**
258 * In this test case, Invalid message length is given as input and expecting
259 * an exception.
260 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530261 @Test(expected = BgpParseException.class)
262 public void openMessageTest7() throws BgpParseException {
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530263
264 // OPEN Message with invalid header length.
265 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
266 (byte) 0xff, (byte) 0xff, (byte) 0xff,
267 (byte) 0xff, (byte) 0xff, (byte) 0xff,
268 (byte) 0xff, (byte) 0xff, (byte) 0xff,
269 (byte) 0xff, (byte) 0xff, (byte) 0xff,
270 (byte) 0xff, 0x00, 0x1e, 0x01, 0X04,
271 (byte) 0xfe, 0x09, 0x00, (byte) 0xb4,
272 (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f,
273 0x00};
274
275 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
276 buffer.writeBytes(openMsg);
277
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530278 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
279 BgpMessage message;
280 BgpHeader bgpHeader = new BgpHeader();
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530281 message = reader.readFrom(buffer, bgpHeader);
282
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530283 assertThat(message, instanceOf(BgpOpenMsg.class));
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530284 }
285
286 /**
287 * In this test case, Invalid message type is given as input and expecting
288 * an exception.
289 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530290 @Test(expected = BgpParseException.class)
291 public void openMessageTest8() throws BgpParseException {
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530292
293 // OPEN Message with invalid message type.
Shashikanth VHb650bfa2016-04-18 12:54:03 +0530294 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
295 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
296 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x1d, 0x05, 0X04,
297 (byte) 0xfe, 0x09, 0x00, (byte) 0xb4, (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f, 0x00 };
298
299 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
300 buffer.writeBytes(openMsg);
301
302 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
303 BgpMessage message;
304 BgpHeader bgpHeader = new BgpHeader();
305 message = reader.readFrom(buffer, bgpHeader);
306
307 assertThat(message, instanceOf(BgpOpenMsg.class));
308 }
309
310 /**
311 * This test case checks open message with route policy distribution capability.
312 */
313 @Test
314 public void openMessageTest9() throws BgpParseException {
315
316 // OPEN Message with capabilities.
317 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
318 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
319 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x38, 0x01, 0x04, 0x00,
320 0x64, 0x00, (byte) 0xb4, (byte) 0xc0, (byte) 0xa8, 0x07, 0x35, 0x1b, 0x02, 0x19,
321 0x01, 0x04, 0x00, 0x01, 0x00, 0x01, 0x01, 0x04, 0x40, 0x04, 0x00, 0x47, 0x01,
322 0x04, 0x00, 0x01, 0x00, (byte) 0x85, 0x01, 0x05, 0x00, 0x01, 0x00, (byte) 0xc8,
323 0x00 }; // Four Octet AS Number-CAPABILITY-TLV
324
325 byte[] testOpenMsg;
326 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
327 buffer.writeBytes(openMsg);
328
329 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
330 BgpMessage message;
331 BgpHeader bgpHeader = new BgpHeader();
332
333 message = reader.readFrom(buffer, bgpHeader);
334
335 assertThat(message, instanceOf(BgpOpenMsg.class));
336
337 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
338 message.writeTo(buf);
339
340 int readLen = buf.writerIndex();
341 testOpenMsg = new byte[readLen];
342 buf.readBytes(testOpenMsg, 0, readLen);
343
344 assertThat(testOpenMsg, is(openMsg));
345 }
346
347 /**
348 * In this test case, Invalid multiprotocol capability length is given as input and expecting an exception.
349 */
350 @Test(expected = BgpParseException.class)
351 public void openMessageTest10() throws BgpParseException {
352
353 // OPEN Message with invalid message type.
354 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
355 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
356 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x38, 0x01, 0x04, 0x00,
357 0x64, 0x00, (byte) 0xb4, (byte) 0xc0, (byte) 0xa8, 0x07, 0x35, 0x1b, 0x02, 0x19,
358 0x01, 0x04, 0x00, 0x01, 0x00, 0x01, 0x01, 0x04, 0x40, 0x04, 0x00, 0x47, 0x01,
359 0x04, 0x00, 0x01, 0x00, (byte) 0x85, 0x01, 0x04, 0x00, 0x01, 0x00, (byte) 0xc8,
360 0x00 };
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530361
362 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
363 buffer.writeBytes(openMsg);
364
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530365 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
366 BgpMessage message;
367 BgpHeader bgpHeader = new BgpHeader();
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530368 message = reader.readFrom(buffer, bgpHeader);
369
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530370 assertThat(message, instanceOf(BgpOpenMsg.class));
Vidyashree Ramac95ba002015-11-05 18:10:55 +0530371 }
372}