-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bencoding.cs
executable file
·415 lines (368 loc) · 13.9 KB
/
Bencoding.cs
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
using System;
using System.Collections.Generic;
using Xunit;
using Ditto.Common;
namespace Ditto.UnitTests
{
// Plug in your exception classes (for invalid values) here.
using serializationException = Ditto.Bencoding.EncodingException;
using deserializationException = Ditto.Bencoding.DecodingException;
public class BencodingTests
{
// Plug in your encoding and decoding functions here.
static object deserialize(byte[] bytes) {
return Bencoding.Decode(bytes);
}
static byte[] serialize(object value) {
return Bencoding.Encode(value);
}
// Asserts that deserializing and reserializing doesn't modify a value.
public void AssertRoundTrip(byte[] bytes) {
var value = deserialize(bytes);
var reserialized = serialize(value);
Assert.Equal(bytes, reserialized);
}
[Fact]
public void PositiveIntegerFromValue()
{
Int64 value = 12345;
var encoded = serialize(value);
Assert.Equal("i12345e".ToASCII(), encoded);
AssertRoundTrip(encoded);
}
[Fact]
public void PositiveIntegerFromBytes()
{
var bytes = "i13e".ToASCII();
var result = deserialize(bytes);
Assert.Equal((Int64) 13, result);
AssertRoundTrip(bytes);
}
[Fact]
public void NegativeIntegerFromBytes()
{
var bytes = "i-3153e".ToASCII();
var result = deserialize(bytes);
Assert.Equal((Int64) (-3153), result);
AssertRoundTrip(bytes);
}
[Fact]
public void NegativeIntegerFromValue()
{
Int64 value = -9876;
var encoded = serialize(value);
Assert.Equal("i-9876e".ToASCII(), encoded);
AssertRoundTrip(encoded);
}
[Fact]
public void ZeroIntegerFromBytes()
{
var value = "i0e".ToASCII();
var result = deserialize(value);
Assert.Equal(typeof(Int64), result.GetType());
var typedResult = (Int64) result;
Assert.Equal(0, typedResult);
AssertRoundTrip(value);
}
[Fact]
public void LargePositiveIntegerFromBytes()
{
var value = "i42897244160e".ToASCII();
var result = deserialize(value);
Assert.Equal(typeof(Int64), result.GetType());
var typedResult = (Int64) result;
Assert.Equal(42897244160, typedResult);
AssertRoundTrip(value);
}
[Fact]
public void InvalidLeadingZerosPositiveIntegerFromBytes()
{
var value = "i05e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidMultipleMinusInIntegerFromBytes()
{
var value = "i--33e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidNonInitialMinusInIntegerFromBytes()
{
var value = "i3-3e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidLeadingZeroesNegativeIntegerFromBytes()
{
var value = "i-03e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidNegativeZeroIntegerFromBytes()
{
var value = "i-0e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidEmptyIntegerFromBytes()
{
var value = "ie".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void UnsupportedGiganticIntegerFromBytes()
{
var value = (
"i" +
"123456789012345678901234567890123456789012345678901234567890" +
"123456789012345678901234567890123456789012345678901234567890" +
"123456789012345678901234567890123456789012345678901234567890" +
"123456789012345678901234567890123456789012345678901234567890" +
"e").ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidLeadingCharacterFromBytes()
{
var value = "z0e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void StringFromBytes()
{
var value = "5:hello".ToASCII();
var result = deserialize(value);
Assert.Equal(typeof(byte[]), result.GetType());
var typedResult = (byte[]) result;
Assert.Equal("hello".ToASCII(), typedResult);
AssertRoundTrip(value);
}
[Fact]
public void StringFromValue()
{
var value = "hello world".ToASCII();
var encoded = serialize(value);
Assert.Equal("11:hello world".ToASCII(), encoded);
AssertRoundTrip(encoded);
}
[Fact]
public void InvalidJamesBond()
{
var value = "007".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidDigitsThenLetters()
{
var value = "22twentytwo".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidSpaceousStringStart()
{
var value = "22 :idontknowaoutyou".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidAyeAyeAyeAyeAye() {
var value = "iiiiiiiiii13e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidPositivelyPositiveInteger() {
var value = "i+1e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidSpaceousStartInteger() {
var value = "i 1e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidSpaceousEndInteger() {
var value = "i1 e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void EmptyStringFromValue()
{
var value = "0:".ToASCII();
var result = deserialize(value);
Assert.Equal(typeof(byte[]), result.GetType());
var typedResult = (byte[]) result;
Assert.Equal(new byte[]{}, typedResult);
AssertRoundTrip(value);
}
[Fact]
public void InvalidLeadingZeroesStringLengthFromBytes()
{
var value = "05:hello".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidNegativeStringLengthFromBytes()
{
var value = "-5:hello".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidStringLongerThanDataFromBytes()
{
var value = "50:hello".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidStringLongerThanPossibleFromBytes()
{
var value =
"987654332198765433219876543321987654332198765433219876543321:hello".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void EmptyListFromBytes()
{
var value = "le".ToASCII();
var result = deserialize(value);
Assert.Equal(typeof(List<object>), result.GetType());
var typedResult = (List<object>) result;
Assert.Equal(0, typedResult.Count);
AssertRoundTrip(value);
}
[Fact]
public void IntegerListFromBytes()
{
var value = "li1ei2ei3ee".ToASCII();
var result = deserialize(value);
Assert.Equal(typeof(List<object>), result.GetType());
AssertRoundTrip(value);
}
[Fact]
public void StringAndIntegerListFromValue()
{
var value = new List<object> { (Int64) 1234, "hello".ToASCII(), (Int64) (-5678), "world".ToASCII() };
var encoded = serialize(value);
Assert.Equal("li1234e5:helloi-5678e5:worlde".ToASCII(), encoded);
AssertRoundTrip(encoded);
}
[Fact]
public void EmptyDictionaryFromBytes()
{
var value = "de".ToASCII();
var result = deserialize(value);
Assert.Equal(typeof(Dictionary<byte[], object>), result.GetType());
var typedResult = (Dictionary<byte[], object>) result;
Assert.Equal(0, typedResult.Count);
AssertRoundTrip(value);
}
[Fact]
public void IntegerDictionaryFromValue()
{
var value = "d1:1i2e1:3i4ee".ToASCII();
var result = deserialize(value);
Assert.Equal(typeof(Dictionary<byte[], object>), result.GetType());
var typedResult = (Dictionary<byte[], object>) result;
Assert.Equal((Int64) 2, typedResult.Count);
Assert.Equal((Int64) 2, typedResult["1".ToASCII()]);
Assert.Equal((Int64) 4, typedResult["3".ToASCII()]);
AssertRoundTrip(value);
}
[Fact]
public void IntegerListDictionaryFromBytes()
{
var value = new Dictionary<byte[], object> {
{"hello".ToASCII(), (Int64) 1234},
{"world".ToASCII(), (Int64) (-5678)}
};
var encoded = serialize(value);
Assert.Equal("d5:helloi1234e5:worldi-5678ee".ToASCII(), encoded);
AssertRoundTrip(encoded);
}
[Fact]
public void InvalidDuplicateKeysDictionaryFromValue()
{
var value = new Dictionary<byte[], object> {
{"hello".ToASCII(), (Int64) 1234},
{"hello".ToASCII(), (Int64) (-5678)}
};
Assert.Equal(2, value.Count); // verify that we did add two keys
Assert.Throws<serializationException>(() => serialize(value));
}
[Fact]
public void NestedListDictionaryFromValue()
{
var value = "d1:1li2ee1:3li4eee".ToASCII();
var result = deserialize(value);
Assert.Equal(typeof(Dictionary<byte[], object>), result.GetType());
AssertRoundTrip(value);
}
[Fact]
public void InvalidTwoKeyOneValueDictionaryFromBytes()
{
var value = new Dictionary<byte[], object> {
{
"hello".ToASCII(),
new List<object> {
new Dictionary<byte[], object> {
{"world".ToASCII(), (Int64) 102436}
}
}
}
};
var encoded = serialize(value);
Assert.Equal("d5:hellold5:worldi102436eeee".ToASCII(), encoded);
AssertRoundTrip(encoded);
}
[Fact]
public void InvalidMisorderedKeysDictionaryFromBytes()
{
var value = "d1:1i2e1:3e".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidDuplicateKeysDictionaryFromBytes()
{
var value = "d1:3i4e1:1i2ee".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void DictionaryFromMisorderedDictionaryFromValue()
{
var value = "d1:1i2e1:1i2ee".ToASCII();
Assert.Throws<deserializationException>(() => deserialize(value));
}
[Fact]
public void InvalidIntegerKeyedDictionaryFromBytes()
{
var value = new Dictionary<byte[], object> {
{"zzz".ToASCII(), "last".ToASCII()},
{"zz".ToASCII(), "dictionary".ToASCII()},
{"zza".ToASCII(), "whose".ToASCII()},
{"".ToASCII(), "disordered".ToASCII()},
{"a".ToASCII(), "with".ToASCII()},
{"az".ToASCII(), "keys".ToASCII()},
{"azb".ToASCII(), "invalid".ToASCII()},
{"aza".ToASCII(), "two".ToASCII()}
};
var encoded = serialize(value);
Assert.Equal(
("d0:10:disordered1:a4:with2:az4:keys3:aza3:two3:azb" +
"7:invalid2:zz10:dictionary3:zza5:whose3:zzz4:laste").ToASCII(),
encoded);
AssertRoundTrip(encoded);
}
[Fact]
public void TorrentLikeFromBytes()
{
var value = (
"d8:announce35:udp://tracker.openbittorrent.com:8013:announce-list" +
"ll35:udp://tracker.openbittorrent.com:80el33:udp://tracker.opentrackr.org:1337ee" +
"4:infod6:lengthi7e4:name7:example12:piece lengthi7e6:pieces20:0I0')s000000v0-0o0?0" +
"4:salt3:200e8:url-listl57:https://mgnt.ca/123456fc77d23aca05a8b58066bb55fe06c72f8e/" +
"56:http://mgnt.ca/123456fc77d23aca05a8b58066bb55fe06c72f8e/ee").ToASCII();
var result = deserialize(value);
Assert.Equal(typeof(Dictionary<byte[], object>), result.GetType());
AssertRoundTrip(value);
}
}
}