@Test public void shouldBeAbleToWriteToThenReadFromChannel() throws Throwable { // Given RecordingByteChannel channel = new RecordingByteChannel(); // When byte[] data = new byte[]{1, 2, 3, 4, 5}; channel.write( ByteBuffer.wrap( data ) ); ByteBuffer buffer = ByteBuffer.allocate( 10 ); int bytesRead = channel.read( buffer ); // Then assertThat( bytesRead, equalTo( 5 ) ); assertThat( buffer.array(), equalTo( new byte[]{1, 2, 3, 4, 5, 0, 0, 0, 0, 0} ) ); }
public static byte[] serialize( Message... messages ) throws IOException { final RecordingByteChannel rawData = new RecordingByteChannel(); final MessageFormat.Writer packer = new PackStreamMessageFormatV1.Writer( new PackStream.Packer( new BufferedChannelOutput( rawData )), NO_OP ); for ( Message message : messages ) { packer.write( message ); } packer.flush(); return rawData.getBytes(); }
private byte[] serialize( int chunkSize, Message... msgs ) throws IOException { byte[][] serialized = new byte[msgs.length][]; for ( int i = 0; i < msgs.length; i++ ) { RecordingByteChannel channel = new RecordingByteChannel(); PackStreamMessageFormatV1.Writer format = new PackStreamMessageFormatV1.Writer( new PackStream.Packer( new BufferedChannelOutput( channel ) ), NO_OP ); format.write( msgs[i] ).flush(); serialized[i] = channel.getBytes(); } return Chunker.chunk( chunkSize, serialized ); } }
@Test public void shouldHandleUnknownMarkerBytes() throws Throwable { // Given I send a message with an invalid type final RecordingByteChannel rawData = new RecordingByteChannel(); final BufferedChannelOutput out = new BufferedChannelOutput( rawData ); final PackStream.Packer packer = new PackStream.Packer( out ); packer.packStructHeader( 2, (char) PackStreamMessageFormatV1.MessageTypes.MSG_RUN ); out.writeByte( PackStream.RESERVED_C4 ); // Invalid marker byte out.flush(); byte[] invalidMessage = rawData.getBytes(); // When client.connect( address ) .send( acceptedVersions( 1, 0, 0, 0 ) ) .send( chunk( 32, invalidMessage ) ); // Then assertThat( client, eventuallyRecieves( new byte[]{0, 0, 0, 1} ) ); assertThat( client, eventuallyRecieves( msgFailure( new Neo4jError( Status.Request.InvalidFormat, "Unable to read MSG_RUN message. Error was: Wrong type received. Expected TEXT, received: " + "RESERVED (0xff)." ) ) ) ); }
@Test public void shouldHandleUnknownMessages() throws Throwable { // Given I send a message with an invalid type final RecordingByteChannel rawData = new RecordingByteChannel(); final PackStream.Packer packer = new PackStream.Packer( new BufferedChannelOutput( rawData ) ); packer.packStructHeader( 1, (char) 0x66 ); // Invalid message type packer.pack( 1234 ); packer.flush(); byte[] invalidMessage = rawData.getBytes(); // When client.connect( address ) .send( acceptedVersions( 1, 0, 0, 0 ) ) .send( chunk( 32, invalidMessage ) ); // Then assertThat( client, eventuallyRecieves( new byte[]{0, 0, 0, 1} ) ); assertThat( client, eventuallyRecieves( msgFailure( new Neo4jError( Status.Request.Invalid, "0x66 is not a valid message type." ) ) ) ); }
@Test public void shouldHandleMessagesWithIncorrectFields() throws Throwable { // Given I send a message with the wrong types in its fields final RecordingByteChannel rawData = new RecordingByteChannel(); final PackStream.Packer packer = new PackStream.Packer( new BufferedChannelOutput( rawData ) ); packer.packStructHeader( 2, (char) PackStreamMessageFormatV1.MessageTypes.MSG_RUN ); packer.pack( "RETURN 1" ); packer.pack( 1234 ); // Should've been a map packer.flush(); byte[] invalidMessage = rawData.getBytes(); // When client.connect( address ) .send( acceptedVersions( 1, 0, 0, 0 ) ) .send( chunk( 32, invalidMessage ) ); // Then assertThat( client, eventuallyRecieves( new byte[]{0, 0, 0, 1} ) ); assertThat( client, eventuallyRecieves( msgFailure( new Neo4jError( Status.Request.InvalidFormat, "Unable to read MSG_RUN message. Error was: Wrong type received. Expected MAP, received: " + "INTEGER (0xff)." ) ) ) ); }
private <T extends Message> T serializeAndDeserialize( T msg ) throws IOException { RecordingByteChannel channel = new RecordingByteChannel(); MessageFormat.Reader reader = new PackStreamMessageFormatV1.Reader( new PackStream.Unpacker( new BufferedChannelInput( 16 ).reset( channel ) ) ); MessageFormat.Writer writer = new PackStreamMessageFormatV1.Writer( new PackStream.Packer( new BufferedChannelOutput( channel ) ), NO_OP ); writer.write( msg ).flush(); channel.eof(); return unpack( reader, channel ); }