OpenPacketDecoder { public OpenPacket decodeOpenPacket(ByteBuf buffer) { OpenPacket packet = new OpenPacket(); ProtocolPacketUtils.verifyPacketSize(buffer, BGPv4Constants.BGP_PACKET_MIN_SIZE_OPEN, -1); packet.setProtocolVersion(buffer.readUnsignedByte()); if(packet.getProtocolVersion() != BGPv4Constants.BGP_VERSION) throw new UnsupportedVersionNumberException(BGPv4Constants.BGP_VERSION); packet.setAutonomousSystem(buffer.readUnsignedShort()); packet.setHoldTime(buffer.readUnsignedShort()); packet.setBgpIdentifier(buffer.readUnsignedInt()); if((packet.getBgpIdentifier() & IPV4_MULTICAST_MASK) == IPV4_MULTICAST_MASK) throw new BadBgpIdentifierException(); int parameterLength = buffer.readUnsignedByte(); if(parameterLength > 0) { while(buffer.isReadable()) { int parameterType = buffer.readUnsignedByte(); int paramLength = buffer.readUnsignedByte(); ByteBuf valueBuffer = buffer.readBytes(paramLength); switch(parameterType) { case BGPv4Constants.BGP_OPEN_PARAMETER_TYPE_AUTH: break; case BGPv4Constants.BGP_OPEN_PARAMETER_TYPE_CAPABILITY: packet.getCapabilities().addAll(CapabilityCodec.decodeCapabilities(valueBuffer)); break; default: throw new UnsupportedOptionalParameterException(); } } } return packet; }  NotificationPacket decodeOpenNotificationPacket(ByteBuf buffer, int errorSubcode); OpenPacket decodeOpenPacket(ByteBuf buffer);  }
@Test public void testDecodeBasicOpenPacket() { OpenPacket open = safeDowncast(decoder.decodeOpenPacket(buildProtocolPacket(new byte[] { (byte)0x04, (byte)0xfc, (byte)0x00, (byte)0x00, (byte)0xb4, (byte)0xc0, (byte)0xa8, (byte)0x09, (byte)0x01, (byte)0x0, })), OpenPacket.class); Assert.assertEquals(4, open.getProtocolVersion()); Assert.assertEquals(64512, open.getAutonomousSystem()); Assert.assertEquals(180, open.getHoldTime()); Assert.assertEquals(0, open.getCapabilities().size()); Assert.assertEquals(((192L<<24) | (168L << 16) | (9L << 8) | 1L), open.getBgpIdentifier()); }
@Test public void testDecodeBasicOpenPacketBadProtocolVersion() throws Exception { UnsupportedVersionNumberException ex = (new AssertExecption() { @Override protected void doExecute() { safeDowncast(decoder.decodeOpenPacket(buildProtocolPacket(new byte[] { (byte)0x05, (byte)0xfc, (byte)0x00, (byte)0x00, (byte)0xb4, (byte)0xc0, (byte)0xa8, (byte)0x09, (byte)0x01, (byte)0x0, })), OpenPacket.class); } }).execute(UnsupportedVersionNumberException.class); Assert.assertEquals(4, ex.getSupportedProtocolVersion()); }
@Test public void testDecodeBasicOpenPacketBadBgpIdentifier() throws Exception { (new AssertExecption() { @Override protected void doExecute() { safeDowncast(decoder.decodeOpenPacket(buildProtocolPacket(new byte[] { (byte)0x04, (byte)0xfc, (byte)0x00, (byte)0x00, (byte)0xb4, (byte)0xe0, (byte)0x0, (byte)0x0, (byte)0x01, (byte)0x0, })), OpenPacket.class); } }).execute(BadBgpIdentifierException.class); }
@Test public void testDecodeBasicOpenPacketUnsupportedOptionaParameter() throws Exception { (new AssertExecption() { @Override protected void doExecute() { safeDowncast(decoder.decodeOpenPacket(buildProtocolPacket(new byte[] { (byte)0x04, (byte)0xfc, (byte)0x00, (byte)0x00, (byte)0xb4, (byte)0xc0, (byte)0xa8, (byte)0x09, (byte)0x01, (byte)0x2, (byte)0x03, (byte)0x00 })), OpenPacket.class); } }).execute(UnsupportedOptionalParameterException.class); }
@Test public void testDecodeFullOpenPacket() { Capability cap; OpenPacket open = safeDowncast(decoder.decodeOpenPacket(buildProtocolPacket(new byte[] { (byte)0x04, (byte)0xfc, (byte)0x00, (byte)0x00, (byte)0xb4, (byte)0xc0, (byte)0xa8, (byte)0x09, (byte)0x01, (byte)0x18, (byte)0x02, (byte)0x06, (byte)0x01, (byte)0x04, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x02, (byte)0x80, (byte)0x00, (byte)0x02, (byte)0x02, (byte)0x02, (byte)0x00, (byte)0x02, (byte)0x06, (byte)0x41, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0xfc, (byte)0x00 })), OpenPacket.class); Assert.assertEquals(4, open.getProtocolVersion()); Assert.assertEquals(64512, open.getAutonomousSystem()); Assert.assertEquals(180, open.getHoldTime()); Assert.assertEquals(((192L<<24) | (168L << 16) | (9L << 8) | 1L), open.getBgpIdentifier()); Assert.assertEquals(4, open.getCapabilities().size()); cap = open.getCapabilities().remove(0); Assert.assertEquals(MultiProtocolCapability.class, cap.getClass()); Assert.assertEquals(AddressFamily.IPv4, ((MultiProtocolCapability)cap).getAfi()); Assert.assertEquals(SubsequentAddressFamily.NLRI_UNICAST_FORWARDING, ((MultiProtocolCapability)cap).getSafi()); cap = open.getCapabilities().remove(0); Assert.assertEquals(UnknownCapability.class, cap.getClass()); Assert.assertEquals(128, ((UnknownCapability)cap).getCapabilityType()); cap = open.getCapabilities().remove(0); Assert.assertEquals(RouteRefreshCapability.class, cap.getClass()); cap = open.getCapabilities().remove(0); Assert.assertEquals(AutonomousSystem4Capability.class, cap.getClass()); Assert.assertEquals(64512, ((AutonomousSystem4Capability)cap).getAutonomousSystem()); }
@Test public void testDecodeFullOpenPacketOneParameter() { Capability cap; OpenPacket open = safeDowncast(decoder.decodeOpenPacket(buildProtocolPacket(new byte[] { (byte)0x04, (byte)0xfc, (byte)0x00, (byte)0x00, (byte)0xb4, (byte)0xc0, (byte)0xa8, (byte)0x09, (byte)0x01, (byte)0x10, (byte)0x02, (byte)0x0e, (byte)0x01, (byte)0x04, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x00, (byte)0x41, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0xfc, (byte)0x00 })), OpenPacket.class); Assert.assertEquals(4, open.getProtocolVersion()); Assert.assertEquals(64512, open.getAutonomousSystem()); Assert.assertEquals(180, open.getHoldTime()); Assert.assertEquals(((192L<<24) | (168L << 16) | (9L << 8) | 1L), open.getBgpIdentifier()); Assert.assertEquals(3, open.getCapabilities().size()); cap = open.getCapabilities().remove(0); Assert.assertEquals(MultiProtocolCapability.class, cap.getClass()); Assert.assertEquals(AddressFamily.IPv4, ((MultiProtocolCapability)cap).getAfi()); Assert.assertEquals(SubsequentAddressFamily.NLRI_UNICAST_FORWARDING, ((MultiProtocolCapability)cap).getSafi()); cap = open.getCapabilities().remove(0); Assert.assertEquals(RouteRefreshCapability.class, cap.getClass()); cap = open.getCapabilities().remove(0); Assert.assertEquals(AutonomousSystem4Capability.class, cap.getClass()); Assert.assertEquals(64512, ((AutonomousSystem4Capability)cap).getAutonomousSystem()); }