1 # Written by Arno Bakker
2 # see LICENSE.txt for license information
10 #from urllib2 import urlopen # urllib blocks on reading, HTTP/1.1 persist conn problem?
11 from traceback import print_exc
14 from BaseLib.Core.BitTornado.zurllib import urlopen
15 from BaseLib.Core.simpledefs import STATEDIR_ITRACKER_DIR
16 from BaseLib.Test.test_as_server import TestAsServer
17 from BaseLib.Test.btconn import BTConnection
18 from BaseLib.Core.BitTornado.BT1.MessageID import *
20 from BaseLib.Core.API import *
24 class TestTracking(TestAsServer):
26 Testing seeding via new tribler API:
29 def setUpPreSession(self):
30 """ override TestAsServer """
31 TestAsServer.setUpPreSession(self)
33 self.config.set_megacache(False)
34 self.config.set_internal_tracker(True)
38 self.subtest_add_remove_torrent()
39 self.subtest_tlookup1()
40 self.subtest_tlookup2()
43 def subtest_add_remove_torrent(self):
45 sourcefn = os.path.join(os.getcwd(),"file.wmv")
46 tdef.add_content(sourcefn)
47 tdef.set_tracker(self.session.get_internal_tracker_url())
50 torrentfn = os.path.join(self.session.get_state_dir(),"gen.torrent")
52 infohash = tdef.get_infohash()
53 hexinfohash = binascii.hexlify(infohash)
55 self.session.add_to_internal_tracker(tdef)
57 self.check_http_presence(hexinfohash,True)
59 self.session.remove_from_internal_tracker(tdef)
60 print >> sys.stderr,"test: Give network thread running tracker time to detect we removed the torrent file"
63 self.check_http_presence(hexinfohash,False)
64 self.check_disk_presence(hexinfohash,False)
66 def check_http_presence(self,hexinfohash,present):
67 print >> sys.stderr,"test: infohash is",hexinfohash
68 url = 'http://127.0.0.1:'+str(self.session.get_listen_port())+'/'
69 print >> sys.stderr,"test: tracker lives at",url
74 # WARNING: this test depends on the output of the tracker. If that
75 # is changed, also change this.
76 print >> sys.stderr,"test: tracker returned:",data
78 self.assert_(data.find(hexinfohash) != -1)
80 self.assert_(data.find(hexinfohash) == -1)
82 def check_disk_presence(self,hexinfohash,present):
83 itrackerdir = os.path.join(self.session.get_state_dir(),STATEDIR_ITRACKER_DIR)
84 for filename in os.listdir(itrackerdir):
85 if filename.startswith(hexinfohash):
95 def subtest_tlookup1(self):
97 httpseeds.append('http://www.example.com/file.wmv')
98 self._test_tlookup(httpseeds)
100 def subtest_tlookup2(self):
102 httpseeds.append('http://www.example.com/file.wmv')
103 httpseeds.append('http://icecast.freezer.com/file.wmv')
104 self._test_tlookup(httpseeds)
107 def _test_tlookup(self,httpseedlist):
109 fn = os.path.join(os.getcwd(),"file.wmv")
111 t.set_tracker(self.session.get_internal_tracker_url())
112 t.set_urllist(httpseedlist)
114 wantdata = bencode(t.get_metainfo())
116 self.session.add_to_internal_tracker(t)
119 (scheme, netloc, path, pars, query, fragment) = urlparse.urlparse(self.session.get_internal_tracker_url())
120 urlprefix = scheme+'://'+netloc+'/tlookup?'
121 for httpseed in httpseedlist:
122 quoted = urllib.quote(httpseed)
123 url = urlprefix+quoted
124 #url = "http://www.cs.vu.nl/~arno/index.html"
125 print >>sys.stderr,"test: Asking tracker for",url
126 # F*ing BitTornado/Python crap: using normal urlopen here results in
127 # an infinitely hanging read (even if read(1024))
129 gotdata = conn.read()
130 print >>sys.stderr,"test: Tracker sent",len(gotdata)
132 self.assertEquals(wantdata,gotdata)
136 suite = unittest.TestSuite()
137 suite.addTest(unittest.makeSuite(TestTracking))
141 if __name__ == "__main__":