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_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_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_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 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_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_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 _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_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_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_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 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_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_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_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 _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 _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 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_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 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_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 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_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 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 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 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_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_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_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_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_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 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_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_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 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_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_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_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_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_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_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_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))
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 _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 ---------------------------"