Skip to content

Commit c9a9894

Browse files
authored
Add unit test for negative values in ByteBufferStreamInput::readVLong (elastic#115749)
1 parent d0f71fc commit c9a9894

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

server/src/test/java/org/elasticsearch/common/io/stream/ByteBufferStreamInputTests.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,52 @@ protected StreamInput getStreamInput(BytesReference bytesReference) throws IOExc
2121
final BytesRef bytesRef = bytesReference.toBytesRef();
2222
return new ByteBufferStreamInput(ByteBuffer.wrap(bytesRef.bytes, bytesRef.offset, bytesRef.length));
2323
}
24+
25+
public void testReadVLongNegative() throws IOException {
26+
for (int i = 0; i < 1024; i++) {
27+
long write = randomNegativeLong();
28+
BytesStreamOutput out = new BytesStreamOutput();
29+
out.writeVLongNoCheck(write);
30+
long read = getStreamInput(out.bytes()).readVLong();
31+
assertEquals(write, read);
32+
}
33+
}
34+
35+
public void testReadVLongBounds() throws IOException {
36+
long write = Long.MAX_VALUE;
37+
BytesStreamOutput out = new BytesStreamOutput();
38+
out.writeVLongNoCheck(write);
39+
long read = getStreamInput(out.bytes()).readVLong();
40+
assertEquals(write, read);
41+
42+
write = Long.MIN_VALUE;
43+
out = new BytesStreamOutput();
44+
out.writeVLongNoCheck(write);
45+
read = getStreamInput(out.bytes()).readVLong();
46+
assertEquals(write, read);
47+
}
48+
49+
public void testReadVIntNegative() throws IOException {
50+
for (int i = 0; i < 1024; i++) {
51+
int write = randomNegativeInt();
52+
BytesStreamOutput out = new BytesStreamOutput();
53+
out.writeVInt(write);
54+
int read = getStreamInput(out.bytes()).readVInt();
55+
assertEquals(write, read);
56+
}
57+
}
58+
59+
public void testReadVIntBounds() throws IOException {
60+
int write = Integer.MAX_VALUE;
61+
BytesStreamOutput out = new BytesStreamOutput();
62+
out.writeVInt(write);
63+
long read = getStreamInput(out.bytes()).readVInt();
64+
assertEquals(write, read);
65+
66+
write = Integer.MIN_VALUE;
67+
out = new BytesStreamOutput();
68+
out.writeVInt(write);
69+
read = getStreamInput(out.bytes()).readVInt();
70+
assertEquals(write, read);
71+
}
2472
}

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,13 @@ public static long randomNonNegativeLong() {
10321032
return randomLong() & Long.MAX_VALUE;
10331033
}
10341034

1035+
/**
1036+
* @return a <code>long</code> between <code>Long.MIN_VALUE</code> and <code>-1</code> (inclusive) chosen uniformly at random.
1037+
*/
1038+
public static long randomNegativeLong() {
1039+
return randomLong() | Long.MIN_VALUE;
1040+
}
1041+
10351042
/**
10361043
* @return an <code>int</code> between <code>0</code> and <code>Integer.MAX_VALUE</code> (inclusive) chosen uniformly at random.
10371044
*/

0 commit comments

Comments
 (0)