2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser.impl;
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;
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;
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
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
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
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
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
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
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
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
126 private static MessageRegistry reg;
129 public static void setupClass() throws Exception {
130 reg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
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,
140 wrong = ByteArray.cutBytes(wrong, 16);
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.",
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
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());
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));
176 final Notification m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(ByteArray.getAllBytes(buffer)), null);
177 assertTrue(m instanceof Keepalive);
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
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());
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)))
207 final ByteBuf bytes = Unpooled.buffer();
208 ParserTest.reg.serializeMessage(open, bytes);
209 assertArrayEquals(OPEN_BMSG, ByteArray.getAllBytes(bytes));
211 final Notification m = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
212 assertTrue(m instanceof Open);
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());
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
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());
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
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());
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
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());
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()));
287 Notification msg = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
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());
294 notMsg = new NotifyBuilder().setErrorCode(BGPError.CONNECTION_NOT_SYNC.getCode()).setErrorSubcode(
295 BGPError.CONNECTION_NOT_SYNC.getSubcode()).build();
299 ParserTest.reg.serializeMessage(notMsg, bytes);
301 msg = ParserTest.reg.parseMessage(Unpooled.copiedBuffer(bytes), null);
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());
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
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());
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
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());
347 public void testParseUpdMsgWithUnrecognizedAttribute() throws BGPDocumentedException, BGPParsingException {
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());
360 public void testParseUpdMsgWithMandatoryAttributesPresent() throws BGPDocumentedException, BGPParsingException {
362 final Notification msg = reg.parseMessage(Unpooled.copiedBuffer(UPD_MSG_WITH_MANDATORY_ATTRIBUTES_PRESENT),
364 assertTrue(msg instanceof Update);
365 } catch (final BGPDocumentedException e) {
366 fail("Exception should not have occured.");
371 public void testParseUpdMsgWithOneMandatoryAttributeNotPresent()
372 throws BGPDocumentedException, BGPParsingException {
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());
386 public void testParseUpdMsgWithMultipleMandatoryAttributesNotPresent()
387 throws BGPDocumentedException, BGPParsingException {
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());
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));
408 final Notification m = ParserTest.reg.parseMessage(
409 Unpooled.copiedBuffer(ByteArray.getAllBytes(buffer)), null);
411 assertTrue(m instanceof RouteRefresh);