blob: d5210177e0c41bebe69c1bb8dbb62bc1be5f3523 [file] [log] [blame]
Vidyashree Ramac95ba002015-11-05 18:10:55 +05301/*
2 * Copyright 2014-2015 Open Networking Laboratory
3 *
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;
21import org.onosproject.bgpio.exceptions.BGPParseException;
Vidyashree Ramac95ba002015-11-05 18:10:55 +053022import org.onosproject.bgpio.types.BGPHeader;
23
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 */
31public class BGPOpenMsgTest {
32
33 /**
34 * This test case checks open message without optional parameter.
35 */
36 @Test
37 public void openMessageTest1() throws BGPParseException {
38 //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
51 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
52 BGPMessage message;
53 BGPHeader bgpHeader = new BGPHeader();
54 message = reader.readFrom(buffer, bgpHeader);
55
56 assertThat(message, instanceOf(BGPOpenMsg.class));
57 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
72 public void openMessageTest2() throws BGPParseException {
73
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
94 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
95 BGPMessage message;
96 BGPHeader bgpHeader = new BGPHeader();
97
98 message = reader.readFrom(buffer, bgpHeader);
99
100 assertThat(message, instanceOf(BGPOpenMsg.class));
101 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
116 public void openMessageTest3() throws BGPParseException {
117
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
138 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
139 BGPMessage message;
140 BGPHeader bgpHeader = new BGPHeader();
141
142 message = reader.readFrom(buffer, bgpHeader);
143
144 assertThat(message, instanceOf(BGPOpenMsg.class));
145 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
159 public void openMessageTest4() throws BGPParseException {
160
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
181 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
182 BGPMessage message;
183 BGPHeader bgpHeader = new BGPHeader();
184
185 message = reader.readFrom(buffer, bgpHeader);
186
187 assertThat(message, instanceOf(BGPOpenMsg.class));
188
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 */
203 @Test(expected = BGPParseException.class)
204 public void openMessageTest5() throws BGPParseException {
205
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
220 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
221 BGPMessage message;
222 BGPHeader bgpHeader = new BGPHeader();
223 message = reader.readFrom(buffer, bgpHeader);
224
225 assertThat(message, instanceOf(BGPOpenMsg.class));
226 }
227
228 /**
229 * In this test case, Marker is set as 0 in input and expecting
230 * an exception.
231 */
232 @Test(expected = BGPParseException.class)
233 public void openMessageTest6() throws BGPParseException {
234
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
249 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
250 BGPMessage message;
251 BGPHeader bgpHeader = new BGPHeader();
252 message = reader.readFrom(buffer, bgpHeader);
253
254 assertThat(message, instanceOf(BGPOpenMsg.class));
255 }
256
257 /**
258 * In this test case, Invalid message length is given as input and expecting
259 * an exception.
260 */
261 @Test(expected = BGPParseException.class)
262 public void openMessageTest7() throws BGPParseException {
263
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
278 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
279 BGPMessage message;
280 BGPHeader bgpHeader = new BGPHeader();
281 message = reader.readFrom(buffer, bgpHeader);
282
283 assertThat(message, instanceOf(BGPOpenMsg.class));
284 }
285
286 /**
287 * In this test case, Invalid message type is given as input and expecting
288 * an exception.
289 */
290 @Test(expected = BGPParseException.class)
291 public void openMessageTest8() throws BGPParseException {
292
293 // OPEN Message with invalid message type.
294 byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
295 (byte) 0xff, (byte) 0xff, (byte) 0xff,
296 (byte) 0xff, (byte) 0xff, (byte) 0xff,
297 (byte) 0xff, (byte) 0xff, (byte) 0xff,
298 (byte) 0xff, (byte) 0xff, (byte) 0xff,
299 (byte) 0xff, 0x00, 0x1d, 0x05, 0X04,
300 (byte) 0xfe, 0x09, 0x00, (byte) 0xb4,
301 (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f,
302 0x00};
303
304 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
305 buffer.writeBytes(openMsg);
306
307 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
308 BGPMessage message;
309 BGPHeader bgpHeader = new BGPHeader();
310 message = reader.readFrom(buffer, bgpHeader);
311
312 assertThat(message, instanceOf(BGPOpenMsg.class));
313 }
314}