NLRICodec { public static NetworkLayerReachabilityInformation decodeNLRI(ByteBuf buffer) { NetworkLayerReachabilityInformation nlri = new NetworkLayerReachabilityInformation(); int prefixLength = buffer.readUnsignedByte(); byte[] prefixBytes = null; if(prefixLength > 0) { prefixBytes = new byte[NetworkLayerReachabilityInformation.calculateOctetsForPrefixLength(prefixLength)]; buffer.readBytes(prefixBytes); } nlri.setPrefix(prefixLength, prefixBytes); return nlri; }  static NetworkLayerReachabilityInformation decodeNLRI(ByteBuf buffer); static int calculateEncodedNLRILength(NetworkLayerReachabilityInformation nlri); static void encodeNLRI(ByteBuf buffer, NetworkLayerReachabilityInformation nlri);  }
@Test public void testDecodeNLRIZeroPrefixLengthNullPrefix() { NetworkLayerReachabilityInformation nlri = NLRICodec.decodeNLRI(buildProtocolPacket(new byte[] { 0x00, })); Assert.assertEquals(0, nlri.getPrefixLength()); }
@Test public void testDecodeNLRIFourBitsPrefixLengthOneBytePrefix() { NetworkLayerReachabilityInformation nlri = NLRICodec.decodeNLRI(buildProtocolPacket(new byte[] { 0x04, (byte)0xac, })); Assert.assertEquals(4, nlri.getPrefixLength()); Assert.assertEquals(1, nlri.getPrefix().length); assertArraysEquals(new byte[] { (byte)0xa0, }, nlri.getPrefix()); }
@Test public void testDecodeNLRIEightBitsPrefixLengthOneBytePrefix() { NetworkLayerReachabilityInformation nlri = NLRICodec.decodeNLRI(buildProtocolPacket(new byte[] { 0x08, (byte)0xac, })); Assert.assertEquals(8, nlri.getPrefixLength()); Assert.assertEquals(1, nlri.getPrefix().length); assertArraysEquals(new byte[] { (byte)0xac, }, nlri.getPrefix()); }
@Test public void testDecodeNLRITwelveBitsPrefixLengthTwoBytePrefix() { NetworkLayerReachabilityInformation nlri = NLRICodec.decodeNLRI(buildProtocolPacket(new byte[] { 0x0c, (byte)0xac, 0x12 })); Assert.assertEquals(12, nlri.getPrefixLength()); Assert.assertEquals(2, nlri.getPrefix().length); assertArraysEquals(new byte[] { (byte)0xac, 0x10 }, nlri.getPrefix()); }
@Test public void testDecodeNLRISixteenBitsPrefixLengthTwoBytePrefix() { NetworkLayerReachabilityInformation nlri = NLRICodec.decodeNLRI(buildProtocolPacket(new byte[] { 0x10, (byte)0xac, 0x12 })); Assert.assertEquals(16, nlri.getPrefixLength()); Assert.assertEquals(2, nlri.getPrefix().length); assertArraysEquals(new byte[] { (byte)0xac, 0x12 }, nlri.getPrefix()); }
@Test public void testDecodeNLRITwentyBitsPrefixLengthThreeBytePrefix() { NetworkLayerReachabilityInformation nlri = NLRICodec.decodeNLRI(buildProtocolPacket(new byte[] { 0x14, (byte)0xac, 0x12, (byte)0xfe })); Assert.assertEquals(20, nlri.getPrefixLength()); Assert.assertEquals(3, nlri.getPrefix().length); assertArraysEquals(new byte[] { (byte)0xac, 0x12, (byte)0xf0 }, nlri.getPrefix()); }
@Test public void testDecodeNLRITwentyFourBitsPrefixLengthThreeBytePrefix() { NetworkLayerReachabilityInformation nlri = NLRICodec.decodeNLRI(buildProtocolPacket(new byte[] { 0x18, (byte)0xac, 0x12, (byte)0xfe })); Assert.assertEquals(24, nlri.getPrefixLength()); Assert.assertEquals(3, nlri.getPrefix().length); assertArraysEquals(new byte[] { (byte)0xac, 0x12, (byte)0xfe }, nlri.getPrefix()); }
@Test public void testDecodeNLRITwentyEightBitsPrefixLengthFourBytePrefix() { NetworkLayerReachabilityInformation nlri = NLRICodec.decodeNLRI(buildProtocolPacket(new byte[] { 0x1c, (byte)0xac, 0x12, (byte)0xfe, (byte)0xff })); Assert.assertEquals(28, nlri.getPrefixLength()); Assert.assertEquals(4, nlri.getPrefix().length); assertArraysEquals(new byte[] { (byte)0xac, 0x12, (byte)0xfe, (byte)0xf0 }, nlri.getPrefix()); }
@Test public void testDecodeNLRIThirtyTwoFourBitsPrefixLengthFourBytePrefix() { NetworkLayerReachabilityInformation nlri = NLRICodec.decodeNLRI(buildProtocolPacket(new byte[] { 0x20, (byte)0xac, 0x12, (byte)0xfe, (byte)0xff })); Assert.assertEquals(32, nlri.getPrefixLength()); Assert.assertEquals(4, nlri.getPrefix().length); assertArraysEquals(new byte[] { (byte)0xac, 0x12, (byte)0xfe, (byte)0xff }, nlri.getPrefix()); }