Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 | */ |
Ray Milkey | 753ad63 | 2015-11-06 08:46:33 -0800 | [diff] [blame] | 16 | package org.onosproject.bgpio.protocol; |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.jboss.netty.buffer.ChannelBuffers; |
| 20 | import org.junit.Test; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 21 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 22 | import org.onosproject.bgpio.types.BgpHeader; |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 23 | |
Ray Milkey | 753ad63 | 2015-11-06 08:46:33 -0800 | [diff] [blame] | 24 | import static org.hamcrest.MatcherAssert.assertThat; |
| 25 | import static org.hamcrest.Matchers.instanceOf; |
| 26 | import static org.hamcrest.core.Is.is; |
| 27 | |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 28 | /** |
| 29 | * Test cases for BGP Open Message. |
| 30 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 31 | public class BgpOpenMsgTest { |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * This test case checks open message without optional parameter. |
| 35 | */ |
| 36 | @Test |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 37 | public void openMessageTest1() throws BgpParseException { |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 51 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 52 | BgpMessage message; |
| 53 | BgpHeader bgpHeader = new BgpHeader(); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 54 | message = reader.readFrom(buffer, bgpHeader); |
| 55 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 56 | assertThat(message, instanceOf(BgpOpenMsg.class)); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 72 | public void openMessageTest2() throws BgpParseException { |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 94 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 95 | BgpMessage message; |
| 96 | BgpHeader bgpHeader = new BgpHeader(); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 97 | |
| 98 | message = reader.readFrom(buffer, bgpHeader); |
| 99 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 100 | assertThat(message, instanceOf(BgpOpenMsg.class)); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 116 | public void openMessageTest3() throws BgpParseException { |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 138 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 139 | BgpMessage message; |
| 140 | BgpHeader bgpHeader = new BgpHeader(); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 141 | |
| 142 | message = reader.readFrom(buffer, bgpHeader); |
| 143 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 144 | assertThat(message, instanceOf(BgpOpenMsg.class)); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 159 | public void openMessageTest4() throws BgpParseException { |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 181 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 182 | BgpMessage message; |
| 183 | BgpHeader bgpHeader = new BgpHeader(); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 184 | |
| 185 | message = reader.readFrom(buffer, bgpHeader); |
| 186 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 187 | assertThat(message, instanceOf(BgpOpenMsg.class)); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 203 | @Test(expected = BgpParseException.class) |
| 204 | public void openMessageTest5() throws BgpParseException { |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 220 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 221 | BgpMessage message; |
| 222 | BgpHeader bgpHeader = new BgpHeader(); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 223 | message = reader.readFrom(buffer, bgpHeader); |
| 224 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 225 | assertThat(message, instanceOf(BgpOpenMsg.class)); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /** |
| 229 | * In this test case, Marker is set as 0 in input and expecting |
| 230 | * an exception. |
| 231 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 232 | @Test(expected = BgpParseException.class) |
| 233 | public void openMessageTest6() throws BgpParseException { |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 249 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 250 | BgpMessage message; |
| 251 | BgpHeader bgpHeader = new BgpHeader(); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 252 | message = reader.readFrom(buffer, bgpHeader); |
| 253 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 254 | assertThat(message, instanceOf(BgpOpenMsg.class)); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
| 258 | * In this test case, Invalid message length is given as input and expecting |
| 259 | * an exception. |
| 260 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 261 | @Test(expected = BgpParseException.class) |
| 262 | public void openMessageTest7() throws BgpParseException { |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 278 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 279 | BgpMessage message; |
| 280 | BgpHeader bgpHeader = new BgpHeader(); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 281 | message = reader.readFrom(buffer, bgpHeader); |
| 282 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 283 | assertThat(message, instanceOf(BgpOpenMsg.class)); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
| 287 | * In this test case, Invalid message type is given as input and expecting |
| 288 | * an exception. |
| 289 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 290 | @Test(expected = BgpParseException.class) |
| 291 | public void openMessageTest8() throws BgpParseException { |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 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 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 307 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 308 | BgpMessage message; |
| 309 | BgpHeader bgpHeader = new BgpHeader(); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 310 | message = reader.readFrom(buffer, bgpHeader); |
| 311 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 312 | assertThat(message, instanceOf(BgpOpenMsg.class)); |
Vidyashree Rama | c95ba00 | 2015-11-05 18:10:55 +0530 | [diff] [blame] | 313 | } |
| 314 | } |