2015-07-26 01:40:07 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2015-09-23 18:13:00 +02:00
|
|
|
import libtorrent as lt
|
2015-07-26 01:40:07 +02:00
|
|
|
|
|
|
|
import unittest
|
2016-01-04 05:34:57 +01:00
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import shutil
|
2015-10-10 03:21:04 +02:00
|
|
|
import binascii
|
2015-07-26 01:40:07 +02:00
|
|
|
|
|
|
|
# test torrent_info
|
|
|
|
|
|
|
|
class test_torrent_info(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_bencoded_constructor(self):
|
|
|
|
info = lt.torrent_info({ 'info': {'name': 'test_torrent', 'length': 1234,
|
|
|
|
'piece length': 16 * 1024,
|
|
|
|
'pieces': 'aaaaaaaaaaaaaaaaaaaa'}})
|
|
|
|
|
|
|
|
self.assertEqual(info.num_files(), 1)
|
|
|
|
|
|
|
|
f = info.files()
|
|
|
|
self.assertEqual(f.file_path(0), 'test_torrent')
|
|
|
|
self.assertEqual(f.file_size(0), 1234)
|
|
|
|
self.assertEqual(info.total_size(), 1234)
|
|
|
|
|
2016-01-04 05:34:57 +01:00
|
|
|
class test_alerts(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_alert(self):
|
|
|
|
|
2016-01-05 07:32:04 +01:00
|
|
|
ses = lt.session({'alert_mask': lt.alert.category_t.all_categories})
|
2016-01-04 05:34:57 +01:00
|
|
|
shutil.copy(os.path.join('..', '..', 'test', 'test_torrents', 'base.torrent'), '.')
|
|
|
|
ti = lt.torrent_info('base.torrent');
|
|
|
|
h = ses.add_torrent({'ti': ti, 'save_path': '.'})
|
|
|
|
time.sleep(1)
|
|
|
|
ses.remove_torrent(h)
|
2016-01-06 06:17:51 +01:00
|
|
|
ses.wait_for_alert(1000) # milliseconds
|
2016-01-04 05:34:57 +01:00
|
|
|
alerts = ses.pop_alerts()
|
|
|
|
for a in alerts:
|
2016-01-06 06:17:51 +01:00
|
|
|
print(a.message())
|
|
|
|
|
|
|
|
st = h.status()
|
|
|
|
print(st.next_announce)
|
|
|
|
print(st.name)
|
|
|
|
print(st.errc.message())
|
|
|
|
print(st.pieces)
|
|
|
|
print(st.last_seen_complete)
|
|
|
|
print(st.completed_time)
|
|
|
|
print(st.progress)
|
|
|
|
print(st.num_pieces)
|
|
|
|
print(st.distributed_copies)
|
|
|
|
print(st.paused)
|
|
|
|
print(st.info_hash)
|
2016-01-04 05:34:57 +01:00
|
|
|
|
2015-07-26 01:40:07 +02:00
|
|
|
class test_bencoder(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_bencode(self):
|
|
|
|
|
|
|
|
encoded = lt.bencode({'a': 1, 'b': [1,2,3], 'c': 'foo'})
|
2016-01-11 04:53:54 +01:00
|
|
|
self.assertEqual(encoded, b'd1:ai1e1:bli1ei2ei3ee1:c3:fooe')
|
2015-07-26 01:40:07 +02:00
|
|
|
|
|
|
|
def test_bdecode(self):
|
|
|
|
|
2016-01-11 04:53:54 +01:00
|
|
|
encoded = b'd1:ai1e1:bli1ei2ei3ee1:c3:fooe'
|
2015-07-26 01:40:07 +02:00
|
|
|
decoded = lt.bdecode(encoded)
|
2016-01-11 04:53:54 +01:00
|
|
|
self.assertEqual(decoded, {b'a': 1, b'b': [1,2,3], b'c': b'foo'})
|
2015-07-26 01:40:07 +02:00
|
|
|
|
2015-10-10 03:21:04 +02:00
|
|
|
class test_sha1hash(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_sha1hash(self):
|
|
|
|
h = 'a0'*20
|
|
|
|
s = lt.sha1_hash(binascii.unhexlify(h))
|
|
|
|
self.assertEqual(h, str(s))
|
|
|
|
|
2015-11-11 06:57:51 +01:00
|
|
|
|
2016-01-14 02:35:54 +01:00
|
|
|
class test_session(unittest.TestCase):
|
|
|
|
def test_post_session_stats(self):
|
|
|
|
s = lt.session({'alert_mask': lt.alert.category_t.stats_notification})
|
|
|
|
s.post_session_stats()
|
|
|
|
a = s.wait_for_alert(1000)
|
|
|
|
self.assertTrue(isinstance(a, lt.session_stats_alert))
|
|
|
|
self.assertTrue(isinstance(a.values, dict))
|
|
|
|
self.assertTrue(len(a.values) > 0)
|
|
|
|
|
2015-07-26 01:40:07 +02:00
|
|
|
if __name__ == '__main__':
|
2016-01-11 04:53:54 +01:00
|
|
|
print(lt.__version__)
|
2015-11-11 06:57:51 +01:00
|
|
|
unittest.main()
|
2015-07-26 01:40:07 +02:00
|
|
|
|