Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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; |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +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; |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +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 | |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 28 | /** |
| 29 | * Test for Notification message. |
| 30 | */ |
| 31 | public class BgpNotificationMsgTest { |
| 32 | |
| 33 | /** |
| 34 | * Notification message with error code, error subcode and data. |
| 35 | * |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 36 | * @throws BgpParseException while decoding and encoding notification message |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 37 | */ |
| 38 | @Test |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 39 | public void bgpNotificationMessageTest1() throws BgpParseException { |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 40 | byte[] notificationMsg = new byte[] {(byte) 0xff, (byte) 0xff, |
| 41 | (byte) 0xff, (byte) 0xff, |
| 42 | (byte) 0xff, (byte) 0xff, |
| 43 | (byte) 0xff, (byte) 0xff, |
| 44 | (byte) 0xff, (byte) 0xff, |
| 45 | (byte) 0xff, (byte) 0xff, |
| 46 | (byte) 0xff, (byte) 0xff, |
| 47 | (byte) 0xff, (byte) 0xff, 0x00, |
| 48 | 0x17, 0x03, 0x02, 0x02, |
| 49 | (byte) 0xfe, (byte) 0xb0}; |
| 50 | |
| 51 | byte[] testNotificationMsg = {0}; |
| 52 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 53 | buffer.writeBytes(notificationMsg); |
| 54 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 55 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 56 | BgpMessage message = null; |
| 57 | BgpHeader bgpHeader = new BgpHeader(); |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 58 | |
| 59 | message = reader.readFrom(buffer, bgpHeader); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 60 | assertThat(message, instanceOf(BgpNotificationMsg.class)); |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 61 | |
| 62 | ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); |
| 63 | message.writeTo(buf); |
| 64 | testNotificationMsg = buf.array(); |
| 65 | |
| 66 | int iReadLen = buf.writerIndex() - 0; |
| 67 | testNotificationMsg = new byte[iReadLen]; |
| 68 | buf.readBytes(testNotificationMsg, 0, iReadLen); |
| 69 | assertThat(testNotificationMsg, is(notificationMsg)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Notification message without data. |
| 74 | * |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 75 | * @throws BgpParseException while decoding and encoding notification message |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 76 | */ |
| 77 | @Test |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 78 | public void bgpNotificationMessageTest2() throws BgpParseException { |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 79 | byte[] notificationMsg = new byte[] {(byte) 0xff, (byte) 0xff, |
| 80 | (byte) 0xff, (byte) 0xff, |
| 81 | (byte) 0xff, (byte) 0xff, |
| 82 | (byte) 0xff, (byte) 0xff, |
| 83 | (byte) 0xff, (byte) 0xff, |
| 84 | (byte) 0xff, (byte) 0xff, |
| 85 | (byte) 0xff, (byte) 0xff, |
| 86 | (byte) 0xff, (byte) 0xff, 0x00, |
| 87 | 0x15, 0x03, 0x02, 0x00}; |
| 88 | |
| 89 | byte[] testNotificationMsg = {0}; |
| 90 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 91 | buffer.writeBytes(notificationMsg); |
| 92 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 93 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 94 | BgpMessage message = null; |
| 95 | BgpHeader bgpHeader = new BgpHeader(); |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 96 | |
| 97 | message = reader.readFrom(buffer, bgpHeader); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 98 | assertThat(message, instanceOf(BgpNotificationMsg.class)); |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 99 | |
| 100 | ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); |
| 101 | message.writeTo(buf); |
| 102 | testNotificationMsg = buf.array(); |
| 103 | |
| 104 | int iReadLen = buf.writerIndex() - 0; |
| 105 | testNotificationMsg = new byte[iReadLen]; |
| 106 | buf.readBytes(testNotificationMsg, 0, iReadLen); |
| 107 | assertThat(testNotificationMsg, is(notificationMsg)); |
| 108 | } |
| 109 | |
| 110 | //Negative scenarios |
| 111 | /** |
| 112 | * Notification message with wrong maker value. |
| 113 | * |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 114 | * @throws BgpParseException while decoding and encoding notification message |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 115 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 116 | @Test(expected = BgpParseException.class) |
| 117 | public void bgpNotificationMessageTest3() throws BgpParseException { |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 118 | byte[] notificationMsg = new byte[] {(byte) 0xff, (byte) 0xff, |
| 119 | (byte) 0xff, (byte) 0xff, |
| 120 | (byte) 0xff, (byte) 0xff, |
| 121 | (byte) 0xff, (byte) 0xff, |
| 122 | (byte) 0xff, (byte) 0xff, |
| 123 | 0x01, (byte) 0xff, |
| 124 | (byte) 0xff, (byte) 0xff, |
| 125 | (byte) 0xff, (byte) 0xff, 0x00, |
| 126 | 0x15, 0x03, 0x02, 0x00}; |
| 127 | |
| 128 | byte[] testNotificationMsg = {0}; |
| 129 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 130 | buffer.writeBytes(notificationMsg); |
| 131 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 132 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 133 | BgpMessage message = null; |
| 134 | BgpHeader bgpHeader = new BgpHeader(); |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 135 | |
| 136 | message = reader.readFrom(buffer, bgpHeader); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 137 | assertThat(message, instanceOf(BgpNotificationMsg.class)); |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 138 | |
| 139 | ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); |
| 140 | message.writeTo(buf); |
| 141 | testNotificationMsg = buf.array(); |
| 142 | |
| 143 | int iReadLen = buf.writerIndex() - 0; |
| 144 | testNotificationMsg = new byte[iReadLen]; |
| 145 | buf.readBytes(testNotificationMsg, 0, iReadLen); |
| 146 | assertThat(testNotificationMsg, is(notificationMsg)); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Notification message without error subcode. |
| 151 | * |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 152 | * @throws BgpParseException while decoding and encoding notification message |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 153 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 154 | @Test(expected = BgpParseException.class) |
| 155 | public void bgpNotificationMessageTest4() throws BgpParseException { |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 156 | byte[] notificationMsg = new byte[] {(byte) 0xff, (byte) 0xff, |
| 157 | (byte) 0xff, (byte) 0xff, |
| 158 | (byte) 0xff, (byte) 0xff, |
| 159 | (byte) 0xff, (byte) 0xff, |
| 160 | (byte) 0xff, (byte) 0xff, |
| 161 | (byte) 0xff, (byte) 0xff, |
| 162 | (byte) 0xff, (byte) 0xff, |
| 163 | (byte) 0xff, (byte) 0xff, 0x00, |
| 164 | 0x14, 0x03, 0x02}; |
| 165 | |
| 166 | byte[] testNotificationMsg = {0}; |
| 167 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 168 | buffer.writeBytes(notificationMsg); |
| 169 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 170 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 171 | BgpMessage message = null; |
| 172 | BgpHeader bgpHeader = new BgpHeader(); |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 173 | |
| 174 | message = reader.readFrom(buffer, bgpHeader); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 175 | assertThat(message, instanceOf(BgpNotificationMsg.class)); |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 176 | |
| 177 | ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); |
| 178 | message.writeTo(buf); |
| 179 | testNotificationMsg = buf.array(); |
| 180 | |
| 181 | int iReadLen = buf.writerIndex() - 0; |
| 182 | testNotificationMsg = new byte[iReadLen]; |
| 183 | buf.readBytes(testNotificationMsg, 0, iReadLen); |
| 184 | assertThat(testNotificationMsg, is(notificationMsg)); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Notification message with wrong message length. |
| 189 | * |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 190 | * @throws BgpParseException while decoding and encoding notification message |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 191 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 192 | @Test(expected = BgpParseException.class) |
| 193 | public void bgpNotificationMessageTest5() throws BgpParseException { |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 194 | byte[] notificationMsg = new byte[] {(byte) 0xff, (byte) 0xff, |
| 195 | (byte) 0xff, (byte) 0xff, |
| 196 | (byte) 0xff, (byte) 0xff, |
| 197 | (byte) 0xff, (byte) 0xff, |
| 198 | (byte) 0xff, (byte) 0xff, |
| 199 | (byte) 0xff, (byte) 0xff, |
| 200 | (byte) 0xff, (byte) 0xff, |
| 201 | (byte) 0xff, (byte) 0xff, 0x00, |
| 202 | 0x14, 0x03, 0x02, 0x02}; |
| 203 | |
| 204 | byte[] testNotificationMsg = {0}; |
| 205 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 206 | buffer.writeBytes(notificationMsg); |
| 207 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 208 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 209 | BgpMessage message = null; |
| 210 | BgpHeader bgpHeader = new BgpHeader(); |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 211 | |
| 212 | message = reader.readFrom(buffer, bgpHeader); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 213 | assertThat(message, instanceOf(BgpNotificationMsg.class)); |
Priyanka B | 6f4899c | 2015-10-30 17:12:21 +0530 | [diff] [blame] | 214 | |
| 215 | ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); |
| 216 | message.writeTo(buf); |
| 217 | testNotificationMsg = buf.array(); |
| 218 | |
| 219 | int iReadLen = buf.writerIndex() - 0; |
| 220 | testNotificationMsg = new byte[iReadLen]; |
| 221 | buf.readBytes(testNotificationMsg, 0, iReadLen); |
| 222 | assertThat(testNotificationMsg, is(notificationMsg)); |
| 223 | } |
Ray Milkey | 753ad63 | 2015-11-06 08:46:33 -0800 | [diff] [blame] | 224 | } |