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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
import json
import os
import platform
import socket
#import tempfile
import mock
import requests
from leap.base import config
from leap.base import exceptions
from leap.util.fileutil import mkdir_p
from leap.testing.basetest import BaseLeapTest
try:
import unittest2 as unittest
except ImportError:
import unittest
_system = platform.system()
class ProviderTest(BaseLeapTest):
# override per test fixtures
def setUp(self):
pass
def tearDown(self):
pass
class BareHomeTestCase(ProviderTest):
__name__ = "provider_config_tests"
def test_should_raise_if_missing_eip_json(self):
with self.assertRaises(exceptions.MissingConfigFileError):
config.get_config_json(os.path.join(self.home, 'eip.json'))
class ProviderDefinitionTestCase(ProviderTest):
# XXX See how to merge with test_providers
# -- kali 2012-08-24 00:38
__name__ = "provider_config_tests"
def setUp(self):
# dump a sample eip file
# XXX Move to Use EIP Spec Instead!!!
EIP_JSON = {
"provider": "testprovider.org",
"transport": "openvpn",
"openvpn_protocol": "tcp",
"openvpn_port": "80",
"openvpn_ca_certificate": "~/.config/leap/testprovider.org/"
"keys/ca/testprovider-ca-cert-"
"2013-01-01.pem",
"openvpn_client_certificate": "~/.config/leap/testprovider.org/"
"keys/client/openvpn-2012-09-31.pem",
"connect_on_login": True,
"block_cleartext_traffic": True,
"primary_gateway": "usa_west",
"secondary_gateway": "france",
"management_password": "oph7Que1othahwiech6J"
}
path = os.path.join(self.home, '.config', 'leap')
mkdir_p(path)
with open(os.path.join(path, 'eip.json'), 'w') as fp:
json.dump(EIP_JSON, fp)
def test_complete_file(self):
with mock.patch.object(requests, "get") as mock_method:
mock_method.return_value.status_code = 200
mock_method.return_value.json = {
#XXX get from providers template
u'api_uri': u'https://api.testprovider.org/',
u'api_version': u'0.1.0',
u'ca_cert': u'8aab80ae4326fd30721689db813733783fe0bd7e',
u'ca_cert_uri': u'https://testprovider.org/cacert.pem',
u'description': {u'en': u'This is a test provider'},
u'display_name': {u'en': u'Test Provider'},
u'domain': u'testprovider.org',
u'enrollment_policy': u'open',
u'public_key': u'cb7dbd679f911e85bc2e51bd44afd7308ee19c21',
u'serial': 1,
u'services': [u'eip'],
u'version': u'0.1.0'}
cf = config.Configuration("http://localhost/")
self.assertIn('default', cf.providers)
#
# provider fetch tests block
#
# these tests below should move to wherever
# we put the fetcher for provider files and related stuff.
# TODO:
# - We're instantiating a ProviderTest because we're doing the home wipeoff
# on setUpClass instead of the setUp (for speedup of the general cases).
# We really should be testing all of them in the same testCase, and
# doing an extra wipe of the tempdir... but be careful!!!! do not mess with
# os.environ home more than needed... that could potentially bite!
class ProviderFetchConError(ProviderTest):
def test_connection_error(self):
with mock.patch.object(requests, "get") as mock_method:
mock_method.side_effect = requests.ConnectionError
cf = config.Configuration()
self.assertIsInstance(cf.error, str)
class ProviderFetchHttpError(ProviderTest):
def test_file_not_found(self):
with mock.patch.object(requests, "get") as mock_method:
mock_method.side_effect = requests.HTTPError
cf = config.Configuration()
self.assertIsInstance(cf.error, str)
class ProviderFetchInvalidUrl(ProviderTest):
def test_invalid_url(self):
cf = config.Configuration("ht")
self.assertTrue(cf.error)
# end provider fetch tests
class ConfigHelperFunctions(BaseLeapTest):
__name__ = "config_helper_tests"
def setUp(self):
pass
def tearDown(self):
pass
# tests
@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"),
os.path.expanduser(
'~/.config/leap/foo/bar/test')
)
@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(),
os.path.expanduser('~/.config/leap'))
@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(),
os.path.expanduser('~/.config/leap/providers/default/')
)
# 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
if __name__ == "__main__":
unittest.main()
|