async def test_piece_manager_get_piece(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) piece_manager = create_piece_manager(torrent_file) piece = await piece_manager.get_piece() assert type(piece) == Piece
def test_create_piece_manager_last_piece(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) piece_manager = create_piece_manager(torrent_file) last_piece = piece_manager.pieces[len(piece_manager.pieces) - 1] assert torrent_file.length % torrent_file.piece_length == last_piece.length
def test_generate_torrent_not_duplicate_tracker(): files = _files_list() for path in files: tracker_list = generate_torrent_file(path).trackers for _ in range(len(tracker_list)): tracker = tracker_list.pop() for t in tracker_list: assert t.tracker_type != tracker.tracker_type or t.url != tracker.url or t.path != tracker.path or t.port != tracker.port
def test_all_pieces_in_status(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) piece_manager = create_piece_manager(torrent_file) status_dict = piece_manager.pieces_status() assert len(piece_manager.pieces) == sum( [i for i in status_dict.values()])
def test_create_piece_manager_pieces_size(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) pieces_manager = create_piece_manager(torrent_file) size = 0 for piece in pieces_manager.pieces: size += piece.length assert torrent_file.length == size
def test_create_piece_manager_all_pieces_available(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) pieces_manager = create_piece_manager(torrent_file) ptr = 0 for piece in pieces_manager.pieces: assert torrent_file.pieces[ptr:ptr + 20] == piece.piece_hash ptr += 20
def test_generate_torrent_file_multi(): files = _files_list() for path in files: torrent_file = generate_torrent_file(path) od_struct = torrent_file.od_data try: if od_struct[b'info'][b'files']: assert torrent_file.multi_file except KeyError: assert torrent_file.multi_file == False
def test_udp_request_tracker_tracker_from_another_torrent(): files = _files_list() for tracker in generate_torrent_file(files[4]).trackers: if tracker.url == 'explodie.org': tracker_other_torrent = tracker torrent_file = generate_torrent_file(files[0]) peer_manager = Peer_manager() thread = udp_request(tracker_other_torrent, torrent_file, peer_manager) sleep(1) assert not thread.is_alive()
def test_save_torrent_file_data_the_same(): for path in _files_list(): save_torrent_file(path) new_torrent_file_path = os.path.join(DIR_NAME, 'torrent_files', os.path.basename(path)) with open(path, 'rb') as f: base_torrent_file = f.read() with open(new_torrent_file_path, 'rb') as f: new_torrent_file = f.read() assert new_torrent_file == base_torrent_file delete_file(new_torrent_file_path)
def test_create_handshake(): files = _files_list() for f in files: torrent_file = generate_torrent_file(f) info_hash = torrent_file.info_hash peer_id = torrent_file.peer_id handshake = Handshake(info_hash, peer_id) protocol = 'BitTorrent protocol'.encode('utf-8') valid_packet = pack('>B19s8x20s20s', 19, protocol, info_hash, peer_id.encode('utf-8')) assert handshake.send_bytes() == valid_packet
def test_create_url_for_http_tracker(): path = _files_list()[3] torrent_file = generate_torrent_file(path) peer_manager = Peer_manager() for tracker in torrent_file.trackers: #TODO add asser for query string url = create_url_for_http_tracker(torrent_file, tracker, torrent_file.length) schema, netloc, path, _, query, _ = urlparse(url) base_url = schema + '://' + netloc + path assert base_url == 'https://ipv6.torrent.ubuntu.com:443/announce' or base_url == 'https://torrent.ubuntu.com:443/announce'
def test_udp_request_good_tracker(): files = _files_list() torrent_file = generate_torrent_file(files[2]) peer_manager = Peer_manager() for tracker in torrent_file.trackers: if tracker.url == 'exodus.desync.com': valid_tracker = tracker thread = udp_request(valid_tracker, torrent_file, peer_manager) sleep(30) assert peer_manager.peers.empty() == False kill_thread(thread)
def test_parse_handshake(): files = _files_list() for f in files: torrent_file = generate_torrent_file(f) info_hash = torrent_file.info_hash peer_id = torrent_file.peer_id handshake = Handshake(info_hash, peer_id) protocol = 'BitTorrent protocol'.encode('utf-8') valid_packet = pack('>B19s8x20s20s', 19, protocol, info_hash, peer_id.encode('utf-8')) new_handshake = Handshake.parse(handshake.send_bytes()) assert new_handshake.send_bytes() == handshake.send_bytes() assert Handshake.parse(b'\x13BitTorrent protocol\x00\x00\x00\x00\x00\x00\x00\x00\xdd\x82U\xec\xdc|\xa5_\xb0\xbb\xf8\x13#\xd8pb\xdb\x1fm\x1c-PC0001-713259777527')
def test_generate_torrent_file_trackers(): files = _files_list() for path in files: torrent = generate_torrent_file(path) list_of_trackers = decode_raw_data(read_file(path))[b'announce-list'] list_of_trackers = [ create_tracker(tracker.decode('utf-8')) for track_list in list_of_trackers for tracker in track_list ] list_of_trackers = [tracker for tracker in list_of_trackers if tracker] for tracker in list_of_trackers: assert tracker in torrent.trackers
async def test_request_creation(): torrent_file = generate_torrent_file(_files_list()[3]) piece_manager = create_piece_manager(torrent_file) block_size = 2 ** 14 for i in range(100): piece = await piece_manager.get_piece() piece_size = torrent_file.piece_length block_offset = 0 for i in range(int(piece_size / block_size)): request_message = Request(piece.index, block_offset, block_size) block_offset += block_size assert request_message
def test_udp_request_tracker_with_no_response(): path = _files_list()[2] torrent_file = generate_torrent_file(path) for tracker in torrent_file.trackers: if tracker.url == "tracker.yify-torrents.com" and tracker.tracker_type == 'udp': no_res_tracker = tracker peer_manager = Peer_manager() thread = udp_request(no_res_tracker, no_res_tracker, peer_manager) assert thread.is_alive() assert peer_manager.peers.empty() sleep(30) assert thread.is_alive() assert peer_manager.peers.empty() kill_thread(thread)
async def test_write_file(): path = _files_list()[3] #only not multi file in _file_list torrent_file = generate_torrent_file(path) file_genrator(torrent_file) delete_file_for_test_if_exsit(torrent_file) file_genrator(torrent_file) piece_manager = create_piece_manager(torrent_file) asyncio.gather(write_pieces_to_memory(torrent_file, piece_manager.done_queue)) for piece in piece_manager.pieces: full_piece_with_bytes(piece) await piece_manager.put_in_queue(piece) piece.piece_written() await piece_manager.done_queue.put(None) check_all_data_wirtten_in_right_place(torrent_file, piece_manager) #close process delete_file_for_test_if_exsit(torrent_file)
def test_tracker_manager_tracker_alive(): files = _files_list() for torrent_path in files: log = logging torrent_file = generate_torrent_file(torrent_path) peer_manager = Peer_manager() tracker_threads = tracker_manager(torrent_file, peer_manager) sleep(10) for thread in tracker_threads: if thread.result_queue.empty(): assert thread.is_alive() else: assert not thread.is_alive() for thread in tracker_threads: kill_thread(thread)
def test_http_request_good_tracker(): #TODO the test fail if you run the untest more then one time in a row fix it... path = _files_list()[3] torrent_file = generate_torrent_file(path) peer_manager = Peer_manager() for tracker in torrent_file.trackers: if tracker.url == 'torrent.ubuntu.com': good_tracker = tracker url = create_url_for_http_tracker(torrent_file, tracker, torrent_file.length) thread = http_request(url, peer_manager) sleep(15) assert thread.is_alive() empty_status = peer_manager.peers.empty() kill_thread(thread) assert empty_status == False #first kill the thread the assert chek otherwise the all the pytest stuck
def test_torrent_file_repr(): files = _files_list() for path in files: tracker_file = generate_torrent_file(path) assert tracker_file
def test_save_torrent_file(): for path in _files_list(): save_torrent_file(path) new_torrent_file_path = os.path.join(DIR_NAME, 'torrent_files', os.path.basename(path)) assert os.path.exists(new_torrent_file_path) delete_file(new_torrent_file_path)
async def test_get_piece_many_pieces_at_the_same_time(): files = _files_list() for torrent_path in files: torrent_file = generate_torrent_file(torrent_path) piece_manager = create_piece_manager(torrent_file) await gathering(piece_manager)
def test_tracker_with_http_schema(): path = _files_list()[2] torrent_file = generate_torrent_file(path) for tracker in torrent_file.trackers: if tracker.tracker_type == 'http': assert tracker.schema == "http"
def test_tracker_repr(): files = _files_list() for path in files: torrent_file = generate_torrent_file(path) for tracker in torrent_file.trackers: assert tracker
def test_generate_torrent_file_path(): files = _files_list() for path in files: torrent_file = generate_torrent_file(path) assert torrent_file.path == path