desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Test the monitoring of a transaction in a Trustchain wallet'
@deferred(timeout=10) def test_monitor_transaction(self):
def on_transaction(transaction): self.assertEqual(transaction, 'a') return self.tc_wallet.monitor_transaction('abc.1').addCallback(on_transaction)
'Test the address of a Trustchain wallet'
def test_address(self):
self.assertIsInstance(self.tc_wallet.get_address(), str)
'Test the retrieval of transactions of a dummy wallet'
@deferred(timeout=10) def test_get_transaction(self):
def on_transactions(transactions): self.assertIsInstance(transactions, list) return self.tc_wallet.get_transactions().addCallback(on_transactions)
'Test the minimum unit of a Trustchain wallet'
def test_min_unit(self):
self.assertEqual(self.tc_wallet.min_unit(), 1)
'Test waiting for an introduction candidate in the TrustChain wallet'
def test_wait_for_intro_of_candidate(self):
candidate = MockObject() candidate.sock_addr = None return self.tc_wallet.wait_for_intro_of_candidate(candidate)
'Test the identifier of a dummy wallet'
def test_wallet_id(self):
self.assertEqual(self.dummy_wallet.get_identifier(), 'DUM') self.assertEqual(DummyWallet1().get_identifier(), 'DUM1') self.assertEqual(DummyWallet2().get_identifier(), 'DUM2')
'Test the name of a dummy wallet'
def test_wallet_name(self):
self.assertEqual(self.dummy_wallet.get_name(), 'Dummy') self.assertEqual(DummyWallet1().get_name(), 'Dummy 1') self.assertEqual(DummyWallet2().get_name(), 'Dummy 2')
'Test the creation of a dummy wallet'
@deferred(timeout=10) def test_create_wallet(self):
return self.dummy_wallet.create_wallet()
'Test fetching the balance of a dummy wallet'
@deferred(timeout=10) def test_get_balance(self):
def on_balance(balance): self.assertIsInstance(balance, dict) return self.dummy_wallet.get_balance().addCallback(on_balance)
'Test the transfer of money from a dummy wallet'
@deferred(timeout=10) def test_transfer(self):
def check_transactions(transactions): self.assertEqual(len(transactions), 1) def get_transactions(_): return self.dummy_wallet.get_transactions().addCallback(check_transactions) return self.dummy_wallet.transfer((self.dummy_wallet.balance - 1), None).addCallback(get_transactions)
'Test whether transferring a too large amount of money from a dummy wallet raises an error'
@deferred(timeout=10) def test_transfer_invalid(self):
test_deferred = Deferred() def on_error(failure): self.assertIsInstance(failure.value, InsufficientFunds) test_deferred.callback(None) self.dummy_wallet.transfer((self.dummy_wallet.balance + 1), None).addErrback(on_error) return test_deferred
'Test the monitor loop of a transaction wallet'
@deferred(timeout=10) def test_monitor(self):
self.dummy_wallet.MONITOR_DELAY = 1 return self.dummy_wallet.monitor_transaction('3.0')
'Test an instant the monitor loop of a transaction wallet'
@deferred(timeout=10) def test_monitor_instant(self):
self.dummy_wallet.MONITOR_DELAY = 0 return self.dummy_wallet.monitor_transaction('3.0')
'Test the address of a dummy wallet'
def test_address(self):
self.assertIsInstance(self.dummy_wallet.get_address(), str)
'Test the retrieval of transactions of a dummy wallet'
@deferred(timeout=10) def test_get_transaction(self):
def on_transactions(transactions): self.assertIsInstance(transactions, list) return self.dummy_wallet.get_transactions().addCallback(on_transactions)
'Test the minimum unit of a dummy wallet'
def test_min_unit(self):
self.assertEqual(self.dummy_wallet.min_unit(), 1)
'Test the generation of a random transaction id'
def test_generate_txid(self):
self.assertTrue(self.dummy_wallet.generate_txid(10)) self.assertEqual(len(self.dummy_wallet.generate_txid(20)), 20)
'Test the unitize method of a Transaction'
def test_unitize(self):
self.assertEqual(Transaction.unitize(1, 1), 1) self.assertEqual(Transaction.unitize(0.03, 0.02), 0.04) self.assertEqual(Transaction.unitize(50, 0.05), 50) self.assertEqual(Transaction.unitize(50.1818, 25), 75)
'Test the addition of a payment to a transaction'
def test_add_payment(self):
self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.transferred_price, Price(2, 'BTC')) self.assertEqual(self.transaction.transferred_quantity, Quantity(3, 'MC')) self.assertTrue(self.transaction.payments)
'Test the retrieval of the last payment'
def test_last_payment(self):
self.assertIsNone(self.transaction.last_payment(True)) self.assertIsNone(self.transaction.last_payment(False)) self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.last_payment(True), self.payment) self.assertEqual(self.transaction.last_payment(False), self.payment)
'Test the process of determining the next payment details during a transaction'
def test_next_payment(self):
def set_transaction_data(trans_price, trans_quantity, payment_price, payment_quantity): self.transaction._transferred_price = trans_price self.transaction._transferred_quantity = trans_quantity self.payment._transferee_price = payment_price self.payment._transferee_quantity = payment...
'Test the to dictionary method of a transaction'
def test_to_dictionary(self):
self.assertDictEqual(self.transaction.to_dictionary(), {'trader_id': '0', 'transaction_number': 1, 'order_number': 2, 'partner_trader_id': '2', 'partner_order_number': 1, 'payment_complete': False, 'price': 100.0, 'price_type': 'BTC', 'quantity': 30.0, 'quantity_type': 'MC', 'transferred_price': 0.0, 'transferred_q...
'Test the status of a transaction'
def test_status(self):
self.assertEqual(self.transaction.status, 'pending') self.payment._success = False self.transaction.add_payment(self.payment) self.assertEqual(self.transaction.status, 'error')
'Test the conversion of a StartTransaction object to the network'
def test_to_network(self):
data = self.start_transaction.to_network() self.assertEqual(data[0], self.start_transaction.message_id.trader_id) self.assertEqual(data[1], self.start_transaction.message_id.message_number) self.assertEqual(data[2], self.start_transaction.transaction_id.trader_id) self.assertEqual(data[3], self.star...
'Return a placeholder message with a specific meta name'
def get_placeholder_msg(self, meta_name):
meta_msg = self.market_community.get_meta_message(meta_name) msg = MockObject() msg.meta = meta_msg return msg
'Test decoding of a payload'
def test_decode_payload(self):
message = MockObject() meta_msg = self.market_community.get_meta_message(u'ask') offer_payload = OfferPayload.Implementation(meta_msg, TraderId('abc'), MessageNumber('3'), OrderNumber(4), Price(5, 'BTC'), Quantity(6, 'MC'), Timeout(3600), Timestamp.now(), 'a', 'b', Ttl(3), '1.2.3.4', 1234) message.paylo...
'Test encoding and decoding of an introduction request'
def test_encode_decode_intro_request(self):
message = MockObject() meta_msg = self.market_community.get_meta_message(u'dispersy-introduction-request') bloomfilter = BloomFilter(0.005, 30, prefix=' ') intro_payload = MarketIntroPayload.Implementation(meta_msg, ('127.0.0.1', 1324), ('127.0.0.1', 1234), ('127.0.0.1', 1234), True, u'public', None,...
'Test encoding and decoding of an offer'
def test_encode_decode_offer(self):
message = MockObject() meta_msg = self.market_community.get_meta_message(u'ask') offer_payload = OfferPayload.Implementation(meta_msg, TraderId('abc'), MessageNumber('3'), OrderNumber(4), Price(5, 'BTC'), Quantity(6, 'MC'), Timeout(3600), Timestamp.now(), 'a', 'b', Ttl(3), '1.2.3.4', 1234) message.paylo...
'Test encoding and decoding of a cancel order'
def test_encode_decode_cancel_order(self):
message = MockObject() meta_msg = self.market_community.get_meta_message(u'cancel-order') cancel_order_payload = CancelOrderPayload.Implementation(meta_msg, TraderId('abc'), MessageNumber('3'), Timestamp.now(), OrderNumber(4), Ttl(2)) message.payload = cancel_order_payload (packet,) = self.conversio...
'Test encoding and decoding of an offer sync'
def test_encode_decode_offer_sync(self):
message = MockObject() meta_msg = self.market_community.get_meta_message(u'offer-sync') offer_payload = OfferSyncPayload.Implementation(meta_msg, TraderId('abc'), MessageNumber('3'), OrderNumber(4), Price(5, 'BTC'), Quantity(6, 'MC'), Timeout(3600), Timestamp.now(), 'a', 'b', Ttl(3), '1.2.3.4', 1234, True) ...
'Test encoding and decoding of an declined trade'
def test_encode_decode_declined_trade(self):
message = MockObject() meta_msg = self.market_community.get_meta_message(u'declined-trade') trade_payload = DeclinedTradePayload.Implementation(meta_msg, TraderId('abc'), MessageNumber('3'), OrderNumber(4), TraderId('def'), OrderNumber(5), 1234, Timestamp.now()) message.payload = trade_payload (pack...
'Test encoding and decoding of a start transaction message'
def test_encode_decode_start_transaction(self):
message = MockObject() meta_msg = self.market_community.get_meta_message(u'start-transaction') transaction_payload = StartTransactionPayload.Implementation(meta_msg, TraderId('abc'), MessageNumber('3'), TraderId('def'), TransactionNumber(5), TraderId('def'), OrderNumber(3), TraderId('abc'), OrderNumber(4), ...
'Test encoding and decoding of a transaction message'
def test_encode_decode_transaction(self):
message = MockObject() meta_msg = self.market_community.get_meta_message(u'end-transaction') transaction_payload = TransactionPayload.Implementation(meta_msg, TraderId('abc'), MessageNumber('3'), TraderId('def'), TransactionNumber(5), Timestamp.now()) message.payload = transaction_payload (packet,) ...
'Test encoding and decoding of wallet info'
def test_encode_decode_wallet_info(self):
message = MockObject() meta_msg = self.market_community.get_meta_message(u'wallet-info') wallet_payload = WalletInfoPayload.Implementation(meta_msg, TraderId('abc'), MessageNumber('3'), TraderId('def'), TransactionNumber(5), WalletAddress('a'), WalletAddress('b'), Timestamp.now()) message.payload = wall...
'Test encoding and decoding of a payment'
def test_encode_decode_payment(self):
message = MockObject() meta_msg = self.market_community.get_meta_message(u'payment') payment_payload = PaymentPayload.Implementation(meta_msg, TraderId('abc'), MessageNumber('3'), TraderId('def'), TransactionNumber(5), Quantity(5, 'MC'), Price(6, 'BTC'), WalletAddress('a'), WalletAddress('b'), PaymentId('ab...
'Test the initialization of the database order repository'
def test_init(self):
self.assertRaises(ValueError, DatabaseOrderRepository, ('g' * 10), None)
'Test the insertion and retrieval of an order in the database'
@blocking_call_on_reactor_thread def test_add_get_order(self):
self.database.add_order(self.order1) orders = self.database.get_all_orders() self.assertEqual(len(orders), 1)
'Test the retrieval of a specific order'
@blocking_call_on_reactor_thread def test_get_specific_order(self):
order_id = OrderId(TraderId('3'), OrderNumber(4)) self.assertIsNone(self.database.get_order(order_id)) self.database.add_order(self.order1) self.assertIsNotNone(self.database.get_order(order_id))
'Test the deletion of an order from the database'
@blocking_call_on_reactor_thread def test_delete_order(self):
self.database.add_order(self.order1) self.assertEqual(len(self.database.get_all_orders()), 1) self.database.delete_order(self.order_id1) self.assertEqual(len(self.database.get_all_orders()), 0)
'Test the retrieval of the next order number from the database'
@blocking_call_on_reactor_thread def test_get_next_order_number(self):
self.assertEqual(self.database.get_next_order_number(), 1) self.database.add_order(self.order1) self.assertEqual(self.database.get_next_order_number(), 5)
'Test the retrieval, addition and deletion of reserved ticks in the database'
@blocking_call_on_reactor_thread def test_add_delete_reserved_ticks(self):
self.database.add_reserved_tick(self.order_id1, self.order_id2, self.order1.total_quantity) self.assertEqual(len(self.database.get_reserved_ticks(self.order_id1)), 1) self.database.delete_reserved_ticks(self.order_id1) self.assertEqual(len(self.database.get_reserved_ticks(self.order_id1)), 0)
'Test the insertion and retrieval of a transaction in the database'
@blocking_call_on_reactor_thread def test_add_get_transaction(self):
self.database.add_transaction(self.transaction1) transactions = self.database.get_all_transactions() self.assertEqual(len(transactions), 1) self.assertEqual(len(self.database.get_payments(self.transaction1.transaction_id)), 1)
'Test the retrieval of a specific transaction'
@blocking_call_on_reactor_thread def test_get_specific_transaction(self):
transaction_id = TransactionId(TraderId('0'), TransactionNumber(4)) self.assertIsNone(self.database.get_transaction(transaction_id)) self.database.add_transaction(self.transaction1) self.assertIsNotNone(self.database.get_transaction(transaction_id))
'Test the deletion of a transaction from the database'
@blocking_call_on_reactor_thread def test_delete_transaction(self):
self.database.add_transaction(self.transaction1) self.assertEqual(len(self.database.get_all_transactions()), 1) self.database.delete_transaction(self.transaction_id1) self.assertEqual(len(self.database.get_all_transactions()), 0)
'Test the retrieval of the next transaction number from the database'
@blocking_call_on_reactor_thread def test_get_next_transaction_number(self):
self.assertEqual(self.database.get_next_transaction_number(), 1) self.database.add_transaction(self.transaction1) self.assertEqual(self.database.get_next_transaction_number(), 5)
'Test the insertion and retrieval of a payment in the database'
@blocking_call_on_reactor_thread def test_add_get_payment(self):
self.database.add_payment(self.payment1) payments = self.database.get_payments(self.transaction_id1) self.assertEqual(len(payments), 1)
'Test addition, retrieval and deletion of ticks in the database'
@blocking_call_on_reactor_thread def test_add_remove_tick(self):
ask = Tick.from_order(self.order1, MessageId(TraderId('0'), MessageNumber('message_number'))) self.database.add_tick(ask) bid = Tick.from_order(self.order2, MessageId(TraderId('0'), MessageNumber('message_number'))) self.database.add_tick(bid) self.assertEqual(len(self.database.get_ticks()), 2) ...
'Test the addition and retrieval of a trader identity in the database'
@blocking_call_on_reactor_thread def test_add_get_trader_identity(self):
self.database.add_trader_identity('a', '123', 1234) self.database.add_trader_identity('b', '124', 1235) traders = self.database.get_traders() self.assertEqual(len(traders), 2) self.assertEqual(traders, [('a', '123', 1234), ('b', '124', 1235)])
'Test the check of the database'
@blocking_call_on_reactor_thread def test_check_database(self):
self.assertEqual(self.database.check_database(unicode(LATEST_DB_VERSION)), LATEST_DB_VERSION)
'Setup a second peer that contains some search results.'
@blocking_call_on_reactor_thread @inlineCallbacks def setup_peer(self):
self.setUpPreSession() self.config2 = self.config.copy() self.config2.set_state_dir(self.getStateDir(2)) self.session2 = Session(self.config2, ignore_singleton=True) (yield self.session2.start()) self.dispersy2 = self.session2.get_dispersy_instance() @blocking_call_on_reactor_thread @inl...
'Test whether we receive results when searching remotely for torrents'
@deferred(timeout=20) def test_torrent_search(self):
test_deferred = Deferred() def on_search_results_torrents(_dummy1, _dummy2, _dummy3, results): self.assertEqual(len(results['result_list']), 1) test_deferred.callback(None) reactor.callLater(2, self.session.search_remote_torrents, [u'test']) self.session.add_observer(on_search_results_to...
'Test whether we receive results when searching remotely for channels'
@deferred(timeout=20) def test_channel_search(self):
test_deferred = Deferred() def on_search_results_channels(_dummy1, _dummy2, _dummy3, results): self.assertEqual(len(results['result_list']), 1) test_deferred.callback(None) reactor.callLater(5, self.session.search_remote_channels, [u'test']) self.session.add_observer(on_search_results_ch...
'Test whether we are creating a search response when we receive a search request'
def test_on_search(self):
def log_incoming_searches(sock_addr, keywords): log_incoming_searches.called = True log_incoming_searches.called = False def create_search_response(id, results, candidate): create_search_response.called = True self.assertEqual(id, 'abc') self.assertEqual(results, []) ...
'Test whether decoding an invalid search response does not crash the program'
@raises(DropPacket) def test_decode_response_invalid(self):
self.search_community._initialize_meta_messages() search_conversion = SearchConversion(self.search_community) search_conversion._decode_search_response(None, 0, 'a[]')
'Test the creation of a torrent in the search community'
@blocking_call_on_reactor_thread def test_create_torrent(self):
with open(os.path.join(TESTS_DATA_DIR, 'bak_single.torrent'), mode='rb') as torrent_file: torrent_data = torrent_file.read() mock_session = MockObject() mock_session.get_collected_torrent = (lambda _: torrent_data) mock_session.open_dbhandler = (lambda _: None) mock_session.notifier = None ...
'Testing whether the right methods are called when a torrent is removed from a playlist'
def test_remove_playlist_torrents(self):
def mocked_load_message(undone, community, packet_id): fake_message = MockObject() fake_message.undone = undone return fake_message def mocked_create_undo(_): mocked_create_undo.called = True mocked_create_undo.called = False def mocked_undo_playlist_torrent(_): m...
'Testing whether a correct Dispersy message is created when we add a torrent to our channel'
@blocking_call_on_reactor_thread def test_create_torrent_from_def(self):
metainfo = {'info': {'name': 'my_torrent', 'piece length': 12345, 'pieces': '12345678901234567890', 'files': [{'path': ['test.txt'], 'length': 1234}]}} torrent = TorrentDef.load_from_dict(metainfo) self.channel_community.initialize() message = self.channel_community._disp_create_torrent_from_torrentd...
'Test the encoding of a torrent file'
def test_encode_torrent(self):
message = MockObject() message.payload = MockObject() message.payload.name = u'test' message.payload.infohash = ('a' * 20) message.payload.timestamp = 1234 message.payload.files = [(u'a', 1234)] message.payload.trackers = ['udp://tracker.openbittorrent.com:80/announce', 'http://google.com'] ...
'Test the decoding of a torrent message'
def test_decode_torrent(self):
self.assertRaises(DropPacket, self.conversion._decode_torrent, None, 0, 'abcd') self.assertRaises(DropPacket, self.conversion._decode_torrent, None, 0, zlib.compress('abcd')) meta = self.channel_community.get_meta_message(u'torrent') msg = MockObject() msg.meta = meta torrent_msg = encode((pack(...
'Testing whether a votecast can be created in the community'
@deferred(timeout=10) def test_create_votecast(self):
def verify(message): self.assertTrue(isinstance(message, Message.Implementation)) return self.community.disp_create_votecast(('c' * 20), 2, 300).addCallback(verify)
'Test the unloading of the preview community'
@deferred(timeout=10) def test_unload_preview(self):
def verify_unloaded(_): self.assertEqual(len(self.dispersy.get_communities()), 1) preview_member = DummyMember(self.dispersy, 2, ('c' * 20)) preview_community = PreviewChannelCommunity(self.dispersy, preview_member, self.member) preview_community.initialize() preview_community.init_timestamp...
'Test whether a torrent is correctly seeded'
@deferred(timeout=60) def test_seeding(self):
self.generate_torrent() def start_download(_): dscfg = self.dscfg_seed.copy() dscfg.set_dest_dir(self.getDestDir()) self.start_download(dscfg) self.setup_seeder(self.tdef, TESTS_DATA_DIR).addCallback(start_download) return self.test_deferred
'Take a screenshot of the widget. You can optionally append a string to the name of the screenshot. The screenshot itself is saved as a JPEG file.'
def screenshot(self, widget, name=None):
pixmap = QPixmap(widget.rect().size()) widget.render(pixmap, QPoint(), QRegion(widget.rect())) self.screenshots_taken += 1 img_name = ('screenshot_%d.jpg' % self.screenshots_taken) if (name is not None): img_name = ('screenshot_%s.jpg' % name) screenshots_dir = os.path.join(os.path.dirna...
'Return the port range of the test bucket assigned.'
def get_bucket_range_port(self):
min_base_port = (1000 if (not os.environ.get('TEST_BUCKET', None)) else ((int(os.environ['TEST_BUCKET']) * 2000) + 2000)) return (min_base_port, (min_base_port + 2000))
'Return five random, free socks5 ports. This is here to make sure that tests in different buckets get assigned different SOCKS5 listen ports. Also, make sure that we have no duplicates in selected socks5 ports.'
def get_socks5_ports(self):
socks5_ports = [] for _ in xrange(0, 5): (min_base_port, max_base_port) = self.get_bucket_range_port() selected_port = get_random_port(min_port=min_base_port, max_port=max_base_port) while (selected_port in self.selected_socks5_ports): selected_port = get_random_port(min_port...
'This method creates a torrent from a local file and saves the torrent in the session state dir. Note that the source file needs to exist.'
def create_local_torrent(self, source_file):
self.assertTrue(os.path.exists(source_file)) tdef = TorrentDef() tdef.add_content(source_file) tdef.set_tracker('http://localhost/announce') tdef.finalize() torrent_path = os.path.join(self.session.config.get_state_dir(), 'seed.torrent') tdef.save(torrent_path) return (tdef, torrent_path...
'Tests the create_torrent_file() function.'
def test_create_torrent(self):
def _on_torrent_created(result): lt_session = libtorrent.session() p = {'save_path': self._temp_dir, 'ti': libtorrent.torrent_info(result['torrent_file_path'])} handle = lt_session.add_torrent(p) self.assertTrue(handle.is_valid()) lt_session.remove_torrent(handle) del...
'create and save torrent definition used in this test file'
def create_tdef(self):
tdef = TorrentDef() sourcefn = os.path.join(TESTS_DATA_DIR, 'video.avi') tdef.add_content(sourcefn) tdef.set_tracker('http://localhost/announce') tdef.finalize() torrentfn = os.path.join(self.session.config.get_state_dir(), 'gen.torrent') tdef.save(torrentfn) return tdef
'testing call resume data alert'
@deferred(timeout=10) def test_save_resume(self):
tdef = self.create_tdef() impl = LibtorrentDownloadImpl(self.session, tdef) def resume_ready(_): '\n check if resume data is ready\n ' basename = (binascii.hexlify(tdef.get_infohash()...
'Test whether the selected files are set correctly'
def test_selected_files(self):
def mocked_set_file_prios(_): mocked_set_file_prios.called = True mocked_set_file_prios.called = False mocked_file = MockObject() mocked_file.path = 'my/path' mock_torrent_info = MockObject() mock_torrent_info.files = (lambda : [mocked_file, mocked_file]) self.libtorrent_download_imp...
'Test whether we return the right share mode when requested in the LibtorrentDownloadImpl'
def test_get_share_mode(self):
self.libtorrent_download_impl.handle.status().share_mode = False self.assertFalse(self.libtorrent_download_impl.get_share_mode()) self.libtorrent_download_impl.handle.status().share_mode = True self.assertTrue(self.libtorrent_download_impl.get_share_mode())
'Test whether we set the right share mode in LibtorrentDownloadImpl'
def test_set_share_mode(self):
def mocked_set_share_mode(val): self.assertTrue(val) mocked_set_share_mode.called = True mocked_set_share_mode.called = False self.libtorrent_download_impl.handle.set_share_mode = mocked_set_share_mode self.libtorrent_download_impl.set_share_mode(True) self.assertTrue(mocked_set_shar...
'Test whether setting the priority calls the right methods in LibtorrentDownloadImpl'
def test_set_priority(self):
def mocked_set_priority(prio): self.assertEqual(prio, 1234) mocked_set_priority.called = True mocked_set_priority.called = False self.libtorrent_download_impl.handle.set_priority = mocked_set_priority self.libtorrent_download_impl.set_priority(1234) self.assertTrue(mocked_set_priorit...
'Testing whether changing the configuration on runtime calls the right methods in LibtorrentDownloadImpl'
def test_dlconfig_cb_change(self):
def mocked_set_upload_limit(prio): self.assertEqual(prio, (3 * 1024)) mocked_set_upload_limit.called = True mocked_set_upload_limit.called = False self.libtorrent_download_impl.handle.set_upload_limit = mocked_set_upload_limit def mocked_set_download_limit(prio): self.assertEqual...
'Testing whether trackers are added to the libtorrent handler in LibtorrentDownloadImpl'
def test_add_trackers(self):
def mocked_add_trackers(tracker_info): self.assertIsInstance(tracker_info, dict) self.assertEqual(tracker_info['url'], 'http://google.com') mocked_add_trackers.called = True mocked_add_trackers.called = False self.libtorrent_download_impl.handle.add_tracker = mocked_add_trackers ...
'Testing whether error alerts are processed correctly'
def test_process_error_alert(self):
url = 'http://google.com' mock_alert = MockObject() mock_alert.msg = None mock_alert.category = (lambda : lt.alert.category_t.error_notification) mock_alert.status_code = 123 mock_alert.url = url self.libtorrent_download_impl.process_alert(mock_alert, 'tracker_error_alert') self.assertEq...
'Test whether a tracking warning alert is processed correctly'
def test_tracker_warning_alert(self):
url = 'http://google.com' mock_alert = MockObject() mock_alert.category = (lambda : lt.alert.category_t.error_notification) mock_alert.url = url mock_alert.message = (lambda : 'test') self.libtorrent_download_impl.process_alert(mock_alert, 'tracker_warning_alert') self.assertEqual(self.libto...
'Testing whether the right operations happen when we receive metadata'
@deferred(timeout=10) def test_on_metadata_received_alert(self):
test_deferred = Deferred() def mocked_checkpoint(): test_deferred.callback(None) self.libtorrent_download_impl.handle.trackers = (lambda : []) self.libtorrent_download_impl.handle.save_resume_data = (lambda : None) torrent_dict = {'name': 'test', 'piece length': 42, 'pieces': '', 'files':...
'Testing whether the right operations happen when we receive metadata but the torrent info is invalid'
def test_metadata_received_invalid_info(self):
def mocked_checkpoint(): raise RuntimeError('This code should not be reached!') self.libtorrent_download_impl.checkpoint = mocked_checkpoint self.libtorrent_download_impl.handle.get_torrent_info = (lambda : None) self.libtorrent_download_impl.on_metadata_received_alert(None)
'Testing whether the right operations happen after a torrent checked alert is received'
def test_torrent_checked_alert(self):
def mocked_pause_checkpoint(): mocked_pause_checkpoint.called = True mocked_pause_checkpoint.called = False self.libtorrent_download_impl.handle.pause = mocked_pause_checkpoint self.libtorrent_download_impl.checkpoint = mocked_pause_checkpoint mock_alert = MockObject() mock_alert.categor...
'Testing whether the right length of the content of the download is returned'
def test_get_length(self):
self.libtorrent_download_impl.length = 1234 self.assertEqual(self.libtorrent_download_impl.get_length(), 1234)
'Testing whether the right list of files is returned when fetching files from a download'
def test_get_dest_files(self):
self.libtorrent_download_impl.handle.file_priority = (lambda _: 123) mocked_file = MockObject() mocked_file.path = 'test' mock_torrent_info = MockObject() mock_torrent_info.files = (lambda : [mocked_file]) self.libtorrent_download_impl.handle.get_torrent_info = (lambda : mock_torrent_info) d...
'Testing whether the right vod file index is returned in LibtorrentDownloadImpl'
def test_get_vod_fileindex(self):
self.libtorrent_download_impl.vod_index = None self.assertEqual(self.libtorrent_download_impl.get_vod_fileindex(), (-1)) self.libtorrent_download_impl.vod_index = 42 self.assertEqual(self.libtorrent_download_impl.get_vod_fileindex(), 42)
'Testing whether the right vod file size is returned in LibtorrentDownloadImpl'
def test_get_vod_filesize(self):
mock_file_entry = MockObject() mock_file_entry.size = 42 mock_torrent_info = MockObject() mock_torrent_info.file_at = (lambda _: mock_file_entry) self.libtorrent_download_impl.handle.get_torrent_info = (lambda : mock_torrent_info) self.libtorrent_download_impl.vod_index = None self.assertEqu...
'Testing whether the right piece progress is returned in LibtorrentDownloadImpl'
def test_get_piece_progress(self):
self.assertEqual(self.libtorrent_download_impl.get_piece_progress(None), 1.0) self.libtorrent_download_impl.handle.status().pieces = [True, False] self.assertEqual(self.libtorrent_download_impl.get_piece_progress([0, 1], True), 0.5) self.libtorrent_download_impl.handle.status = (lambda : None) self....
'Testing whether the right byte progress is returned in LibtorrentDownloadImpl'
def test_get_byte_progress(self):
self.assertEqual(self.libtorrent_download_impl.get_byte_progress([((-1), 0, 0)], False), 1.0) def map_file(_dummy1, start_byte, _dummy2): res = MockObject() res.piece = int((start_byte / 250)) return res self.libtorrent_download_impl.handle.get_torrent_info().num_pieces = (lambda : 4...
'Testing whether an exception in the setup method of LibtorrentDownloadImpl is handled correctly'
def test_setup_exception(self):
self.libtorrent_download_impl.setup() self.assertIsInstance(self.libtorrent_download_impl.error, Exception)
'Testing the tracker reply alert in LibtorrentDownloadImpl'
def test_tracker_reply_alert(self):
mock_alert = MockObject() mock_alert.url = 'http://google.com' mock_alert.num_peers = 42 self.libtorrent_download_impl.on_tracker_reply_alert(mock_alert) self.assertEqual(self.libtorrent_download_impl.tracker_status['http://google.com'], [42, 'Working'])
'Testing whether the stop method in LibtorrentDownloadImpl invokes the correct method'
def test_stop(self):
def mocked_stop_remove(removestate, removecontent): self.assertFalse(removestate) self.assertFalse(removecontent) mocked_stop_remove.called = True mocked_stop_remove.called = False self.libtorrent_download_impl.stop_remove = mocked_stop_remove self.libtorrent_download_impl.stop()...
'Testing whether the right operations are performed when we get a torrent finished alert'
def test_download_finish_alert(self):
status = self.libtorrent_download_impl.handle.status() status.paused = False status.state = DLSTATUS_DOWNLOADING status.progress = 0.9 status.error = None status.total_wanted = 33 status.download_payload_rate = 928 status.upload_payload_rate = 928 status.all_time_upload = 42 stat...
'Testing whether a correct pieces bitmask is returned when requested'
def test_get_pieces_bitmask(self):
self.libtorrent_download_impl.handle.status().pieces = [True, False, True, False, False] self.assertEqual(self.libtorrent_download_impl.get_pieces_base64(), 'oA==') self.libtorrent_download_impl.handle.status().pieces = [(True * 16)] self.assertEqual(self.libtorrent_download_impl.get_pieces_base64(), 'g...
'Testing whether the correct operations happen when an error is raised during resume data saving'
@deferred(timeout=10) def test_resume_data_failed(self):
test_deferred = Deferred() def on_error(_): test_deferred.callback(None) mock_alert = MockObject() mock_alert.msg = 'test error' self.libtorrent_download_impl.deferreds_resume.append(Deferred().addErrback(self.libtorrent_download_impl._on_resume_err).addCallback(on_error)) self.libtor...
'Testing the metainfo fetching method when the DHT is not ready'
def test_get_metainfo_not_ready(self):
self.ltmgr.initialize() self.assertFalse(self.ltmgr.get_metainfo(('a' * 20), None))
'Testing the metainfo fetching method'
@deferred(timeout=20) def test_get_metainfo(self):
test_deferred = Deferred() def metainfo_cb(metainfo): self.assertEqual(metainfo, 'test') test_deferred.callback(None) self.ltmgr.initialize() self.ltmgr.is_dht_ready = (lambda : True) self.ltmgr.metainfo_cache[('a' * 20).encode('hex')] = {'meta_info': 'test'} self.ltmgr.get_metai...
'Testing whether the callback is correctly invoked when we received metainfo'
@deferred(timeout=20) def test_got_metainfo(self):
test_deferred = Deferred() self.ltmgr.initialize() def metainfo_cb(metainfo): self.assertDictEqual(metainfo, {'info': {'pieces': ['a']}, 'leechers': 0, 'nodes': [], 'seeders': 0, 'initial peers': []}) test_deferred.callback(None) fake_handle = MockObject() torrent_info = MockObjec...
'Testing whether the callback is correctly invoked when we received metainfo after timeout'
@deferred(timeout=20) def test_got_metainfo_timeout(self):
test_deferred = Deferred() def metainfo_timeout_cb(metainfo): self.assertEqual(metainfo, ('a' * 20)) test_deferred.callback(None) fake_handle = MockObject() self.ltmgr.initialize() self.ltmgr.metainfo_requests[('a' * 20).encode('hex')] = {'handle': fake_handle, 'timeout_callbacks': [...
'Testing the addition of a torrent to the libtorrent manager'
def test_add_torrent(self):
mock_handle = MockObject() mock_handle.info_hash = (lambda : ('a' * 20)) mock_ltsession = MockObject() mock_ltsession.add_torrent = (lambda _: mock_handle) mock_ltsession.stop_upnp = (lambda : None) mock_ltsession.save_state = (lambda : None) self.ltmgr.get_session = (lambda *_: mock_ltsessi...
'Testing whether starting the download of a corrupt torrent file raises an exception'
def test_start_download_corrupt(self):
self.ltmgr.metadata_tmpdir = tempfile.mkdtemp(suffix=u'tribler_metainfo_tmpdir') corrupt_file = os.path.join(self.LIBTORRENT_FILES_DIR, 'corrupt_torrent.torrent') self.assertRaises(TorrentFileException, self.ltmgr.start_download, torrentfilename=corrupt_file)
'Test the starting of a download when there are no new trackers'
def test_start_download_duplicate(self):
mock_tdef = MockObject() mock_tdef.get_infohash = (lambda : ('a' * 20)) mock_tdef.get_trackers_as_single_tuple = (lambda : tuple()) mock_download = MockObject() mock_download.get_def = (lambda : mock_tdef) self.tribler_session.get_download = (lambda _: mock_download) self.ltmgr.tribler_sessi...
'Test setting the proxy settings'
def test_set_proxy_settings(self):
def on_proxy_set(settings): self.assertTrue(settings) self.assertEqual(settings.hostname, 'a') self.assertEqual(settings.port, 1234) self.assertEqual(settings.username, 'abc') self.assertEqual(settings.password, 'def') mock_lt_session = MockObject() mock_lt_session.se...