2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.bgpio.protocol;
18 import org.jboss.netty.buffer.ChannelBuffer;
19 import org.jboss.netty.buffer.ChannelBuffers;
20 import org.junit.Test;
21 import org.onosproject.bgpio.exceptions.BgpParseException;
22 import org.onosproject.bgpio.types.BgpHeader;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.instanceOf;
26 import static org.hamcrest.core.Is.is;
29 * Test for Notification message.
31 public class BgpNotificationMsgTest {
34 * Notification message with error code, error subcode and data.
36 * @throws BgpParseException while decoding and encoding notification message
39 public void bgpNotificationMessageTest1() throws BgpParseException {
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};
51 byte[] testNotificationMsg = {0};
52 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
53 buffer.writeBytes(notificationMsg);
55 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
56 BgpMessage message = null;
57 BgpHeader bgpHeader = new BgpHeader();
59 message = reader.readFrom(buffer, bgpHeader);
60 assertThat(message, instanceOf(BgpNotificationMsg.class));
62 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
64 testNotificationMsg = buf.array();
66 int iReadLen = buf.writerIndex() - 0;
67 testNotificationMsg = new byte[iReadLen];
68 buf.readBytes(testNotificationMsg, 0, iReadLen);
69 assertThat(testNotificationMsg, is(notificationMsg));
73 * Notification message without data.
75 * @throws BgpParseException while decoding and encoding notification message
78 public void bgpNotificationMessageTest2() throws BgpParseException {
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};
89 byte[] testNotificationMsg = {0};
90 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
91 buffer.writeBytes(notificationMsg);
93 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
94 BgpMessage message = null;
95 BgpHeader bgpHeader = new BgpHeader();
97 message = reader.readFrom(buffer, bgpHeader);
98 assertThat(message, instanceOf(BgpNotificationMsg.class));
100 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
101 message.writeTo(buf);
102 testNotificationMsg = buf.array();
104 int iReadLen = buf.writerIndex() - 0;
105 testNotificationMsg = new byte[iReadLen];
106 buf.readBytes(testNotificationMsg, 0, iReadLen);
107 assertThat(testNotificationMsg, is(notificationMsg));
112 * Notification message with wrong maker value.
114 * @throws BgpParseException while decoding and encoding notification message
116 @Test(expected = BgpParseException.class)
117 public void bgpNotificationMessageTest3() throws BgpParseException {
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,
124 (byte) 0xff, (byte) 0xff,
125 (byte) 0xff, (byte) 0xff, 0x00,
126 0x15, 0x03, 0x02, 0x00};
128 byte[] testNotificationMsg = {0};
129 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
130 buffer.writeBytes(notificationMsg);
132 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
133 BgpMessage message = null;
134 BgpHeader bgpHeader = new BgpHeader();
136 message = reader.readFrom(buffer, bgpHeader);
137 assertThat(message, instanceOf(BgpNotificationMsg.class));
139 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
140 message.writeTo(buf);
141 testNotificationMsg = buf.array();
143 int iReadLen = buf.writerIndex() - 0;
144 testNotificationMsg = new byte[iReadLen];
145 buf.readBytes(testNotificationMsg, 0, iReadLen);
146 assertThat(testNotificationMsg, is(notificationMsg));
150 * Notification message without error subcode.
152 * @throws BgpParseException while decoding and encoding notification message
154 @Test(expected = BgpParseException.class)
155 public void bgpNotificationMessageTest4() throws BgpParseException {
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,
166 byte[] testNotificationMsg = {0};
167 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
168 buffer.writeBytes(notificationMsg);
170 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
171 BgpMessage message = null;
172 BgpHeader bgpHeader = new BgpHeader();
174 message = reader.readFrom(buffer, bgpHeader);
175 assertThat(message, instanceOf(BgpNotificationMsg.class));
177 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
178 message.writeTo(buf);
179 testNotificationMsg = buf.array();
181 int iReadLen = buf.writerIndex() - 0;
182 testNotificationMsg = new byte[iReadLen];
183 buf.readBytes(testNotificationMsg, 0, iReadLen);
184 assertThat(testNotificationMsg, is(notificationMsg));
188 * Notification message with wrong message length.
190 * @throws BgpParseException while decoding and encoding notification message
192 @Test(expected = BgpParseException.class)
193 public void bgpNotificationMessageTest5() throws BgpParseException {
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};
204 byte[] testNotificationMsg = {0};
205 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
206 buffer.writeBytes(notificationMsg);
208 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
209 BgpMessage message = null;
210 BgpHeader bgpHeader = new BgpHeader();
212 message = reader.readFrom(buffer, bgpHeader);
213 assertThat(message, instanceOf(BgpNotificationMsg.class));
215 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
216 message.writeTo(buf);
217 testNotificationMsg = buf.array();
219 int iReadLen = buf.writerIndex() - 0;
220 testNotificationMsg = new byte[iReadLen];
221 buf.readBytes(testNotificationMsg, 0, iReadLen);
222 assertThat(testNotificationMsg, is(notificationMsg));