def subtest_old_tribler(self): # The tracker gives Tribler try: self.myss.settimeout(10.0) conn, addr = self.myss.accept() options = '\x00\x00\x00\x00\x00\x10\x00\x00' s = BTConnection('', 0, conn, user_option_pattern=options, user_infohash=self.infohash, myid=self.myid) s.read_handshake_medium_rare() # Seeing that we're an T<=3.5.0 peer, he should directly # initiate an overlay conn with us conn2, addr2 = self.myss.accept() s2 = OLConnection(self.my_keypair, '', 0, conn2, self.mylistenport) time.sleep(5) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close() s2.send('bla') s2.close() print >> sys.stderr, "test: Good, Tribler made overlay conn with us" except socket.timeout: print >> sys.stderr, "test: Bad, Tribler did not attempt to start an overlay conn with us" self.assert_(False)
def subtest_good_friendship_stats(self): """ Send a valid message-id from a registered crawler peer """ print >> sys.stderr, "-" * 80, "\ntest: good friendship stats" s = OLConnection(self.my_keypair, "localhost", self.hisport) t = time.time() - 100.0 msg_dict = {'current time': int(t)} payload = bencode(msg_dict) self.send_crawler_request(s, CRAWLER_FRIENDSHIP_STATS, 0, 0, payload) error, payload = self.receive_crawler_reply(s, CRAWLER_FRIENDSHIP_STATS, 0) assert error == 0 d = bdecode(payload) if DEBUG: print >> sys.stderr, "test: Got FRIENDSHIPSTATISTICS", ` d ` stats = d['stats'] self.assert_(len(stats) == 1) record = d['stats'][0] self.assert_(record[0] == bin2str(self.his_permid)) # source_permid self.assert_(record[1] == bin2str(self.some_permid)) # target_permid self.assert_(record[2] == 0) # isForwarder time.sleep(1) s.close()
def subtest_dialback(self): """ Send a valid request, disconnect, and wait for an incomming connection with the reply """ print >>sys.stderr, "-"*80, "\ntest: dialback" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) s = OLConnection(self.my_keypair, "localhost", self.hisport, mylistenport=self.listen_port) self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 42, 0, "SELECT * FROM peer") s.close() # wait for reply try: conn, addr = self.listen_socket.accept() except socket.timeout: if DEBUG: print >> sys.stderr,"test_crawler: timeout, bad, peer didn't connect to send the crawler reply" assert False, "test_crawler: timeout, bad, peer didn't connect to send the crawler reply" s = OLConnection(self.my_keypair, "", 0, conn, mylistenport=self.listen_port) # read reply error, payload = self.receive_crawler_reply(s, CRAWLER_DATABASE_QUERY, 42) assert error == 0 if DEBUG: print >>sys.stderr, cPickle.loads(payload) time.sleep(1)
def subtest_invalid_frequency(self): """ Send two valid requests shortly after each other. However, indicate that the frequency should be large. This should result in a frequency error """ print >>sys.stderr, "-"*80, "\ntest: invalid_invalid_frequency" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) s = OLConnection(self.my_keypair, "localhost", self.hisport) self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 42, 0, "SELECT * FROM peer") error, payload = self.receive_crawler_reply(s, CRAWLER_DATABASE_QUERY, 42) assert error == 0 # try on the same connection self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 42, 1000, "SELECT * FROM peer") error, payload = self.receive_crawler_reply(s, CRAWLER_DATABASE_QUERY, 42) assert error == 254 # should give a frequency erro s.close() # try on a new connection s = OLConnection(self.my_keypair, "localhost", self.hisport) self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 42, 1000, "SELECT * FROM peer") error, payload = self.receive_crawler_reply(s, CRAWLER_DATABASE_QUERY, 42) assert error == 254 # should give a frequency error time.sleep(1) s.close()
def subtest_dialback(self): """ Send a valid request, disconnect, and wait for an incomming connection with the reply """ print >>sys.stderr, "-"*80, "\ntest: dialback" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) s = OLConnection(self.my_keypair, "localhost", self.hisport, mylistenport=self.listen_port) self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 42, 0, "SELECT * FROM peer") s.close() # wait for reply try: conn, addr = self.listen_socket.accept() except socket.timeout: if DEBUG: print >> sys.stderr,"test_crawler: timeout, bad, peer didn't connect to send the crawler reply" assert False, "test_crawler: timeout, bad, peer didn't connect to send the crawler reply" s = OLConnection(self.my_keypair, "", 0, conn, mylistenport=self.listen_port) # read reply error, payload = self.receive_crawler_reply(s, CRAWLER_DATABASE_QUERY, 42) assert error == 0 if DEBUG: print >>sys.stderr, cPickle.loads(payload) time.sleep(1)
def singtest_connect_overlay(self): """ """ # 1. Accept the data connection Tribler wants to establish with us self.myss.settimeout(10.0) conn, addr = self.myss.accept() s = BTConnection('',0,conn,user_infohash=self.infohash,myid=self.myid) s.read_handshake_medium_rare() extmsg = self.create_good_tribler_extend_hs() s.send(extmsg) resp = s.recv() self.assert_(len(resp) > 0) print >> sys.stderr,"test: Data conn replies",getMessageName(resp[0]) # 2. Tribler should now try to establish an overlay connection with us self.myss.settimeout(10.0) conn, addr = self.myss.accept() options = '\x00\x00\x00\x00\x00\x00\x00\x00' s2 = OLConnection(self.my_keypair,'',0,conn,mylistenport=self.mylistenport) # Desired behaviour is that the accept() succeeds. If not it will time # out, and throw an exception, causing this test to fail. time.sleep(3) s.close() s2.close()
def _test_bad(self,gen_drequest_func): s = OLConnection(self.my_keypair,'localhost',self.hisport) print >> sys.stderr,"\ntest: ",gen_drequest_func msg = gen_drequest_func() s.send(msg) time.sleep(5) # the other side should not like this and close the connection resp = s.recv() self.assert_(len(resp)==0) s.close() # However, closing the connection is the specified behaviour, check # that he doesn't connect back try: self.myss.settimeout(10.0) print >> sys.stderr,"test: See if peer connects back (would be bad)" conn, addr = self.myss.accept() s = BTConnection('',0,conn,mylistenport=self.mylistenport,user_infohash=dialback_infohash) s.read_handshake_medium_rare() resp = s.recv() print >> sys.stderr,"test: Got reply back, len",len(resp),"see if expected" self.assert_(len(resp) > 0) self.assert_(resp[0] != DIALBACK_REPLY) print >> sys.stderr,"test: Reply was acceptable",getMessageName(resp[0]) except socket.timeout: self.assert_(True) print >> sys.stderr,"test: Good, accept() timed out"
def singtest_connect_overlay(self): """ """ # 1. Accept the data connection Tribler wants to establish with us self.myss.settimeout(10.0) conn, addr = self.myss.accept() s = BTConnection('',0,conn,user_infohash=self.infohash,myid=self.myid) s.read_handshake_medium_rare() extmsg = self.create_good_tribler_extend_hs() s.send(extmsg) resp = s.recv() self.assert_(len(resp) > 0) print >> sys.stderr,"test: Data conn replies",getMessageName(resp[0]) # 2. Tribler should now try to establish an overlay connection with us self.myss.settimeout(10.0) conn, addr = self.myss.accept() options = '\x00\x00\x00\x00\x00\x00\x00\x00' s2 = OLConnection(self.my_keypair,'',0,conn,mylistenport=self.mylistenport) # Desired behaviour is that the accept() succeeds. If not it will time # out, and throw an exception, causing this test to fail. time.sleep(3) s.close() s2.close()
def subtest_invalid_permid(self): """ Send crawler messages from a non-crawler peer """ print >> sys.stderr, "-" * 80, "\ntest: invalid_permid" # make sure that the OLConnection is NOT in the crawler_db crawler_db = CrawlerDBHandler.getInstance() assert not self.my_permid in crawler_db.getCrawlers() # We are not a registered crawler, any request from us should # be denied messages = [ CRAWLER_REQUEST, CRAWLER_REQUEST + CRAWLER_DATABASE_QUERY, CRAWLER_REQUEST + CRAWLER_DATABASE_QUERY, CRAWLER_REQUEST + chr(0) ] for msg in messages: s = OLConnection(self.my_keypair, "localhost", self.hisport) s.send(msg) response = s.recv() assert response == "", "response type is %s" % getMessageName( response[0]) time.sleep(1) s.close()
def subtest_invalid_sql_query(self): """ Send an invalid sql query from a registered crawler peer """ print >>sys.stderr, "-"*80, "\ntest: invalid_sql_query" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) s = OLConnection(self.my_keypair, "localhost", self.hisport) queries = ["FOO BAR"] for query in queries: self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 0, 0, query) error, payload = self.receive_crawler_reply(s, CRAWLER_DATABASE_QUERY, 0) assert error == 1 if DEBUG: print >>sys.stderr, payload time.sleep(1) s.close()
def subtest_invalid_sql_query(self): """ Send an invalid sql query from a registered crawler peer """ print >> sys.stderr, "-" * 80, "\ntest: invalid_sql_query" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) s = OLConnection(self.my_keypair, "localhost", self.hisport) queries = ["FOO BAR"] for query in queries: self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 0, 0, query) error, payload = self.receive_crawler_reply( s, CRAWLER_DATABASE_QUERY, 0) assert error == 1 if DEBUG: print >> sys.stderr, payload time.sleep(1) s.close()
def subtest_valid_messageid(self): """ Send a valid message-id from a registered crawler peer """ print >> sys.stderr, "-" * 80, "\ntest: valid_messageid" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) s = OLConnection(self.my_keypair, "localhost", self.hisport) queries = [ "SELECT name FROM category", "SELECT * FROM peer", "SELECT * FROM torrent" ] for query in queries: self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 0, 0, query) error, payload = self.receive_crawler_reply( s, CRAWLER_DATABASE_QUERY, 0) assert error == 0 if DEBUG: print >> sys.stderr, cPickle.loads(payload) time.sleep(1) s.close()
def subtest_good_buddycast(self,oversion): """ test good BUDDYCAST messages """ print >>sys.stderr,"test: good BUDDYCAST",oversion s = OLConnection(self.my_keypair,'localhost',self.hisport,myoversion=oversion) msg = self.create_good_buddycast_payload(oversion) s.send(msg) s.b.s.settimeout(60.0) try: while True: resp = s.recv() self.assert_(len(resp) > 0) print >>sys.stderr,"test: good BUDDYCAST: Got reply",getMessageName(resp[0]) if resp[0] == BUDDYCAST: break elif resp[0] == GET_METADATA: self.check_get_metadata(resp[1:]) elif resp[0] == KEEP_ALIVE: if oversion >= 3: self.check_keep_alive(resp[1:]) else: print >> sys.stderr,"test: Tribler sent KEEP_ALIVE, not allowed in olproto ver",oversion self.assert_(False) except socket.timeout: print >> sys.stderr,"test: Timeout, bad, peer didn't reply with BUDDYCAST message" self.assert_(False) self.check_buddycast(resp[1:],oversion) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_good_friendship_stats(self): """ Send a valid message-id from a registered crawler peer """ print >>sys.stderr, "-"*80, "\ntest: good friendship stats" s = OLConnection(self.my_keypair, "localhost", self.hisport) t = time.time() - 100.0 msg_dict = {'current time':int(t)} payload = bencode(msg_dict) self.send_crawler_request(s, CRAWLER_FRIENDSHIP_STATS, 0, 0, payload) error, payload = self.receive_crawler_reply(s, CRAWLER_FRIENDSHIP_STATS, 0) assert error == 0 d = bdecode(payload) if DEBUG: print >>sys.stderr, "test: Got FRIENDSHIPSTATISTICS",`d` stats = d['stats'] self.assert_(len(stats) == 1) record = d['stats'][0] self.assert_(record[0] == bin2str(self.his_permid)) # source_permid self.assert_(record[1] == bin2str(self.some_permid)) # target_permid self.assert_(record[2] == 0) # isForwarder time.sleep(1) s.close()
def subtest_invalid_frequency(self): """ Send two valid requests shortly after each other. However, indicate that the frequency should be large. This should result in a frequency error """ print >>sys.stderr, "-"*80, "\ntest: invalid_invalid_frequency" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) s = OLConnection(self.my_keypair, "localhost", self.hisport) self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 42, 0, "SELECT * FROM peer") error, payload = self.receive_crawler_reply(s, CRAWLER_DATABASE_QUERY, 42) assert error == 0 # try on the same connection self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 42, 1000, "SELECT * FROM peer") error, payload = self.receive_crawler_reply(s, CRAWLER_DATABASE_QUERY, 42) assert error == 254 # should give a frequency erro s.close() # try on a new connection s = OLConnection(self.my_keypair, "localhost", self.hisport) self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 42, 1000, "SELECT * FROM peer") error, payload = self.receive_crawler_reply(s, CRAWLER_DATABASE_QUERY, 42) assert error == 254 # should give a frequency error time.sleep(1) s.close()
def subtest_valid_nat_check(self): """ Send a CRAWLER_NATCHECK message to the Tribler instance. A reply containing a nat type should be returned. """ print >>sys.stderr, "-"*80, "\ntest: subtest_valid_nat_check" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) s = OLConnection(self.my_keypair, "localhost", self.hisport, mylistenport=self.listen_port) self.send_crawler_request(s, CRAWLER_NATCHECK, 42, 0, "") s.close() if DEBUG: print >>sys.stderr, "test_natcheck: the nat-check code allows for a 10 minute delay in reporting the nat stats" self.listen_socket.settimeout(11 * 60) # wait for reply try: conn, addr = self.listen_socket.accept() except socket.timeout: if DEBUG: print >> sys.stderr,"test_natcheck: timeout, bad, peer didn't connect to send the crawler reply" assert False, "test_natcheck: timeout, bad, peer didn't connect to send the crawler reply" s = OLConnection(self.my_keypair, "", 0, conn, mylistenport=self.listen_port) # read reply error, payload = self.receive_crawler_reply(s, CRAWLER_NATCHECK, 42) assert error == 0 if DEBUG: print >>sys.stderr, "test_natcheck:", bdecode(payload) time.sleep(1)
def test_channelcast(self): torrent_data = {'announce':"http://localhost", 'info':{'name':'Hello 123', 'files':[{'length':100, 'path':['license.txt']}]}} infohash = bin2str(sha(bencode(torrent_data['info'])).digest()) self.channelcastdb.addOwnTorrent(infohash,torrent_data) s = OLConnection(self.my_keypair,'localhost',self.hisport) chcast = ChannelCastCore(None, s, self.session, None, log = '', dnsindb = None) # Good message chdata = chcast.createChannelCastMessage() if chdata is None or len(chdata) ==0: print "test: no subscriptions for us.. hence do not send" else: msg = CHANNELCAST + bencode(chdata) print "test: channelcast msg created", repr(chdata) s.send(msg) time.sleep(3) # Bad message if chdata is None or len(chdata)==0: pass else: pub_id, pub_name, infohash, torrenthash, name, timestamp, signature = chdata[0] chdata = [(pub_id, pub_name, infohash, torrenthash, name, 12343, signature)] msg = CHANNELCAST + bencode(chdata) print "test: channelcast msg created", repr(chdata) s.send(msg) time.sleep(20) # the other side should have closed the connection, as it is invalid message s.close()
def subtest_old_tribler(self): # The tracker gives Tribler try: self.myss.settimeout(10.0) conn, addr = self.myss.accept() options = '\x00\x00\x00\x00\x00\x10\x00\x00' s = BTConnection('',0,conn,user_option_pattern=options,user_infohash=self.infohash,myid=self.myid) s.read_handshake_medium_rare() # Seeing that we're an T<=3.5.0 peer, he should directly # initiate an overlay conn with us conn2, addr2 = self.myss.accept() s2 = OLConnection(self.my_keypair,'',0,conn2,self.mylistenport) time.sleep(5) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close() s2.send('bla') s2.close() print >> sys.stderr,"test: Good, Tribler made overlay conn with us" except socket.timeout: print >> sys.stderr,"test: Bad, Tribler did not attempt to start an overlay conn with us" self.assert_(False)
def _test_bad(self, gen_drequest_func): s = OLConnection(self.my_keypair, 'localhost', self.hisport) print >> sys.stderr, "\ntest: ", gen_drequest_func msg = gen_drequest_func() s.send(msg) time.sleep(5) # the other side should not like this and close the connection resp = s.recv() self.assert_(len(resp) == 0) s.close() # However, closing the connection is the specified behaviour, check # that he doesn't connect back try: self.myss.settimeout(10.0) print >> sys.stderr, "test: See if peer connects back (would be bad)" conn, addr = self.myss.accept() s = BTConnection('', 0, conn, mylistenport=self.mylistenport, user_infohash=dialback_infohash) s.read_handshake_medium_rare() resp = s.recv() print >> sys.stderr, "test: Got reply back, len", len( resp), "see if expected" self.assert_(len(resp) > 0) self.assert_(resp[0] != DIALBACK_REPLY) print >> sys.stderr, "test: Reply was acceptable", getMessageName( resp[0]) except socket.timeout: self.assert_(True) print >> sys.stderr, "test: Good, accept() timed out"
def subtest_bad_votecast(self, vdata): s = OLConnection(self.my_keypair,'localhost',self.hisport) vcast = VoteCastCore(None, s, self.session, None, log = '', dnsindb = None) print >> sys.stderr, "Test Bad VoteCast", `vdata` msg = VOTECAST+bencode(vdata) s.send(msg) self.assert_(len(s.recv())==0) s.close()
def subtest_bad_channelcast(self, chdata): s = OLConnection(self.my_keypair,'localhost',self.hisport) chcast = ChannelCastCore(None, s, self.session, None, log = '', dnsindb = None) print >> sys.stderr, "Test Bad ChannelCast", `chdata` msg = CHANNELCAST+bencode(chdata) s.send(msg) self.assert_(len(s.recv())==0) s.close()
def subtest_bad_votecast(self, vdata): s = OLConnection(self.my_keypair, 'localhost', self.hisport) vcast = VoteCastCore(None, s, self.session, None, log='', dnsindb=None) print >> sys.stderr, "Test Bad VoteCast", ` vdata ` msg = VOTECAST + bencode(vdata) s.send(msg) self.assert_(len(s.recv()) == 0) s.close()
def _test_bad(self, gen_soverlap_func): print >> sys.stderr, "test: bad SOCIAL_OVERLAP", gen_soverlap_func s = OLConnection(self.my_keypair, 'localhost', self.hisport) msg = gen_soverlap_func() s.send(msg) time.sleep(5) # the other side should not like this and close the connection self.assert_(len(s.recv()) == 0) s.close()
def _test_bad(self,gen_friendship_func): print >>sys.stderr,"test: bad friendship",gen_friendship_func s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = gen_friendship_func() s.send(msg) time.sleep(5) # the other side should not like this and close the connection self.assert_(len(s.recv())==0) s.close()
def _test_bad(self,gen_soverlap_func): print >>sys.stderr,time.asctime(),'-', "test: bad SOCIAL_OVERLAP",gen_soverlap_func s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = gen_soverlap_func() s.send(msg) time.sleep(5) # the other side should not like this and close the connection self.assert_(len(s.recv())==0) s.close()
def subtest_channelcast(self): print >> sys.stderr, "test: channelcast----------------------" s = OLConnection(self.my_keypair, 'localhost', self.hisport) chcast = ChannelCastCore(None, s, self.session, None, log='', dnsindb=None) #Send Empty ChannelCast message chdata = {} print >> sys.stderr, "Test Good ChannelCast", ` chdata ` msg = CHANNELCAST + bencode(chdata) s.send(msg) resp = s.recv() if len(resp) > 0: print >> sys.stderr, "test: channelcast: got", getMessageName( resp[0]) self.assert_(resp[0] == CHANNELCAST) print >> sys.stderr, "test: channelcast: got msg", ` bdecode( resp[1:]) ` chdata_rcvd = bdecode(resp[1:]) self.assert_(validChannelCastMsg(chdata_rcvd) == True) s.close() #Now, send a bad ChannelCast message. # The other side should close the connection # Create bad message by manipulating a good one #bad infohash chdata = deepcopy(chdata_rcvd) for k, v in chdata.items(): v['infohash'] = 234 self.subtest_bad_channelcast(chdata) #bad torrentname chdata = deepcopy(chdata_rcvd) for k, v in chdata.items(): v['torrentname'] = 1231 self.subtest_bad_channelcast(chdata) #bad signature.. temporarily disabled. # Got to enable when signature validation in validChannelCastMsg are enabled # chdata = deepcopy(chdata_rcvd) # value_list = chdata.values() # if len(value_list)>0: # chdata['sdfg234sadf'] = value_list[0] # self.subtest_bad_channelcast(chdata) #Bad message format chdata = {'2343ww34': ''} self.subtest_bad_channelcast(chdata) #Bad print >> sys.stderr, "End of channelcast test---------------------------"
def _test_dreply(self, gen_dreply, good, diff_ips_test=False): for i in range(self.NLISTENERS): print >> sys.stderr, "test: waiting for #", i, "listenport", self.mylistenport[ i] conn, addr = self.myss[i].accept() s = OLConnection(self.mykeypairs[i], '', 0, conn, self.mylistenport[i]) while True: msg = s.recv() self.assert_(len(msg) > 0) print >> sys.stderr, "test: Received overlay message", getMessageName( msg[0]) if msg[0] == DIALBACK_REQUEST: break self.assert_(msg[0] == DIALBACK_REQUEST) self.check_drequest(msg[1:]) # Proper behaviour is to try to send a reply using a new return connection s2 = BTConnection('localhost', self.hisport, mylistenport=self.mylistenport[i], user_infohash=dialback_infohash) s2.read_handshake_medium_rare(close_ok=True) if gen_dreply is not None: resp = gen_dreply(i) print >> sys.stderr, "test: sending DIALBACK_REPLY #", i s2.send(resp) time.sleep(2) # the other side should always close the # connection, either because we're done or he didn't like our # bad DIALBACK_REPLY message msg = s2.recv() if len(msg) > 0: print >> sys.stderr, "test: Received unexpected data", getMessageName( msg[0]) self.assert_(len(msg) == 0) s2.close() # Not really necessary, but helps with test_dialback_active2 s.close() ext_ip = self.session.get_external_ip() print >> sys.stderr, "test: External IP address after test is", ext_ip if diff_ips_test: if self.config.sessconfig['dialback_trust_superpeers'] == 1: good = True else: good = False if good: self.assert_(ext_ip == REPLY_IP) else: self.assert_(ext_ip == self.myoriginalip)
def test_votecast(self): self.votecastdb.subscribe('nitin') s = OLConnection(self.my_keypair,'localhost',self.hisport) votecast = VoteCastCore(None, s, self.session, None, log = '', dnsindb = None) data = votecast.createVoteCastMessage() if data is None and len(data)==0: print >>sys.stderr, "test: no votes" else: msg = VOTECAST + bencode(data) s.send(msg) s.close()
def subtest_good_tribler_g2g_v2(self): self._test_good(self.create_good_tribler_extend_hs_v2,infohash=self.infohash) # We've said we're a Tribler peer, and we initiated the connection, so # now *we* should now try to establish an overlay-swarm connection. s = OLConnection(self.my_keypair,'localhost',self.hisport,mylistenport=self.mylistenport) # the connection should be intact, so this should not throw an # exception: time.sleep(5) s.send('bla') s.close()
def _test_bad(self,gen_soverlap_func): print >>sys.stderr,"test: bad BUDDYCAST",gen_soverlap_func s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = gen_soverlap_func() s.send(msg) time.sleep(5) # the other side should not like this and close the connection x = s.recv() print "response: %s" % x self.assert_(len(x)==0) s.close()
def subtest_bad_channelcast(self, chdata): s = OLConnection(self.my_keypair, 'localhost', self.hisport) chcast = ChannelCastCore(None, s, self.session, None, log='', dnsindb=None) print >> sys.stderr, "Test Bad ChannelCast", ` chdata ` msg = CHANNELCAST + bencode(chdata) s.send(msg) self.assert_(len(s.recv()) == 0) s.close()
def test_channel_update(self): s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = CHANNEL_QUERY+bencode({'q':'p:345fsdf34fe345ed344g5', 'id': 'a' * 20}) s.send(msg) resp = s.recv() if len(resp) > 0: print >>sys.stderr,"test: good CH_QUERY: got",getMessageName(resp[0]) self.assert_(resp[0] == CHANNEL_QUERY_REPLY) self.check_chquery_reply(resp[1:]) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_channelcast(self): print >>sys.stderr,"test: channelcast----------------------" s = OLConnection(self.my_keypair,'localhost',self.hisport) chcast = ChannelCastCore(None, s, self.session, None, log = '', dnsindb = None) #Send Empty ChannelCast message chdata = {} print >> sys.stderr, "Test Good ChannelCast", `chdata` msg = CHANNELCAST+bencode(chdata) s.send(msg) resp = s.recv() if len(resp) > 0: print >>sys.stderr,"test: channelcast: got",getMessageName(resp[0]) self.assert_(resp[0]==CHANNELCAST) print >>sys.stderr, "test: channelcast: got msg", `bdecode(resp[1:])` chdata_rcvd = bdecode(resp[1:]) self.assert_(validChannelCastMsg(chdata_rcvd)==True) s.close() #Now, send a bad ChannelCast message. # The other side should close the connection # Create bad message by manipulating a good one #bad infohash chdata = deepcopy(chdata_rcvd) for k,v in chdata.items(): v['infohash'] = 234 self.subtest_bad_channelcast(chdata) #bad torrentname chdata = deepcopy(chdata_rcvd) for k,v in chdata.items(): v['torrentname'] = 1231 self.subtest_bad_channelcast(chdata) #bad signature.. temporarily disabled. # Got to enable when signature validation in validChannelCastMsg are enabled # chdata = deepcopy(chdata_rcvd) # value_list = chdata.values() # if len(value_list)>0: # chdata['sdfg234sadf'] = value_list[0] # self.subtest_bad_channelcast(chdata) #Bad message format chdata = {'2343ww34':''} self.subtest_bad_channelcast(chdata) #Bad print>>sys.stderr, "End of channelcast test---------------------------"
def _test_dreply(self,gen_dreply,good,diff_ips_test=False): for i in range(self.NLISTENERS): print >> sys.stderr,"test: waiting for #",i,"listenport",self.mylistenport[i] conn, addr = self.myss[i].accept() s = OLConnection(self.mykeypairs[i],'',0,conn,self.mylistenport[i]) while True: msg = s.recv() self.assert_(len(msg) > 0) print >> sys.stderr,"test: Received overlay message",getMessageName(msg[0]) if msg[0] == DIALBACK_REQUEST: break self.assert_(msg[0] == DIALBACK_REQUEST) self.check_drequest(msg[1:]) # Proper behaviour is to try to send a reply using a new return connection s2 = BTConnection('localhost',self.hisport,mylistenport=self.mylistenport[i],user_infohash=dialback_infohash) s2.read_handshake_medium_rare(close_ok = True) if gen_dreply is not None: resp = gen_dreply(i) print >> sys.stderr,"test: sending DIALBACK_REPLY #",i s2.send(resp) time.sleep(2) # the other side should always close the # connection, either because we're done or he didn't like our # bad DIALBACK_REPLY message msg = s2.recv() if len(msg) > 0: print >> sys.stderr,"test: Received unexpected data",getMessageName(msg[0]) self.assert_(len(msg)==0) s2.close() # Not really necessary, but helps with test_dialback_active2 s.close() ext_ip = self.session.get_external_ip() print >>sys.stderr,"test: External IP address after test is",ext_ip if diff_ips_test: if self.config.sessconfig['dialback_trust_superpeers'] == 1: good = True else: good = False if good: self.assert_(ext_ip == REPLY_IP) else: self.assert_(ext_ip == self.myoriginalip)
def test_channelcast(self): torrent_data = { 'announce': "http://localhost", 'info': { 'name': 'Hello 123', 'files': [{ 'length': 100, 'path': ['license.txt'] }] } } infohash = bin2str(sha(bencode(torrent_data['info'])).digest()) self.channelcastdb.addOwnTorrent(infohash, torrent_data) s = OLConnection(self.my_keypair, 'localhost', self.hisport) chcast = ChannelCastCore(None, s, self.session, None, log='', dnsindb=None) # Good message chdata = chcast.createChannelCastMessage() if chdata is None or len(chdata) == 0: print "test: no subscriptions for us.. hence do not send" else: msg = CHANNELCAST + bencode(chdata) print "test: channelcast msg created", repr(chdata) s.send(msg) time.sleep(3) # Bad message if chdata is None or len(chdata) == 0: pass else: pub_id, pub_name, infohash, torrenthash, name, timestamp, signature = chdata[ 0] chdata = [(pub_id, pub_name, infohash, torrenthash, name, 12343, signature)] msg = CHANNELCAST + bencode(chdata) print "test: channelcast msg created", repr(chdata) s.send(msg) time.sleep(20) # the other side should have closed the connection, as it is invalid message s.close()
def subtest_good_soverlap(self): """ test good SOCIAL_OVERLAP messages """ print >> sys.stderr, "test: good SOCIAL_OVERLAP" s = OLConnection(self.my_keypair, 'localhost', self.hisport) msg = self.create_good_soverlap() s.send(msg) resp = s.recv() self.assert_(resp[0] == SOCIAL_OVERLAP) self.check_soverlap(resp[1:]) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_good_soverlap(self,name): """ test good SOCIAL_OVERLAP messages """ print >>sys.stderr,time.asctime(),'-', "test: good SOCIAL_OVERLAP" s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = self.create_good_soverlap(name) s.send(msg) resp = s.recv() self.assert_(resp[0] == SOCIAL_OVERLAP) self.check_soverlap(resp[1:]) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def test_votecast(self): self.votecastdb.subscribe('nitin') s = OLConnection(self.my_keypair, 'localhost', self.hisport) votecast = VoteCastCore(None, s, self.session, None, log='', dnsindb=None) data = votecast.createVoteCastMessage() if data is None and len(data) == 0: print >> sys.stderr, "test: no votes" else: msg = VOTECAST + bencode(data) s.send(msg) s.close()
def test_channel_query(self): s = OLConnection(self.my_keypair, 'localhost', self.hisport) msg = CHANNEL_QUERY + bencode({'q': 'k:dutiet', 'id': 'a' * 20}) s.send(msg) resp = s.recv() if len(resp) > 0: print >> sys.stderr, "test: good CH_QUERY: got", getMessageName( resp[0]) self.assert_(resp[0] == CHANNEL_QUERY_REPLY) self.check_chquery_reply(resp[1:]) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: #s.send('bla') s.close()
def subtest_good_bartercast(self): """ test good BARTERCAST messages """ print >>sys.stderr,"test: good BARTERCAST" s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = self.create_good_bartercast() s.send(msg) resp = s.recv() self.assert_(resp[0] == BARTERCAST) self.check_bartercast(resp[1:]) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_channel_keyword_query(self,nickname): print >>sys.stderr,"test: chquery keyword-----------------------------" s = OLConnection(self.my_keypair,'localhost',self.hisport) data = {} uq = u'CHANNEL k '+nickname data['q'] = uq.encode("UTF-8") data['id'] = 'b' * 20 msg = QUERY + bencode(data) s.send(msg) resp = s.recv() #print >> sys.stderr, "printing resp", resp if len(resp) > 0: print >>sys.stderr,"test: chquery: got",getMessageName(resp[0]) self.assert_(resp[0]==QUERY_REPLY) self.check_chquery_reply(resp[1:],nickname) print >>sys.stderr,"test:",`bdecode(resp[1:])` s.close()
def subtest_channel_permid_query(self, nickname): print >> sys.stderr, "test: chquery permid-----------------------------" s = OLConnection(self.my_keypair, "localhost", self.hisport) data = {} uq = u"CHANNEL p " + bin2str(self.hispermid) data["q"] = uq.encode("UTF-8") data["id"] = "b" * 20 msg = QUERY + bencode(data) s.send(msg) resp = s.recv() # print >> sys.stderr, "printing resp", resp if len(resp) > 0: print >> sys.stderr, "test: chquery: got", getMessageName(resp[0]) self.assert_(resp[0] == QUERY_REPLY) self.check_chquery_reply(resp[1:], nickname) print >> sys.stderr, "test:", ` bdecode(resp[1:]) ` s.close()
def subtest_channel_keyword_query(self, nickname): print >> sys.stderr, "test: chquery keyword-----------------------------" s = OLConnection(self.my_keypair, 'localhost', self.hisport) data = {} uq = u'CHANNEL k ' + nickname data['q'] = uq.encode("UTF-8") data['id'] = 'b' * 20 msg = QUERY + bencode(data) s.send(msg) resp = s.recv() #print >> sys.stderr, "printing resp", resp if len(resp) > 0: print >> sys.stderr, "test: chquery: got", getMessageName(resp[0]) self.assert_(resp[0] == QUERY_REPLY) self.check_chquery_reply(resp[1:], nickname) print >> sys.stderr, "test:", ` bdecode(resp[1:]) ` s.close()
def subtest_good_bartercast(self): """ test good BARTERCAST messages """ print >> sys.stderr, "test: good BARTERCAST" s = OLConnection(self.my_keypair, 'localhost', self.hisport) msg = self.create_good_bartercast() s.send(msg) resp = s.recv() print >> sys.stderr, "test: reply message", getMessageName(resp[0]) self.assert_(resp[0] == BARTERCAST) self.check_bartercast(resp[1:]) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_good_friendship_req(self, mtype, fwd=None, mresp=None, socover=False, expectreply=True): print >> sys.stderr, "test: good FRIENDSHIP", mtype, fwd s = OLConnection(self.my_keypair, 'localhost', self.hisport) if socover: tso = TestSocialOverlap("test_all") msg = tso.create_good_soverlap() s.send(msg) msg = self.create_good_friendship_payload(mtype, fwd, mresp) s.send(msg) s.b.s.settimeout(10.0) try: while True: resp = s.recv() self.assert_(len(resp) > 0) print >> sys.stderr, "test: good FRIENDSHIP: Got reply", getMessageName( resp[0]) if resp[0] == FRIENDSHIP: break elif resp[0] == SOCIAL_OVERLAP: pass else: self.assert_(False) except socket.timeout: if expectreply: print >> sys.stderr, "test: Timeout, bad, peer didn't reply with FRIENDSHIP message" self.assert_(False) else: print >> sys.stderr, "test: Timeout, good, wasn't expecting a reply" self.assert_(True) if expectreply: self.check_friendship(resp[1:], RESP, None, mresp) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_good_rquery(self): """ test good QUERY messages """ print >>sys.stderr,"test: good QUERY" s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = self.create_good_rquery() s.send(msg) resp = s.recv() if len(resp) > 0: print >>sys.stderr,"test: good QUERY: got",getMessageName(resp[0]) self.assert_(resp[0] == QUERY_REPLY) self.check_rquery_reply(resp[1:]) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_good_simpleplustorrents_query(self): """ test good QUERY messages: SIMPLE+METADATA """ print >>sys.stderr,"test: good QUERY SIMPLE+METADATA" s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = self.create_good_simpleplustorrents_query() s.send(msg) resp = s.recv() if len(resp) > 0: print >>sys.stderr,"test: good QUERY: got",getMessageName(resp[0]) self.assert_(resp[0] == QUERY_REPLY) self.check_rquery_reply("SIMPLE+METADATA",resp[1:]) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_good_simpleplustorrents_query(self,keyword,goodtorrents): """ test good QUERY messages: SIMPLE+METADATA """ print >>sys.stderr,time.asctime(),'-', "test: good QUERY SIMPLE+METADATA",`keyword` s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = self.create_good_simpleplustorrents_query(keyword) s.send(msg) resp = s.recv() if len(resp) > 0: print >>sys.stderr,time.asctime(),'-', "test: good QUERY: got",getMessageName(resp[0]) self.assert_(resp[0] == QUERY_REPLY) self.check_rquery_reply("SIMPLE+METADATA",resp[1:],goodtorrents) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_valid_nat_check(self): """ Send a CRAWLER_NATCHECK message to the Tribler instance. A reply containing a nat type should be returned. """ print >> sys.stderr, "-" * 80, "\ntest: subtest_valid_nat_check" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) s = OLConnection(self.my_keypair, "localhost", self.hisport, mylistenport=self.listen_port) self.send_crawler_request(s, CRAWLER_NATCHECK, 42, 0, "") s.close() if DEBUG: print >> sys.stderr, "test_natcheck: the nat-check code allows for a 10 minute delay in reporting the nat stats" self.listen_socket.settimeout(11 * 60) # wait for reply try: conn, addr = self.listen_socket.accept() except socket.timeout: if DEBUG: print >> sys.stderr, "test_natcheck: timeout, bad, peer didn't connect to send the crawler reply" assert False, "test_natcheck: timeout, bad, peer didn't connect to send the crawler reply" s = OLConnection(self.my_keypair, "", 0, conn, mylistenport=self.listen_port) # read reply error, payload = self.receive_crawler_reply(s, CRAWLER_NATCHECK, 42) assert error == 0 if DEBUG: print >> sys.stderr, "test_natcheck:", bdecode(payload) time.sleep(1)
def subtest_votecast(self): print >> sys.stderr, "test: votecast-----------------------------" s = OLConnection(self.my_keypair, 'localhost', self.hisport) vcast = VoteCastCore(None, s, self.session, None, log='', dnsindb=None) #Send Good VoteCast message vdata = {self.hispermid: {'vote': -1, 'time_stamp': 12345345}} print >> sys.stderr, "Test Good VoteCast", ` vdata ` msg = VOTECAST + bencode(vdata) s.send(msg) resp = s.recv() #print >> sys.stderr, "printing resp", resp if len(resp) > 0: print >> sys.stderr, "test: votecast: got", getMessageName(resp[0]) self.assert_(resp[0] == VOTECAST) print >> sys.stderr, "test: votecast: got msg", ` bdecode(resp[1:]) ` vdata_rcvd = bdecode(resp[1:]) self.assert_(validVoteCastMsg(vdata_rcvd) == True) s.close() #Now, send a bad ChannelCast messages # The other side should close the connection #Bad time_stamp: it can only int vdata = {bin2str(self.hispermid): {'vote': -1, 'time_stamp': 'halo'}} self.subtest_bad_votecast(vdata) #Bad Vote: Vote can only -1 or 2 vdata = { bin2str(self.hispermid): { 'vote': -15, 'time_stamp': 12345345 } } self.subtest_bad_votecast(vdata) # Bad Message format ... Correct format is 'time_stamp' vdata = {bin2str(self.hispermid): {'vote': -15, 'timestamp': 12345345}} self.subtest_bad_votecast(vdata) print >> sys.stderr, "End of votecast test"
def test_good_get_metadata_url(self): # 1. Establish overlay connection to Tribler s = OLConnection(self.my_keypair,'localhost',self.hisport) for tdef in [self.tdef1,self.tdef2]: msg = self.create_good_get_metadata(tdef.get_infohash()) s.send(msg) try: s.b.s.settimeout(10.0) resp = s.recv() self.assert_(len(resp) > 0) print >>sys.stderr,"test: Got reply",getMessageName(resp[0]) self.assert_(resp[0] == METADATA) self.check_metadata(resp[1:],tdef) except socket.timeout: print >> sys.stderr,"test: Timeout, bad, peer didn't reply with METADATA message" self.assert_(False) s.close()
def test_good_get_metadata_url(self): # 1. Establish overlay connection to Tribler s = OLConnection(self.my_keypair,'localhost',self.hisport) for tdef in [self.tdef1,self.tdef2]: msg = self.create_good_get_metadata(tdef.get_infohash()) s.send(msg) try: s.b.s.settimeout(10.0) resp = s.recv() self.assert_(len(resp) > 0) print >>sys.stderr,"test: Got reply",getMessageName(resp[0]) self.assert_(resp[0] == METADATA) self.check_metadata(resp[1:],tdef) except socket.timeout: print >> sys.stderr,"test: Timeout, bad, peer didn't reply with METADATA message" self.assert_(False) s.close()
def subtest_good_buddycast(self, oversion): """ test good BUDDYCAST messages """ print >> sys.stderr, "test: good BUDDYCAST", oversion s = OLConnection(self.my_keypair, 'localhost', self.hisport, myoversion=oversion) msg = self.create_good_buddycast_payload(oversion) s.send(msg) s.b.s.settimeout(60.0) try: while True: resp = s.recv() self.assert_(len(resp) > 0) print >> sys.stderr, "test: good BUDDYCAST: Got reply", getMessageName( resp[0]) if resp[0] == BUDDYCAST: break elif resp[0] == GET_METADATA: self.check_get_metadata(resp[1:]) elif resp[0] == KEEP_ALIVE: if oversion >= 3: self.check_keep_alive(resp[1:]) else: print >> sys.stderr, "test: Tribler sent KEEP_ALIVE, not allowed in olproto ver", oversion self.assert_(False) except socket.timeout: print >> sys.stderr, "test: Timeout, bad, peer didn't reply with BUDDYCAST message" self.assert_(False) self.check_buddycast(resp[1:], oversion) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_good_bartercast(self): """ test good BARTERCAST messages """ print >>sys.stderr,time.asctime(),'-', "test: good BARTERCAST" s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = self.create_good_bartercast() s.send(msg) while True: resp = s.recv() print >>sys.stderr,time.asctime(),'-', "test: reply message",getMessageName(resp[0]) if resp[0] == KEEP_ALIVE: continue else: break self.assert_(resp[0] == BARTERCAST) self.check_bartercast(resp[1:]) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_good_friendship_req(self,mtype,fwd=None,mresp=None,socover=False,expectreply=True): print >>sys.stderr,"test: good FRIENDSHIP",mtype,fwd s = OLConnection(self.my_keypair,'localhost',self.hisport) if socover: tso = TestSocialOverlap("test_all") msg = tso.create_good_soverlap() s.send(msg) msg = self.create_good_friendship_payload(mtype,fwd,mresp) s.send(msg) s.b.s.settimeout(10.0) try: while True: resp = s.recv() self.assert_(len(resp) > 0) print >>sys.stderr,"test: good FRIENDSHIP: Got reply",getMessageName(resp[0]) if resp[0] == FRIENDSHIP: break elif resp[0] == SOCIAL_OVERLAP: pass else: self.assert_(False) except socket.timeout: if expectreply: print >> sys.stderr,"test: Timeout, bad, peer didn't reply with FRIENDSHIP message" self.assert_(False) else: print >> sys.stderr,"test: Timeout, good, wasn't expecting a reply" self.assert_(True) if expectreply: self.check_friendship(resp[1:],RESP,None,mresp) time.sleep(10) # the other side should not have closed the connection, as # this is all valid, so this should not throw an exception: s.send('bla') s.close()
def subtest_valid_messageid(self): """ Send a valid message-id from a registered crawler peer """ print >>sys.stderr, "-"*80, "\ntest: valid_messageid" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) s = OLConnection(self.my_keypair, "localhost", self.hisport) queries = ["SELECT name FROM category", "SELECT * FROM peer", "SELECT * FROM torrent"] for query in queries: self.send_crawler_request(s, CRAWLER_DATABASE_QUERY, 0, 0, query) error, payload = self.receive_crawler_reply(s, CRAWLER_DATABASE_QUERY, 0) assert error == 0 if DEBUG: print >>sys.stderr, cPickle.loads(payload) time.sleep(1) s.close()
def subtest_invalid_messageid(self): """ Send an invalid message-id from a registered crawler peer """ print >>sys.stderr, "-"*80, "\ntest: invalid_messageid" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) # We are a registered crawler, start sending invalid messages messages = [CRAWLER_REQUEST, CRAWLER_REQUEST + chr(0), CRAWLER_REPLY, CRAWLER_REPLY + chr(0)] for msg in messages: s = OLConnection(self.my_keypair, "localhost", self.hisport) s.send(msg) response = s.recv() assert response == "", "response type is %s" % getMessageName(response[0]) time.sleep(1) s.close()
def _test_2fast(self, genresdict): """ test ASK_FOR_HELP, METADATA, PIECES_RESERVED and STOP_DOWNLOAD_HELP sequence """ # 1. Establish overlay connection to Tribler s = OLConnection(self.my_keypair, 'localhost', self.hisport, mylistenport=self.mylistenport2) (func, good) = genresdict[ASK_FOR_HELP] msg = func() s.send(msg) if good: resp = s.recv() self.assert_(resp[0] == GET_METADATA) self.check_get_metadata(resp[1:]) print >> sys.stderr, "test: Got GET_METADATA for torrent, good" else: resp = s.recv() self.assert_(len(resp) == 0) s.close() return (func, good) = genresdict[METADATA] msg = func() s.send(msg) if good: # 2. Accept the data connection Tribler wants to establish with us, the coordinator self.myss2.settimeout(10.0) conn, addr = self.myss2.accept() s3 = BTConnection('', 0, conn, user_infohash=self.infohash, myid=self.myid2) s3.read_handshake_medium_rare() msg = UNCHOKE s3.send(msg) print >> sys.stderr, "test: Got data connection to us, as coordinator, good" else: resp = s.recv() self.assert_(len(resp) == 0) s.close() return # 3. Our tracker says there is another peer (also us) on port 4810 # Now accept a connection on that port and pretend we're a seeder self.myss.settimeout(10.0) conn, addr = self.myss.accept() options = '\x00\x00\x00\x00\x00\x00\x00\x00' s2 = BTConnection('', 0, conn, user_option_pattern=options, user_infohash=self.infohash, myid=self.myid) s2.read_handshake_medium_rare() numpieces = 10 # must correspond to the torrent in test/extend_hs_dir b = Bitfield(numpieces) for i in range(numpieces): b[i] = True self.assert_(b.complete()) msg = BITFIELD + b.tostring() s2.send(msg) msg = UNCHOKE s2.send(msg) print >> sys.stderr, "test: Got BT connection to us, as fake seeder, good" # 4. Await a RESERVE_PIECES message on the overlay connection resp = s.recv() self.assert_(resp[0] == RESERVE_PIECES) pieces = self.check_reserve_pieces(resp[1:]) print >> sys.stderr, "test: Got RESERVE_PIECES, good" (func, good) = genresdict[PIECES_RESERVED] # 5. Reply with PIECES_RESERVED msg = func(pieces) s.send(msg) if good: # 6. Await REQUEST on fake seeder while True: resp = s2.recv() self.assert_(len(resp) > 0) print "test: Fake seeder got message", getMessageName(resp[0]) if resp[0] == REQUEST: self.check_request(resp[1:], pieces) print >> sys.stderr, "test: Fake seeder got REQUEST for reserved piece, good" break else: resp = s.recv() self.assert_(len(resp) == 0) s.close() return (func, good) = genresdict[STOP_DOWNLOAD_HELP] # 5. Reply with STOP_DOWNLOAD_HELP msg = func() s.send(msg) # the other side should close the connection, whether the msg was good or bad resp = s.recv() self.assert_(len(resp) == 0) s.close()