From 65b3497981d4971c683fcae6b80015d9bcff3a7e Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Fri, 18 Dec 2009 22:00:37 +0100 Subject: [PATCH] [unittest] add security UT module --- libraries/tests/run | 5 ++- libraries/tests/security.py | 76 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 libraries/tests/security.py diff --git a/libraries/tests/run b/libraries/tests/run index 4f0ed8ead..150fdc2d6 100755 --- a/libraries/tests/run +++ b/libraries/tests/run @@ -19,11 +19,12 @@ if "--debug" in sys.argv: etpUi['debug'] = True from tests import db, client, server, misc, fetchers, tools, i18n, spm, \ - qa, core + qa, core, security rc = 0 # Add to the list the module to test -mods = [db, client, server, misc, fetchers, tools, i18n, spm, qa, core] +mods = [db, client, server, misc, fetchers, tools, i18n, spm, qa, core, + security] tests = [] for mod in mods: diff --git a/libraries/tests/security.py b/libraries/tests/security.py new file mode 100644 index 000000000..df55e9c39 --- /dev/null +++ b/libraries/tests/security.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +import sys +sys.path.insert(0, 'client') +sys.path.insert(0, '../../client') +sys.path.insert(0, '.') +sys.path.insert(0, '../') +import os +import unittest +import tempfile +import shutil +from entropy.security import Repository +import tests._misc as _misc + +class SecurityTest(unittest.TestCase): + + def setUp(self): + """ + NOTE: this requires gnupg as test-dependency. + """ + self._gpg_home = tempfile.mkdtemp() + Repository.GPG_HOME = self._gpg_home + self._repository = Repository() + sys.stdout.write("%s called\n" % (self,)) + sys.stdout.flush() + + def tearDown(self): + """ + tearDown is run after each test + """ + del self._repository + shutil.rmtree(self._gpg_home, True) + sys.stdout.write("%s ran\n" % (self,)) + sys.stdout.flush() + + def test_gpg_handling(self): + + # available keys should be empty + self.assertEqual(self._repository.get_keys(), {}) + + # now fill + self._repository.create_keypair("foo.org", name_email = "foo@foo.org", + expiration_days = 10) + + # key created ? + self.assertNotEqual(self._repository.get_keys(), {}) + self.assert_(self._repository.is_pubkey_available("foo.org")) + + # sign file + rand_file = _misc.get_random_file() + asc_file = rand_file + ".asc" + self._repository.sign_file("foo.org", rand_file) + self._repository.verify_file("foo.org", rand_file, asc_file) + + # try to verify against wrong file + wrong_rand_file = _misc.get_random_file_md5() + self.assertEqual( + self._repository.verify_file("foo.org", wrong_rand_file, asc_file), + False) + + # now craft signature + with open(asc_file, "w") as asc_f: + asc_f.write("0") + asc_f.flush() + + self.assertEqual( + self._repository.verify_file("foo.org", rand_file, asc_file), + False) + + os.remove(asc_file) + +if __name__ == '__main__': + if "--debug" in sys.argv: + sys.argv.remove("--debug") + from entropy.const import etpUi + etpUi['debug'] = True + unittest.main()