-
Notifications
You must be signed in to change notification settings - Fork 0
/
metainfo_test.ts
111 lines (97 loc) · 3.82 KB
/
metainfo_test.ts
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
import {
assertEquals,
assertNotEquals,
} from "https://deno.land/std@0.96.0/testing/asserts.ts#^";
import {
MultiFileInfoDict,
parseMetainfo,
SingleFileInfoDict,
} from "./metainfo.ts";
Deno.test("Metainfo - parseMetainfo() - single file", async () => {
const file = await Deno.readFile("./test_data/singlefile.torrent");
const metainfo = await parseMetainfo(file);
assertNotEquals(metainfo, null);
assertEquals(metainfo!.comment, "comment");
assertEquals(metainfo!.announce, "http://example.com/announce");
assertEquals(metainfo!.encoding, "UTF-8");
assertEquals(
metainfo!.createdBy,
"https://github.com/rclarey/torrent/blob/master/tools/make_torrent.ts",
);
assertEquals(metainfo!.creationDate, 1602023427);
const info = metainfo!.info as SingleFileInfoDict;
assertEquals(info.pieceLength, 262144);
assertEquals(info.name, "singlefile.txt");
assertEquals(info.length, 447135744);
assertEquals(info.pieces.length, 1706);
assertEquals(info.private, 0);
});
Deno.test("Metainfo - parseMetainfo() - multi file", async () => {
const file = await Deno.readFile("./test_data/multifile.torrent");
const metainfo = await parseMetainfo(file);
assertNotEquals(metainfo, null);
assertEquals(metainfo!.comment, "comment");
assertEquals(metainfo!.announce, "http://example.com/announce");
assertEquals(metainfo!.encoding, "UTF-8");
assertEquals(
metainfo!.createdBy,
"https://github.com/rclarey/torrent/blob/master/tools/make_torrent.ts",
);
assertEquals(metainfo!.creationDate, 1599690859);
const info = metainfo!.info as MultiFileInfoDict;
assertEquals(info.pieceLength, 524288);
assertEquals(info.name, "multifile");
assertEquals(info.pieces.length, 1855);
assertEquals(info.private, 0);
assertEquals(info.files.length, 2);
const [file1, file2] = info.files;
assertEquals(file1.length, 447135744);
assertEquals(file1.path.join("/"), "file1.txt");
assertEquals(file2.length, 525148160);
assertEquals(file2.path.join("/"), "dir/file2.txt");
});
Deno.test("Metainfo - parseMetainfo() - minimal fields", async () => {
const file = await Deno.readFile("./test_data/minimal.torrent");
const metainfo = await parseMetainfo(file);
assertNotEquals(metainfo, null);
assertEquals(metainfo!.comment, undefined);
assertEquals(metainfo!.announce, "http://example.com/announce");
assertEquals(metainfo!.encoding, undefined);
assertEquals(
metainfo!.createdBy,
undefined,
);
assertEquals(metainfo!.creationDate, undefined);
const info = metainfo!.info as SingleFileInfoDict;
assertEquals(info.pieceLength, 262144);
assertEquals(info.name, "singlefile.txt");
assertEquals(info.length, 447135744);
assertEquals(info.pieces.length, 1706);
// even though this is missing in the .torrent it gets
// filled with a default value
assertEquals(info.private, 0);
});
Deno.test("Metainfo - parseMetainfo() - extra fields", async () => {
const file = await Deno.readFile("./test_data/extra.torrent");
const metainfo = await parseMetainfo(file);
assertNotEquals(metainfo, null);
assertEquals(metainfo!.comment, "comment");
assertEquals(metainfo!.announce, "http://example.com/announce");
assertEquals(metainfo!.encoding, "UTF-8");
assertEquals(
metainfo!.createdBy,
"https://github.com/rclarey/torrent/blob/master/tools/make_torrent.ts",
);
assertEquals(metainfo!.creationDate, 1602024152);
const info = metainfo!.info as SingleFileInfoDict;
assertEquals(info.pieceLength, 262144);
assertEquals(info.name, "singlefile.txt");
assertEquals(info.length, 447135744);
assertEquals(info.pieces.length, 1706);
assertEquals(info.private, 0);
});
Deno.test("Metainfo - parseMetainfo() - missing fields", async () => {
const file = await Deno.readFile("./test_data/missing.torrent");
const metainfo = await parseMetainfo(file);
assertEquals(metainfo, null);
});