Example #1
0
 def test_normalize(self):
     """
     Test the `normalize` function.
     """
     # Test that the "normalize" function can be called via
     # apply_transformation.
     self.assertEqual(
         '192.168.0.1',
         apply_transformation('ipv4_address.normalize', '192.168.000.001'))
     # Test that leading zeros are removed.
     self.assertEqual(
         '10.0.0.1',
         normalize('010.000.000000.1'))
     self.assertEqual(
         '10.0.0.1/16',
         normalize('010.000.000000.1/016'))
     # Test the raise_error_if_malformed parameter.
     self.assertEqual(
         '192.168.0.1',
         normalize('192.168.0.1'))
     self.assertEqual(
         '192.168.0.1',
         normalize('192.168.0.1', raise_error_if_malformed=False))
     self.assertEqual(
         '192.168.0.1',
         normalize('192.168.0.1', raise_error_if_malformed=True))
     self.assertEqual(
         'not an IP address',
         normalize('not an IP address'))
     self.assertEqual(
         'not an IP address',
         normalize('not an IP address', raise_error_if_malformed=False))
     with self.assertRaises(ValueError):
         normalize('not an IP address', raise_error_if_malformed=True)
Example #2
0
 def test_strip_mask(self):
     """
     Test the `strip_mask` function.
     """
     # Test that the "strip_mask" function can be called via
     # apply_transformation.
     self.assertEqual(
         '192.168.0.1',
         apply_transformation('ipv4_address.strip_mask', '192.168.0.1/24'))
     # Test that an address is not implicitly normalized
     self.assertEqual('192.168.000.1', strip_mask('192.168.000.1/24'))
     # Test the raise_error_if_malformed option.
     self.assertEqual('192.168.0.1', strip_mask('192.168.0.1/24'))
     self.assertEqual(
         '192.168.0.1',
         strip_mask('192.168.0.1/24', raise_error_if_malformed=False))
     self.assertEqual(
         '192.168.0.1',
         strip_mask('192.168.0.1/24', raise_error_if_malformed=True))
     self.assertEqual('192.168.0.256/24', strip_mask('192.168.0.256/24'))
     self.assertEqual(
         '192.168.0.256/24',
         strip_mask('192.168.0.256/24', raise_error_if_malformed=False))
     with self.assertRaises(ValueError):
         strip_mask('192.168.0.256/24', raise_error_if_malformed=True)
     # Not having a mask should not result in an error.
     self.assertEqual(
         '192.168.0.1',
         strip_mask('192.168.0.1', raise_error_if_malformed=True))
Example #3
0
 def test_net_address(self):
     """
     Test the `net_address` function.
     """
     # Test that the "net_address" function can be called via
     # apply_transformation.
     self.assertEqual(
         '2001:db8::/32',
         apply_transformation('ipv6_address.net_address', '2001:db8::1/32'))
     # Test that an address is implicitly normalized
     self.assertEqual('2001:db8::/32', net_address('2001:DB8::1/32'))
     # Test the raise_error_if_malformed option.
     self.assertEqual('2001:db8::/32', net_address('2001:db8:0::1/32'))
     self.assertEqual(
         '2001:db8::/32',
         net_address('2001:db8:0::1/32', raise_error_if_malformed=False))
     self.assertEqual(
         '2001:db8::/32',
         net_address('2001:db8:0::1/32', raise_error_if_malformed=True))
     self.assertEqual('2001:db8:0:::1/32', net_address('2001:db8:0:::1/32'))
     self.assertEqual(
         '2001:db8:0:::1/32',
         net_address('2001:db8:0:::1/32', raise_error_if_malformed=False))
     with self.assertRaises(ValueError):
         net_address('2001:db8:0:::1/32', raise_error_if_malformed=True)
     # An address is also considered malformed if the mask is missing.
     with self.assertRaises(ValueError):
         net_address('2001:db8::', raise_error_if_malformed=True)
     # We also want to test two corner cases.
     self.assertEqual('2001:db8::1/128', net_address('2001:db8::1/128'))
     self.assertEqual('::/0', net_address('2001:db8::1/0'))
Example #4
0
 def test_apply_transformation(self):
     """
     Test the `apply_transformation` function.
     """
     self.assertEqual('ABC', apply_transformation('string.to_upper', 'aBc'))
     # Test that multiple positional arguments are passed correctly.
     self.assertEqual(
         'aBcdeF', apply_transformation('string.add_suffix', 'aBc', 'deF'))
     # Test that mixed of positional and keyword arguments are passed
     # correctly.
     self.assertEqual(
         'aBcdeF',
         apply_transformation('string.add_suffix', 'aBc', suffix='deF'))
     # Test that multiple keyword arugments are passed correctly.
     self.assertEqual(
         'aBcdeF',
         apply_transformation('string.add_suffix',
                              value='aBc',
                              suffix='deF'))
Example #5
0
 def test_hash(self):
     """
     Test the ``hash`` function.
     """
     # The sha512 method should be used by default.
     self.assertEqual(
         '$6$ER1w5uHGxSXlzqTs$SaPcEH603VqKUdnredBtfjY1lrTIcMJdwQc62yKWeXiY2'
         'vkSBinPQ3ZGaFtT1hN4hs3H4jmpGwVA0fwzAyauy0',
         apply_transformation('passlib.hash',
                              'test',
                              rounds=5000,
                              salt='ER1w5uHGxSXlzqTs'))
     # The sha256 method should also be available.
     self.assertEqual(
         '$5$4P5LdkrXcicslB.6$Ajx85AjQYx/MSDlgdFuoOyja2v0Cy7LqzHqAcqQ1ke.',
         apply_transformation('passlib.hash',
                              'test',
                              'sha256_crypt',
                              rounds=5000,
                              salt='4P5LdkrXcicslB.6'))
