forked from Blender3D/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bittorrent.py
95 lines (72 loc) · 2.99 KB
/
test_bittorrent.py
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
import unittest
import struct
from tornado.testing import AsyncTestCase, gen_test
from bittorrent import utils
from bittorrent.torrent import Torrent
from bittorrent.protocol.message import KeepAlive, Choke, Have, Bitfield
from bittorrent.tracker import Tracker, HTTPTracker, UDPTracker, TrackerResponse
class TestTorrentReader(unittest.TestCase):
def test_read(self):
Torrent('torrents/archlinux-2013.12.01-dual.iso.torrent')
class TestProtocolMessages(unittest.TestCase):
def test_keep_alive(self):
self.assertIsInstance(KeepAlive.unpack(''), KeepAlive)
self.assertIsInstance(KeepAlive.unpack(KeepAlive().pack(), with_header=True), KeepAlive)
def test_choke(self):
self.assertIsInstance(Choke.unpack(''), Choke)
self.assertIsInstance(Choke.unpack(Choke().pack(), with_header=True), Choke)
def test_have(self):
self.assertEqual(Have.unpack(Have(piece=123).pack(), with_header=True).piece, 123)
self.assertRaises(struct.error, Have(piece=12345678910).pack)
def test_bitfield(self):
b = {
0: True,
1: False,
3: True
}
c = {
0: True,
1: False,
2: False,
3: True,
4: False,
5: False,
6: False,
7: False
}
self.assertEqual(
Bitfield.unpack(Bitfield(b).pack(with_header=True), with_header=True).bitfield,
c
)
class TestTracker(AsyncTestCase):
def test_autodetect(self):
self.assertIsInstance(Tracker('udp://tracker.openbittorrent.com:80/announce', None), UDPTracker)
self.assertIsInstance(Tracker('http://torrent.ubuntu.com:6969/announce', None), HTTPTracker)
self.assertRaises(ValueError, Tracker, 'gopher://tracker.openbittorrent.com:80/announce', None)
self.assertRaises(ValueError, Tracker, 'tracker.openbittorrent.com:80/announce', None)
@gen_test
def test_http(self):
torrent = Torrent('torrents/archlinux-2013.12.01-dual.iso.torrent')
tracker = Tracker('http://tracker.archlinux.org:6969/announce', torrent)
response = yield tracker.announce(utils.peer_id(), 6881)
self.assertIsInstance(response, TrackerResponse)
@gen_test
def test_udp(self):
torrent = Torrent('torrents/archlinux-2013.12.01-dual.iso.torrent')
tracker = Tracker('udp://tracker.openbittorrent.com:80/announce', torrent)
response = yield tracker.announce(utils.peer_id(), 6881)
self.assertIsInstance(response, TrackerResponse)
class TestUtils(unittest.TestCase):
def test_fill(self):
class FakeFile(object):
def __init__(self):
self.content = ''
def write(self, data):
self.content += data
def seek(self, index):
pass
handle = FakeFile()
utils.fill(handle, 1024)
self.assertEquals(handle.content, '\x00' * 1024)
if __name__ == '__main__':
unittest.main()