From 7d63e687e4c3b6e4da4599a7e685dc5c44457a64 Mon Sep 17 00:00:00 2001 From: drebs Date: Sat, 13 Apr 2013 12:33:59 -0300 Subject: Use BaseConfig for configuring Soledad. --- src/leap/soledad/tests/__init__.py | 18 ++++- src/leap/soledad/tests/test_crypto.py | 82 ++++++++----------- src/leap/soledad/tests/test_soledad.py | 143 +++++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+), 50 deletions(-) create mode 100644 src/leap/soledad/tests/test_soledad.py (limited to 'src/leap/soledad/tests') diff --git a/src/leap/soledad/tests/__init__.py b/src/leap/soledad/tests/__init__.py index 8ceafe99..1dbbe8ab 100644 --- a/src/leap/soledad/tests/__init__.py +++ b/src/leap/soledad/tests/__init__.py @@ -31,9 +31,7 @@ class BaseSoledadTest(BaseLeapTest): self._db2 = u1db.open(self.db2_file, create=True, document_factory=LeapDocument) # initialize soledad by hand so we can control keys - self._soledad = Soledad(self.email, gnupg_home=self.gnupg_home, - bootstrap=False, - prefix=self.tempdir) + self._soledad = self._soledad_instance(user=self.email) self._soledad._init_dirs() self._soledad._gpg = GPGWrapper(gnupghome=self.gnupg_home) #self._soledad._gpg.import_keys(PUBLIC_KEY) @@ -49,6 +47,20 @@ class BaseSoledadTest(BaseLeapTest): self._db2.close() self._soledad.close() + def _soledad_instance(self, user='leap@leap.se', prefix='', + bootstrap=False, gnupg_home='/gnupg', + secret_path='/secret.gpg', + local_db_path='/soledad.u1db'): + return Soledad( + user, + gnupg_home=self.tempdir+prefix+gnupg_home, + secret_path=self.tempdir+prefix+secret_path, + local_db_path=self.tempdir+prefix+local_db_path, + bootstrap=bootstrap) + + def _gpgwrapper_instance(self): + return GPGWrapper(gnupghome="%s/gnupg" % self.tempdir) + # Key material for testing KEY_FINGERPRINT = "E36E738D69173C13D709E44F2F455E2824D18DDF" diff --git a/src/leap/soledad/tests/test_crypto.py b/src/leap/soledad/tests/test_crypto.py index fdecbeef..919ec88c 100644 --- a/src/leap/soledad/tests/test_crypto.py +++ b/src/leap/soledad/tests/test_crypto.py @@ -1,4 +1,29 @@ +# -*- coding: utf-8 -*- +# test_crypto.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 . + + +""" +Tests for cryptographic related stuff. +""" + + import os + + from leap.common.testing.basetest import BaseLeapTest from leap.soledad.backends.leap_backend import LeapDocument from leap.soledad.tests import BaseSoledadTest @@ -6,10 +31,7 @@ from leap.soledad.tests import ( KEY_FINGERPRINT, PRIVATE_KEY, ) -from leap.soledad import ( - Soledad, - KeyAlreadyExists, -) +from leap.soledad import KeyAlreadyExists from leap.soledad.util import GPGWrapper try: @@ -93,8 +115,7 @@ class RecoveryDocumentTestCase(BaseSoledadTest): def test_import_recovery_document_raw(self): rd = self._soledad.export_recovery_document(None) gnupg_home = self.gnupg_home = "%s/gnupg2" % self.tempdir - s = Soledad('anotheruser@leap.se', gnupg_home=gnupg_home, - bootstrap=False, prefix=self.tempdir) + s = self._soledad_instance(user='anotheruser@leap.se', prefix='/2') s._init_dirs() s._gpg = GPGWrapper(gnupghome=gnupg_home) s.import_recovery_document(rd, None) @@ -119,8 +140,7 @@ class RecoveryDocumentTestCase(BaseSoledadTest): def test_import_recovery_document_crypt(self): rd = self._soledad.export_recovery_document('123456') gnupg_home = self.gnupg_home = "%s/gnupg2" % self.tempdir - s = Soledad('anotheruser@leap.se', gnupg_home=gnupg_home, - bootstrap=False, prefix=self.tempdir) + s = self._soledad_instance(user='anotheruser@leap.se') s._init_dirs() s._gpg = GPGWrapper(gnupghome=gnupg_home) s.import_recovery_document(rd, '123456') @@ -143,52 +163,19 @@ class RecoveryDocumentTestCase(BaseSoledadTest): ) -class SoledadAuxMethods(BaseLeapTest): - - def setUp(self): - pass - - def tearDown(self): - pass - - def _soledad_instance(self, prefix=None): - return Soledad('leap@leap.se', bootstrap=False, - prefix=prefix or self.tempdir+'/soledad') - - def _gpgwrapper_instance(self): - return GPGWrapper(gnupghome="%s/gnupg" % self.tempdir) - - def test__init_dirs(self): - sol = self._soledad_instance() - sol._init_dirs() - self.assertTrue(os.path.isdir(sol.prefix)) - - def test__init_db(self): - sol = self._soledad_instance() - sol._init_dirs() - sol._gpg = self._gpgwrapper_instance() - #self._soledad._gpg.import_keys(PUBLIC_KEY) - if not sol._has_privkey(): - sol._set_privkey(PRIVATE_KEY) - if not sol._has_symkey(): - sol._gen_symkey() - sol._load_symkey() - sol._init_db() - from leap.soledad.backends.sqlcipher import SQLCipherDatabase - self.assertIsInstance(sol._db, SQLCipherDatabase) +class CryptoMethodsTestCase(BaseSoledadTest): def test__gen_privkey(self): - sol = self._soledad_instance() + sol = self._soledad_instance(user='user@leap.se', prefix='/4') sol._init_dirs() sol._gpg = GPGWrapper(gnupghome="%s/gnupg2" % self.tempdir) self.assertFalse(sol._has_privkey(), 'Should not have a private key ' 'at this point.') - sol._set_privkey(PRIVATE_KEY) + sol._gen_privkey() self.assertTrue(sol._has_privkey(), 'Could not generate privkey.') def test__gen_symkey(self): - sol = Soledad('leap@leap.se', bootstrap=False, - prefix=self.tempdir+'/soledad3') + sol = self._soledad_instance(user='user@leap.se', prefix='/3') sol._init_dirs() sol._gpg = GPGWrapper(gnupghome="%s/gnupg3" % self.tempdir) if not sol._has_privkey(): @@ -199,11 +186,12 @@ class SoledadAuxMethods(BaseLeapTest): self.assertTrue(sol._has_symkey(), "Could not generate symkey.") def test__has_keys(self): - sol = self._soledad_instance() + sol = self._soledad_instance(user='leap@leap.se', prefix='/5') sol._init_dirs() - sol._gpg = self._gpgwrapper_instance() + sol._gpg = GPGWrapper(gnupghome=self.tempdir+"/5/gnupg") self.assertFalse(sol._has_keys()) sol._set_privkey(PRIVATE_KEY) + sol._has_privkey() self.assertFalse(sol._has_keys()) sol._gen_symkey() self.assertTrue(sol._has_keys()) diff --git a/src/leap/soledad/tests/test_soledad.py b/src/leap/soledad/tests/test_soledad.py new file mode 100644 index 00000000..92d5182b --- /dev/null +++ b/src/leap/soledad/tests/test_soledad.py @@ -0,0 +1,143 @@ +# -*- coding: utf-8 -*- +# test_soledad.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 . + + +""" +Tests for general Soledad functionality. +""" + + +import os +import re +import tempfile +try: + import simplejson as json +except ImportError: + import json # noqa + + +from leap.soledad.tests import BaseSoledadTest +from leap.soledad import Soledad + + +class AuxMethodsTestCase(BaseSoledadTest): + + def test__init_dirs(self): + sol = self._soledad_instance(prefix='/_init_dirs') + sol._init_dirs() + local_db_dir = os.path.dirname(sol._config.get_local_db_path()) + gnupg_home = os.path.dirname(sol._config.get_gnupg_home()) + secret_path = os.path.dirname(sol._config.get_secret_path()) + self.assertTrue(os.path.isdir(local_db_dir)) + self.assertTrue(os.path.isdir(gnupg_home)) + self.assertTrue(os.path.isdir(secret_path)) + + def test__init_db(self): + sol = self._soledad_instance() + sol._init_dirs() + sol._gpg = self._gpgwrapper_instance() + #self._soledad._gpg.import_keys(PUBLIC_KEY) + if not sol._has_privkey(): + sol._set_privkey(PRIVATE_KEY) + if not sol._has_symkey(): + sol._gen_symkey() + sol._load_symkey() + sol._init_db() + from leap.soledad.backends.sqlcipher import SQLCipherDatabase + self.assertIsInstance(sol._db, SQLCipherDatabase) + + def test__init_config_default(self): + """ + Test if configuration defaults point to the correct place. + """ + sol = Soledad(user='leap@leap.se', bootstrap=False) + self.assertTrue(bool(re.match( + '.*/\.config/leap/soledad/gnupg', sol._config.get_gnupg_home()))) + self.assertTrue(bool(re.match( + '.*/\.config/leap/soledad/secret.gpg', + sol._config.get_secret_path()))) + self.assertTrue(bool(re.match( + '.*/\.config/leap/soledad/soledad.u1db', + sol._config.get_local_db_path()))) + self.assertEqual( + 'http://provider/soledad/shared', + sol._config.get_shared_db_url()) + + def test__init_config_defaults(self): + """ + Test if configuration defaults point to the correct place. + """ + # we use regexp match here because HOME environment variable is + # changed by the BaseLeapTest class but BaseConfig does not capture + # that change. + sol = Soledad(user='leap@leap.se', bootstrap=False) + self.assertTrue(bool(re.match( + '.*/\.config/leap/soledad/gnupg', sol._config.get_gnupg_home()))) + self.assertTrue(bool(re.match( + '.*/\.config/leap/soledad/secret.gpg', + sol._config.get_secret_path()))) + self.assertTrue(bool(re.match( + '.*/\.config/leap/soledad/soledad.u1db', + sol._config.get_local_db_path()))) + self.assertEqual( + 'http://provider/soledad/shared', + sol._config.get_shared_db_url()) + + def test__init_config_from_file(self): + """ + Test if configuration is correctly read from file. + """ + # we use regexp match here because HOME environment variable is + # changed by the BaseLeapTest class but BaseConfig does not capture + # that change. + config_values = { + "gnupg_home": "value_1", + "secret_path": "value_2", + "local_db_path": "value_3", + "shared_db_url": "value_4" + } + tmpfile = tempfile.mktemp(dir=self.tempdir) + f = open(tmpfile, 'w') + f.write(json.dumps(config_values)) + f.close() + sol = Soledad( + user='leap@leap.se', + bootstrap=False, + config_path=tmpfile) + self.assertEqual('value_1', sol._config.get_gnupg_home()) + self.assertEqual('value_2', sol._config.get_secret_path()) + self.assertEqual('value_3', sol._config.get_local_db_path()) + self.assertEqual('value_4', sol._config.get_shared_db_url()) + + def test__init_config_from_params(self): + """ + Test if configuration is correctly read from file. + """ + # we use regexp match here because HOME environment variable is + # changed by the BaseLeapTest class but BaseConfig does not capture + # that change. + sol = Soledad( + user='leap@leap.se', + bootstrap=False, + gnupg_home='value_4', + secret_path='value_3', + local_db_path='value_2', + shared_db_url='value_1') + self.assertEqual('value_4', sol._config.get_gnupg_home()) + self.assertEqual('value_3', sol._config.get_secret_path()) + self.assertEqual('value_2', sol._config.get_local_db_path()) + self.assertEqual('value_1', sol._config.get_shared_db_url()) -- cgit v1.2.3