summaryrefslogtreecommitdiff
path: root/tests/integration/test_fullscrape.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/test_fullscrape.py')
-rw-r--r--tests/integration/test_fullscrape.py148
1 files changed, 148 insertions, 0 deletions
diff --git a/tests/integration/test_fullscrape.py b/tests/integration/test_fullscrape.py
new file mode 100644
index 0000000..45b3ffb
--- /dev/null
+++ b/tests/integration/test_fullscrape.py
@@ -0,0 +1,148 @@
1"""The full scrape, in every format the tracker offers.
2
3Each format is a separate writer, and they differ in more than cosmetics:
4one is binary, one is bencoded, three are line based and one dumps the
5tracker's own state for a restart. A worker thread produces them in chunks,
6so these also exercise the chunked transfer the ordinary replies never take.
7"""
8
9import gzip
10import struct
11import time
12import unittest
13
14from harness import TrackerTestCase, bdecode
15
16HEX = b"0123456789ABCDEF"
17
18
19class FullScrape(TrackerTestCase):
20 def setUp(self):
21 TrackerTestCase.setUp(self)
22 self.seeding = self.hash_for("fsA")
23 self.leeching = self.hash_for("fsB")
24 self.tracker.announce(self.seeding, port=6001, left=0)
25 self.tracker.announce(self.leeching, port=6002, left=4096)
26
27 def scrape_all(self, query, headers=None):
28 body, head = self.tracker.get_with_headers(query, headers)
29 if head.get("Content-Encoding") == "gzip":
30 body = gzip.decompress(body)
31 return body
32
33 # --------------------------------------------------------- bencoded
34
35 def test_a_bare_scrape_returns_every_torrent(self):
36 answer = bdecode(self.scrape_all("/scrape"))
37 self.assertIn(self.seeding, answer[b"files"])
38 self.assertIn(self.leeching, answer[b"files"])
39 self.assertEqual(answer[b"files"][self.seeding][b"complete"], 1)
40 self.assertEqual(answer[b"files"][self.leeching][b"incomplete"], 1)
41
42 def test_the_bencoded_format_matches_the_bare_scrape(self):
43 self.assertEqual(bdecode(self.scrape_all("/stats?mode=tpbs&format=ben")), bdecode(self.scrape_all("/scrape")))
44
45 def test_a_full_scrape_can_be_gzipped(self):
46 plain = self.scrape_all("/scrape")
47 body, head = self.tracker.get_with_headers("/scrape", {"Accept-Encoding": "gzip"})
48 self.assertEqual(head.get("Content-Encoding"), "gzip")
49 self.assertEqual(gzip.decompress(body), plain)
50
51 # ----------------------------------------------------------- binary
52
53 def test_the_binary_format_is_a_hash_and_two_counts(self):
54 body = self.scrape_all("/stats?mode=tpbs&format=bin")
55 self.assertEqual(len(body) % 28, 0, "twenty byte hash plus two counts")
56 entries = {body[i : i + 20]: struct.unpack(">II", body[i + 20 : i + 28]) for i in range(0, len(body), 28)}
57 self.assertEqual(entries[self.seeding], (1, 0), "one seeder, no leechers")
58 self.assertEqual(entries[self.leeching], (0, 1), "no seeders, one leecher")
59
60 # ------------------------------------------------------- line based
61
62 def entries_from_lines(self, body):
63 return {line.split(b":")[0]: line.split(b":")[1:] for line in body.splitlines() if line}
64
65 def test_the_text_format_is_hex_colon_seeders_colon_leechers(self):
66 entries = self.entries_from_lines(self.scrape_all("/stats?mode=tpbs&format=txt"))
67 self.assertEqual(entries[self.seeding.hex().upper().encode()], [b"1", b"0"])
68 self.assertEqual(entries[self.leeching.hex().upper().encode()], [b"0", b"1"])
69
70 def test_the_text_plus_format_adds_the_completed_count(self):
71 entries = self.entries_from_lines(self.scrape_all("/stats?mode=tpbs&format=txtp"))
72 self.assertEqual(entries[self.seeding.hex().upper().encode()], [b"1", b"0", b"0"])
73
74 def test_the_urlencoded_format_escapes_the_hash(self):
75 body = self.scrape_all("/stats?mode=tpbs&format=url")
76 lines = [line for line in body.splitlines() if line]
77 self.assertEqual(len(lines), 2)
78 for line in lines:
79 escaped = line.rsplit(b":", 2)[0]
80 self.assertIn(b"%", escaped, "these hashes are mostly unprintable bytes")
81
82 def test_the_state_dump_carries_a_stamp_rather_than_a_peer_count(self):
83 """What a restart reads back. The second column is the minute the
84 torrent was last touched and not a count of anybody, by the wall
85 clock, which is the only one two runs of the tracker agree on."""
86 entries = self.entries_from_lines(self.scrape_all("/stats?mode=statedump"))
87 self.assertIn(self.seeding.hex().upper().encode(), entries)
88 stamp, completed = entries[self.seeding.hex().upper().encode()]
89 self.assertAlmostEqual(int(stamp), int(time.time()) // 60, delta=1, msg="announced a moment ago")
90 self.assertEqual(int(completed), 0)
91
92 # ------------------------------------------------------------ shape
93
94 def test_every_format_sees_the_same_torrents(self):
95 counts = {
96 "ben": len(bdecode(self.scrape_all("/stats?mode=tpbs&format=ben"))[b"files"]),
97 "bin": len(self.scrape_all("/stats?mode=tpbs&format=bin")) // 28,
98 "txt": len(self.entries_from_lines(self.scrape_all("/stats?mode=tpbs&format=txt"))),
99 "txtp": len(self.entries_from_lines(self.scrape_all("/stats?mode=tpbs&format=txtp"))),
100 "url": len([l for l in self.scrape_all("/stats?mode=tpbs&format=url").splitlines() if l]),
101 }
102 self.assertEqual(set(counts.values()), {2}, "all formats, two torrents: %r" % counts)
103
104
105class FullScrapeChunking(TrackerTestCase):
106 def test_a_scrape_larger_than_one_chunk_arrives_whole(self):
107 """The writer hands the socket a chunk at a time, so a scrape that
108 spans several is the case where the seams show."""
109 for i in range(400):
110 self.tracker.announce(struct.pack(">I", i) + b"\0" * 16, port=7000 + i, left=0)
111 answer = bdecode(self.tracker.get("/scrape"))
112 self.assertEqual(len(answer[b"files"]), 400)
113
114
115if __name__ == "__main__":
116 unittest.main()
117
118
119class PartialScrapeOrder(TrackerTestCase):
120 """A bencoded dictionary names its keys in order, once each. The full
121 scrape got that from walking the buckets; the partial scrape used to
122 echo whatever order the client asked in."""
123
124 def setUp(self):
125 TrackerTestCase.setUp(self)
126 self.hashes = [bytes([i]) + b"\0" * 19 for i in (9, 1, 5, 3, 7)]
127 for i, h in enumerate(self.hashes):
128 self.tracker.announce(h, port=6001 + i, left=0)
129
130 def scrape_many(self, hashes):
131 import urllib.parse
132
133 query = "&".join("info_hash=" + urllib.parse.quote_from_bytes(h, safe="") for h in hashes)
134 return bdecode(self.tracker.get("/scrape?" + query))
135
136 def test_the_keys_come_back_in_order(self):
137 answer = self.scrape_many(self.hashes)
138 self.assertEqual(list(answer[b"files"]), sorted(self.hashes))
139
140 def test_reversing_the_question_does_not_reverse_the_answer(self):
141 self.assertEqual(
142 list(self.scrape_many(self.hashes[::-1])[b"files"]), list(self.scrape_many(self.hashes)[b"files"])
143 )
144
145 def test_a_hash_asked_for_twice_is_named_once(self):
146 asked = [self.hashes[0], self.hashes[1], self.hashes[0]]
147 answer = self.scrape_many(asked)
148 self.assertEqual(list(answer[b"files"]), sorted({self.hashes[0], self.hashes[1]}))