Vidyashree Rama
Committed by Gerrit Code Review

[ONOS-2610] Unit test for BGP open message

Change-Id: I9f74a53ff3d888742b6a38553de1fa3a7f881bc9
1 +/*
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 + */
16 +package org.onosproject.bgp;
17 +
18 +import static org.hamcrest.MatcherAssert.assertThat;
19 +import static org.hamcrest.Matchers.instanceOf;
20 +import static org.hamcrest.core.Is.is;
21 +
22 +import org.jboss.netty.buffer.ChannelBuffer;
23 +import org.jboss.netty.buffer.ChannelBuffers;
24 +import org.junit.Test;
25 +import org.onosproject.bgpio.exceptions.BGPParseException;
26 +import org.onosproject.bgpio.protocol.BGPFactories;
27 +import org.onosproject.bgpio.protocol.BGPMessage;
28 +import org.onosproject.bgpio.protocol.BGPMessageReader;
29 +import org.onosproject.bgpio.protocol.BGPOpenMsg;
30 +import org.onosproject.bgpio.types.BGPHeader;
31 +
32 +/**
33 + * Test cases for BGP Open Message.
34 + */
35 +public class BGPOpenMsgTest {
36 +
37 + /**
38 + * This test case checks open message without optional parameter.
39 + */
40 + @Test
41 + public void openMessageTest1() throws BGPParseException {
42 + //Open message without optional parameter
43 + byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
44 + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
45 + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
46 + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
47 + 0x00, 0x1d, 0x01, 0X04, (byte) 0xfe, 0x09, 0x00,
48 + (byte) 0xb4, (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f,
49 + 0x00};
50 +
51 + byte[] testOpenMsg;
52 + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
53 + buffer.writeBytes(openMsg);
54 +
55 + BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
56 + BGPMessage message;
57 + BGPHeader bgpHeader = new BGPHeader();
58 + message = reader.readFrom(buffer, bgpHeader);
59 +
60 + assertThat(message, instanceOf(BGPOpenMsg.class));
61 + ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
62 + message.writeTo(buf);
63 +
64 + int readLen = buf.writerIndex();
65 + testOpenMsg = new byte[readLen];
66 + buf.readBytes(testOpenMsg, 0, readLen);
67 +
68 + assertThat(testOpenMsg, is(openMsg));
69 + }
70 +
71 + /**
72 + * This test case checks open message with Multiprotocol extension
73 + * capability.
74 + */
75 + @Test
76 + public void openMessageTest2() throws BGPParseException {
77 +
78 + // OPEN Message (MultiProtocolExtension-CAPABILITY).
79 + byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
80 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
81 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
82 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
83 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
84 + (byte) 0xff, 0x00, 0x25,
85 + 0x01, //BGP Header
86 + 0X04, //Version
87 + (byte) 0x00, (byte) 0xc8, // AS Number
88 + 0x00, (byte) 0xb4, // Hold time
89 + (byte) 0xb6, (byte) 0x02, 0x5d,
90 + (byte) 0xc8, // BGP Identifier
91 + 0x08, 0x02, 0x06, // Opt Parameter length
92 + 0x01, 0x04, 0x00, 0x00, 0x00, (byte) 0xc8}; // Multiprotocol CAPABILITY
93 +
94 + byte[] testOpenMsg;
95 + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
96 + buffer.writeBytes(openMsg);
97 +
98 + BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
99 + BGPMessage message;
100 + BGPHeader bgpHeader = new BGPHeader();
101 +
102 + message = reader.readFrom(buffer, bgpHeader);
103 +
104 + assertThat(message, instanceOf(BGPOpenMsg.class));
105 + ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
106 + message.writeTo(buf);
107 +
108 + int readLen = buf.writerIndex();
109 + testOpenMsg = new byte[readLen];
110 + buf.readBytes(testOpenMsg, 0, readLen);
111 +
112 + assertThat(testOpenMsg, is(openMsg));
113 + }
114 +
115 + /**
116 + * This test case checks open message with Four-octet AS number
117 + * capability.
118 + */
119 + @Test
120 + public void openMessageTest3() throws BGPParseException {
121 +
122 + // OPEN Message (Four-Octet AS number capability).
123 + byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
124 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
125 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
126 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
127 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
128 + (byte) 0xff, 0x00, 0x25,
129 + 0x01, //BGPHeader
130 + 0X04, //Version
131 + (byte) 0x00, (byte) 0xc8, //AS Number
132 + 0x00, (byte) 0xb4, //Hold Time
133 + (byte) 0xb6, (byte) 0x02, 0x5d,
134 + (byte) 0xc8, //BGP Identifier
135 + 0x08, 0x02, 0x06, //Opt Parameter Length
136 + 0x41, 0x04, 0x00, 0x01, 0x00, 0x01}; //Four Octet AS Number-CAPABILITY-TLV
137 +
138 + byte[] testOpenMsg;
139 + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
140 + buffer.writeBytes(openMsg);
141 +
142 + BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
143 + BGPMessage message;
144 + BGPHeader bgpHeader = new BGPHeader();
145 +
146 + message = reader.readFrom(buffer, bgpHeader);
147 +
148 + assertThat(message, instanceOf(BGPOpenMsg.class));
149 + ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
150 + message.writeTo(buf);
151 +
152 + int readLen = buf.writerIndex();
153 + testOpenMsg = new byte[readLen];
154 + buf.readBytes(testOpenMsg, 0, readLen);
155 +
156 + assertThat(testOpenMsg, is(openMsg));
157 + }
158 +
159 + /**
160 + * This test case checks open message with capabilities.
161 + */
162 + @Test
163 + public void openMessageTest4() throws BGPParseException {
164 +
165 + // OPEN Message with capabilities.
166 + byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
167 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
168 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
169 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
170 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
171 + (byte) 0xff, 0x00, 0x2b,
172 + 0x01, //BGPHeader
173 + 0X04, //Version
174 + (byte) 0x00, (byte) 0xc8, //AS Number
175 + 0x00, (byte) 0xb4, //Hold Time
176 + (byte) 0xb6, (byte) 0x02, 0x5d, (byte) 0xc8, //BGP Identifier
177 + 0x0e, 0x02, 0x0c, //Opt Parameter Length
178 + 0x01, 0x04, 0x00, 0x00, 0x00, (byte) 0xc8, // Multiprotocol extension capability
179 + 0x41, 0x04, 0x00, 0x01, 0x00, 0x01}; //Four Octet AS Number-CAPABILITY-TLV
180 +
181 + byte[] testOpenMsg;
182 + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
183 + buffer.writeBytes(openMsg);
184 +
185 + BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
186 + BGPMessage message;
187 + BGPHeader bgpHeader = new BGPHeader();
188 +
189 + message = reader.readFrom(buffer, bgpHeader);
190 +
191 + assertThat(message, instanceOf(BGPOpenMsg.class));
192 +
193 + ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
194 + message.writeTo(buf);
195 +
196 + int readLen = buf.writerIndex();
197 + testOpenMsg = new byte[readLen];
198 + buf.readBytes(testOpenMsg, 0, readLen);
199 +
200 + assertThat(testOpenMsg, is(openMsg));
201 + }
202 +
203 + /**
204 + * In this test case, Invalid version is given as input and expecting
205 + * an exception.
206 + */
207 + @Test(expected = BGPParseException.class)
208 + public void openMessageTest5() throws BGPParseException {
209 +
210 + // OPEN Message with invalid version number.
211 + byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
212 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
213 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
214 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
215 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
216 + (byte) 0xff, 0x00, 0x1d, 0x01, 0X05,
217 + (byte) 0xfe, 0x09, 0x00, (byte) 0xb4,
218 + (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f,
219 + 0x00};
220 +
221 + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
222 + buffer.writeBytes(openMsg);
223 +
224 + BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
225 + BGPMessage message;
226 + BGPHeader bgpHeader = new BGPHeader();
227 + message = reader.readFrom(buffer, bgpHeader);
228 +
229 + assertThat(message, instanceOf(BGPOpenMsg.class));
230 + }
231 +
232 + /**
233 + * In this test case, Marker is set as 0 in input and expecting
234 + * an exception.
235 + */
236 + @Test(expected = BGPParseException.class)
237 + public void openMessageTest6() throws BGPParseException {
238 +
239 + // OPEN Message with marker set to 0.
240 + byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
241 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
242 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
243 + (byte) 0x00, (byte) 0xff, (byte) 0xff,
244 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
245 + 0x00, 0x00, 0x1d, 0x01, 0X04,
246 + (byte) 0xfe, 0x09, 0x00, (byte) 0xb4,
247 + (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f,
248 + 0x00};
249 +
250 + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
251 + buffer.writeBytes(openMsg);
252 +
253 + BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
254 + BGPMessage message;
255 + BGPHeader bgpHeader = new BGPHeader();
256 + message = reader.readFrom(buffer, bgpHeader);
257 +
258 + assertThat(message, instanceOf(BGPOpenMsg.class));
259 + }
260 +
261 + /**
262 + * In this test case, Invalid message length is given as input and expecting
263 + * an exception.
264 + */
265 + @Test(expected = BGPParseException.class)
266 + public void openMessageTest7() throws BGPParseException {
267 +
268 + // OPEN Message with invalid header length.
269 + byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
270 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
271 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
272 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
273 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
274 + (byte) 0xff, 0x00, 0x1e, 0x01, 0X04,
275 + (byte) 0xfe, 0x09, 0x00, (byte) 0xb4,
276 + (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f,
277 + 0x00};
278 +
279 + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
280 + buffer.writeBytes(openMsg);
281 +
282 + BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
283 + BGPMessage message;
284 + BGPHeader bgpHeader = new BGPHeader();
285 + message = reader.readFrom(buffer, bgpHeader);
286 +
287 + assertThat(message, instanceOf(BGPOpenMsg.class));
288 + }
289 +
290 + /**
291 + * In this test case, Invalid message type is given as input and expecting
292 + * an exception.
293 + */
294 + @Test(expected = BGPParseException.class)
295 + public void openMessageTest8() throws BGPParseException {
296 +
297 + // OPEN Message with invalid message type.
298 + byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
299 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
300 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
301 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
302 + (byte) 0xff, (byte) 0xff, (byte) 0xff,
303 + (byte) 0xff, 0x00, 0x1d, 0x05, 0X04,
304 + (byte) 0xfe, 0x09, 0x00, (byte) 0xb4,
305 + (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f,
306 + 0x00};
307 +
308 + ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
309 + buffer.writeBytes(openMsg);
310 +
311 + BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
312 + BGPMessage message;
313 + BGPHeader bgpHeader = new BGPHeader();
314 + message = reader.readFrom(buffer, bgpHeader);
315 +
316 + assertThat(message, instanceOf(BGPOpenMsg.class));
317 + }
318 +}