[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Simplify test helper and assertions



commit 751d9e9b1c8e4eb305951e411137d89ba35a2328
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Oct 6 19:33:50 2019 -0700

    Simplify test helper and assertions
---
 test/integ/control/controller.py | 69 +++++++++++++++-------------------------
 1 file changed, 26 insertions(+), 43 deletions(-)

diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index c7394575..bcfc4606 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -1127,13 +1127,13 @@ class TestController(unittest.TestCase):
     runner = test.runner.get_runner()
 
     with runner.get_tor_controller() as controller:
-      # Try mapping one element, make sure the result is as expected.
+      # try mapping one element, ensuring results are as expected
 
       map1 = {'1.2.1.2': 'ifconfig.me'}
       x = controller.map_address(map1)
       self.assertEqual(x, map1)
 
-      # Try mapping two elements, make sure the result is as expected.
+      # try mapping two elements, ensuring results are as expected
 
       map2 = {'1.2.3.4': 'foobar.example.com',
               '1.2.3.5': 'barfuzz.example.com'}
@@ -1141,81 +1141,64 @@ class TestController(unittest.TestCase):
       x = controller.map_address(map2)
       self.assertEqual(x, map2)
 
-      # Try mapping zero elements, get an error.
+      # try mapping zero elements
 
       self.assertRaises(stem.InvalidRequest, controller.map_address, {})
 
-      # Try a virtual mapping to IPv4
+      # try a virtual mapping to IPv4, the default virtualaddressrange is 127.192.0.0/10
 
       map3 = {'0.0.0.0': 'quux'}
       x = controller.map_address(map3)
       self.assertEquals(len(x), 1)
       addr1, target = list(x.items())[0]
 
-      # The default IPv4 virtualaddressrange is 127.192.0.0/10
-
       self.assertTrue(addr1.startswith('127.'), '%s did not start with 127.' % addr1)
       self.assertEquals(target, 'quux')
 
-      # Try a virtual mapping to IPv6
+      # try a virtual mapping to IPv6, the default IPv6 virtualaddressrange is FE80::/10
 
       map4 = {'::': 'quibble'}
       x = controller.map_address(map4)
       self.assertEquals(len(x), 1)
       addr2, target = list(x.items())[0]
 
-      # The default IPv6 virtualaddressrange is FE80::/10
-
       self.assertTrue(addr2.startswith('[fe'), '%s did not start with [fe.' % addr2)
       self.assertEquals(target, 'quibble')
 
-      def parse_mapping_list(s):
-        # Helper function -- parse the response from getinfo address-mappings
-        # into a dict.
-
-        result = dict()
-
-        for line in s.split('\n'):
-          if not line.strip():
-            continue
+      def address_mappings(addr_type):
+        response = controller.get_info(['address-mappings/%s' % addr_type])
+        result = {}
 
+        for line in response['address-mappings/%s' % addr_type].splitlines():
           k, v, timeout = line.split()
           result[k] = v
 
         return result
 
-      # Ask for a list of all the address mappings we've added.
+      # ask for a list of all the address mappings we've added
 
-      x = controller.get_info(['address-mappings/control'])
-      m = parse_mapping_list(x['address-mappings/control'])
+      self.assertEquals({
+        '1.2.1.2': 'ifconfig.me',
+        '1.2.3.4': 'foobar.example.com',
+        '1.2.3.5': 'barfuzz.example.com',
+        addr1: 'quux',
+        addr2: 'quibble',
+      }, address_mappings('control'))
 
-      self.assertEquals(m['1.2.1.2'], 'ifconfig.me')
-      self.assertEquals(m['1.2.3.4'], 'foobar.example.com')
-      self.assertEquals(m['1.2.3.5'], 'barfuzz.example.com')
-      self.assertEquals(m[addr1], 'quux')
-      self.assertEquals(m[addr2], 'quibble')
+      # ask for a list of all the address mappings
 
-      # Ask for a list of all the address mappings.
-
-      x = controller.get_info(['address-mappings/all'])
-      m = parse_mapping_list(x['address-mappings/all'])
-
-      self.assertEquals(m['1.2.1.2'], 'ifconfig.me')
-      self.assertEquals(m['1.2.3.4'], 'foobar.example.com')
-      self.assertEquals(m['1.2.3.5'], 'barfuzz.example.com')
-      self.assertEquals(m[addr1], 'quux')
-      self.assertEquals(m[addr2], 'quibble')
+      self.assertEquals({
+        '1.2.1.2': 'ifconfig.me',
+        '1.2.3.4': 'foobar.example.com',
+        '1.2.3.5': 'barfuzz.example.com',
+        addr1: 'quux',
+        addr2: 'quibble',
+      }, address_mappings('all'))
 
       # Now ask for a list of only the mappings configured with the
       # configuration.  Ours should not be there.
 
-      x = controller.get_info(['address-mappings/config'])
-      m = parse_mapping_list(x['address-mappings/config'])
-
-      self.assertEquals(m.get('1.2.1.2'), None)
-      self.assertEquals(m.get('1.2.3.4'), None)
-      self.assertEquals(m.get(addr1), None)
-      self.assertEquals(m.get(addr2), None)
+      self.assertEquals({}, address_mappings('config'))
 
   @test.require.controller
   @test.require.online



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits