Fix most bgp-parser-impl checkstyle
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / ParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.parser.impl;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.net.UnknownHostException;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
24 import org.opendaylight.protocol.bgp.parser.BGPError;
25 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
26 import org.opendaylight.protocol.bgp.parser.impl.message.update.NextHopAttributeParser;
27 import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginAttributeParser;
28 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
29 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
30 import org.opendaylight.protocol.util.ByteArray;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Keepalive;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.KeepaliveBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Notify;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.NotifyBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Open;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.ProtocolVersion;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.RouteRefresh;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.RouteRefreshBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
44 import org.opendaylight.yangtools.yang.binding.Notification;
45
46 public class ParserTest {
47     private static final byte[] OPEN_BMSG = new byte[] {
48         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
49         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
50         (byte) 0x00, (byte) 0x1d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x64, (byte) 0x00, (byte) 0xb4,
51         (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00
52     };
53
54     private static final byte[] KEEPALIVE_BMSG = new byte[] {
55         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
56         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
57         (byte) 0x00, (byte) 0x13, (byte) 0x04
58     };
59
60     private static final byte[] NOTIFICATION_BMSG = new byte[] {
61         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
62         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
63         (byte) 0x00, (byte) 0x17, (byte) 0x03, (byte) 0x02, (byte) 0x04, (byte) 0x04, (byte) 0x09
64     };
65
66     private static final byte[] UPD_MSG_WITH_UNRECOGNIZED_ATTRIBUTE = new byte[] {
67         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
68         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
69         (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x79, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00,
70         (byte) 0x62, (byte) 0x90, (byte) 0x0e, (byte) 0x00, (byte) 0x34, (byte) 0x40, (byte) 0x04, (byte) 0x47,
71         (byte) 0x04, (byte) 0x0a, (byte) 0x19, (byte) 0x02, (byte) 0x1b, (byte) 0x00, (byte) 0x00, (byte) 0x01,
72         (byte) 0x00, (byte) 0x27, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
73         (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x1a, (byte) 0x02,
74         (byte) 0x00, (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x48, (byte) 0x02,
75         (byte) 0x01, (byte) 0x00, (byte) 0x04, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x28, (byte) 0x02,
76         (byte) 0x03, (byte) 0x00, (byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
77         (byte) 0x43, (byte) 0x40, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00,
78         (byte) 0x40, (byte) 0x05, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte )0x64, (byte) 0x00,
79         (byte) 0x63, (byte) 0x19, (byte) 0x04, (byte) 0x02, (byte) 0x00, (byte) 0x08, (byte) 0x4f, (byte) 0x66,
80         (byte) 0x2d, (byte) 0x39, (byte) 0x6b, (byte) 0x2d, (byte) 0x30, (byte) 0x33, (byte) 0x04, (byte) 0x03,
81         (byte) 0x00, (byte) 0x01, (byte) 0x72, (byte) 0x04, (byte) 0x04, (byte) 0x00, (byte) 0x04, (byte) 0x2b,
82         (byte) 0x2b, (byte) 0x2b, (byte) 0x2b
83     };
84
85     // Update message with one IPv4 prefix NLRI and all mandatory path attributes present
86     private static final byte[] UPD_MSG_WITH_MANDATORY_ATTRIBUTES_PRESENT = new byte[] {
87         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
88         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
89         (byte) 0x00, (byte) 0x38, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x1c, (byte) 0x40,
90         (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x40, (byte) 0x02, (byte) 0x00, (byte) 0x40, (byte) 0x03,
91         (byte) 0x04, (byte) 0x0a, (byte) 0x12, (byte) 0xa2, (byte) 0xf1, (byte) 0x80, (byte) 0x04, (byte) 0x04,
92         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x05, (byte) 0x04, (byte) 0x00,
93         (byte) 0x00, (byte) 0x00, (byte) 0x64, (byte) 0x20, (byte) 0xc0, (byte) 0xa8, (byte) 0xfe, (byte) 0x03
94     };
95
96     // Update message with one IPv4 prefix NLRI and all mandatory path attributes except ORIGIN present
97     private static final byte[] UPD_MSG_WITH_ONE_MANDATORY_ATTRIBUTE_NOT_PRESENT = new byte[] {
98         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
99         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
100         (byte) 0x00, (byte) 0x34, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x18, (byte) 0x40,
101         (byte) 0x02, (byte) 0x00, (byte) 0x40, (byte) 0x03, (byte) 0x04, (byte) 0x0a, (byte) 0x12, (byte) 0xa2,
102         (byte) 0xf1, (byte) 0x80, (byte) 0x04, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
103         (byte) 0x40, (byte) 0x05, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x64, (byte) 0x20,
104         (byte) 0xc0, (byte) 0xa8, (byte) 0xfe, (byte) 0x03
105     };
106
107     // Update message with one IPv4 prefix NLRI and ORIGIN, AS_PATH and NEXT_HOP mandatory path attributes not present
108     private static final byte[] UPD_MSG_WITH_MULTIPLE_MANDATORY_ATTRIBUTES_NOT_PRESENT = new byte[] {
109         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
110         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
111         (byte) 0x00, (byte) 0x2a, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0e, (byte) 0x80,
112         (byte) 0x04, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x40, (byte) 0x05,
113         (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x64, (byte) 0x20, (byte) 0xc0, (byte) 0xa8,
114         (byte) 0xfe, (byte) 0x03
115     };
116
117     private static final byte[] RR_MSG = new byte[] {
118         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
119         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
120         (byte) 0x00, (byte) 0x17, (byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x01
121     };
122
123     private static MessageRegistry reg;
124
125     @BeforeClass
126     public static void setupClass() throws Exception {
127         reg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
128     }
129
130     @Test
131     public void testHeaderErrors() throws BGPParsingException, BGPDocumentedException {
132         byte[] wrong = new byte[] {
133             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
134             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
135             (byte) 0x00
136         };
137         wrong = ByteArray.cutBytes(wrong, 16);
138         try {
139             ParserTest.reg.parseMessage(Unpooled.copiedBuffer(wrong), null);
140             fail("Exception should have occcured.");
141         } catch (final IllegalArgumentException e) {
142             assertEquals("Too few bytes in passed array. Passed: " + wrong.length + ". Expected: >= 19.",
143                 e.getMessage());
144             return;
145         }
146         fail();
147     }
148
149     @Test
150     public void testBadMsgType() throws BGPParsingException {
151         final byte[] bytes = {
152             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
153             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
154             (byte) 0x00, (byte) 0x13, (byte) 0x08
155         };
156         try {
157             ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
158             fail("Exception should have occured.");
159         } catch (final BGPDocumentedException e) {
160             assertEquals(BGPError.BAD_MSG_TYPE, e.getError());
161             return;
162         }
163         fail();
164     }
165
166     @Test
167     public void testKeepAliveMsg() throws BGPParsingException, BGPDocumentedException {
168         final Notification keepAlive = new KeepaliveBuilder().build();
169         final ByteBuf buffer = Unpooled.buffer();
170         ParserTest.reg.serializeMessage(keepAlive, buffer);
171         assertArrayEquals(KEEPALIVE_BMSG, ByteArray.getAllBytes(buffer));
172
173         final Notification m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(ByteArray.getAllBytes(buffer)), null);
174         assertTrue(m instanceof Keepalive);
175     }
176
177     @Test
178     public void testBadKeepAliveMsg() throws BGPParsingException {
179         final byte[] bytes = new byte[] {
180             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
181             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
182             (byte) 0x00, (byte) 0x14, (byte) 0x04, (byte) 0x05
183         };
184
185         try {
186             ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
187             fail("Exception should have occured.");
188         } catch (final BGPDocumentedException e) {
189             assertThat(e.getMessage(), containsString("Message length field not within valid range."));
190             assertEquals(BGPError.BAD_MSG_LENGTH, e.getError());
191             return;
192         }
193         fail();
194     }
195
196     @Test
197     public void testOpenMessage() throws UnknownHostException, BGPParsingException, BGPDocumentedException {
198         final Notification open = new OpenBuilder().setMyAsNumber(100).setHoldTimer(180)
199             .setBgpIdentifier(new Ipv4Address("20.20.20.20")).setVersion(
200             new ProtocolVersion((short) 4)).build();
201         final ByteBuf bytes = Unpooled.buffer();
202         ParserTest.reg.serializeMessage(open, bytes);
203         assertArrayEquals(OPEN_BMSG, ByteArray.getAllBytes(bytes));
204
205         final Notification m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
206
207         assertTrue(m instanceof Open);
208         assertEquals(100, ((Open) m).getMyAsNumber().intValue());
209         assertEquals(180, ((Open) m).getHoldTimer().intValue());
210         assertEquals(new Ipv4Address("20.20.20.20"), ((Open) m).getBgpIdentifier());
211         assertTrue(((Open) m).getBgpParameters().isEmpty());
212     }
213
214     @Test
215     public void testBadHoldTimeError() throws BGPParsingException {
216         final byte[] bMsg = new byte[] {
217             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
218             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
219             (byte) 0x00, (byte) 0x1d, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x64, (byte) 0x00, (byte) 0x01,
220             (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00
221         };
222
223         try {
224             ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bMsg), null);
225             fail("Exception should have occured.");
226         } catch (final BGPDocumentedException e) {
227             assertEquals("Hold time value not acceptable.", e.getMessage());
228             assertEquals(BGPError.HOLD_TIME_NOT_ACC, e.getError());
229             return;
230         }
231         fail();
232     }
233
234     @Test
235     public void testBadMsgLength() throws BGPParsingException {
236         final byte[] bMsg = new byte[] {
237             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
238             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
239             (byte) 0x00, (byte) 0x1b, (byte) 0x01, (byte) 0x04, (byte) 0x00, (byte) 0x64, (byte) 0x00, (byte) 0xb4,
240             (byte) 0xff, (byte) 0xff, (byte) 0xff
241         };
242
243         try {
244             ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bMsg), null);
245             fail("Exception should have occured.");
246         } catch (final BGPDocumentedException e) {
247             assertEquals("Open message too small.", e.getMessage());
248             assertEquals(BGPError.BAD_MSG_LENGTH, e.getError());
249         }
250     }
251
252     @Test
253     public void testBadVersion() throws BGPParsingException {
254         final byte[] bMsg = new byte[] {
255             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
256             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
257             (byte) 0x00, (byte) 0x1d, (byte) 0x01, (byte) 0x08, (byte) 0x00, (byte) 0x64, (byte) 0x00, (byte) 0xb4,
258             (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0x00
259         };
260
261         try {
262             ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bMsg), null);
263             fail("Exception should have occured.");
264         } catch (final BGPDocumentedException e) {
265             assertEquals("BGP Protocol version 8 not supported.", e.getMessage());
266             assertEquals(BGPError.VERSION_NOT_SUPPORTED, e.getError());
267             return;
268         }
269         fail();
270     }
271
272     @Test
273     public void testNotificationMsg() throws BGPParsingException, BGPDocumentedException {
274         Notification notMsg = new NotifyBuilder().setErrorCode(BGPError.OPT_PARAM_NOT_SUPPORTED.getCode())
275             .setErrorSubcode(BGPError.OPT_PARAM_NOT_SUPPORTED.getSubcode()).setData(new byte[] { 4, 9 }).build();
276         final ByteBuf bytes = Unpooled.buffer();
277         ParserTest.reg.serializeMessage(notMsg, bytes);
278         assertArrayEquals(NOTIFICATION_BMSG, ByteArray.subByte(bytes.array(),0,bytes.writerIndex()));
279
280         Notification msg = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
281
282         assertTrue(msg instanceof Notify);
283         assertEquals(BGPError.OPT_PARAM_NOT_SUPPORTED, BGPError.forValue(((Notify) msg).getErrorCode(),
284             ((Notify) msg).getErrorSubcode()));
285         assertArrayEquals(new byte[] { 4, 9 }, ((Notify) msg).getData());
286
287         notMsg = new NotifyBuilder().setErrorCode(BGPError.CONNECTION_NOT_SYNC.getCode()).setErrorSubcode(
288             BGPError.CONNECTION_NOT_SYNC.getSubcode()).build();
289
290         bytes.clear();
291
292         ParserTest.reg.serializeMessage(notMsg, bytes);
293
294         msg = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
295
296         assertTrue(msg instanceof Notify);
297         assertEquals(BGPError.CONNECTION_NOT_SYNC, BGPError.forValue(((Notify) msg).getErrorCode(),
298             ((Notify) msg).getErrorSubcode()));
299         assertNull(((Notify) msg).getData());
300     }
301
302     @Test
303     public void testWrongLength() throws BGPParsingException {
304         final byte[] bMsg = new byte[] {
305             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
306             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
307             (byte) 0x00, (byte) 0x14, (byte) 0x03, (byte) 0x02
308         };
309
310         try {
311             ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bMsg), null);
312             fail("Exception should have occured.");
313         } catch (final BGPDocumentedException e) {
314             assertEquals("Notification message too small.", e.getMessage());
315             assertEquals(BGPError.BAD_MSG_LENGTH, e.getError());
316             return;
317         }
318         fail();
319     }
320
321     @Test
322     public void testUnrecognizedError() throws BGPParsingException, BGPDocumentedException {
323         final byte[] bMsg = new byte[] {
324             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
325             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
326             (byte) 0x00, (byte) 0x15, (byte) 0x03, (byte) 0x02, (byte) 0xaa
327         };
328
329         try {
330             ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bMsg), null);
331             fail("Exception should have occured.");
332         } catch (final IllegalArgumentException e) {
333             assertEquals("BGP Error code 2 and subcode 170 not recognized.", e.getMessage());
334             return;
335         }
336         fail();
337     }
338
339     @Test
340     public void testParseUpdMsgWithUnrecognizedAttribute() throws BGPDocumentedException, BGPParsingException {
341         try {
342             reg.parseMessage(Unpooled.copiedBuffer(UPD_MSG_WITH_UNRECOGNIZED_ATTRIBUTE), null);
343             fail("Exception should have occured.");
344         } catch (final BGPDocumentedException e) {
345             assertEquals("Well known attribute not recognized.", e.getMessage());
346             assertEquals(BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED, e.getError());
347             return;
348         }
349         fail();
350     }
351
352     @Test
353     public void testParseUpdMsgWithMandatoryAttributesPresent() throws BGPDocumentedException, BGPParsingException {
354         try {
355             final Notification msg = reg.parseMessage(Unpooled.copiedBuffer(UPD_MSG_WITH_MANDATORY_ATTRIBUTES_PRESENT),
356                 null);
357             assertTrue(msg instanceof Update);
358         } catch (final BGPDocumentedException e) {
359             fail("Exception should not have occured.");
360         }
361     }
362
363     @Test
364     public void testParseUpdMsgWithOneMandatoryAttributeNotPresent()
365         throws BGPDocumentedException, BGPParsingException {
366         try {
367             reg.parseMessage(Unpooled.copiedBuffer(UPD_MSG_WITH_ONE_MANDATORY_ATTRIBUTE_NOT_PRESENT), null);
368             fail("Exception should have occured.");
369         } catch (final BGPDocumentedException e) {
370             assertEquals(BGPError.MANDATORY_ATTR_MISSING_MSG + "ORIGIN", e.getMessage());
371             assertEquals(BGPError.WELL_KNOWN_ATTR_MISSING, e.getError());
372             assertArrayEquals(new byte[] { OriginAttributeParser.TYPE }, e.getData());
373             return;
374         }
375         fail();
376     }
377
378     @Test
379     public void testParseUpdMsgWithMultipleMandatoryAttributesNotPresent()
380         throws BGPDocumentedException, BGPParsingException {
381         try {
382             reg.parseMessage(Unpooled.copiedBuffer(UPD_MSG_WITH_MULTIPLE_MANDATORY_ATTRIBUTES_NOT_PRESENT), null);
383             fail("Exception should have occured.");
384         } catch (final BGPDocumentedException e) {
385             assertEquals(BGPError.MANDATORY_ATTR_MISSING_MSG + "NEXT_HOP", e.getMessage());
386             assertEquals(BGPError.WELL_KNOWN_ATTR_MISSING, e.getError());
387             assertArrayEquals(new byte[] { NextHopAttributeParser.TYPE }, e.getData());
388             return;
389         }
390         fail();
391     }
392
393     @Test
394     public void testRouteRefreshMsg() throws BGPDocumentedException, BGPParsingException {
395         final Notification rrMsg = new RouteRefreshBuilder().setAfi(Ipv4AddressFamily.class)
396             .setSafi(UnicastSubsequentAddressFamily.class).build();
397         final ByteBuf buffer = Unpooled.buffer();
398         ParserTest.reg.serializeMessage(rrMsg, buffer);
399         assertArrayEquals(RR_MSG, ByteArray.getAllBytes(buffer));
400
401         final Notification m = ParserTest.reg.parseMessage(
402             Unpooled.copiedBuffer(ByteArray.getAllBytes(buffer)), null);
403
404         assertTrue(m instanceof RouteRefresh);
405     }
406 }