Skip to content
Navigation Menu
{{ message }}
forked from dampcake/bencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBencodeTest.java
More file actions
428 lines (342 loc) · 15.4 KB
/
Copy pathBencodeTest.java
File metadata and controls
428 lines (342 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package com.dampcake.bencode;
import org.junit.Before;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;
import java.io.EOFException;
import java.io.InvalidObjectException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentSkipListMap;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
/**
* Unit tests for Bencode.
*/
@SuppressWarnings("unchecked")
public class BencodeTest {
private Bencode instance;
@Before
public void setUp() {
instance = new Bencode();
}
@Test
public void testConstructorNullCharset() {
NullPointerException exception = assertThrows(NullPointerException.class, () -> new Bencode(null));
assertEquals("charset cannot be null", exception.getMessage());
}
@Test
public void testConstructorWithCharset() {
Bencode bencode = new Bencode(Charset.forName("US-ASCII"));
assertEquals(bencode.getCharset(), Charset.forName("US-ASCII"));
}
@Test
public void testTypeString() {
assertSame(Type.STRING, instance.type("7".getBytes()));
}
@Test
public void testTypeNumber() {
assertSame(Type.NUMBER, instance.type("i1".getBytes()));
}
@Test
public void testTypeList() {
assertSame(Type.LIST, instance.type("l123".getBytes()));
}
@Test
public void testTypeDictionary() {
assertSame(Type.DICTIONARY, instance.type("dtesting".getBytes()));
}
@Test
public void testTypeUnknown() {
assertSame(Type.UNKNOWN, instance.type("unknown".getBytes()));
}
@Test
public void testTypeEmpty() {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.type(new byte[0]));
assertThat(exception.getCause(), instanceOf(EOFException.class));
}
@Test
public void testTypeNullBytes() {
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.type(null));
assertEquals("bytes cannot be null", exception.getMessage());
}
@Test
public void testDecodeNullBytes() {
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.decode(null, Type.STRING));
assertEquals("bytes cannot be null", exception.getMessage());
}
@Test
public void testDecodeNullType() {
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.decode("12:Hello World!".getBytes(), null));
assertEquals("type cannot be null", exception.getMessage());
}
@Test
public void testDecodeUnknownType() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> instance.decode("12:Hello World!".getBytes(), Type.UNKNOWN));
assertEquals("type cannot be UNKNOWN", exception.getMessage());
}
@Test
public void testDecodeString() {
String decoded = instance.decode("12:Hello World!".getBytes(), Type.STRING);
assertEquals("Hello World!", decoded);
}
@Test
public void testDecodeStringMultiByteCodePoints() {
String decoded = instance.decode("7:Garçon".getBytes(), Type.STRING);
assertEquals("Garçon", decoded);
}
@Test
public void testDecodeEmptyString() {
String decoded = instance.decode("0:123".getBytes(), Type.STRING);
assertEquals("", decoded);
}
@Test
public void testDecodeStringNaN() throws Exception {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("1c3:Testing".getBytes(), Type.STRING));
assertThat(exception.getCause(), instanceOf(InvalidObjectException.class));
}
@Test
public void testDecodeStringEOF() throws Exception {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("123456".getBytes(), Type.STRING));
assertThat(exception.getCause(), instanceOf(EOFException.class));
}
@Test
public void testDecodeStringEmpty() throws Exception {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("".getBytes(), Type.STRING));
assertThat(exception.getCause(), instanceOf(EOFException.class));
}
@Test
public void testDecodeNumber() throws Exception {
long decoded = instance.decode("i123456e123".getBytes(), Type.NUMBER);
assertEquals(123456, decoded);
}
@Test
public void testDecodeNumberNaN() throws Exception {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("i123cbve1".getBytes(), Type.NUMBER));
assertThat(exception.getCause(), instanceOf(NumberFormatException.class));
}
@Test
public void testDecodeNumberEOF() throws Exception {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("i123".getBytes(), Type.NUMBER));
assertThat(exception.getCause(), instanceOf(EOFException.class));
}
@Test
public void testDecodeNumberEmpty() throws Exception {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("".getBytes(), Type.NUMBER));
assertThat(exception.getCause(), instanceOf(EOFException.class));
}
@Test
public void testDecodeList() throws Exception {
List<Object> decoded = instance.decode("l5:Hello6:World!li123ei456eeetesting".getBytes(), Type.LIST);
assertEquals(3, decoded.size());
assertEquals("Hello", decoded.get(0));
assertEquals("World!", decoded.get(1));
List<Object> list = (List<Object>) decoded.get(2);
assertEquals(123L, list.get(0));
assertEquals(456L, list.get(1));
}
@Test
public void testDecodeListByteArray() throws Exception {
instance = new Bencode(true);
List<Object> decoded = instance.decode("l5:Hello6:World!li123ei456eeetesting".getBytes(), Type.LIST);
assertEquals(3, decoded.size());
assertThat(decoded.get(0), instanceOf(ByteBuffer.class));
assertEquals("Hello", new String(((ByteBuffer) decoded.get(0)).array()));
assertThat(decoded.get(1), instanceOf(ByteBuffer.class));
assertEquals("World!", new String(((ByteBuffer) decoded.get(1)).array()));
List<Object> list = (List<Object>) decoded.get(2);
assertEquals(123L, list.get(0));
assertEquals(456L, list.get(1));
}
@Test
public void testDecodeListEmpty() throws Exception {
List<Object> decoded = instance.decode("le123".getBytes(), Type.LIST);
assertTrue(decoded.isEmpty());
}
@Test
public void testDecodeListInvalidItem() throws Exception {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("l2:Worlde".getBytes(), Type.LIST));
assertThat(exception.getCause(), instanceOf(InvalidObjectException.class));
}
@Test
public void testDecodeListEOF() throws Exception {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("l5:Hello".getBytes(), Type.LIST));
assertThat(exception.getCause(), instanceOf(EOFException.class));
}
@Test
public void testDecodeDictionary() throws Exception {
Map<String, Object> decoded = instance.decode("d4:dictd3:1234:test3:4565:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee".getBytes(), Type.DICTIONARY);
assertEquals(4, decoded.size());
assertEquals("value", decoded.get("string"));
assertEquals(123456L, decoded.get("number"));
List<Object> list = (List<Object>) decoded.get("list");
assertEquals(2, list.size());
assertEquals("list-item-1", list.get(0));
assertEquals("list-item-2", list.get(1));
Map<String, Object> map = (Map<String, Object>) decoded.get("dict");
assertEquals(2, map.size());
assertEquals("test", map.get("123"));
assertEquals("thing", map.get("456"));
}
@Test
public void testDecodeDebianTracker() throws Exception {
Map<String, Object> decoded = instance.decode("d8:intervali900e5:peersld2:ip12:146.71.73.514:porti63853eeee".getBytes(), Type.DICTIONARY);
assertEquals(2, decoded.size());
assertEquals(900L, decoded.get("interval"));
List<Object> list = (List<Object>) decoded.get("peers");
assertEquals(1, list.size());
Map<String, Object> map = (Map<String, Object>) list.get(0);
assertEquals("146.71.73.51", map.get("ip"));
assertEquals(63853L, map.get("port"));
}
@Test
public void testDecodeDictionaryByteArray() throws Exception {
instance = new Bencode(true);
Map<String, Object> decoded = instance.decode("d4:dictd3:1234:test3:4565:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee".getBytes(), Type.DICTIONARY);
assertEquals(4, decoded.size());
assertThat(decoded.get("string"), instanceOf(ByteBuffer.class));
assertEquals("value", new String(((ByteBuffer) decoded.get("string")).array()));
assertEquals(123456L, decoded.get("number"));
List<Object> list = (List<Object>) decoded.get("list");
assertEquals(2, list.size());
assertThat(list.get(0), instanceOf(ByteBuffer.class));
assertEquals("list-item-1", new String(((ByteBuffer) list.get(0)).array()));
assertThat(list.get(1), instanceOf(ByteBuffer.class));
assertEquals("list-item-2", new String(((ByteBuffer) list.get(1)).array()));
Map<String, Object> map = (Map<String, Object>) decoded.get("dict");
assertEquals(2, map.size());
assertThat(map.get("123"), instanceOf(ByteBuffer.class));
assertEquals("test", new String(((ByteBuffer) map.get("123")).array()));
assertThat(map.get("456"), instanceOf(ByteBuffer.class));
assertEquals("thing", new String(((ByteBuffer) map.get("456")).array()));
}
@Test
public void testDecodeDictionaryEmpty() throws Exception {
Map<String, Object> decoded = instance.decode("de123test".getBytes(), Type.DICTIONARY);
assertTrue(decoded.isEmpty());
}
@Test
public void testDecodeDictionaryInvalidItem() throws Exception {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("d4:item5:value3:testing".getBytes(), Type.DICTIONARY));
assertThat(exception.getCause(), instanceOf(InvalidObjectException.class));
}
@Test
public void testDecodeDictionaryEOF() throws Exception {
BencodeException exception = assertThrows(BencodeException.class, () -> instance.decode("d4:item5:test".getBytes(), Type.DICTIONARY));
assertThat(exception.getCause(), instanceOf(EOFException.class));
}
@Test
public void testWriteString() throws Exception {
byte[] encoded = instance.encode("Hello World!");
assertEquals("12:Hello World!", new String(encoded, instance.getCharset()));
}
@Test
public void testWriteStringMultiByteCodePoints() throws Exception {
byte[] encoded = instance.encode("Garçon");
assertEquals("7:Garçon", new String(encoded, instance.getCharset()));
}
@Test
public void testWriteStringEmpty() throws Exception {
byte[] encoded = instance.encode("");
assertEquals("0:", new String(encoded, instance.getCharset()));
}
@Test
public void testWriteStringNull() throws Exception {
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.encode((String) null));
assertEquals("s cannot be null", exception.getMessage());
}
@Test
public void testWriteNumber() throws Exception {
byte[] encoded = instance.encode(123456);
assertEquals("i123456e", new String(encoded, instance.getCharset()));
}
@Test
public void testWriteNumberDecimal() throws Exception {
byte[] encoded = instance.encode(123.456);
assertEquals("i123e", new String(encoded, instance.getCharset()));
}
@Test
public void testWriteNumberNull() throws Exception {
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.encode((Number) null));
assertEquals("n cannot be null", exception.getMessage());
}
@Test
public void testWriteList() throws Exception {
byte[] encoded = instance.encode(new ArrayList<Object>() {{
add("Hello");
add("World!");
add(new ArrayList<Object>() {{
add(123);
add(456);
}});
}});
assertEquals("l5:Hello6:World!li123ei456eee", new String(encoded, instance.getCharset()));
}
@Test
public void testWriteListEmpty() throws Exception {
byte[] encoded = instance.encode(new ArrayList<Object>());
assertEquals("le", new String(encoded, instance.getCharset()));
}
@Test
public void testWriteListNullItem() throws Exception {
ThrowingRunnable runnable = () -> instance.encode(new ArrayList<Object>() {{
add("Hello");
add("World!");
add(new ArrayList<Object>() {{
add(null);
add(456);
}});
}});
BencodeException exception = assertThrows(BencodeException.class, runnable);
assertThat(exception.getCause(), instanceOf(NullPointerException.class));
}
@Test
public void testWriteListNull() throws Exception {
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.encode((List<?>) null));
assertEquals("l cannot be null", exception.getMessage());
}
@Test
public void testWriteDictionary() throws Exception {
byte[] encoded = instance.encode(new LinkedHashMap<Object, Object>() {{
put("string", "value");
put("number", 123456);
put("list", new ArrayList<Object>() {{
add("list-item-1");
add("list-item-2");
}});
put("dict", new ConcurrentSkipListMap<Integer, String>() {{
put(123, "test");
put(456, "thing");
}});
}});
assertEquals("d4:dictd3:1234:test3:4565:thinge4:listl11:list-item-111:list-item-2e6:numberi123456e6:string5:valuee",
new String(encoded, instance.getCharset()));
}
@Test
public void testWriteDictionaryEmpty() throws Exception {
byte[] encoded = instance.encode(new HashMap<Object, Object>());
assertEquals("de", new String(encoded, instance.getCharset()));
}
@Test
public void testWriteDictionaryKeyCastException() throws Exception {
ThrowingRunnable runnable = () -> instance.encode(new TreeMap<Object, Object>() {{
put("string", "value");
put(123, "number-key");
}});
assertThrows(ClassCastException.class, runnable);
}
@Test
public void testWriteDictionaryNull() throws Exception {
NullPointerException exception = assertThrows(NullPointerException.class, () -> instance.encode((Map<?, ?>) null));
assertEquals("m cannot be null", exception.getMessage());
}
}
You can’t perform that action at this time.
