summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkali <kali@leap.se>2012-08-24 01:01:06 +0900
committerkali <kali@leap.se>2012-08-24 01:01:06 +0900
commitc2955d9655e3e7ccb3c5fc9e7d00a0d493d48a40 (patch)
treeff21bf162a97b0cc448775d597b619226f191dfc
parentbd154da54eb022d12d225a84cea1053f868eab56 (diff)
parentdc10833bedcdecf081a7c79678614c5521445164 (diff)
Merge branch 'get-definition.json' into providers-static
adjusted some loose ends to make tests pass again. this merge is still half-baked regarding functionality: I've left in place old Configuration class used by some tests antialias did on his branch. I plan to merge that functionality with the more recent JSONConfig and Spec classes. Conflicts: src/leap/base/configuration.py src/leap/eip/config.py src/leap/eip/tests/test_config.py
-rw-r--r--src/leap/base/config.py71
-rw-r--r--src/leap/base/tests/test_config.py185
-rw-r--r--src/leap/baseapp/mainwindow.py13
-rw-r--r--src/leap/eip/tests/test_config.py124
-rw-r--r--src/leap/testing/basetest.py19
5 files changed, 282 insertions, 130 deletions
diff --git a/src/leap/base/config.py b/src/leap/base/config.py
index 9493d511..dbd2e2c0 100644
--- a/src/leap/base/config.py
+++ b/src/leap/base/config.py
@@ -167,6 +167,7 @@ def get_config_json(config_file=None):
@rtype: dictionary
"""
if not config_file:
+ #TODO: NOT SURE WHAT this default should be, if anything
fpath = get_config_file('eip.json')
if not os.path.isfile(fpath):
dpath, cfile = os.path.split(fpath)
@@ -174,11 +175,11 @@ def get_config_json(config_file=None):
mkdir_p(dpath)
with open(fpath, 'wb') as configfile:
configfile.flush()
- config_file = open(fpath)
+ return json.load(open(fpath))
- config = json.load(config_file)
-
- return config
+ else:
+ #TODO: add validity checks of file
+ return json.load(open(config_file))
def get_definition_file(url=None):
@@ -187,3 +188,65 @@ def get_definition_file(url=None):
#TODO: determine good default location of definition file.
r = requests.get(url)
return r.json
+
+
+def is_internet_up():
+ """TODO: Build more robust network diagnosis capabilities
+ """
+ try:
+ requests.get('http://128.30.52.45', timeout=1)
+ return True
+ except requests.Timeout: # as err:
+ pass
+ return False
+
+#
+# XXX merge conflict
+# tests are still using this deprecated Configuration object.
+# moving it here transiently until I clean merge commit.
+# -- kali 2012-08-24 00:32
+#
+
+
+class Configuration(object):
+ """
+ All configurations (providers et al) will be managed in this class.
+ """
+ def __init__(self, provider_url=None):
+ try:
+ self.providers = {}
+ self.error = False
+ provider_file = self.check_and_get_definition_file(provider_url)
+ self.providers['default'] = get_config_json(provider_file)
+ except (requests.HTTPError, requests.RequestException) as e:
+ self.error = e.message
+ except requests.ConnectionError as e:
+ if e.message == "[Errno 113] No route to host":
+ if not is_internet_up:
+ self.error = "No valid internet connection found"
+ else:
+ self.error = "Provider server appears currently down."
+
+ def check_and_get_definition_file(self, provider_url):
+ """
+ checks if provider definition.json file is present.
+ if not downloads one from the web.
+ """
+ default_provider_path = get_default_provider_path()
+
+ if not os.path.isdir(default_provider_path):
+ mkdir_p(default_provider_path)
+
+ definition_file = get_config_file(
+ 'definition.json',
+ folder=default_provider_path)
+
+ if os.path.isfile(definition_file):
+ return
+
+ else:
+ r = requests.get(provider_url)
+ r.raise_for_status()
+ with open(definition_file, 'wb') as f:
+ f.write(json.dumps(r.json, indent=4))
+ return definition_file
diff --git a/src/leap/base/tests/test_config.py b/src/leap/base/tests/test_config.py
new file mode 100644
index 00000000..c5231de2
--- /dev/null
+++ b/src/leap/base/tests/test_config.py
@@ -0,0 +1,185 @@
+import os
+import platform
+import socket
+import tempfile
+
+import mock
+import requests
+
+from leap.base import config
+from leap.testing.basetest import BaseLeapTest
+
+
+try:
+ import unittest2 as unittest
+except ImportError:
+ import unittest
+
+_system = platform.system()
+
+
+class DefinitionTestCase(BaseLeapTest):
+
+ # XXX See how to merge with test_providers
+ # -- kali 2012-08-24 00:38
+
+ __name__ = "provider_config_tests"
+
+ def setUp(self):
+ self.old_home = os.environ['HOME']
+ self.home = tempfile.mkdtemp()
+ os.environ['HOME'] = self.home
+ pass
+
+ #Not correct removing the test directories but will be refactor out
+ #with kali's new test classes
+ def tearDown(self):
+ os.environ['HOME'] = self.old_home
+ pass
+
+ 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 = {
+ 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)
+
+ 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)
+
+ 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)
+
+ def test_invalid_url(self):
+ cf = config.Configuration("ht")
+ self.assertTrue(cf.error)
+
+
+class ConfigHelperFunctions(BaseLeapTest):
+
+ __name__ = "config_helper_tests"
+
+ def setUp(self):
+ pass
+
+ def tearDown(self):
+ pass
+
+ #
+ # 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' %
+ config.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(),
+ #XXX not correct!!!
+ #hardcoded home
+ '/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/' %
+ config.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
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/src/leap/baseapp/mainwindow.py b/src/leap/baseapp/mainwindow.py
index bc844437..912a51b6 100644
--- a/src/leap/baseapp/mainwindow.py
+++ b/src/leap/baseapp/mainwindow.py
@@ -11,6 +11,8 @@ from PyQt4.QtGui import (QMainWindow, QWidget, QVBoxLayout, QMessageBox,
QTextBrowser, qApp)
from PyQt4.QtCore import (pyqtSlot, pyqtSignal, QTimer)
+from leap.base.configuration import Configuration
+
from leap.baseapp.dialogs import ErrorDialog
from leap.eip import exceptions as eip_exceptions
@@ -18,6 +20,9 @@ from leap.eip.eipconnection import EIPConnection
from leap.gui import mainwindow_rc
+#TODO: Get rid of this and do something clever
+DEFAULT_PROVIDER_URL = "http://localhost/definition.json"
+
class LeapWindow(QMainWindow):
#XXX tbd: refactor into model / view / controller
@@ -30,6 +35,8 @@ class LeapWindow(QMainWindow):
super(LeapWindow, self).__init__()
self.debugmode = getattr(opts, 'debug', False)
+ self.configuration = Configuration()
+
self.vpn_service_started = False
self.createWindowHeader()
@@ -81,6 +88,12 @@ class LeapWindow(QMainWindow):
# bunch of self checks.
# XXX move somewhere else alltogether.
#
+ if self.configuration.error is True:
+ dialog = ErrorDialog()
+ dialog.criticalMessage(
+ 'There is a problem with the default '
+ 'definition.json file',
+ 'error')
if self.conductor.missing_provider is True:
dialog = ErrorDialog()
diff --git a/src/leap/eip/tests/test_config.py b/src/leap/eip/tests/test_config.py
index 2b949a19..3c5a1cde 100644
--- a/src/leap/eip/tests/test_config.py
+++ b/src/leap/eip/tests/test_config.py
@@ -14,12 +14,6 @@ from leap.eip import config as eip_config
_system = platform.system()
-#
-# XXX we moved a lot of stuff from eip_config
-# to base_config.
-# We should move most of these tests too.
-#
-
class EIPConfigTest(BaseLeapTest):
@@ -35,19 +29,6 @@ class EIPConfigTest(BaseLeapTest):
# helpers
#
- def get_username(self):
- return base_config.get_username()
-
- def get_groupname(self):
- return base_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.tempfile,
@@ -87,102 +68,6 @@ class EIPConfigTest(BaseLeapTest):
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(
- base_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(
- base_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?
- """
- #XXX bad home assumption
- self.assertEqual(
- base_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
- """
- base_config.validate_ip('3.3.3.3')
- with self.assertRaises(socket.error):
- base_config.validate_ip('255.255.255.256')
- with self.assertRaises(socket.error):
- base_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
@@ -197,15 +82,6 @@ class EIPConfigTest(BaseLeapTest):
self.assertEqual(command, 'openvpn')
self.assertEqual(args, self.get_expected_openvpn_args())
- # json config
-
- def test_get_config_json(self):
- config_js = base_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()
diff --git a/src/leap/testing/basetest.py b/src/leap/testing/basetest.py
index 1ea26363..a55b0525 100644
--- a/src/leap/testing/basetest.py
+++ b/src/leap/testing/basetest.py
@@ -1,4 +1,5 @@
import os
+import platform
import shutil
import tempfile
@@ -7,6 +8,10 @@ try:
except ImportError:
import unittest
+from leap.base.config import get_username, get_groupname
+
+_system = platform.system()
+
class BaseLeapTest(unittest.TestCase):
@@ -43,5 +48,15 @@ class BaseLeapTest(unittest.TestCase):
def get_tempfile(self, filename):
return os.path.join(self.tempdir, filename)
-if __name__ == "__main__":
- unittest.main()
+ def get_username(self):
+ return get_username()
+
+ def get_groupname(self):
+ return 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)