diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /testing/mozbase/moznetwork/tests/test.py | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'testing/mozbase/moznetwork/tests/test.py')
-rw-r--r-- | testing/mozbase/moznetwork/tests/test.py | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/testing/mozbase/moznetwork/tests/test.py b/testing/mozbase/moznetwork/tests/test.py new file mode 100644 index 000000000..79eee6b03 --- /dev/null +++ b/testing/mozbase/moznetwork/tests/test.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +""" +Unit-Tests for moznetwork +""" + +import os +import mock +import mozinfo +import moznetwork +import re +import subprocess +import unittest + + +def verify_ip_in_list(ip): + """ + Helper Method to check if `ip` is listed in Network Adresses + returned by ipconfig/ifconfig, depending on the platform in use + + :param ip: IPv4 address in the xxx.xxx.xxx.xxx format as a string + Example Usage: + verify_ip_in_list('192.168.0.1') + + returns True if the `ip` is in the list of IPs in ipconfig/ifconfig + """ + + # Regex to match IPv4 addresses. + # 0-255.0-255.0-255.0-255, note order is important here. + regexip = re.compile("((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}" + "(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)") + + if mozinfo.isLinux or mozinfo.isMac or mozinfo.isBsd: + # if "/sbin/ifconfig" exist, use it because it may not be in the + # PATH (at least on some linux platforms) + if os.path.isfile('/sbin/ifconfig') and os.access('/sbin/ifconfig', + os.X_OK): + args = ['/sbin/ifconfig'] + else: + args = ["ifconfig"] + + if mozinfo.isWin: + args = ["ipconfig"] + + ps = subprocess.Popen(args, stdout=subprocess.PIPE) + standardoutput, standarderror = ps.communicate() + + # Generate a list of IPs by parsing the output of ip/ifconfig + ip_list = [x.group() for x in re.finditer(regexip, standardoutput)] + + # Check if ip is in list + if ip in ip_list: + return True + else: + return False + + +class TestGetIP(unittest.TestCase): + + def test_get_ip(self): + """ Attempt to test the IP address returned by + moznetwork.get_ip() is valid """ + + ip = moznetwork.get_ip() + + # Check the IP returned by moznetwork is in the list + self.assertTrue(verify_ip_in_list(ip)) + + def test_get_ip_using_get_interface(self): + """ Test that the control flow path for get_ip() using + _get_interface_list() is works """ + + if mozinfo.isLinux or mozinfo.isMac: + + with mock.patch('socket.gethostbyname') as byname: + # Force socket.gethostbyname to return None + byname.return_value = None + + ip = moznetwork.get_ip() + + # Check the IP returned by moznetwork is in the list + self.assertTrue(verify_ip_in_list(ip)) + + +if __name__ == '__main__': + unittest.main() |