blob: 78edf133e9838cfb53b66ffe68689140c94fd08c [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;
21import org.onosproject.bgpio.exceptions.BGPParseException;
Priyanka B6f4899c2015-10-30 17:12:21 +053022import org.onosproject.bgpio.types.BGPHeader;
23
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 *
36 * @throws BGPParseException while decoding and encoding notification message
37 */
38 @Test
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};
50
51 byte[] testNotificationMsg = {0};
52 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
53 buffer.writeBytes(notificationMsg);
54
55 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
56 BGPMessage message = null;
57 BGPHeader bgpHeader = new BGPHeader();
58
59 message = reader.readFrom(buffer, bgpHeader);
60 assertThat(message, instanceOf(BGPNotificationMsg.class));
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 *
75 * @throws BGPParseException while decoding and encoding notification message
76 */
77 @Test
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};
88
89 byte[] testNotificationMsg = {0};
90 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
91 buffer.writeBytes(notificationMsg);
92
93 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
94 BGPMessage message = null;
95 BGPHeader bgpHeader = new BGPHeader();
96
97 message = reader.readFrom(buffer, bgpHeader);
98 assertThat(message, instanceOf(BGPNotificationMsg.class));
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 *
114 * @throws BGPParseException while decoding and encoding notification message
115 */
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,
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
132 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
133 BGPMessage message = null;
134 BGPHeader bgpHeader = new BGPHeader();
135
136 message = reader.readFrom(buffer, bgpHeader);
137 assertThat(message, instanceOf(BGPNotificationMsg.class));
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 *
152 * @throws BGPParseException while decoding and encoding notification message
153 */
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,
164 0x14, 0x03, 0x02};
165
166 byte[] testNotificationMsg = {0};
167 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
168 buffer.writeBytes(notificationMsg);
169
170 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
171 BGPMessage message = null;
172 BGPHeader bgpHeader = new BGPHeader();
173
174 message = reader.readFrom(buffer, bgpHeader);
175 assertThat(message, instanceOf(BGPNotificationMsg.class));
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 *
190 * @throws BGPParseException while decoding and encoding notification message
191 */
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};
203
204 byte[] testNotificationMsg = {0};
205 ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
206 buffer.writeBytes(notificationMsg);
207
208 BGPMessageReader<BGPMessage> reader = BGPFactories.getGenericReader();
209 BGPMessage message = null;
210 BGPHeader bgpHeader = new BGPHeader();
211
212 message = reader.readFrom(buffer, bgpHeader);
213 assertThat(message, instanceOf(BGPNotificationMsg.class));
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 Milkey753ad632015-11-06 08:46:33 -0800224}