2 * Copyright (c) 2014 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.spi;
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;
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.rev171207.Keepalive;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.KeepaliveBuilder;
25 import org.opendaylight.yangtools.yang.binding.Notification;
27 public class AbstractMessageRegistryTest {
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 };
33 private final AbstractMessageRegistry registry = new AbstractMessageRegistry() {
36 protected void serializeMessageImpl(final Notification message, final ByteBuf buffer) {
37 buffer.writeBytes(keepAliveBMsg);
41 protected Notification parseBody(final int type, final ByteBuf body, final int messageLength,
42 final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
43 return new KeepaliveBuilder().build();
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));
54 final Notification not = this.registry.parseMessage(Unpooled.copiedBuffer(keepAliveBMsg), null);
55 assertTrue(not instanceof Keepalive);
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 };
64 this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
66 } catch (BGPDocumentedException | BGPParsingException e) {
67 assertTrue(e instanceof BGPDocumentedException);
68 Assert.assertEquals("Marker not set to ones.", e.getMessage());
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 };
78 this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
80 } catch (BGPDocumentedException | BGPParsingException e) {
81 assertTrue(e instanceof BGPDocumentedException);
82 Assert.assertEquals("Message length field not within valid range.", e.getMessage());
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 };
92 this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
94 } catch (BGPDocumentedException | BGPParsingException e) {
95 assertTrue(e instanceof BGPParsingException);
96 Assert.assertTrue(e.getMessage().startsWith("Size doesn't match size specified in header."));
101 public void testBGPHeaderParser() throws Exception {
102 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
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());
112 public void testMessageParser() throws Exception {
113 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
116 msgReg.serializeMessage(null, Unpooled.EMPTY_BUFFER);
117 } catch (final NullPointerException e) {
120 assertEquals("BGPMessage is mandatory.", ex);