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