summaryrefslogtreecommitdiff
path: root/src/leap/eip/tests/test_config.py
blob: 051faa29ac908d233974e94b9b6be43864afe085 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import ConfigParser
import os
import platform
import shutil
import socket
import tempfile

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from leap.eip import config

_system = platform.system()


class NotImplementedError(Exception):
    pass

# XXX use mock_open here?


class EIPConfigTest(unittest.TestCase):

    __name__ = "eip_config_tests"

    def setUp(self):
        self.old_path = os.environ['PATH']

        self.tdir = tempfile.mkdtemp()

        bin_tdir = os.path.join(
            self.tdir,
            'bin')
        os.mkdir(bin_tdir)
        os.environ['PATH'] = bin_tdir

    def tearDown(self):
        os.environ['PATH'] = self.old_path
        shutil.rmtree(self.tdir)
    #
    # helpers
    #

    def get_username(self):
        return config.get_username()

    def get_groupname(self):
        return config.get_groupname()

    def _missing_test_for_plat(self, do_raise=False):
        if do_raise:
            raise NotImplementedError(
                "This test is not implemented "
                "for the running platform: %s" %
                _system)

    def touch_exec(self):
        tfile = os.path.join(
            self.tdir,
            'bin',
            'openvpn')
        open(tfile, 'bw').close()

    def get_empty_config(self):
        _config = ConfigParser.ConfigParser()
        return _config

    def get_minimal_config(self):
        _config = ConfigParser.ConfigParser()
        return _config

    def get_expected_openvpn_args(self):
        args = []
        username = self.get_username()
        groupname = self.get_groupname()

        args.append('--user')
        args.append(username)
        args.append('--group')
        args.append(groupname)
        args.append('--management-client-user')
        args.append(username)
        args.append('--management-signal')
        args.append('--management')

        #XXX hey!
        #get platform switches here!
        args.append('/tmp/.eip.sock')
        args.append('unix')
        args.append('--config')
        #XXX bad assumption. FIXME: expand $HOME
        args.append('/home/%s/.config/leap/providers/default/openvpn.conf' %
                    username)
        return args

    #
    # tests
    #

    # XXX fixme! /home/user should
    # be replaced for proper home lookup.

    @unittest.skipUnless(_system == "Linux", "linux only")
    def test_lin_get_config_file(self):
        """
        config file path where expected? (linux)
        """
        self.assertEqual(
            config.get_config_file(
                'test', folder="foo/bar"),
            '/home/%s/.config/leap/foo/bar/test' %
            self.get_username())

    @unittest.skipUnless(_system == "Darwin", "mac only")
    def test_mac_get_config_file(self):
        """
        config file path where expected? (mac)
        """
        self._missing_test_for_plat(do_raise=True)

    @unittest.skipUnless(_system == "Windows", "win only")
    def test_win_get_config_file(self):
        """
        config file path where expected?
        """
        self._missing_test_for_plat(do_raise=True)

    #
    # XXX hey, I'm raising exceptions here
    # on purpose. just wanted to make sure
    # that the skip stuff is doing it right.
    # If you're working on win/macos tests,
    # feel free to remove tests that you see
    # are too redundant.

    @unittest.skipUnless(_system == "Linux", "linux only")
    def test_lin_get_config_dir(self):
        """
        nice config dir? (linux)
        """
        self.assertEqual(
            config.get_config_dir(),
            '/home/%s/.config/leap' %
            self.get_username())

    @unittest.skipUnless(_system == "Darwin", "mac only")
    def test_mac_get_config_dir(self):
        """
        nice config dir? (mac)
        """
        self._missing_test_for_plat(do_raise=True)

    @unittest.skipUnless(_system == "Windows", "win only")
    def test_win_get_config_dir(self):
        """
        nice config dir? (win)
        """
        self._missing_test_for_plat(do_raise=True)

    # provider paths

    @unittest.skipUnless(_system == "Linux", "linux only")
    def test_get_default_provider_path(self):
        """
        is default provider path ok?
        """
        self.assertEqual(
            config.get_default_provider_path(),
            '/home/%s/.config/leap/providers/default/' %
            self.get_username())

    # validate ip

    def test_validate_ip(self):
        """
        check our ip validation
        """
        config.validate_ip('3.3.3.3')
        with self.assertRaises(socket.error):
            config.validate_ip('255.255.255.256')
        with self.assertRaises(socket.error):
            config.validate_ip('foobar')

    @unittest.skip
    def test_validate_domain(self):
        """
        code to be written yet
        """
        pass

    # build command string
    # these tests are going to have to check
    # many combinations. we should inject some
    # params in the function call, to disable
    # some checks.

    def test_build_ovpn_command_empty_config(self):
        _config = self.get_empty_config()
        command, args = config.build_ovpn_command(
            _config,
            do_pkexec_check=False)
        self.assertEqual(command, 'openvpn')
        self.assertEqual(args, self.get_expected_openvpn_args())

    # json config

    def test_get_config_json(self):
        config_js = config.get_config_json()
        self.assertTrue(isinstance(config_js, dict))
        self.assertTrue('transport' in config_js)
        self.assertTrue('provider' in config_js)
        self.assertEqual(config_js['provider'], "testprovider.org")


if __name__ == "__main__":
    unittest.main()