Skip to content

Commit 10b5d2b

Browse files
Add unit tests for Base64 and Charsetfunctions
These tests were written using Diffblue Cover.
1 parent dc080e0 commit 10b5d2b

2 files changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2010-2019 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.util;
27+
28+
import org.junit.Assert;
29+
import org.junit.Rule;
30+
import org.junit.Test;
31+
import org.junit.rules.ExpectedException;
32+
33+
import java.io.IOException;
34+
35+
public class Base64Test {
36+
37+
@Rule public final ExpectedException thrown = ExpectedException.none();
38+
39+
@Test
40+
public void testEncodeBytes() throws IOException {
41+
Assert.assertEquals("", Base64.encodeBytes(new byte[0]));
42+
Assert.assertEquals("QHE=",
43+
Base64.encodeBytes(new byte[] {49, 121, 64, 113, -63, 43, -24, 62, 4, 48}, 2, 2, 0));
44+
}
45+
46+
@Test
47+
public void testEncodeBytes2() throws IOException {
48+
thrown.expect(IllegalArgumentException.class);
49+
Base64.encodeBytes(new byte[0], -2, -2, -56);
50+
}
51+
52+
@Test
53+
public void testEncodeBytes3() throws IOException {
54+
thrown.expect(IllegalArgumentException.class);
55+
Base64.encodeBytes(new byte[] {64, -128, 32, 18, 16, 16, 0, 18, 16},
56+
2064072977, -2064007440, 10);
57+
}
58+
59+
@Test
60+
public void testEncodeBytes4() {
61+
thrown.expect(NullPointerException.class);
62+
Base64.encodeBytes(null);
63+
}
64+
65+
@Test
66+
public void testEncodeBytes5() throws IOException {
67+
thrown.expect(IllegalArgumentException.class);
68+
Base64.encodeBytes(null, 32766, 0, 8);
69+
}
70+
71+
@Test
72+
public void testEncodeBytesToBytes1() throws IOException {
73+
Assert.assertArrayEquals(new byte[] {95, 68, 111, 78, 55, 45, 61, 61},
74+
Base64.encodeBytesToBytes(new byte[] {-108, -19, 24, 32}, 0, 4, 32));
75+
Assert.assertArrayEquals(new byte[] {95, 68, 111, 78, 55, 67, 111, 61},
76+
Base64.encodeBytesToBytes(new byte[] {-108, -19, 24, 32, -35}, 0, 5, 40));
77+
Assert.assertArrayEquals(new byte[] {95, 68, 111, 78, 55, 67, 111, 61},
78+
Base64.encodeBytesToBytes(new byte[] {-108, -19, 24, 32, -35}, 0, 5, 32));
79+
Assert.assertArrayEquals(new byte[] {87, 50, 77, 61},
80+
Base64.encodeBytesToBytes(new byte[] {115, 42, 123, 99, 10, -33, 75, 30, 91, 99}, 8, 2, 48));
81+
Assert.assertArrayEquals(new byte[] {87, 50, 77, 61},
82+
Base64.encodeBytesToBytes(new byte[] {115, 42, 123, 99, 10, -33, 75, 30, 91, 99}, 8, 2, 56));
83+
Assert.assertArrayEquals(new byte[] {76, 53, 66, 61},
84+
Base64.encodeBytesToBytes(new byte[] {113, 42, 123, 99, 10, -33, 75, 30, 88, 99}, 8, 2, 36));Assert.assertArrayEquals(new byte[] {87, 71, 77, 61},
85+
Base64.encodeBytesToBytes(new byte[] {113, 42, 123, 99, 10, -33, 75, 30, 88, 99}, 8, 2, 4));
86+
}
87+
88+
@Test
89+
public void testEncodeBytesToBytes2() throws IOException {
90+
thrown.expect(IllegalArgumentException.class);
91+
Base64.encodeBytesToBytes(new byte[] {83, 10, 91, 67, 42, -1, 107, 62, 91, 67}, 8, 6, 26);
92+
}
93+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2010-2019 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.util;
27+
28+
import org.java_websocket.exceptions.InvalidDataException;
29+
import org.junit.Assert;
30+
import org.junit.Test;
31+
32+
import java.nio.ByteBuffer;
33+
34+
public class CharsetfunctionsTest {
35+
36+
@Test
37+
public void testAsciiBytes() {
38+
Assert.assertArrayEquals(new byte[] {102, 111, 111}, Charsetfunctions.asciiBytes("foo"));
39+
}
40+
41+
@Test
42+
public void testStringUtf8ByteBuffer() throws InvalidDataException {
43+
Assert.assertEquals("foo", Charsetfunctions.stringUtf8(ByteBuffer.wrap(new byte[] {102, 111, 111})));
44+
}
45+
46+
47+
@Test
48+
public void testIsValidUTF8off() {
49+
Assert.assertFalse(Charsetfunctions.isValidUTF8(ByteBuffer.wrap(new byte[] {100}), 2));
50+
Assert.assertFalse(Charsetfunctions.isValidUTF8(ByteBuffer.wrap(new byte[] {(byte) 128}), 0));
51+
52+
Assert.assertTrue(Charsetfunctions.isValidUTF8(ByteBuffer.wrap(new byte[] {100}), 0));
53+
}
54+
55+
@Test
56+
public void testIsValidUTF8() {
57+
Assert.assertFalse(Charsetfunctions.isValidUTF8(ByteBuffer.wrap(new byte[] {(byte) 128})));
58+
59+
Assert.assertTrue(Charsetfunctions.isValidUTF8(ByteBuffer.wrap(new byte[] {100})));
60+
}
61+
62+
@Test
63+
public void testStringAscii1() {
64+
Assert.assertEquals("oBar", Charsetfunctions.stringAscii(new byte[] {102, 111, 111, 66, 97, 114}, 2, 4));
65+
66+
}
67+
68+
@Test
69+
public void testStringAscii2() {
70+
Assert.assertEquals("foo", Charsetfunctions.stringAscii(new byte[] {102, 111, 111}));
71+
}
72+
73+
@Test
74+
public void testUtf8Bytes() {
75+
Assert.assertArrayEquals(new byte[] {102, 111, 111, 66, 97, 114}, Charsetfunctions.utf8Bytes("fooBar"));
76+
}
77+
}

0 commit comments

Comments
 (0)