summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/moznetwork/tests/test.py
blob: 79eee6b03874b49c533c2e25af37ef98e4531776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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()