RouteRefreshPacketDecoder { public BGPv4Packet decodeRouteRefreshPacket(ByteBuf buffer) { RouteRefreshPacket packet = null; try { AddressFamily af = AddressFamily.fromCode(buffer .readUnsignedShort()); buffer.readByte(); SubsequentAddressFamily saf = SubsequentAddressFamily .fromCode(buffer.readUnsignedByte()); packet = new RouteRefreshPacket(af, saf); if (buffer.isReadable()) { OutboundRouteFilter orf = new OutboundRouteFilter(af, saf); orf.setRefreshType(ORFRefreshType.fromCode(buffer.readUnsignedByte())); while(buffer.isReadable()) { ORFType orfType = ORFType.fromCode(buffer.readUnsignedByte()); ByteBuf entriesBuffer = buffer.readSlice(buffer.readUnsignedShort()); buffer.readBytes(entriesBuffer); orf.addAllORFEntries(decodeORFEntries(entriesBuffer, orfType)); } packet.setOutboundRouteFilter(orf); } } catch(Exception e) { log.error("cannot decode ROUTE_REFRESH packet, suppressing it from further processing", e); packet = null; } return packet; }  BGPv4Packet decodeRouteRefreshPacket(ByteBuf buffer);  }
@Test public void testSimpleRouteRefreshPacket() { RouteRefreshPacket packet = safeDowncast(decoder.decodeRouteRefreshPacket(buildProtocolPacket(new byte[] { (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, })), RouteRefreshPacket.class); Assert.assertEquals(AddressFamily.IPv4, packet.getAddressFamily()); Assert.assertEquals(SubsequentAddressFamily.NLRI_UNICAST_FORWARDING, packet.getSubsequentAddressFamily()); Assert.assertNull(packet.getOutboundRouteFilter()); }
@Test public void testMinimalORFRouteRefreshPacket() { RouteRefreshPacket packet = safeDowncast(decoder.decodeRouteRefreshPacket(buildProtocolPacket(new byte[] { (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x01, (byte)0x40, (byte)0x00, (byte)0x9, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x0, (byte)0x00, (byte)0x0, })), RouteRefreshPacket.class); Assert.assertEquals(AddressFamily.IPv4, packet.getAddressFamily()); Assert.assertEquals(SubsequentAddressFamily.NLRI_UNICAST_FORWARDING, packet.getSubsequentAddressFamily()); OutboundRouteFilter orf = packet.getOutboundRouteFilter(); Assert.assertNotNull(orf); Assert.assertEquals(AddressFamily.IPv4, orf.getAddressFamily()); Assert.assertEquals(SubsequentAddressFamily.NLRI_UNICAST_FORWARDING, orf.getSubsequentAddressFamily()); Assert.assertEquals(ORFRefreshType.IMMEDIATE, orf.getRefreshType()); Assert.assertEquals(1, orf.getEntries().size()); AddressPrefixBasedORFEntry entry; List<ORFEntry> entries = orf.getEntries().get(ORFType.ADDRESS_PREFIX_BASED); Assert.assertEquals(2, entries.size()); entry = (AddressPrefixBasedORFEntry)entries.remove(0); Assert.assertEquals(ORFAction.REMOVE_ALL, entry.getAction()); Assert.assertEquals(ORFMatch.PERMIT, entry.getMatch()); Assert.assertEquals(0, entry.getSequence()); Assert.assertEquals(0, entry.getMinLength()); Assert.assertEquals(0, entry.getMaxLength()); Assert.assertNull(entry.getPrefix()); entry = (AddressPrefixBasedORFEntry)entries.remove(0); Assert.assertEquals(ORFAction.ADD, entry.getAction()); Assert.assertEquals(ORFMatch.PERMIT, entry.getMatch()); Assert.assertEquals(1, entry.getSequence()); Assert.assertEquals(0, entry.getMinLength()); Assert.assertEquals(0, entry.getMaxLength()); Assert.assertEquals(new NetworkLayerReachabilityInformation(0, null), entry.getPrefix()); }
@Test public void testCompleteRouteRefreshPacket() { RouteRefreshPacket packet = safeDowncast(decoder.decodeRouteRefreshPacket(buildProtocolPacket(new byte[] { (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x01, (byte)0x40, (byte)0x00, (byte)0x2C, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x10, (byte)0x20, (byte)0x10, (byte)0xac, (byte)0x10, (byte)0x40, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0x18, (byte)0x1c, (byte)0x18, (byte)0xc0, (byte)0xa8, 0x10, (byte)0x20, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x03, (byte)0x18, (byte)0x1c, (byte)0x18, (byte)0xc0, (byte)0xa8, 0x20, (byte)0x60, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x04, (byte)0x18, (byte)0x1c, (byte)0x18, (byte)0xc0, (byte)0xa8, 0x30, })), RouteRefreshPacket.class); Assert.assertEquals(AddressFamily.IPv4, packet.getAddressFamily()); Assert.assertEquals(SubsequentAddressFamily.NLRI_UNICAST_FORWARDING, packet.getSubsequentAddressFamily()); OutboundRouteFilter orf = packet.getOutboundRouteFilter(); Assert.assertNotNull(orf); Assert.assertEquals(AddressFamily.IPv4, orf.getAddressFamily()); Assert.assertEquals(SubsequentAddressFamily.NLRI_UNICAST_FORWARDING, orf.getSubsequentAddressFamily()); Assert.assertEquals(ORFRefreshType.IMMEDIATE, orf.getRefreshType()); Assert.assertEquals(1, orf.getEntries().size()); AddressPrefixBasedORFEntry entry; List<ORFEntry> entries = orf.getEntries().get(ORFType.ADDRESS_PREFIX_BASED); Assert.assertEquals(5, entries.size()); entry = (AddressPrefixBasedORFEntry)entries.remove(0); Assert.assertEquals(ORFAction.REMOVE_ALL, entry.getAction()); Assert.assertEquals(ORFMatch.PERMIT, entry.getMatch()); Assert.assertEquals(0, entry.getSequence()); Assert.assertEquals(0, entry.getMinLength()); Assert.assertEquals(0, entry.getMaxLength()); Assert.assertNull(entry.getPrefix()); entry = (AddressPrefixBasedORFEntry)entries.remove(0); Assert.assertEquals(ORFAction.ADD, entry.getAction()); Assert.assertEquals(ORFMatch.PERMIT, entry.getMatch()); Assert.assertEquals(1, entry.getSequence()); Assert.assertEquals(16, entry.getMinLength()); Assert.assertEquals(32, entry.getMaxLength()); Assert.assertEquals(new NetworkLayerReachabilityInformation(16, new byte[] { (byte)0xac, 0x10, }), entry.getPrefix()); entry = (AddressPrefixBasedORFEntry)entries.remove(0); Assert.assertEquals(ORFAction.REMOVE, entry.getAction()); Assert.assertEquals(ORFMatch.PERMIT, entry.getMatch()); Assert.assertEquals(2, entry.getSequence()); Assert.assertEquals(24, entry.getMinLength()); Assert.assertEquals(28, entry.getMaxLength()); Assert.assertEquals(new NetworkLayerReachabilityInformation(24, new byte[] { (byte)0xc0, (byte)0xa8, 0x10, }), entry.getPrefix()); entry = (AddressPrefixBasedORFEntry)entries.remove(0); Assert.assertEquals(ORFAction.ADD, entry.getAction()); Assert.assertEquals(ORFMatch.DENY, entry.getMatch()); Assert.assertEquals(3, entry.getSequence()); Assert.assertEquals(24, entry.getMinLength()); Assert.assertEquals(28, entry.getMaxLength()); Assert.assertEquals(new NetworkLayerReachabilityInformation(24, new byte[] { (byte)0xc0, (byte)0xa8, 0x20, }), entry.getPrefix()); entry = (AddressPrefixBasedORFEntry)entries.remove(0); Assert.assertEquals(ORFAction.REMOVE, entry.getAction()); Assert.assertEquals(ORFMatch.DENY, entry.getMatch()); Assert.assertEquals(4, entry.getSequence()); Assert.assertEquals(24, entry.getMinLength()); Assert.assertEquals(28, entry.getMaxLength()); Assert.assertEquals(new NetworkLayerReachabilityInformation(24, new byte[] { (byte)0xc0, (byte)0xa8, 0x30, }), entry.getPrefix()); }
@Test public void testBrokenAFIMinimalORFRouteRefreshPacket() { Assert.assertNull(decoder.decodeRouteRefreshPacket(buildProtocolPacket(new byte[] { (byte)0x01, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x01, (byte)0x40, (byte)0x00, (byte)0x9, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x0, (byte)0x00, (byte)0x0, }))); }
@Test public void testBrokenSAFIMinimalORFRouteRefreshPacket() { Assert.assertNull(decoder.decodeRouteRefreshPacket(buildProtocolPacket(new byte[] { (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x05, (byte)0x01, (byte)0x40, (byte)0x00, (byte)0x9, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x0, (byte)0x00, (byte)0x0, }))); }
@Test public void testBrokenRefresMinimalORFRouteRefreshPacket() { Assert.assertNull(decoder.decodeRouteRefreshPacket(buildProtocolPacket(new byte[] { (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x03, (byte)0x40, (byte)0x00, (byte)0x9, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x0, (byte)0x00, (byte)0x0, }))); }
@Test public void testBrokenRefreshMinimalORFRouteRefreshPacket() { Assert.assertNull(decoder.decodeRouteRefreshPacket(buildProtocolPacket(new byte[] { (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x03, (byte)0x40, (byte)0x00, (byte)0x9, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x0, (byte)0x00, (byte)0x0, }))); }
@Test public void testBrokenTypeAFIMinimalORFRouteRefreshPacket() { Assert.assertNull(decoder.decodeRouteRefreshPacket(buildProtocolPacket(new byte[] { (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x01, (byte)0x41, (byte)0x00, (byte)0x9, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x0, (byte)0x00, (byte)0x0, }))); }
@Test public void testBrokenLengthMinimalORFRouteRefreshPacket() { Assert.assertNull(decoder.decodeRouteRefreshPacket(buildProtocolPacket(new byte[] { (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x01, (byte)0x40, (byte)0x00, (byte)0xa, (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x0, (byte)0x00, (byte)0x0, }))); }
@Test public void testBrokenActionMinimalORFRouteRefreshPacket() { Assert.assertNull(decoder.decodeRouteRefreshPacket(buildProtocolPacket(new byte[] { (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x01, (byte)0x40, (byte)0x00, (byte)0x9, (byte)0xc0, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x0, (byte)0x00, (byte)0x0, }))); }