6dfa57ee45b1b23bcc72faf097ffa7a1c9dcadb1
[onosfw.git] /
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.bgpio.protocol;
17
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;
23
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.instanceOf;
26 import static org.hamcrest.core.Is.is;
27
28 /**
29  * Test for Notification message.
30  */
31 public 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     }
224 }