ResponseDecoder extends ByteToMessageDecoder { private static long hash(byte[] data) { return Hashing.murmur3_32().newHasher().putBytes(data).hash().padToLong(); } ResponseDecoder(File spoolFolder);   }
@Test public void shouldDropInvalidGetBlobResponses() throws Exception { byte[] blobData = new byte[] {1, 2, 3}; String blobId = "blobId"; byte[] blobIdBytes = blobId.getBytes(Charsets.UTF_8); byte mask = createMask(1, 1); ByteBuf buf = Unpooled.buffer(); buf.writeInt(1 + 1 + 8 + 4 + blobIdBytes.length + 8 + blobData.length); buf.writeByte(Messages.HEADER_BLOB); buf.writeByte(mask); buf.writeLong(3L); buf.writeInt(blobIdBytes.length); buf.writeBytes(blobIdBytes); buf.writeLong(hash(mask, 3L, blobData) + 1); buf.writeBytes(blobData); EmbeddedChannel channel = new EmbeddedChannel(new ResponseDecoder(folder.newFolder())); channel.writeInbound(buf); assertNull(channel.readInbound()); }
@Test public void shouldDecodeValidGetSegmentResponses() throws Exception { UUID uuid = new UUID(1, 2); byte[] data = new byte[] {3, 4, 5}; ByteBuf buf = Unpooled.buffer(); buf.writeInt(data.length + 25); buf.writeByte(Messages.HEADER_SEGMENT); buf.writeLong(uuid.getMostSignificantBits()); buf.writeLong(uuid.getLeastSignificantBits()); buf.writeLong(hash(data)); buf.writeBytes(data); EmbeddedChannel channel = new EmbeddedChannel(new ResponseDecoder(folder.newFolder())); channel.writeInbound(buf); GetSegmentResponse response = (GetSegmentResponse) channel.readInbound(); assertEquals(uuid, UUID.fromString(response.getSegmentId())); assertArrayEquals(data, response.getSegmentData()); }
@Test public void shouldDropInvalidGetSegmentResponses() throws Exception { UUID uuid = new UUID(1, 2); byte[] data = new byte[] {3, 4, 5}; ByteBuf buf = Unpooled.buffer(); buf.writeInt(data.length + 25); buf.writeByte(Messages.HEADER_SEGMENT); buf.writeLong(uuid.getMostSignificantBits()); buf.writeLong(uuid.getLeastSignificantBits()); buf.writeLong(hash(data) + 1); buf.writeBytes(data); EmbeddedChannel channel = new EmbeddedChannel(new ResponseDecoder(folder.newFolder())); channel.writeInbound(buf); assertNull(channel.readInbound()); }