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