def test_valid_torrent_file_announce_list_correct(self): create_valid_metainfo({ "nodes": [["127.0.0.1", 8081]], "info": { "name": "my_torrent", "piece length": 12345, "pieces": "12345678901234567890", "length": 42 }, "announce-list": [[]] })
def make_torrent_file(input, userabortflag=None, userprogresscallback=lambda x: None): """ Create a torrent file from the supplied input. Returns a (infohash,metainfo) pair, or (None,None) on userabort. """ (info, piece_length) = makeinfo(input, userabortflag, userprogresscallback) if userabortflag is not None and userabortflag.isSet(): return None, None if info is None: return None, None metainfo = {'info': info, 'encoding': input['encoding'], 'creation date': long(time())} metainfo = create_valid_metainfo(metainfo) # http://www.bittorrent.org/beps/bep_0005.html says both announce and nodes # are not allowed, but some torrents (Azureus?) apparently violate this. if input['nodes'] is None and input['announce'] is None: raise ValueError('No tracker set') for key in ['announce', 'announce-list', 'nodes', 'comment', 'created by', 'httpseeds', 'url-list']: if input[key] is not None and len(input[key]) > 0: metainfo[key] = input[key] if key == 'comment': metainfo['comment.utf-8'] = uniconvert(input['comment'], 'utf-8') if 'private' in input: metainfo['info']['private'] = input['private'] if 'anonymous' in input: metainfo['info']['anonymous'] = input['anonymous'] # Two places where infohash calculated, here and in TorrentDef. # Elsewhere: must use TorrentDef.get_infohash() to allow P2PURLs. infohash = sha1(bencode(info)).digest() return infohash, metainfo
def _on_load(torrent_def): torrent_def.metainfo = create_valid_metainfo( torrent_def.get_metainfo()) self.assertTrue(valid_torrent_file(torrent_def.get_metainfo())) self.assertEqual( torrent_def.get_metainfo(), TorrentDef.load(TORRENT_UBUNTU_FILE).get_metainfo()) self.assertEqual(torrent_def.infohash, TorrentDef.load(TORRENT_UBUNTU_FILE).infohash)
def _create(metainfo): # TODO: replace with constructor # raises ValueErrors if not good metainfo_fixed = create_valid_metainfo(metainfo) t = TorrentDef() t.metainfo = metainfo_fixed t.metainfo_valid = True # copy stuff into self.input maketorrent.copy_metainfo_to_input(t.metainfo, t.input) # Two places where infohash calculated, here and in maketorrent.py # Elsewhere: must use TorrentDef.get_infohash() to allow P2PURLs. t.infohash = sha1(bencode(metainfo['info'])).digest() assert isinstance(t.infohash, str), "INFOHASH has invalid type: %s" % type(t.infohash) assert len(t.infohash) == INFOHASH_LENGTH, "INFOHASH has invalid length: %d" % len(t.infohash) return t
def make_torrent_file(input, userabortflag=None, userprogresscallback=lambda x: None): """ Create a torrent file from the supplied input. Returns a (infohash,metainfo) pair, or (None,None) on userabort. """ (info, piece_length) = makeinfo(input, userabortflag, userprogresscallback) if userabortflag is not None and userabortflag.isSet(): return None, None if info is None: return None, None metainfo = { 'info': info, 'encoding': input['encoding'], 'creation date': long(time()) } metainfo = create_valid_metainfo(metainfo) # http://www.bittorrent.org/beps/bep_0005.html says both announce and nodes # are not allowed, but some torrents (Azureus?) apparently violate this. if input['nodes'] is None and input['announce'] is None: raise ValueError('No tracker set') for key in [ 'announce', 'announce-list', 'nodes', 'comment', 'created by', 'httpseeds', 'url-list' ]: if input[key] is not None and len(input[key]) > 0: metainfo[key] = input[key] if key == 'comment': metainfo['comment.utf-8'] = uniconvert(input['comment'], 'utf-8') if 'private' in input: metainfo['info']['private'] = input['private'] if 'anonymous' in input: metainfo['info']['anonymous'] = input['anonymous'] # Two places where infohash calculated, here and in TorrentDef. # Elsewhere: must use TorrentDef.get_infohash() to allow P2PURLs. infohash = sha1(bencode(info)).digest() return infohash, metainfo
def _create(metainfo): # TODO: replace with constructor # raises ValueErrors if not good metainfo_fixed = create_valid_metainfo(metainfo) t = TorrentDef() t.metainfo = metainfo_fixed t.metainfo_valid = True # copy stuff into self.input maketorrent.copy_metainfo_to_input(t.metainfo, t.input) # Two places where infohash calculated, here and in maketorrent.py # Elsewhere: must use TorrentDef.get_infohash() to allow P2PURLs. t.infohash = sha1(bencode(metainfo['info'])).digest() assert isinstance( t.infohash, str), "INFOHASH has invalid type: %s" % type(t.infohash) assert len( t.infohash ) == INFOHASH_LENGTH, "INFOHASH has invalid length: %d" % len( t.infohash) return t
def test_valid_torrent_file_announce_dht_invalid_url(self): create_valid_metainfo({"info": {}, "announce": "dht:test"})
def test_valid_torrent_file_no_info(self): create_valid_metainfo({})
def test_valid_torrent_file_invalid_metainfo_type(self): create_valid_metainfo([])
def test_valid_torrent_file_announce_list_correct(self): create_valid_metainfo({"nodes": [["127.0.0.1", 8081]], "info": {"name": "my_torrent", "piece length": 12345, "pieces": "12345678901234567890", "length": 42}, "announce-list": [[]]})
def test_valid_torrent_file_root_hash(self): create_valid_metainfo({"info": {"root hash": "12345"}})
def _on_load(torrent_def): torrent_def.metainfo = create_valid_metainfo(torrent_def.get_metainfo()) self.assertTrue(valid_torrent_file(torrent_def.get_metainfo())) self.assertEqual(torrent_def.get_metainfo(), TorrentDef.load(TORRENT_UBUNTU_FILE).get_metainfo()) self.assertEqual(torrent_def.infohash, TorrentDef.load(TORRENT_UBUNTU_FILE).infohash)