async def test_udpsession_no_port(self): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, self.socket_mgr) self.assertFalse(session.is_failed) self.socket_mgr.transport = None with self.assertRaises(ValueError): await session.connect() self.assertTrue(session.is_failed)
async def test_udpsession_cancel_operation(fake_udp_socket_manager): session = UdpTrackerSession("127.0.0.1", ("localhost", 8475), "/announce", 0, None, fake_udp_socket_manager) task = ensure_future(session.connect_to_tracker()) with pytest.raises(CancelledError): task.cancel() await task await session.cleanup()
async def test_udpsession_cancel_operation(self): session = UdpTrackerSession("127.0.0.1", ("localhost", 8475), "/announce", 0, self.socket_mgr) task = ensure_future(session.connect_to_tracker()) with self.assertRaises(CancelledError): task.cancel() await task await session.cleanup()
async def test_udpsession_handle_wrong_action_transaction(self): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, self.socket_mgr) self.assertFalse(session.is_failed) self.socket_mgr.response = struct.pack("!qq4s", 123, 123, b"test") with self.assertRaises(ValueError): await session.connect() self.assertTrue(session.is_failed)
async def test_udpsession_response_too_short(fake_udp_socket_manager): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, None, fake_udp_socket_manager) assert not session.is_failed fake_udp_socket_manager.response = struct.pack("!i", 123) with pytest.raises(ValueError): await session.scrape() assert session.is_failed
async def test_udpsession_no_port(fake_udp_socket_manager): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, None, fake_udp_socket_manager) assert not session.is_failed fake_udp_socket_manager.transport = None with pytest.raises(ValueError): await session.connect() assert session.is_failed
async def test_udpsession_response_wrong_transaction_id(self): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, self.socket_mgr) self.assertFalse(session.is_failed) self.socket_mgr.response = struct.pack("!ii", 0, 1337) with self.assertRaises(ValueError): await session.scrape() self.assertTrue(session.is_failed)
async def test_udpsession_handle_wrong_action_transaction( fake_udp_socket_manager): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, None, fake_udp_socket_manager) assert not session.is_failed fake_udp_socket_manager.response = struct.pack("!qq4s", 123, 123, b"test") with pytest.raises(ValueError): await session.connect() assert session.is_failed
async def test_udpsession_handle_packet(fake_udp_socket_manager): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, None, fake_udp_socket_manager) session.action = 123 session.transaction_id = 124 assert not session.is_failed fake_udp_socket_manager.response = struct.pack("!iiq", 123, 124, 126) await session.connect() assert not session.is_failed
async def test_udpsession_handle_packet(self): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, self.socket_mgr) session.action = 123 session.transaction_id = 124 self.assertFalse(session.is_failed) self.socket_mgr.response = struct.pack("!iiq", 123, 124, 126) await session.connect() self.assertFalse(session.is_failed)
async def test_udpsession_mismatch(fake_udp_socket_manager): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, None, fake_udp_socket_manager) session.action = 123 session.transaction_id = 124 session.infohash_list = [b'\x00' * 20] assert not session.is_failed fake_udp_socket_manager.response = struct.pack("!ii", 123, 124) with pytest.raises(ValueError): await session.scrape() assert session.is_failed
async def test_udpsession_response_list_len_mismatch(self): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, self.socket_mgr) session.action = 123 session.transaction_id = 123 self.assertFalse(session.is_failed) session._infohash_list = [b"test", b"test2"] self.socket_mgr.response = struct.pack("!iiiii", 123, 123, 0, 1, 2) with self.assertRaises(ValueError): await session.scrape() self.assertTrue(session.is_failed)
async def test_udpsession_response_list_len_mismatch(fake_udp_socket_manager): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, None, fake_udp_socket_manager) session.action = 123 session.transaction_id = 123 assert not session.is_failed session._infohash_list = [b"test", b"test2"] fake_udp_socket_manager.response = struct.pack("!iiiii", 123, 123, 0, 1, 2) with pytest.raises(ValueError): await session.scrape() assert session.is_failed await session.cleanup()
async def test_big_correct_run(self): session = UdpTrackerSession("localhost", ("192.168.1.1", 1234), "/announce", 0, self.socket_mgr) self.assertFalse(session.is_failed) self.socket_mgr.response = struct.pack("!iiq", session.action, session.transaction_id, 126) await session.connect() session.infohash_list = [b"test"] self.socket_mgr.response = struct.pack("!iiiii", session.action, session.transaction_id, 0, 1, 2) await session.scrape() self.assertTrue(session.is_finished)
async def test_udpsession_timeout(fake_udp_socket_manager): sleep_future = Future() fake_udp_socket_manager.send_request = lambda *_: sleep_future transport, _ = await get_event_loop().create_datagram_endpoint( lambda: DatagramProtocol(), local_addr=('127.0.0.1', 0), family=socket.AF_INET) _, port = transport.get_extra_info('sockname') session = UdpTrackerSession("localhost", ("127.0.0.1", port), "/announce", .1, None, fake_udp_socket_manager) with pytest.raises(ValueError): await session.connect_to_tracker() transport.close()
async def test_udpsession_correct_handle(self): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 5, self.socket_mgr) session.ip_address = "127.0.0.1" session.infohash_list.append(b'test') self.socket_mgr.response = struct.pack("!iiq", 0, session.transaction_id, 2) await session.connect() self.socket_mgr.response = struct.pack("!iiiii", 2, session.transaction_id, 0, 1, 2) await session.scrape() self.assertFalse(session.is_failed) await session.cleanup()
async def test_big_correct_run(fake_udp_socket_manager): session = UdpTrackerSession("localhost", ("192.168.1.1", 1234), "/announce", 0, None, fake_udp_socket_manager) assert not session.is_failed fake_udp_socket_manager.response = struct.pack("!iiq", session.action, session.transaction_id, 126) await session.connect() session.infohash_list = [b"test"] fake_udp_socket_manager.response = struct.pack("!iiiii", session.action, session.transaction_id, 0, 1, 2) await session.scrape() assert session.is_finished await session.cleanup()
async def test_udpsession_correct_handle(fake_udp_socket_manager): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 5, None, fake_udp_socket_manager) session.ip_address = "127.0.0.1" session.infohash_list.append(b'test') fake_udp_socket_manager.response = struct.pack("!iiq", 0, session.transaction_id, 2) await session.connect() fake_udp_socket_manager.response = struct.pack("!iiiii", 2, session.transaction_id, 0, 1, 2) await session.scrape() assert not session.is_failed await session.cleanup()
async def test_udpsession_handle_response_wrong_len(self): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, self.socket_mgr) self.assertFalse(session.is_failed) self.socket_mgr.response = b"too short" with self.assertRaises(ValueError): await session.connect() self.assertTrue(session.is_failed) # After receiving a correct packet, it session should still be in a failed state session.action = 123 session.transaction_id = 124 self.socket_mgr.response = struct.pack("!iiq", 123, 124, 126) await session.connect() self.assertTrue(session.is_failed)
async def test_udpsession_handle_response_wrong_len(fake_udp_socket_manager): session = UdpTrackerSession("localhost", ("localhost", 4782), "/announce", 0, None, fake_udp_socket_manager) assert not session.is_failed fake_udp_socket_manager.response = b"too short" with pytest.raises(ValueError): await session.connect() assert session.is_failed # After receiving a correct packet, it session should still be in a failed state session.action = 123 session.transaction_id = 124 fake_udp_socket_manager.response = struct.pack("!iiq", 123, 124, 126) await session.connect() assert session.is_failed
def test_failed_unicode_udp(fake_udp_socket_manager): session = UdpTrackerSession("localhost", ("localhost", 8475), "/announce", 0, None, fake_udp_socket_manager) with pytest.raises(ValueError): session.failed('\xd0')
def test_failed_unicode_udp(self): session = UdpTrackerSession("localhost", ("localhost", 8475), "/announce", 0, self.socket_mgr) self.assertRaises(ValueError, session.failed, '\xd0')