def _test_dreply(self, gen_dreply, good, diff_ips_test=False): for i in range(self.NLISTENERS): print >> sys.stderr, "test: waiting for #", i, "listenport", self.mylistenport[ i] conn, addr = self.myss[i].accept() s = OLConnection(self.mykeypairs[i], '', 0, conn, self.mylistenport[i]) while True: msg = s.recv() self.assert_(len(msg) > 0) print >> sys.stderr, "test: Received overlay message", getMessageName( msg[0]) if msg[0] == DIALBACK_REQUEST: break self.assert_(msg[0] == DIALBACK_REQUEST) self.check_drequest(msg[1:]) # Proper behaviour is to try to send a reply using a new return connection s2 = BTConnection('localhost', self.hisport, mylistenport=self.mylistenport[i], user_infohash=dialback_infohash) s2.read_handshake_medium_rare(close_ok=True) if gen_dreply is not None: resp = gen_dreply(i) print >> sys.stderr, "test: sending DIALBACK_REPLY #", i s2.send(resp) time.sleep(2) # the other side should always close the # connection, either because we're done or he didn't like our # bad DIALBACK_REPLY message msg = s2.recv() if len(msg) > 0: print >> sys.stderr, "test: Received unexpected data", getMessageName( msg[0]) self.assert_(len(msg) == 0) s2.close() # Not really necessary, but helps with test_dialback_active2 s.close() ext_ip = self.session.get_external_ip() print >> sys.stderr, "test: External IP address after test is", ext_ip if diff_ips_test: if self.config.sessconfig['dialback_trust_superpeers'] == 1: good = True else: good = False if good: self.assert_(ext_ip == REPLY_IP) else: self.assert_(ext_ip == self.myoriginalip)
def _test_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 receive_crawler_reply(self, sock, message_id, channel_id): # Sending a reply from a Tribler peer to a Crawler # SIZE INDEX # 1 byte: 0 CRAWLER_REPLY (from Tribler.Core.MessageID) # 1 byte: 1 --MESSAGE-SPECIFIC-ID-- # 1 byte: 2 Channel id # 1 byte: 3 Parts left # 1 byte: 4 Indicating success (0) or failure (non 0) # n byte: 5... Reply payload if DEBUG: print >>sys.stderr, "test_crawler: receive_crawler_reply: waiting for channel",channel_id parts = [] while True: response = sock.recv() if response: if response[0] == CRAWLER_REPLY and response[1] == message_id and ord(response[2]) == channel_id: parts.append(response[5:]) if DEBUG: print >>sys.stderr, "test_crawler: received", getMessageName(response[0:2]), "channel", channel_id, "length", sum([len(part) for part in parts]), "parts left", ord(response[3]) if ord(response[3]): # there are parts left continue return ord(response[4]), "".join(parts) return -1, ""
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 receive_crawler_reply(self, sock, message_id, channel_id): # Sending a reply from a Tribler peer to a Crawler # SIZE INDEX # 1 byte: 0 CRAWLER_REPLY (from Tribler.Core.MessageID) # 1 byte: 1 --MESSAGE-SPECIFIC-ID-- # 1 byte: 2 Channel id # 1 byte: 3 Parts left # 1 byte: 4 Indicating success (0) or failure (non 0) # n byte: 5... Reply payload if DEBUG: print >> sys.stderr, "test_crawler: receive_crawler_reply: waiting for channel", channel_id parts = [] while True: response = sock.recv() if response: if response[0] == CRAWLER_REPLY and response[ 1] == message_id and ord(response[2]) == channel_id: parts.append(response[5:]) if DEBUG: print >> sys.stderr, "test_crawler: received", getMessageName( response[0:2] ), "channel", channel_id, "length", sum([ len(part) for part in parts ]), "parts left", ord(response[3]) if ord(response[3]): # there are parts left continue return ord(response[4]), "".join(parts) return -1, ""
def got_message(self,dns,message): """ received message from peer, pass to upper layer """ if DEBUG: print >> sys.stderr,"dlbreturn: got_message",getMessageName(message[0]) if self.usermsghandler is None: if DEBUG: print >> sys.stderr,"dlbreturn: User receive callback not set" return try: ret = self.usermsghandler(dns,message) if ret is None: if DEBUG: print >> sys.stderr,"dlbreturn: INTERNAL ERROR:", \ "User receive callback returned None, not True or False" ret = False return ret except: # Catch all print_exc(file=sys.stderr) return False
def got_message(self, dns, message): """ received message from peer, pass to upper layer """ if DEBUG: print >> sys.stderr, "dlbreturn: got_message", getMessageName( message[0]) if self.usermsghandler is None: if DEBUG: print >> sys.stderr, "dlbreturn: User receive callback not set" return try: ret = self.usermsghandler(dns, message) if ret is None: if DEBUG: print >> sys.stderr,"dlbreturn: INTERNAL ERROR:", \ "User receive callback returned None, not True or False" ret = False return ret except: # Catch all print_exc(file=sys.stderr) return False
def test_good_get_metadata_url(self): # 1. Establish overlay connection to Tribler s = OLConnection(self.my_keypair,'localhost',self.hisport) for tdef in [self.tdef1,self.tdef2]: msg = self.create_good_get_metadata(tdef.get_infohash()) s.send(msg) try: s.b.s.settimeout(10.0) resp = s.recv() self.assert_(len(resp) > 0) print >>sys.stderr,"test: Got reply",getMessageName(resp[0]) self.assert_(resp[0] == METADATA) self.check_metadata(resp[1:],tdef) except socket.timeout: print >> sys.stderr,"test: Timeout, bad, peer didn't reply with METADATA message" self.assert_(False) s.close()
def test_good_get_metadata_url(self): # 1. Establish overlay connection to Tribler s = OLConnection(self.my_keypair, 'localhost', self.hisport) for tdef in [self.tdef1, self.tdef2]: msg = self.create_good_get_metadata(tdef.get_infohash()) s.send(msg) try: s.b.s.settimeout(10.0) resp = s.recv() self.assert_(len(resp) > 0) print >> sys.stderr, "test: Got reply", getMessageName(resp[0]) self.assert_(resp[0] == METADATA) self.check_metadata(resp[1:], tdef) except socket.timeout: print >> sys.stderr, "test: Timeout, bad, peer didn't reply with METADATA message" self.assert_(False) s.close()
def subtest_invalid_messageid(self): """ Send an invalid message-id from a registered crawler peer """ print >>sys.stderr, "-"*80, "\ntest: invalid_messageid" # make sure that the OLConnection IS in the crawler_db crawler_db = CrawlerDBHandler.getInstance() crawler_db.temporarilyAddCrawler(self.my_permid) # We are a registered crawler, start sending invalid messages messages = [CRAWLER_REQUEST, CRAWLER_REQUEST + chr(0), CRAWLER_REPLY, CRAWLER_REPLY + chr(0)] for msg in messages: s = OLConnection(self.my_keypair, "localhost", self.hisport) s.send(msg) response = s.recv() assert response == "", "response type is %s" % getMessageName(response[0]) time.sleep(1) s.close()
def _test_qreply(self,gen_qreply,good): print >> sys.stderr,"test: waiting for reply" s = self.openconn msg = s.recv() self.assert_(len(msg) > 0) print >> sys.stderr,"test: Received overlay message",getMessageName(msg[0]) self.assert_(msg[0] == QUERY) id = self.check_rquery(msg[1:]) resp = gen_qreply(id) print >> sys.stderr,"test: sending QUERY_REPLY" s.send(resp) if good: 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() else: # the other side should not like this and close the connection self.assert_(len(s.recv())==0) 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_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()