testing/mozbase/moznetwork/tests/test.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/usr/bin/env python
michael@0 2 """
michael@0 3 Unit-Tests for moznetwork
michael@0 4 """
michael@0 5
michael@0 6 import mock
michael@0 7 import mozinfo
michael@0 8 import moznetwork
michael@0 9 import re
michael@0 10 import subprocess
michael@0 11 import unittest
michael@0 12
michael@0 13
michael@0 14 def verify_ip_in_list(ip):
michael@0 15 """
michael@0 16 Helper Method to check if `ip` is listed in Network Adresses
michael@0 17 returned by ipconfig/ifconfig, depending on the platform in use
michael@0 18
michael@0 19 :param ip: IPv4 address in the xxx.xxx.xxx.xxx format as a string
michael@0 20 Example Usage:
michael@0 21 verify_ip_in_list('192.168.0.1')
michael@0 22
michael@0 23 returns True if the `ip` is in the list of IPs in ipconfig/ifconfig
michael@0 24 """
michael@0 25
michael@0 26 # Regex to match IPv4 addresses.
michael@0 27 # 0-255.0-255.0-255.0-255, note order is important here.
michael@0 28 regexip = re.compile("((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}"
michael@0 29 "(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)")
michael@0 30
michael@0 31 if mozinfo.isLinux or mozinfo.isMac or mozinfo.isBsd:
michael@0 32 args = ["ifconfig"]
michael@0 33
michael@0 34 if mozinfo.isWin:
michael@0 35 args = ["ipconfig"]
michael@0 36
michael@0 37 ps = subprocess.Popen(args, stdout=subprocess.PIPE)
michael@0 38 standardoutput, standarderror = ps.communicate()
michael@0 39
michael@0 40 # Generate a list of IPs by parsing the output of ip/ifconfig
michael@0 41 ip_list = [x.group() for x in re.finditer(regexip, standardoutput)]
michael@0 42
michael@0 43 # Check if ip is in list
michael@0 44 if ip in ip_list:
michael@0 45 return True
michael@0 46 else:
michael@0 47 return False
michael@0 48
michael@0 49
michael@0 50 class TestGetIP(unittest.TestCase):
michael@0 51
michael@0 52 def test_get_ip(self):
michael@0 53 """ Attempt to test the IP address returned by
michael@0 54 moznetwork.get_ip() is valid """
michael@0 55
michael@0 56 ip = moznetwork.get_ip()
michael@0 57
michael@0 58 # Check the IP returned by moznetwork is in the list
michael@0 59 self.assertTrue(verify_ip_in_list(ip))
michael@0 60
michael@0 61 def test_get_ip_using_get_interface(self):
michael@0 62 """ Test that the control flow path for get_ip() using
michael@0 63 _get_interface_list() is works """
michael@0 64
michael@0 65 if mozinfo.isLinux or mozinfo.isMac:
michael@0 66
michael@0 67 with mock.patch('socket.gethostbyname') as byname:
michael@0 68 # Force socket.gethostbyname to return None
michael@0 69 byname.return_value = None
michael@0 70
michael@0 71 ip = moznetwork.get_ip()
michael@0 72
michael@0 73 # Check the IP returned by moznetwork is in the list
michael@0 74 self.assertTrue(verify_ip_in_list(ip))
michael@0 75
michael@0 76
michael@0 77 if __name__ == '__main__':
michael@0 78 unittest.main()

mercurial