Skip to content

Commit c84db27

Browse files
committed
Merge pull request #168 from pontusmelke/1.0-bitmask-error
Fixed bitmasking error
2 parents e523912 + fe884fc commit c84db27

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

driver/src/main/java/org/neo4j/driver/internal/connector/socket/BufferingChunkedInput.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ public BufferingChunkedInput( ReadableByteChannel channel, int bufferCapacity )
8888
this.state = State.AWAITING_CHUNK;
8989
}
9090

91+
/*
92+
* Use only in tests
93+
*/
94+
int remainingChunkSize()
95+
{
96+
return remainingChunkSize;
97+
}
98+
99+
91100
/**
92101
* Internal state machine used for reading data from the channel into the buffer.
93102
*/
@@ -118,7 +127,7 @@ else if ( ctx.buffer.remaining() >= 2 )
118127
{
119128
//only 1 byte in buffer, read that and continue
120129
//to read header
121-
byte partialChunkSize = ctx.buffer.get();
130+
int partialChunkSize = getUnsignedByteFromBuffer( ctx.buffer );
122131
ctx.remainingChunkSize = partialChunkSize << 8;
123132
return IN_HEADER.readChunkSize( ctx );
124133
}
@@ -220,7 +229,7 @@ public State readChunkSize( BufferingChunkedInput ctx ) throws IOException
220229
{
221230
//Now we have enough space to read the rest of the chunk size
222231
byte partialChunkSize = ctx.buffer.get();
223-
ctx.remainingChunkSize = (ctx.remainingChunkSize | partialChunkSize) & 0xFFFF;
232+
ctx.remainingChunkSize = ctx.remainingChunkSize | (partialChunkSize & 0xFF);
224233
return IN_CHUNK;
225234
}
226235
else
@@ -383,6 +392,12 @@ public byte peekByte() throws IOException
383392
return buffer.get( buffer.position() );
384393
}
385394

395+
static int getUnsignedByteFromBuffer( ByteBuffer buffer )
396+
{
397+
return buffer.get() & 0xFF;
398+
}
399+
400+
386401
private boolean hasMoreDataUnreadInCurrentChunk()
387402
{
388403
return remainingChunkSize > 0;

driver/src/test/java/org/neo4j/driver/internal/connector/socket/BufferingChunkedInputTest.java

+53-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,47 @@ public void shouldReadOneByteWhenSplitHeader() throws IOException
9292
assertThat( b2, equalTo( (byte) 37 ) );
9393
}
9494

95+
@Test
96+
public void shouldReadChunkWithSplitHeaderForBigMessages() throws IOException
97+
{
98+
// Given
99+
int packetSize = 384;
100+
BufferingChunkedInput input =
101+
new BufferingChunkedInput( packets( packet( 1 ), packet( -128 ), fillPacket( packetSize, 1 ) ) );
102+
103+
// Then
104+
assertThat( input.readByte(), equalTo( (byte) 1 ) );
105+
assertThat( input.remainingChunkSize(), equalTo( packetSize - 1 ) );
106+
107+
for ( int i = 1; i < packetSize; i++ )
108+
{
109+
assertThat( input.readByte(), equalTo( (byte) 1 ) );
110+
}
111+
assertThat( input.remainingChunkSize(), equalTo( 0 ) );
112+
}
113+
114+
@Test
115+
public void shouldReadChunkWithSplitHeaderForBigMessagesWhenInternalBufferHasOneByte() throws IOException
116+
{
117+
// Given
118+
int packetSize = 32780;
119+
BufferingChunkedInput input =
120+
new BufferingChunkedInput( packets( packet( -128 ), packet( 12 ), fillPacket( packetSize, 1 ) ), 1);
121+
122+
// Then
123+
assertThat( input.readByte(), equalTo( (byte) 1 ) );
124+
assertThat( input.remainingChunkSize(), equalTo( packetSize - 1 ) );
125+
}
126+
127+
@Test
128+
public void shouldReadUnsignedByteFromBuffer() throws IOException
129+
{
130+
ByteBuffer buffer = ByteBuffer.allocate( 1 );
131+
buffer.put( (byte) -1 );
132+
buffer.flip();
133+
assertThat(BufferingChunkedInput.getUnsignedByteFromBuffer( buffer ), equalTo( 255 ));
134+
}
135+
95136
@Test
96137
public void shouldReadOneByteInOneChunkWhenBustingBuffer() throws IOException
97138
{
@@ -415,7 +456,7 @@ public void close() throws IOException
415456
BufferingChunkedInput input = new BufferingChunkedInput( channel );
416457

417458
// Then
418-
assertThat(input.readByte(), equalTo( (byte)11 ));
459+
assertThat( input.readByte(), equalTo( (byte) 11 ) );
419460

420461
}
421462

@@ -435,6 +476,17 @@ public void shouldFailNicelyOnClosedConnections() throws IOException
435476
input.readByte();
436477
}
437478

479+
private ReadableByteChannel fillPacket( int size, int value )
480+
{
481+
int[] ints = new int[size];
482+
for ( int i = 0; i < size; i++ )
483+
{
484+
ints[i] = value;
485+
}
486+
487+
return packet( ints );
488+
}
489+
438490
private ReadableByteChannel packet( int... bytes )
439491
{
440492
byte[] byteArray = new byte[bytes.length];

driver/src/test/java/org/neo4j/driver/internal/connector/socket/ChunkedInputTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
*/
1919
package org.neo4j.driver.internal.connector.socket;
2020

21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
import org.junit.rules.ExpectedException;
24+
import org.mockito.Matchers;
25+
2126
import java.io.ByteArrayInputStream;
2227
import java.io.IOException;
2328
import java.nio.ByteBuffer;
@@ -26,11 +31,6 @@
2631
import java.nio.channels.ReadableByteChannel;
2732
import java.util.Arrays;
2833

29-
import org.junit.Rule;
30-
import org.junit.Test;
31-
import org.junit.rules.ExpectedException;
32-
import org.mockito.Matchers;
33-
3434
import org.neo4j.driver.v1.exceptions.ClientException;
3535
import org.neo4j.driver.v1.util.RecordingByteChannel;
3636

0 commit comments

Comments
 (0)