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