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