blob: 6dfa57ee45b1b23bcc72faf097ffa7a1c9dcadb1 [file] [log] [blame]
Priyanka B6f4899c2015-10-30 17:12:21 +05301/*
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 Milkey753ad632015-11-06 08:46:33 -080016package org.onosproject.bgpio.protocol;
Priyanka B6f4899c2015-10-30 17:12:21 +053017
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.jboss.netty.buffer.ChannelBuffers;
20import org.junit.Test;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053021import org.onosproject.bgpio.exceptions.BgpParseException;
22import org.onosproject.bgpio.types.BgpHeader;
Priyanka B6f4899c2015-10-30 17:12:21 +053023
Ray Milkey753ad632015-11-06 08:46:33 -080024import static org.hamcrest.MatcherAssert.assertThat;
25import static org.hamcrest.Matchers.instanceOf;
26import static org.hamcrest.core.Is.is;
27
Priyanka B6f4899c2015-10-30 17:12:21 +053028/**
29 * Test for Notification message.
30 */
31public class BgpNotificationMsgTest {
32
33 /**
34 * Notification message with error code, error subcode and data.
35 *
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053036 * @throws BgpParseException while decoding and encoding notification message
Priyanka B6f4899c2015-10-30 17:12:21 +053037 */
38 @Test
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053039 public void bgpNotificationMessageTest1() throws BgpParseException {
Priyanka B6f4899c2015-10-30 17:12:21 +053040 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 VH5dd8dbe2015-11-26 13:22:18 +053055 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
56 BgpMessage message = null;
57 BgpHeader bgpHeader = new BgpHeader();
Priyanka B6f4899c2015-10-30 17:12:21 +053058
59 message = reader.readFrom(buffer, bgpHeader);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053060 assertThat(message, instanceOf(BgpNotificationMsg.class));
Priyanka B6f4899c2015-10-30 17:12:21 +053061
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 VH5dd8dbe2015-11-26 13:22:18 +053075 * @throws BgpParseException while decoding and encoding notification message
Priyanka B6f4899c2015-10-30 17:12:21 +053076 */
77 @Test
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053078 public void bgpNotificationMessageTest2() throws BgpParseException {
Priyanka B6f4899c2015-10-30 17:12:21 +053079 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 VH5dd8dbe2015-11-26 13:22:18 +053093 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
94 BgpMessage message = null;
95 BgpHeader bgpHeader = new BgpHeader();
Priyanka B6f4899c2015-10-30 17:12:21 +053096
97 message = reader.readFrom(buffer, bgpHeader);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053098 assertThat(message, instanceOf(BgpNotificationMsg.class));
Priyanka B6f4899c2015-10-30 17:12:21 +053099
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 VH5dd8dbe2015-11-26 13:22:18 +0530114 * @throws BgpParseException while decoding and encoding notification message
Priyanka B6f4899c2015-10-30 17:12:21 +0530115 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530116 @Test(expected = BgpParseException.class)
117 public void bgpNotificationMessageTest3() throws BgpParseException {
Priyanka B6f4899c2015-10-30 17:12:21 +0530118 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 VH5dd8dbe2015-11-26 13:22:18 +0530132 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
133 BgpMessage message = null;
134 BgpHeader bgpHeader = new BgpHeader();
Priyanka B6f4899c2015-10-30 17:12:21 +0530135
136 message = reader.readFrom(buffer, bgpHeader);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530137 assertThat(message, instanceOf(BgpNotificationMsg.class));
Priyanka B6f4899c2015-10-30 17:12:21 +0530138
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 VH5dd8dbe2015-11-26 13:22:18 +0530152 * @throws BgpParseException while decoding and encoding notification message
Priyanka B6f4899c2015-10-30 17:12:21 +0530153 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530154 @Test(expected = BgpParseException.class)
155 public void bgpNotificationMessageTest4() throws BgpParseException {
Priyanka B6f4899c2015-10-30 17:12:21 +0530156 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 VH5dd8dbe2015-11-26 13:22:18 +0530170 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
171 BgpMessage message = null;
172 BgpHeader bgpHeader = new BgpHeader();
Priyanka B6f4899c2015-10-30 17:12:21 +0530173
174 message = reader.readFrom(buffer, bgpHeader);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530175 assertThat(message, instanceOf(BgpNotificationMsg.class));
Priyanka B6f4899c2015-10-30 17:12:21 +0530176
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 VH5dd8dbe2015-11-26 13:22:18 +0530190 * @throws BgpParseException while decoding and encoding notification message
Priyanka B6f4899c2015-10-30 17:12:21 +0530191 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530192 @Test(expected = BgpParseException.class)
193 public void bgpNotificationMessageTest5() throws BgpParseException {
Priyanka B6f4899c2015-10-30 17:12:21 +0530194 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 VH5dd8dbe2015-11-26 13:22:18 +0530208 BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
209 BgpMessage message = null;
210 BgpHeader bgpHeader = new BgpHeader();
Priyanka B6f4899c2015-10-30 17:12:21 +0530211
212 message = reader.readFrom(buffer, bgpHeader);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530213 assertThat(message, instanceOf(BgpNotificationMsg.class));
Priyanka B6f4899c2015-10-30 17:12:21 +0530214
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 Milkey753ad632015-11-06 08:46:33 -0800224}