Skip to content

Commit 28dc712

Browse files
committed
Added tests for org.bson.io.Bits class. Fix for JAVA-816.
1 parent bf23117 commit 28dc712

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

src/main/org/bson/io/Bits.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static void readFully( InputStream in, byte[] b, int length )
3636
public static void readFully( InputStream in, byte[] b, int startOffset, int length )
3737
throws IOException {
3838

39-
if (b.length - startOffset < length) {
39+
if (b.length < length + startOffset) {
4040
throw new IllegalArgumentException("Buffer is too small");
4141
}
4242

src/test/org/bson/io/BitsTest.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2008 - 2013 10gen, Inc. <http://10gen.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.bson.io;
18+
19+
import org.testng.annotations.Test;
20+
21+
import java.io.ByteArrayInputStream;
22+
import java.io.IOException;
23+
import java.util.Arrays;
24+
25+
import static org.testng.Assert.assertEquals;
26+
27+
public class BitsTest {
28+
29+
private final byte[] BYTES = {
30+
41, 0, 0, 0, 16, 105, 49, 0, -12,
31+
-1, -1, -1, 16, 105, 50, 0, 0, 0,
32+
0, -128, 18, 105, 51, 0, -1, -1, -1,
33+
-1, -1, -1, -1, 127, 16, 105, 52, 0,
34+
0, 0, 0, 0, 0
35+
};
36+
37+
@Test
38+
public void testReadFullyWithBufferLargerThanExpected() throws IOException {
39+
final byte[] buffer = new byte[8192];
40+
Bits.readFully(new ByteArrayInputStream(BYTES), buffer, BYTES.length);
41+
assertEquals(BYTES, Arrays.copyOfRange(buffer, 0, BYTES.length));
42+
}
43+
44+
@Test
45+
public void testReadFullyWithOffset() throws IOException {
46+
final int offset = 10;
47+
final byte[] buffer = new byte[8192];
48+
Bits.readFully(new ByteArrayInputStream(BYTES), buffer, offset, BYTES.length);
49+
assertEquals(BYTES, Arrays.copyOfRange(buffer, offset, BYTES.length + offset));
50+
}
51+
52+
@Test
53+
public void testReadFullyWithBufferEqualsToExpected() throws IOException {
54+
final int offset = 10;
55+
final byte[] buffer = new byte[offset+BYTES.length];
56+
Bits.readFully(new ByteArrayInputStream(BYTES), buffer, offset, BYTES.length);
57+
assertEquals(BYTES, Arrays.copyOfRange(buffer, offset, BYTES.length + offset));
58+
}
59+
60+
@Test (expectedExceptions = IllegalArgumentException.class)
61+
public void testReadFullyUsingNotEnoughBigBuffer() throws IOException {
62+
Bits.readFully(new ByteArrayInputStream(BYTES), new byte[2], BYTES.length);
63+
}
64+
65+
@Test (expectedExceptions = IllegalArgumentException.class)
66+
public void testReadFullyUsingNotEnoughBigBufferWithOffset() throws IOException {
67+
final int offset = 10;
68+
final byte[] buffer = new byte[BYTES.length];
69+
Bits.readFully(new ByteArrayInputStream(BYTES), buffer, offset, BYTES.length);
70+
}
71+
72+
@Test
73+
public void testReadInt() {
74+
assertEquals(41, Bits.readInt(BYTES));
75+
}
76+
77+
@Test
78+
public void testReadIntFromInputStream() throws IOException {
79+
assertEquals(41, Bits.readInt(new ByteArrayInputStream(BYTES)));
80+
}
81+
82+
@Test
83+
public void testReadIntWithOffset() {
84+
assertEquals(-12, Bits.readInt(BYTES, 8));
85+
}
86+
87+
@Test
88+
public void testReadIntInBigEndianNotation() {
89+
assertEquals(-12, Bits.readIntBE(new byte[]{-1, -1, -1, -12}, 0));
90+
}
91+
92+
@Test
93+
public void testReadLong() {
94+
assertEquals(Long.MAX_VALUE, Bits.readLong(BYTES, 24));
95+
}
96+
97+
@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class)
98+
public void testReadLongWithNotEnoughData() {
99+
Bits.readLong(Arrays.copyOfRange(BYTES, 24, 30), 0);
100+
}
101+
102+
}

0 commit comments

Comments
 (0)