@Test public void testEncryptedMessageChunking() throws Exception { File file = File.createTempFile("sasltest", ".txt"); try { TransportConf conf = new TransportConf("shuffle", MapConfigProvider.EMPTY); byte[] data = new byte[8 * 1024]; new Random().nextBytes(data); Files.write(data, file); SaslEncryptionBackend backend = mock(SaslEncryptionBackend.class); // It doesn't really matter what we return here, as long as it's not null. when(backend.wrap(any(byte[].class), anyInt(), anyInt())).thenReturn(data); FileSegmentManagedBuffer msg = new FileSegmentManagedBuffer(conf, file, 0, file.length()); SaslEncryption.EncryptedMessage emsg = new SaslEncryption.EncryptedMessage(backend, msg.convertToNetty(), data.length / 8); ByteArrayWritableChannel channel = new ByteArrayWritableChannel(data.length); while (emsg.transfered() < emsg.count()) { channel.reset(); emsg.transferTo(channel, emsg.transfered()); } verify(backend, times(8)).wrap(any(byte[].class), anyInt(), anyInt()); } finally { file.delete(); } }
@Test public void testEncryptedMessageChunking() throws Exception { File file = File.createTempFile("sasltest", ".txt"); try { TransportConf conf = new TransportConf("shuffle", MapConfigProvider.EMPTY); byte[] data = new byte[8 * 1024]; new Random().nextBytes(data); Files.write(data, file); SaslEncryptionBackend backend = mock(SaslEncryptionBackend.class); // It doesn't really matter what we return here, as long as it's not null. when(backend.wrap(any(byte[].class), anyInt(), anyInt())).thenReturn(data); FileSegmentManagedBuffer msg = new FileSegmentManagedBuffer(conf, file, 0, file.length()); SaslEncryption.EncryptedMessage emsg = new SaslEncryption.EncryptedMessage(backend, msg.convertToNetty(), data.length / 8); ByteArrayWritableChannel channel = new ByteArrayWritableChannel(data.length); while (emsg.transfered() < emsg.count()) { channel.reset(); emsg.transferTo(channel, emsg.transfered()); } verify(backend, times(8)).wrap(any(byte[].class), anyInt(), anyInt()); } finally { file.delete(); } }
long count = emsg.transferTo(channel, emsg.transfered()); assertTrue(count < data.length); assertTrue(count > 0); assertEquals(0, emsg.transferTo(channel, emsg.transfered())); assertEquals(1, emsg.transferTo(channel, emsg.transfered())); assertEquals(1, emsg.transferTo(channel, emsg.transfered())); count = emsg.transferTo(channel, emsg.transfered()); assertTrue("Unexpected count: " + count, count > 1 && count < data.length); assertEquals(data.length, emsg.transfered());
long count = emsg.transferTo(channel, emsg.transfered()); assertTrue(count < data.length); assertTrue(count > 0); assertEquals(0, emsg.transferTo(channel, emsg.transfered())); assertEquals(1, emsg.transferTo(channel, emsg.transfered())); assertEquals(1, emsg.transferTo(channel, emsg.transfered())); count = emsg.transferTo(channel, emsg.transfered()); assertTrue("Unexpected count: " + count, count > 1 && count < data.length); assertEquals(data.length, emsg.transfered());