def subtest_good_friendship_fwd_resp_desthim(self,mtype,fwd=None,mresp=None): print >>sys.stderr,"test: good FRIENDSHIP dest = him",mtype,fwd s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = self.create_good_friendship_payload(mtype,fwd,mresp,source=self.destpermid,dest=self.hispermid) s.send(msg) # He should not reply. try: self.destss.settimeout(10.0) conn, addr = self.destss.accept() s = OLConnection(self.dest_keypair,'',0,conn,self.destport) while True: resp = s.recv() self.assert_(len(resp) > 0) print >>sys.stderr,"test: good FRIENDSHIP fwd: Dest got reply",getMessageName(resp[0]) if resp[0] == FRIENDSHIP: self.assert_(False) break elif resp[0] == SOCIAL_OVERLAP: pass else: self.assert_(False) except socket.timeout: print >> sys.stderr,"test: Timeout, good" self.assert_(True)
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 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_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_terms(self,myoversion): """assumes clicklog message 1 and 2 have been sent and digested""" print >>sys.stderr,"\ntest: subtest_terms" term_db = self.session.open_dbhandler(NTFY_TERM) s = OLConnection(self.my_keypair,'localhost',self.hisport,myoversion=myoversion) msg = self.get_good_clicklog_msg(3,myoversion) msg = self.create_payload(msg) s.send(msg) resp = s.recv() self.assert_(len(resp) > 0) termid = term_db.getTermID(u"linux") print >>sys.stderr, "TermID for Linux: %s" % termid #self.assert_(termid == 1) #self.assert_(term_db.getTerm(1)==bin2str(str(u"linux"))) completedTerms = term_db.getTermsStartingWith("li") print >> sys.stderr, "terms starting with l: %s" % completedTerms self.assert_(len(completedTerms)==1) self.assert_(u'linux' in completedTerms) term_db.insertTerm("asd#") completedTerms = term_db.getTermsStartingWith("asd") print >> sys.stderr, "terms starting with asd: %s" % completedTerms self.assert_(len(completedTerms)==1) # Arno, 2010-02-03: Nicolas had 'asd' here, but I don't see any place # where the # should have been stripped. # self.assert_(u'asd#' in completedTerms)
def subtest_terms(self): """assumes clicklog message 1 and 2 have been sent and digested""" term_db = self.session.open_dbhandler(NTFY_TERM) s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = self.get_good_clicklog_msg(3) msg = self.create_payload(msg) s.send(msg) resp = s.recv() termid = term_db.getTermID(str(u"linux")) print >>sys.stderr, "TermID fuer Linux: %s" % termid #self.assert_(termid == 1) #self.assert_(term_db.getTerm(1)==bin2str(str(u"linux"))) completedTerms = term_db.getTermsStartingWith("li") print >> sys.stderr, "terms starting with l: %s" % completedTerms self.assert_(len(completedTerms)==1) self.assert_(str(u'linux') in completedTerms) term_db.insertTerm("asd#") completedTerms = term_db.getTermsStartingWith("asd") print >> sys.stderr, "terms starting with asd: %s" % completedTerms self.assert_(len(completedTerms)==1) self.assert_(str(u'asd') in completedTerms)
def subtest_good_friendship_req_fromhim(self,mtype,fwd=None,mresp=None): print >>sys.stderr,"test: good FRIENDSHIP req from him",mtype,fwd # He should try to forward the request to us, his friend try: self.destss.settimeout(330.0) conn, addr = self.destss.accept() s = OLConnection(self.dest_keypair,'',0,conn,self.destport) while True: resp = s.recv() self.assert_(len(resp) > 0) print >>sys.stderr,"test: good FRIENDSHIP fwd: Dest got reply",getMessageName(resp[0]) if resp[0] == FRIENDSHIP: break elif resp[0] == SOCIAL_OVERLAP: d = bdecode(resp[1:]) print >>sys.stderr,"test: SOCIAL OVERLAP",`d` pass else: self.assert_(False) except socket.timeout: print >> sys.stderr,"test: Timeout, bad, peer didn't connect to FWD dest" self.assert_(False) self.check_friendship(resp[1:],mtype,fwd,mresp,source=self.hispermid,dest=self.mypermid)
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 _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_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_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_good_friendship_fwd_req_desthim(self,mtype,fwd=None,mresp=None): print >>sys.stderr,"test: good FRIENDSHIP dest = him",mtype,fwd s = OLConnection(self.my_keypair,'localhost',self.hisport) msg = self.create_good_friendship_payload(mtype,fwd,mresp,source=self.destpermid,dest=self.hispermid) s.send(msg) # He should try to reply to dest's request, forwarded through my try: self.destss.settimeout(10.0) conn, addr = self.destss.accept() s = OLConnection(self.dest_keypair,'',0,conn,self.destport) while True: resp = s.recv() self.assert_(len(resp) > 0) print >>sys.stderr,"test: good FRIENDSHIP fwd: Dest got reply",getMessageName(resp[0]) if resp[0] == FRIENDSHIP: break elif resp[0] == SOCIAL_OVERLAP: pass else: self.assert_(False) except socket.timeout: print >> sys.stderr,"test: Timeout, bad, peer didn't connect to FWD dest" self.assert_(False) self.check_friendship(resp[1:],RESP,fwd,mresp)
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 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 _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 subtest_good_drequest(self): """ test good DIALBACK_REQUEST messages """ s = OLConnection(self.my_keypair,'localhost',self.hisport,mylistenport=self.mylistenport) msg = self.create_good_drequest() s.send(msg) time.sleep(5) # And connect back to us conn, addr = self.myss.accept() s2 = BTConnection('',0,conn,mylistenport=self.mylistenport,user_infohash=dialback_infohash) s2.read_handshake_medium_rare() resp = s2.recv() print >> sys.stderr,"test: Me got DIALBACK_REPLY from him, len",len(resp) self.assert_(resp[0] == DIALBACK_REPLY) self.check_drequest(resp[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_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 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_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_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 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 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_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 _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 _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 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,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 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_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_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 subtest_valid_query(self): """ Send a CRAWLER_SEEDINGSTATS_QUERY message to the Tribler instance. Execute a valid SQL query. """ print >> sys.stderr, "-" * 80, "\ntest: subtest_valid_query" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) # test with valid data seedingstats_db = SQLiteSeedingStatsCacheDB.getInstance() seedingstats_db.insertMany("SeedingStats", [(50000, 'foobar', 'dummy_seed', 500, 0, 0), (80000, 'bar', 'dummy_seed', 800, 1, 0)]) s = OLConnection(self.my_keypair, "localhost", self.hisport, mylistenport=self.listen_port) queries = [ cPickle.dumps([("read", "SELECT * FROM SeedingStats"), ("read", "SELECT * FROM SeedingStats WHERE crawled = 0")]) ] for query in queries: self.send_crawler_request(s, CRAWLER_SEEDINGSTATS_QUERY, 0, 0, query) error, payload = self.receive_crawler_reply( s, CRAWLER_SEEDINGSTATS_QUERY, 0) assert error == 0, (error, payload) if DEBUG: print >> sys.stderr, "test_seeding_stats:", cPickle.loads( payload)
def subtest_invalid_query(self): """ Send a CRAWLER_SEEDINGSTATS_QUERY message to the Tribler instance. Execute an invalid SQL query. """ print >> sys.stderr, "-" * 80, "\ntest: subtest_invalid_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", cPickle.dumps(["select * from category", ""])] for query in queries: self.send_crawler_request(s, CRAWLER_SEEDINGSTATS_QUERY, 0, 0, query) error, payload = self.receive_crawler_reply( s, CRAWLER_SEEDINGSTATS_QUERY, 0) assert error != 0, error if DEBUG: print >> sys.stderr, "test_seeding_stats:", payload
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_friendship_fwd_dest3rdp(self, mtype, fwd=None, mresp=None): print >> sys.stderr, "test: good FRIENDSHIP dest = 3rd party", mtype, fwd s = OLConnection(self.my_keypair, 'localhost', self.hisport) msg = self.create_good_friendship_payload(mtype, fwd, mresp, source=self.mypermid, dest=self.destpermid) s.send(msg) # He should try to forward the request to us try: self.destss.settimeout(10.0) conn, addr = self.destss.accept() s = OLConnection(self.dest_keypair, '', 0, conn, self.destport) while True: resp = s.recv() self.assert_(len(resp) > 0) print >> sys.stderr, "test: good FRIENDSHIP fwd: Dest got reply", getMessageName( resp[0]) if resp[0] == FRIENDSHIP: break elif resp[0] == SOCIAL_OVERLAP: pass else: self.assert_(False) except socket.timeout: print >> sys.stderr, "test: Timeout, bad, peer didn't connect to FWD dest" self.assert_(False) self.check_friendship(resp[1:], FWD, fwd, mresp, source=self.mypermid, dest=self.destpermid)
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 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_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_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 _test_2fast(self, genresdict): """ test ASK_FOR_HELP, METADATA, PIECES_RESERVED and STOP_DOWNLOAD_HELP sequence """ # 1. Establish overlay connection to Tribler ol_connection = OLConnection(self.my_keypair, 'localhost', self.hisport, mylistenport=self.mylistenport2) # Send ASK_FOR_HELP (generate_data, sent_good_values) = genresdict[ASK_FOR_HELP] msg = generate_data() ol_connection.send(msg) if sent_good_values: resp = ol_connection.recv() self.assert_(resp[0] == GET_METADATA) self.check_get_metadata(resp[1:]) print >> sys.stderr, "test: Got GET_METADATA for torrent, sent_good_values" else: resp = ol_connection.recv() self.assert_(len(resp) == 0) ol_connection.close() return # Send METADATA (generate_data, sent_good_values) = genresdict[METADATA] msg = generate_data() ol_connection.send(msg) if sent_good_values: # 2. Accept the data connection Tribler wants to establish with us, the coordinator self.myss2.settimeout(10.0) conn, addr = self.myss2.accept() #(self,hostname,port,opensock=None,user_option_pattern=None,user_infohash=None,myid=None,mylistenport=None,myoversion=None): bt_connection_2 = BTConnection('', 0, conn, user_infohash=self.infohash, myid=self.myid2) bt_connection_2.read_handshake_medium_rare() msg = UNCHOKE bt_connection_2.send(msg) print >> sys.stderr, "test: Got data connection to us, as coordinator, sent_good_values" else: resp = ol_connection.recv() self.assert_(len(resp) == 0) ol_connection.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' bt_connection = BTConnection('', 0, conn, user_option_pattern=options, user_infohash=self.infohash, myid=self.myid) bt_connection.read_handshake_medium_rare() # Get the number of pieces from the .torrent file torrentfile_content = open(self.torrentfile, "rb") metadata_dict = bdecode(torrentfile_content.read()) torrentfile_content.close() if "length" in metadata_dict["info"]: length = metadata_dict["info"]["length"] else: length = 0 for file in metadata_dict["info"]["files"]: length += file["length"] numpieces = length / metadata_dict["info"]["piece length"] bitf = Bitfield(numpieces) for i in range(numpieces): bitf[i] = True self.assert_(bitf.complete()) msg = BITFIELD + bitf.tostring() bt_connection.send(msg) msg = UNCHOKE bt_connection.send(msg) print >> sys.stderr, "test: Got BT connection to us, as fake seeder, sent_good_values" # 4. Await a RESERVE_PIECES message on the overlay connection resp = ol_connection.recv() self.assert_(resp[0] == RESERVE_PIECES) pieces = self.check_reserve_pieces(resp[1:]) print >> sys.stderr, "test: Got RESERVE_PIECES, sent_good_values" # 5. Reply with PIECES_RESERVED (generate_data, sent_good_values) = genresdict[PIECES_RESERVED] msg = generate_data(pieces) ol_connection.send(msg) if sent_good_values: # 6. Await REQUEST on fake seeder while True: resp = bt_connection.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, sent_good_values" break else: resp = ol_connection.recv() self.assert_(len(resp) == 0) ol_connection.close() return # 7. Reply with STOP_DOWNLOAD_HELP (generate_data, sent_good_values) = genresdict[STOP_DOWNLOAD_HELP] msg = generate_data() ol_connection.send(msg) # the other side should close the connection, whether the msg was sent_good_values or bad resp = ol_connection.recv() self.assert_(len(resp) == 0) ol_connection.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 _test_proxy(self, genresdict): """ Send messages to the helper instance and test it. Testing ASK_FOR_HELP, STOP_HELPING, REQUEST_PIECES, CANCEL_PIECE and METADATA """ # 1. Establish overlay connection to Tribler ol_connection = OLConnection(self.my_keypair, 'localhost', self.hisport, mylistenport=self.mylistenport2) # 2. Send the ASK_FOR_HELP message (generate_data, sent_good_values) = genresdict[ASK_FOR_HELP] msg = generate_data() ol_connection.send(msg) if sent_good_values: # Read the helper's response resp = ol_connection.recv() # Check the helper's response # 3. At this point, the helper does not have the .torrent file, so it requests it with a METADATA message self.assert_(resp[0] == GET_METADATA) self.check_get_metadata(resp[1:]) print >> sys.stderr, "test: Got GET_METADATA for torrent, good" else: # Read the helper's response resp = ol_connection.recv() # Check the helper's response self.assert_(len(resp) == 0) ol_connection.close() return # 4. Send METADATA (generate_data, sent_good_values) = genresdict[METADATA] msg = generate_data() ol_connection.send(msg) if sent_good_values: # 5. At this point the helper is confirming his availability to help # Read the helper's response resp = ol_connection.recv() # Check the helper's response self.assert_(resp[0] == JOIN_HELPERS) self.check_ask_for_help(resp) print >> sys.stderr, "test: Got JOIN_HELPERS for torrent, good" # 6. At this point, the helper will contact the tracker and then wait for REQUEST_PIECES messages # So we send a request pieces message (generate_data, sent_good_values) = genresdict[REQUEST_PIECES] msg = generate_data() ol_connection.send(msg) # At this point the helper will contact the seeders in the swarm to download the requested piece # There is only one seeder in the swarm, the coordinator's twin # 8. 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() # Send a bitfield message to the helper (pretending we are a regular seeder) b = Bitfield(self.numpieces) for i in range(self.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" else: resp = ol_connection.recv() self.assert_(len(resp) == 0) ol_connection.close() return # 7. Accept the data connection the helper wants to establish with us, the coordinator. # The helper will send via this connection the pieces we request it to download. 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" # 9. At this point the helper should sent a PROXY_HAVE message on the overlay connection # resp = ol_connection.recv() # self.assert_(resp[0] == PROXY_HAVE) # print >>sys.stderr,"test: Got PROXY)HAVE, good" # 10. Await REQUEST on fake seeder try: while True: s2.s.settimeout(10.0) 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:]) print >> sys.stderr, "test: Fake seeder got REQUEST for reserved piece, good" break except socket.timeout: print >> sys.stderr, "test: Timeout, bad, fake seeder didn't reply with message" self.assert_(False) # 11. Sent the helper a STOP_HELPING message (generate_data, sent_good_values) = genresdict[STOP_HELPING] msg = generate_data() ol_connection.send(msg) # The other side should close the connection, whether the msg was good or bad resp = ol_connection.recv() self.assert_(len(resp) == 0) ol_connection.close()
def subtest_good_buddycast_clicklog(self, i, actuallyTest = True): """sends two buddy cast messages containing clicklog data, then checks in the DB to find out whether the correct data was stored. This in fact checks quite a lot of things. For example, the messages always contain terms [1,2] later methods require DB setup from these methods in order to perform the DB operations but not the tests (which are still somewhat sensible to cooccuring network operations) the actual testing can be turned of by actuallyTest=False so later tests can still be executed successfully """ s = OLConnection(self.my_keypair,'localhost',self.hisport) prefmsg = self.get_good_clicklog_msg(i) msg = self.create_payload(prefmsg) s.send(msg) resp = s.recv() if len(resp)>0: print >>sys.stderr,"test: reply message %s:%s" % (getMessageName(resp[0]), resp[1:]) else: print >>sys.stderr,"no reply message" #if we have survived this, check if the content of the remote database is correct search_db = self.session.open_dbhandler(NTFY_SEARCH) term_db = self.session.open_dbhandler(NTFY_TERM) pref_db = self.session.open_dbhandler(NTFY_PREFERENCES) torrent_db = self.session.open_dbhandler(NTFY_TORRENTS) # self.getAll("rowid, peer_id, torrent_id, click_position,reranking_strategy", order_by="peer_id, torrent_id") real_prefs = pref_db.getAllEntries() my_peer_id = real_prefs[0][1] real_terms = term_db.getAllEntries() real_search = search_db.getAllEntries() torrent_id = None while not torrent_id: hash = prefmsg['preferences'][0][0] print >> sys.stderr, "hash: %s, bin2str: %s" % (hash, bin2str(hash)) torrent_data = torrent_db.getTorrentID(hash) print >> sys.stderr, "Torrent data for torrent %s: %s" % (prefmsg['preferences'][0][0], torrent_data) torrent_id = torrent_data if not torrent_id: print >> sys.stderr, "torrent not yet saved, waiting..." sleep(1) if i==1: wanted_prefs = [[1,my_peer_id,1,1,2]] wanted_terms = [[1,str(u'linux')], [2,str(u'ubuntu')]] wanted_search = [[1,my_peer_id,'?',1,0], [2,my_peer_id,'?',2,1]] elif i==2: wanted_prefs = [[1,my_peer_id,'?',1,2], [2,my_peer_id,torrent_id,2,2]] wanted_terms = [[1,str(u'linux')], [2,str(u'ubuntu')]] wanted_search = [[1,my_peer_id,'?',1,0], [2,my_peer_id,'?',2,1], [3,my_peer_id,'?',1,0], [4,my_peer_id,'?',2,1]] elif i==3: wanted_prefs = [[1,my_peer_id,'?',1,2], [2,my_peer_id,'?',2,2],[3,my_peer_id,torrent_id,5,2]] wanted_terms = [[1,str(u'linux')], [2,str(u'ubuntu')], [3, str(u'redhat')]] wanted_search = [[1,my_peer_id,'?',1,0], [2,my_peer_id,'?',2,1], [3,my_peer_id,'?',1,0], [4,my_peer_id,'?',2,1], [5,my_peer_id,'?',1,0], [6,my_peer_id,'?',3,1]] print >> sys.stderr, "real_prefs: %s" % real_prefs print >> sys.stderr, "real_terms: %s" % real_terms print >> sys.stderr, "real_search: %s " % real_search print >> sys.stderr, "wanted_prefs: %s" % wanted_prefs print >> sys.stderr, "wanted_terms: %s" % wanted_terms print >> sys.stderr, "wanted_search: %s " % wanted_search if actuallyTest: self.assert_(self.lol_equals(real_search, wanted_search, "good buddycast %d: search" % i)) self.assert_(self.lol_equals(real_terms, wanted_terms, "good buddycast %d: terms" % i)) self.assert_(self.lol_equals(real_prefs, wanted_prefs, "good buddycast %d: prefs" % i))
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 _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()
def subtest_channelcastPlusMetadata(self): ''' Extends channelcast test to channelcast messages enriched with metadata (subtitles) informations ''' print >> sys.stderr, "test: channelcast_subtitles ---------------------------" s = OLConnection(self.my_keypair, 'localhost', self.hisport) chcast = ChannelCastCore(None, s, self.session, None, log='', dnsindb=None) #test send standard channelcast chdata = {} print >> sys.stderr, "Test Good ChannelCast Plus Subtitles", ` chdata ` msg = CHANNELCAST + bencode(chdata) s.send(msg) resp = s.recv() if len(resp) > 0: print >> sys.stderr, "test: channelcast_subtitles: got", getMessageName( resp[0]) self.assert_(resp[0] == CHANNELCAST) print >> sys.stderr, "test: channelcast_subtitles: got msg", ` bdecode( resp[1:]) ` chdata_rcvd = bdecode(resp[1:]) self.assertTrue(validChannelCastMsg(chdata_rcvd)) for entry in chdata_rcvd.itervalues(): if entry[ 'infohash'] == self.infohash1: #the torrent for which two subtitles exist self.assertTrue('rich_metadata' in entry.keys()) richMetadata = entry['rich_metadata'] print >> sys.stderr, "test: channelcast_subtitles: richMetadata entry is ", richMetadata self.assertEquals(6, len(richMetadata)) self.assertEquals(self.mdto.description, richMetadata[0]) self.assertEquals(4, len( richMetadata[2])) #the subtitles mask 4 bytes self.assertTrue(isinstance(richMetadata[3], list)) #the subtitles checsums for checksum in richMetadata[3]: self.assertEquals(20, len(checksum)) #160 bit sha1 checksum self.assertEquals(self.mdto.signature, richMetadata[4]) self.assertEquals(4, len( richMetadata[5])) #the subtitles have mask 32 bit #also must (in this case) be equal to the subtitles mask self.assertEquals(richMetadata[2], richMetadata[5]) print >> sys.stderr, "test: channelcast_subtitles; richMetadata entry is valid and correct" else: self.assertFalse('rich_metadata' in entry.keys()) s.close() #Now, send a bad ChannelCast message. # The other side should close the connection # Create bad message by manipulating a good one #bad bitmask chdata = deepcopy(chdata_rcvd) for k, v in chdata.items(): if 'rich_metadata' in v: v['rich_metadata'][ 2] = 44 #an integer instead of a 4bytes bitmask self.subtest_bad_channelcast(chdata) #Bad message format chdata = deepcopy(chdata_rcvd) for k, v in chdata.items(): if 'rich_metadata' in v: v['rich_metadata'].insert(0, u"asdfafa22") self.subtest_bad_channelcast(chdata) #Bad print >> sys.stderr, "End of channelcast_subtitles test ---------------------------"
def subtest_good_buddycast_clicklog(self, i, myoversion): """sends two buddy cast messages containing clicklog data, then checks in the DB to find out whether the correct data was stored. This in fact checks quite a lot of things. For example, the messages always contain terms [1,2] """ print >> sys.stderr, "\ntest: subtest_good_buddycast_clicklog", i, "selversion", myoversion s = OLConnection(self.my_keypair, 'localhost', self.hisport, myoversion=myoversion) prefmsg = self.get_good_clicklog_msg(i, myoversion) print >> sys.stderr, myoversion, ` prefmsg ` msg = self.create_payload(prefmsg) s.send(msg) resp = s.recv() if len(resp) > 0: print >> sys.stderr, "test: reply message %s:%s" % (getMessageName( resp[0]), resp[1:]) else: print >> sys.stderr, "no reply message" self.assert_(len(resp) > 0) #if we have survived this, check if the content of the remote database is correct search_db = self.session.open_dbhandler(NTFY_SEARCH) term_db = self.session.open_dbhandler(NTFY_TERM) pref_db = self.session.open_dbhandler(NTFY_PREFERENCES) torrent_db = self.session.open_dbhandler(NTFY_TORRENTS) torrent_id = None while not torrent_id: hash = prefmsg['preferences'][0][0] print >> sys.stderr, "hash: %s, bin2str: %s" % (hash, bin2str(hash)) torrent_data = torrent_db.getTorrentID(hash) print >> sys.stderr, "Torrent data for torrent %s: %s" % ( prefmsg['preferences'][0][0], torrent_data) torrent_id = torrent_data if not torrent_id: print >> sys.stderr, "torrent not yet saved, waiting..." sleep(1) # self.getAll("rowid, peer_id, torrent_id, click_position,reranking_strategy", order_by="peer_id, torrent_id") real_prefs = pref_db.getAllEntries() print >> sys.stderr, "test: getAllEntries returned", real_prefs my_peer_id = real_prefs[0][1] real_terms = term_db.getAllEntries() real_search = search_db.getAllEntries() if i == 1: wanted_prefs = [[1, my_peer_id, 1, 1, 2]] wanted_terms = [[1, u'linux'], [2, u'ubuntu']] wanted_search = [[1, my_peer_id, '?', 1, 0], [2, my_peer_id, '?', 2, 1]] elif i == 2: # Arno, 2010-02-04: Nicolas assumed the collected torrent for i=1 # wouldn't be stored in DB? wanted_prefs = [[1, my_peer_id, '?', 1, 2], [2, my_peer_id, torrent_id, 2, 2]] wanted_terms = [[1, u'linux'], [2, u'ubuntu']] wanted_search = [[1, my_peer_id, '?', 1, 0], [2, my_peer_id, '?', 2, 1], [3, my_peer_id, '?', 1, 0], [4, my_peer_id, '?', 2, 1]] elif i == 3: wanted_prefs = [[1, my_peer_id, '?', 1, 2], [2, my_peer_id, '?', 2, 2], [3, my_peer_id, torrent_id, 5, 2]] wanted_terms = [[1, u'linux'], [2, u'ubuntu'], [3, u'redhat']] wanted_search = [[1, my_peer_id, '?', 1, 0], [2, my_peer_id, '?', 2, 1], [3, my_peer_id, '?', 1, 0], [4, my_peer_id, '?', 2, 1], [5, my_peer_id, '?', 1, 0], [6, my_peer_id, '?', 3, 1]] print >> sys.stderr, "real_prefs: %s" % real_prefs print >> sys.stderr, "real_terms: %s" % real_terms print >> sys.stderr, "real_search: %s " % real_search print >> sys.stderr, "wanted_prefs: %s" % wanted_prefs print >> sys.stderr, "wanted_terms: %s" % wanted_terms print >> sys.stderr, "wanted_search: %s " % wanted_search self.assert_( self.lol_equals(real_search, wanted_search, "good buddycast %d: search" % i)) self.assert_( self.lol_equals(real_terms, wanted_terms, "good buddycast %d: terms" % i)) self.assert_( self.lol_equals(real_prefs, wanted_prefs, "good buddycast %d: prefs" % i))