MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / parser-spi / src / test / java / org / opendaylight / protocol / bgp / parser / spi / AbstractMessageRegistryTest.java
1 /*
2  * Copyright (c) 2014 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.spi;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Keepalive;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.KeepaliveBuilder;
25 import org.opendaylight.yangtools.yang.binding.Notification;
26
27 public class AbstractMessageRegistryTest {
28
29     public static final byte[] keepAliveBMsg = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
30         (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
31         (byte) 0xff, (byte) 0x00, (byte) 0x13, (byte) 0x04 };
32
33     private final AbstractMessageRegistry registry = new AbstractMessageRegistry() {
34
35         @Override
36         protected void serializeMessageImpl(final Notification message, final ByteBuf buffer) {
37             buffer.writeBytes(keepAliveBMsg);
38         }
39
40         @Override
41         protected Notification parseBody(final int type, final ByteBuf body, final int messageLength,
42                 final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
43             return new KeepaliveBuilder().build();
44         }
45     };
46
47     @Test
48     public void testRegistry() throws BGPDocumentedException, BGPParsingException {
49         final Notification keepAlive = new KeepaliveBuilder().build();
50         final ByteBuf buffer = Unpooled.buffer();
51         this.registry.serializeMessage(keepAlive, buffer);
52         assertArrayEquals(keepAliveBMsg, ByteArray.getAllBytes(buffer));
53
54         final Notification not = this.registry.parseMessage(Unpooled.copiedBuffer(keepAliveBMsg), null);
55         assertTrue(not instanceof Keepalive);
56     }
57
58     @Test
59     public void testIncompleteMarker() {
60         final byte[] testBytes = new byte[] { (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
61             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
62             (byte) 0xff, (byte) 0x00, (byte) 0x13, (byte) 0x04 };
63         try {
64             this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
65             Assert.fail();
66         } catch (BGPDocumentedException | BGPParsingException e) {
67             assertTrue(e instanceof BGPDocumentedException);
68             Assert.assertEquals("Marker not set to ones.", e.getMessage());
69         }
70     }
71
72     @Test
73     public void testInvalidLength() {
74         final byte[] testBytes = new byte[] { (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, (byte) 0xff,
76             (byte) 0xff, (byte) 0x00, (byte) 0x12, (byte) 0x04 };
77         try {
78             this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
79             Assert.fail();
80         } catch (BGPDocumentedException | BGPParsingException e) {
81             assertTrue(e instanceof BGPDocumentedException);
82             Assert.assertEquals("Message length field not within valid range.", e.getMessage());
83         }
84     }
85
86     @Test
87     public void testInvalidSpecifiedSize() {
88         final byte[] testBytes = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
89             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
90             (byte) 0xff, (byte) 0x00, (byte) 0x13, (byte) 0x04, (byte) 0x04 };
91         try {
92             this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
93             Assert.fail();
94         } catch (BGPDocumentedException | BGPParsingException e) {
95             assertTrue(e instanceof BGPParsingException);
96             Assert.assertTrue(e.getMessage().startsWith("Size doesn't match size specified in header."));
97         }
98     }
99
100     @Test
101     public void testBGPHeaderParser() throws Exception {
102         final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
103         try {
104             msgReg.parseMessage(Unpooled.copiedBuffer(new byte[] { (byte) 0, (byte) 0 }), null);
105             fail("Exception should have occured.");
106         } catch (final IllegalArgumentException e) {
107             assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
108         }
109     }
110
111     @Test
112     public void testMessageParser() throws Exception {
113         final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
114         String ex = "";
115         try {
116             msgReg.serializeMessage(null, Unpooled.EMPTY_BUFFER);
117         } catch (final NullPointerException e) {
118             ex = e.getMessage();
119         }
120         assertEquals("BGPMessage is mandatory.", ex);
121     }
122 }