[Pkg-privacy-commits] [pyptlib] 51/136: Write up some unit tests.

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:25:06 UTC 2015


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository pyptlib.

commit 5cc361cdc2205c08f6ee35c05e37974138ae7d15
Author: George Kadianakis <desnacked at riseup.net>
Date:   Fri Oct 5 00:08:00 2012 -0400

    Write up some unit tests.
---
 pyptlib/server_config.py    |   7 +-
 pyptlib/test/test_client.py |  54 ++++++++++++++
 pyptlib/test/test_server.py | 169 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 228 insertions(+), 2 deletions(-)

diff --git a/pyptlib/server_config.py b/pyptlib/server_config.py
index 57ee02e..c56e127 100644
--- a/pyptlib/server_config.py
+++ b/pyptlib/server_config.py
@@ -52,8 +52,8 @@ class ServerConfig(config.Config):
         bindaddrs = self.get('TOR_PT_SERVER_BINDADDR').split(',')
         for bindaddr in bindaddrs:
             (transport_name, addrport) = bindaddr.split('-')
-            (addr, port) = get_addrport_from_string(addrport)
-            self.serverBindAddr[key] = (addr, port)
+            (addr, port) = self.get_addrport_from_string(addrport)
+            self.serverBindAddr[transport_name] = (addr, port)
 
         # Get transports.
         self.transports = self.get('TOR_PT_SERVER_TRANSPORTS').split(',')
@@ -61,6 +61,9 @@ class ServerConfig(config.Config):
             self.allTransportsEnabled = True
             self.transports.remove('*')
 
+        if sorted(self.transports) != sorted(self.serverBindAddr.keys()):
+            raise config.EnvError("Can't match transports with bind addresses (%s, %s)" % (self.transports, self.serverBindAddr.keys()))
+
     def getExtendedORPort(self):
         return self.extendedORPort
 
