private <T extends Message> T unpack( MessageFormat.Reader reader, RecordingByteChannel channel ) { // Unpack String serialized = HexPrinter.hex( channel.getBytes() ); RecordingMessageHandler messages = new RecordingMessageHandler(); try { reader.read( messages ); } catch ( Throwable e ) { throw new AssertionError( "Failed to unpack message, wire data was:\n" + serialized + "[" + channel .getBytes().length + "b]", e ); } return (T) messages.asList().get( 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)." ) ) ) ); }