Example #6
0
 def test_to_int(self):
     """
     Test the `to_int` function.
     """
     # Test that the "to_int" function can be called via
     # apply_transformation.
     self.assertEqual(5, apply_transformation('misc.to_int', '5'))
     # Test the raise_error_if_malformed parameter.
     self.assertEqual(27, to_int('27'))
     self.assertEqual(27, to_int('27', raise_error_if_malformed=False))
     self.assertEqual(27, to_int('27', raise_error_if_malformed=True))
     self.assertEqual('foo', to_int('foo'))
     self.assertEqual('foo', to_int('foo', raise_error_if_malformed=False))
     with self.assertRaises(ValueError):
         to_int('foo', raise_error_if_malformed=True)
Example #7
0
 def test_broadcast_address(self):
     """
     Test the `broadcast_address` function.
     """
     # Test that the "broadcast_address" function can be called via
     # apply_transformation.
     self.assertEqual(
         '192.168.0.255',
         apply_transformation(
             'ipv4_address.broadcast_address', '192.168.0.1/24'))
     # Test that an address is implicitly normalized
     self.assertEqual(
         '192.168.0.255', broadcast_address('192.168.000.1/24'))
     # Test the raise_error_if_malformed option.
     self.assertEqual(
         '192.168.0.255', broadcast_address('192.168.000.1/24'))
     self.assertEqual(
         '192.168.0.255',
         broadcast_address(
             '192.168.000.1/24', raise_error_if_malformed=False))
     self.assertEqual(
         '192.168.0.255',
         broadcast_address(
             '192.168.000.1/24', raise_error_if_malformed=True))
     self.assertEqual(
         '192.168.0.256/24',
         broadcast_address('192.168.0.256/24'))
     self.assertEqual(
         '192.168.0.256/24',
         broadcast_address(
             '192.168.0.256/24', raise_error_if_malformed=False))
     with self.assertRaises(ValueError):
         broadcast_address(
             '192.168.0.256/24', raise_error_if_malformed=True)
     # An address is also considered malformed if the mask is missing.
     with self.assertRaises(ValueError):
         broadcast_address(
             '192.168.0.1', raise_error_if_malformed=True)
     # We also want to test two corner cases.
     self.assertEqual(
         '192.168.0.1',
         broadcast_address('192.168.0.1/32'))
     self.assertEqual(
         '255.255.255.255',
         broadcast_address('192.168.0.1/0'))
Example #8
0
 def test_normalize(self):
     """
     Test the `normalize` function.
     """
     # Test that the "normalize" function can be called via
     # apply_transformation.
     self.assertEqual(
         '0A:0B:0C:0D:0E:1F',
         apply_transformation('mac_address.normalize', '0a:0B:0c:d:0E:1f'))
     # Test the delimiter parameter.
     self.assertEqual('0A:0B:0C:0D:0E:1F', normalize('0a:0B:0c:d:0E:1f'))
     self.assertEqual('0A:0B:0C:0D:0E:1F',
                      normalize('0a:0B:0c:d:0E:1f', delimiter=':'))
     self.assertEqual('0A:0B:0C:0D:0E:1F',
                      normalize('0a:0B:0c:d:0E:1f', delimiter='colon'))
     self.assertEqual('0A-0B-0C-0D-0E-1F',
                      normalize('0a:0B:0c:d:0E:1f', delimiter='-'))
     self.assertEqual('0A-0B-0C-0D-0E-1F',
                      normalize('0a:0B:0c:d:0E:1f', delimiter='dash'))
     self.assertEqual('0A-0B-0C-0D-0E-1F',
                      normalize('0a:0B:0c:d:0E:1f', delimiter='minus'))
     # Test the target_case parameter.
     self.assertEqual('0A:0B:0C:0D:0E:1F', normalize('0a:0B:0c:d:0E:1f'))
     self.assertEqual('0A:0B:0C:0D:0E:1F',
                      normalize('0a:0B:0c:d:0E:1f', target_case='upper'))
     self.assertEqual('0a:0b:0c:0d:0e:1f',
                      normalize('0a:0B:0c:d:0E:1f', target_case='lower'))
     # Test the raise_error_if_malformed parameter.
     self.assertEqual('0A:0B:0C:0D:0E:1F', normalize('0a:0B:0c:d:0E:1f'))
     self.assertEqual(
         '0A:0B:0C:0D:0E:1F',
         normalize('0a:0B:0c:d:0E:1f', raise_error_if_malformed=False))
     self.assertEqual(
         '0A:0B:0C:0D:0E:1F',
         normalize('0a:0B:0c:d:0E:1f', raise_error_if_malformed=True))
     self.assertEqual('not a MAC address', normalize('not a MAC address'))
     self.assertEqual(
         'not a MAC address',
         normalize('not a MAC address', raise_error_if_malformed=False))
     with self.assertRaises(ValueError):
         normalize('not a MAC address', raise_error_if_malformed=True)