diff --git a/pyptlib/test/test_client.py b/pyptlib/test/test_client.py
new file mode 100644
index 0000000..4fa9af2
--- /dev/null
+++ b/pyptlib/test/test_client.py
@@ -0,0 +1,54 @@
+import os
+import unittest
+
+import pyptlib
+import pyptlib.client
+
+class testClient(unittest.TestCase):
+    def test_legit_environment(self):
+        """Legit environment"""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_CLIENT_TRANSPORTS" : "dummy" }
+
+        os.environ = TEST_ENVIRON
+        pyptlib.client.init(["dummy"])
+
+    def test_bad_environment(self):
+        """Missing TOR_PT_MANAGED_TRANSPORT_VER."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_CLIENT_TRANSPORTS" : "dummy" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.client.init, ["dummy"])
+
+    def test_bad_environment_2(self):
+        """Missing TOR_PT_CLIENT_TRANSPORTS."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.client.init, ["dummy"])
+
+    def test_unknown_transports(self):
+        """Unknown transports"""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_CLIENT_TRANSPORTS" : "are,you,a,badfish,too?" }
+
+        os.environ = TEST_ENVIRON
+        retval = pyptlib.client.init(["dummy"])
+        self.assertEqual(len(retval['transports']), 0)
+
+    def test_bad_protocol_version(self):
+        """Unsupported managed-proxy configuration protocol version."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "666",
+                         "TOR_PT_CLIENT_TRANSPORTS" : "dummy" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.client.init, ["dummy"])
+
+if __name__ == '__main__':
+    unittest.main()
+
diff --git a/pyptlib/test/test_server.py b/pyptlib/test/test_server.py
new file mode 100644
index 0000000..3838ebc
--- /dev/null
+++ b/pyptlib/test/test_server.py
@@ -0,0 +1,169 @@
+import os
+import unittest
+
+import pyptlib
+import pyptlib.server
+
+class testServer(unittest.TestCase):
+    def test_legit_environment(self):
+        """Legit environment."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "127.0.0.1:5555",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556,boom-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,boom" }
+
+        os.environ = TEST_ENVIRON
+        pyptlib.server.init(["dummy"])
+
+    def test_bad_environment(self):
+        """Missing TOR_PT_MANAGED_TRANSPORT_VER."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "127.0.0.1:5555",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556,boom-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,boom" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.server.init, ["dummy"])
+
+    def test_bad_environment_2(self):
+        """Missing TOR_PT_ORPORT."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "127.0.0.1:5555",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556,boom-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,boom" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.server.init, ["dummy"])
+
+    def test_bad_environment_3(self):
+        """Missing TOR_PT_EXTENDED_SERVER_PORT."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556,boom-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,boom" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.server.init, ["dummy"])
+
+    def test_bad_environment_4(self):
+        """TOR_PT_EXTENDED_SERVER_PORT not an addport."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "cakez",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556,boom-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,boom" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.server.init, ["dummy"])
+
+    def test_bad_environment_5(self):
+        """TOR_PT_ORPORT not an addport."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "",
+                         "TOR_PT_ORPORT" : "lulz",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556,boom-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,boom" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.server.init, ["dummy"])
+
+    def test_bad_environment_6(self):
+        """TOR_PT_SERVER_BINDADDR not an addport."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-lyrical_content,boom-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,boom" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.server.init, ["dummy"])
+
+    def test_bad_environment_7(self):
+        """Assymetric TOR_PT_SERVER_TRANSPORTS and TOR_PT_SERVER_BINDADDR."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556,laughs-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,boom" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.server.init, ["dummy"])
+
+    def test_bad_environment_8(self):
+        """Assymetric TOR_PT_SERVER_TRANSPORTS and TOR_PT_SERVER_BINDADDR."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556,laughs-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.server.init, ["dummy"])
+
+    def test_bad_environment_9(self):
+        """Assymetric TOR_PT_SERVER_TRANSPORTS and TOR_PT_SERVER_BINDADDR."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,laughs" }
+
+        os.environ = TEST_ENVIRON
+        self.assertRaises(pyptlib.config.EnvError, pyptlib.server.init, ["dummy"])
+
+    def test_disabled_extorport(self):
+        """Disabled TOR_PT_EXTENDED_SERVER_PORT."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556,boom-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,boom" }
+
+        os.environ = TEST_ENVIRON
+        retval = pyptlib.server.init(["dummy"])
+        self.assertIsNone(retval['ext_orport'])
+
+    def test_unknown_transport(self):
+        """Application only supports unknown transport."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "127.0.0.1:5555",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "dummy-127.0.0.1:5556,boom-127.0.0.1:6666",
+                         "TOR_PT_SERVER_TRANSPORTS" : "dummy,boom" }
+
+        os.environ = TEST_ENVIRON
+        retval = pyptlib.server.init(["inexistent"])
+        self.assertEqual(len(retval['transports']), 0)
+
+    def test_matched_transports(self):
+        """Application only supports some transport."""
+        TEST_ENVIRON = { "TOR_PT_STATE_LOCATION" : "/pt_stat",
+                         "TOR_PT_MANAGED_TRANSPORT_VER" : "1",
+                         "TOR_PT_EXTENDED_SERVER_PORT" : "127.0.0.1:5555",
+                         "TOR_PT_ORPORT" : "127.0.0.1:43210",
+                         "TOR_PT_SERVER_BINDADDR" : "midnight-127.0.0.1:5556,herbie-127.0.0.1:6666,landing-127.0.0.1:9999",
+                         "TOR_PT_SERVER_TRANSPORTS" : "midnight,herbie,landing" }
+
+        os.environ = TEST_ENVIRON
+        retval = pyptlib.server.init(["midnight","landing"])
+        self.assertIn("midnight",retval['transports'])
+        self.assertIn("landing",retval['transports'])
+        self.assertEquals(len(retval['transports']), 2)
+
+
+if __name__ == '__main__':
+    unittest.main()
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/pyptlib.git



More information about the Pkg-privacy-commits mailing list