summaryrefslogtreecommitdiff
path: root/src/leap/services/eip/tests/test_eipconfig.py
diff options
context:
space:
mode:
authorTomas Touceda <chiiph@leap.se>2013-05-07 17:13:50 -0300
committerTomas Touceda <chiiph@leap.se>2013-05-07 17:13:50 -0300
commitbed65dc627d1a064dcbd23e2ade581e91c7f1b28 (patch)
tree476a48110d06417ac34b474fa1346a8af2778593 /src/leap/services/eip/tests/test_eipconfig.py
parent4cc3c7d3f0d63d97df0d40c3e2d0941c5e069eb9 (diff)
parent3f304a2241196edda14deb9fcbe595b434a08ff2 (diff)
Merge remote-tracking branch 'kali/feature/sanitize-config' into develop
Diffstat (limited to 'src/leap/services/eip/tests/test_eipconfig.py')
-rw-r--r--src/leap/services/eip/tests/test_eipconfig.py192
1 files changed, 192 insertions, 0 deletions
diff --git a/src/leap/services/eip/tests/test_eipconfig.py b/src/leap/services/eip/tests/test_eipconfig.py
new file mode 100644
index 00000000..ce04c2fc
--- /dev/null
+++ b/src/leap/services/eip/tests/test_eipconfig.py
@@ -0,0 +1,192 @@
+# -*- coding: utf-8 -*-
+# test_eipconfig.py
+# Copyright (C) 2013 LEAP
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+"""
+tests for eipconfig
+"""
+import copy
+import json
+import os
+import unittest
+
+from leap.common.testing.basetest import BaseLeapTest
+from leap.services.eip.eipconfig import EIPConfig
+
+
+sample_config = {
+ "gateways": [
+ {
+ "capabilities": {
+ "adblock": False,
+ "filter_dns": True,
+ "limited": True,
+ "ports": [
+ "1194",
+ "443",
+ "53",
+ "80"
+ ],
+ "protocols": [
+ "tcp",
+ "udp"],
+ "transport": [
+ "openvpn"],
+ "user_ips": False},
+ "host": "host.dev.example.org",
+ "ip_address": "11.22.33.44",
+ "location": "cyberspace"
+ }],
+ "locations": {
+ "ankara": {
+ "country_code": "XX",
+ "hemisphere": "S",
+ "name": "Antarctica",
+ "timezone": "+2"
+ }
+ },
+ "openvpn_configuration": {
+ "auth": "SHA1",
+ "cipher": "AES-128-CBC",
+ "tls-cipher": "DHE-RSA-AES128-SHA"
+ },
+ "serial": 1,
+ "version": 1
+}
+
+
+class EIPConfigTest(BaseLeapTest):
+
+ __name__ = "eip_config_tests"
+ #provider = "testprovider.example.org"
+
+ maxDiff = None
+
+ def setUp(self):
+ pass
+
+ def tearDown(self):
+ pass
+
+ #
+ # helpers
+ #
+
+ def write_config(self, data):
+ self.configfile = os.path.join(
+ self.tempdir, "eipconfig.json")
+ conf = open(self.configfile, "w")
+ conf.write(json.dumps(data))
+ conf.close()
+
+ def test_load_valid_config(self):
+ """
+ load a sample config
+ """
+ self.write_config(sample_config)
+ config = EIPConfig()
+ self.assertRaises(
+ AssertionError,
+ config.get_clusters)
+ self.assertTrue(config.load(self.configfile))
+ self.assertEqual(
+ config.get_openvpn_configuration(),
+ sample_config["openvpn_configuration"])
+ self.assertEqual(
+ config.get_gateway_ip(),
+ "11.22.33.44")
+ self.assertEqual(config.get_version(), 1)
+ self.assertEqual(config.get_serial(), 1)
+ self.assertEqual(config.get_gateways(),
+ sample_config["gateways"])
+ self.assertEqual(
+ config.get_clusters(), None)
+
+ def test_sanitize_config(self):
+ """
+ check the sanitization of options
+ """
+ # extra parameters
+ data = copy.deepcopy(sample_config)
+ data['openvpn_configuration']["extra_param"] = "FOO"
+ self.write_config(data)
+ config = EIPConfig()
+ config.load(self.configfile)
+ self.assertEqual(
+ config.get_openvpn_configuration(),
+ sample_config["openvpn_configuration"])
+
+ # non allowed chars
+ data = copy.deepcopy(sample_config)
+ data['openvpn_configuration']["auth"] = "SHA1;"
+ self.write_config(data)
+ config = EIPConfig()
+ config.load(self.configfile)
+ self.assertEqual(
+ config.get_openvpn_configuration(),
+ sample_config["openvpn_configuration"])
+
+ # non allowed chars
+ data = copy.deepcopy(sample_config)
+ data['openvpn_configuration']["auth"] = "SHA1>`&|"
+ self.write_config(data)
+ config = EIPConfig()
+ config.load(self.configfile)
+ self.assertEqual(
+ config.get_openvpn_configuration(),
+ sample_config["openvpn_configuration"])
+
+ # lowercase
+ data = copy.deepcopy(sample_config)
+ data['openvpn_configuration']["auth"] = "shaSHA1"
+ self.write_config(data)
+ config = EIPConfig()
+ config.load(self.configfile)
+ self.assertEqual(
+ config.get_openvpn_configuration(),
+ sample_config["openvpn_configuration"])
+
+ # all characters invalid -> null value
+ data = copy.deepcopy(sample_config)
+ data['openvpn_configuration']["auth"] = "sha&*!@#;"
+ self.write_config(data)
+ config = EIPConfig()
+ config.load(self.configfile)
+ self.assertEqual(
+ config.get_openvpn_configuration(),
+ {'cipher': 'AES-128-CBC',
+ 'tls-cipher': 'DHE-RSA-AES128-SHA'})
+
+ # bad_ip
+ data = copy.deepcopy(sample_config)
+ data['gateways'][0]["ip_address"] = "11.22.33.44;"
+ self.write_config(data)
+ config = EIPConfig()
+ config.load(self.configfile)
+ self.assertEqual(
+ config.get_gateway_ip(),
+ None)
+
+ data = copy.deepcopy(sample_config)
+ data['gateways'][0]["ip_address"] = "11.22.33.44`"
+ self.write_config(data)
+ config = EIPConfig()
+ config.load(self.configfile)
+ self.assertEqual(
+ config.get_gateway_ip(),
+ None)
+
+if __name__ == "__main__":
+ unittest.main()