[tor-commits] [ooni-probe/develop] Write unittests also for Tor geoip stuff

isis at torproject.org isis at torproject.org
Thu Jun 6 16:41:37 UTC 2013


commit d73c5fa22a72413fd1c5d24eeb1e9118f0a4662a
Author: Arturo Filastò <art at fuffa.org>
Date:   Fri Mar 15 16:31:14 2013 +0100

    Write unittests also for Tor geoip stuff
---
 ooni/errors.py      |    3 +++
 ooni/geoip.py       |   16 ++++++++++++----
 tests/test_geoip.py |   45 +++++++++++++++++++++++++++++++++++++++------
 3 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/ooni/errors.py b/ooni/errors.py
index 62fcf93..69af726 100644
--- a/ooni/errors.py
+++ b/ooni/errors.py
@@ -138,3 +138,6 @@ class TorStateNotFound(Exception):
 
 class InsufficientPrivileges(Exception):
     pass
+
+class ProbeIPUnknown(Exception):
+    pass
diff --git a/ooni/geoip.py b/ooni/geoip.py
index e6a221b..ae06608 100644
--- a/ooni/geoip.py
+++ b/ooni/geoip.py
@@ -110,19 +110,22 @@ class ProbeIP(object):
         'maxmind': MaxMindGeoIP
     }
     address = None
+    tor_state = config.tor_state
 
     @defer.inlineCallbacks
     def lookup(self):
         try:
             yield self.askTor()
+            log.msg("Found your IP via Tor %s" % self.address)
             defer.returnValue(self.address)
         except errors.TorStateNotFound:
             log.debug("Tor is not running. Skipping IP lookup via Tor.")
-        except:
+        except Exception:
             log.msg("Unable to lookup the probe IP via Tor.")
 
         try:
             yield self.askTraceroute()
+            log.msg("Found your IP via Traceroute %s" % self.address)
             defer.returnValue(self.address)
         except errors.InsufficientPrivileges:
             log.debug("Cannot determine the probe IP address with a traceroute, becase of insufficient priviledges")
@@ -131,9 +134,11 @@ class ProbeIP(object):
 
         try:
             yield self.askGeoIPService()
+            log.msg("Found your IP via a GeoIP service: %s" % self.address)
             defer.returnValue(self.address)
-        except:
+        except Exception, e:
             log.msg("Unable to lookup the probe IP via GeoIPService")
+            raise e
 
     @defer.inlineCallbacks
     def askGeoIPService(self):
@@ -150,6 +155,9 @@ class ProbeIP(object):
             except Exception, e:
                 log.msg("Failed to lookup your IP via %s" % service_name)
 
+        if not self.address:
+            raise errors.ProbeIPUnknown
+
     def askTraceroute(self):
         """
         Perform a UDP traceroute to determine the probes IP address.
@@ -165,8 +173,8 @@ class ProbeIP(object):
         XXX this lookup method is currently broken when there are cached descriptors or consensus documents
         see: https://trac.torproject.org/projects/tor/ticket/8214
         """
-        if config.tor_state:
-            d = config.tor_state.protocol.get_info("address")
+        if self.tor_state:
+            d = self.tor_state.protocol.get_info("address")
             @d.addCallback
             def cb(result):
                 self.strategy = 'tor_get_info_address'
diff --git a/tests/test_geoip.py b/tests/test_geoip.py
index 443d262..0d7165f 100644
--- a/tests/test_geoip.py
+++ b/tests/test_geoip.py
@@ -1,5 +1,7 @@
+from collections import namedtuple
+
 from twisted.web import server, static, resource
-from twisted.internet import reactor
+from twisted.internet import reactor, defer
 from twisted.trial import unittest
 from twisted.python.filepath import FilePath
 from twisted.protocols.policies import WrappingFactory
@@ -58,7 +60,7 @@ class TestGeoIPServices(GeoIPBaseTest):
         gip = TorProjectGeoIP()
         gip.url = self.getUrl('torproject')
         d = gip.lookup()
-        @d.addCallback
+        @d.addBoth
         def cb(res):
             self.assertEqual(res, '127.0.0.1')
         return d
@@ -67,7 +69,7 @@ class TestGeoIPServices(GeoIPBaseTest):
         gip = UbuntuGeoIP()
         gip.url = self.getUrl('ubuntu')
         d = gip.lookup()
-        @d.addCallback
+        @d.addBoth
         def cb(res):
             self.assertEqual(res, '127.0.0.1')
         return d
@@ -76,7 +78,7 @@ class TestGeoIPServices(GeoIPBaseTest):
         gip = MaxMindGeoIP()
         gip.url = self.getUrl('maxmind')
         d = gip.lookup()
-        @d.addCallback
+        @d.addBoth
         def cb(res):
             self.assertEqual(res, '127.0.0.1')
         return d
@@ -93,7 +95,7 @@ class TestProbeIP(GeoIPBaseTest):
 
     def test_ask_geoip_service(self):
         d = self.probe_ip.askGeoIPService()
-        @d.addCallback
+        @d.addBoth
         def cb(res):
             self.assertEqual(self.probe_ip.address, '127.0.0.1')
         return d
@@ -102,7 +104,38 @@ class TestProbeIP(GeoIPBaseTest):
         self.assertRaises(errors.InsufficientPrivileges, self.probe_ip.askTraceroute)
 
     def test_ask_tor(self):
-        pass
+        class MockTorState(object):
+            """
+            This is a Mock Tor state object. It will just pretend to answer to
+            the get_info("address") method call.
+            """
+            protocol = namedtuple('Protocol', 'get_info')
+            def __init__(self):
+                def get_info(key):
+                    return defer.succeed({'XXX': '127.0.0.2'})
+                self.protocol = self.protocol(get_info=get_info)
+
+        self.probe_ip.tor_state = MockTorState()
+        d = self.probe_ip.lookup()
+        @d.addBoth
+        def cb(res):
+            self.assertEqual(self.probe_ip.address, '127.0.0.2')
+        return d
+
+    def test_probe_ip(self):
+        d = self.probe_ip.lookup()
+        @d.addBoth
+        def cb(res):
+            self.assertEqual(self.probe_ip.address, '127.0.0.1')
+            self.assertTrue(self.probe_ip.strategy.startswith('geo_ip_service-'))
+        return d
+
+    def test_failing_probe_ip(self):
+        self.probe_ip.geoIPServices = {}
+
+        d = self.probe_ip.lookup()
+        self.assertFailure(d, errors.ProbeIPUnknown)
+        return d
 
 class TestIPToLocation(unittest.TestCase):
     pass





More information about the tor-commits mailing list