From 16508819893ee97b01538cc23d98a444803b8494 Mon Sep 17 00:00:00 2001 From: twangboy Date: Thu, 15 Nov 2018 13:20:39 -0700 Subject: [PATCH 1/2] Fix tets_nacl on Py3 Move the nacl tests from integration to unit --- tests/integration/modules/test_nacl.py | 129 ------------------------- tests/unit/modules/test_nacl.py | 97 +++++++++++++++++++ 2 files changed, 97 insertions(+), 129 deletions(-) delete mode 100644 tests/integration/modules/test_nacl.py create mode 100644 tests/unit/modules/test_nacl.py diff --git a/tests/integration/modules/test_nacl.py b/tests/integration/modules/test_nacl.py deleted file mode 100644 index 3c5efaf777..0000000000 --- a/tests/integration/modules/test_nacl.py +++ /dev/null @@ -1,129 +0,0 @@ -# -*- coding: utf-8 -*- -''' -Tests for the nacl execution module -''' -# Import Python libs -from __future__ import absolute_import, print_function, unicode_literals - -import salt.utils.stringutils - -# Import Salt Testing libs -from tests.support.case import ModuleCase -from tests.support.unit import skipIf - -try: - import libnacl.secret # pylint: disable=unused-import - import libnacl.sealed # pylint: disable=unused-import - HAS_LIBNACL = True -except ImportError: - HAS_LIBNACL = False - - -@skipIf(not HAS_LIBNACL, 'skipping test_nacl, libnacl is unavailable') -class NaclTest(ModuleCase): - ''' - Test the nacl runner - ''' - def test_keygen(self): - ''' - Test keygen - ''' - # Store the data - ret = self.run_function( - 'nacl.keygen', - ) - self.assertIn('pk', ret) - self.assertIn('sk', ret) - - def test_enc_dec(self): - ''' - Generate keys, encrypt, then decrypt. - ''' - # Store the data - ret = self.run_function( - 'nacl.keygen', - ) - self.assertIn('pk', ret) - self.assertIn('sk', ret) - pk = ret['pk'] - sk = ret['sk'] - - unencrypted_data = salt.utils.stringutils.to_str('hello') - - # Encrypt with pk - ret = self.run_function( - 'nacl.enc', - data=unencrypted_data, - pk=pk, - ) - encrypted_data = ret - - # Decrypt with sk - ret = self.run_function( - 'nacl.dec', - data=encrypted_data, - sk=sk, - ) - self.assertEqual(unencrypted_data, ret) - - def test_sealedbox_enc_dec(self): - ''' - Generate keys, encrypt, then decrypt. - ''' - # Store the data - ret = self.run_function( - 'nacl.keygen', - ) - self.assertIn('pk', ret) - self.assertIn('sk', ret) - pk = ret['pk'] - sk = ret['sk'] - - unencrypted_data = salt.utils.stringutils.to_bytes('hello') - - # Encrypt with pk - ret = self.run_function( - 'nacl.sealedbox_encrypt', - data=unencrypted_data, - pk=pk, - ) - encrypted_data = ret - - # Decrypt with sk - ret = self.run_function( - 'nacl.sealedbox_decrypt', - data=encrypted_data, - sk=sk, - ) - self.assertEqual(unencrypted_data, ret) - - def test_secretbox_enc_dec(self): - ''' - Generate keys, encrypt, then decrypt. - ''' - # Store the data - ret = self.run_function( - 'nacl.keygen', - ) - self.assertIn('pk', ret) - self.assertIn('sk', ret) - pk = ret['pk'] - sk = ret['sk'] - - unencrypted_data = salt.utils.stringutils.to_bytes('hello') - - # Encrypt with pk - ret = self.run_function( - 'nacl.secretbox_encrypt', - data=unencrypted_data, - sk=sk, - ) - encrypted_data = ret - - # Decrypt with sk - ret = self.run_function( - 'nacl.secretbox_decrypt', - data=encrypted_data, - sk=sk, - ) - self.assertEqual(unencrypted_data, ret) diff --git a/tests/unit/modules/test_nacl.py b/tests/unit/modules/test_nacl.py new file mode 100644 index 0000000000..be4a9a0963 --- /dev/null +++ b/tests/unit/modules/test_nacl.py @@ -0,0 +1,97 @@ +# -*- coding: utf-8 -*- +''' +Tests for the nacl execution module +''' +# Import Python libs +from __future__ import absolute_import, print_function, unicode_literals + +# Import Salt libs +import salt.utils.stringutils +import salt.modules.nacl as nacl + +# Import Salt Testing libs +from tests.support.mixins import LoaderModuleMockMixin +from tests.support.unit import TestCase +from tests.support.unit import skipIf + +try: + import libnacl.secret # pylint: disable=unused-import + import libnacl.sealed # pylint: disable=unused-import + HAS_LIBNACL = True +except ImportError: + HAS_LIBNACL = False + + +@skipIf(not HAS_LIBNACL, 'skipping test_nacl, libnacl is unavailable') +class NaclTest(TestCase, LoaderModuleMockMixin): + ''' + Test the nacl runner + ''' + def setup_loader_modules(self): + self.unencrypted_data = salt.utils.stringutils.to_bytes('hello') + self.opts = salt.config.DEFAULT_MINION_OPTS + utils = salt.loader.utils(self.opts) + funcs = salt.loader.minion_mods(self.opts, utils=utils, whitelist=['nacl']) + + return { + nacl: { + '__opts__': self.opts, + '__utils__': utils, + '__salt__': funcs + }, + } + + def setUp(self): + # Generate the keys + ret = nacl.keygen() + self.assertIn('pk', ret) + self.assertIn('sk', ret) + self.pk = ret['pk'] + self.sk = ret['sk'] + + + def test_keygen(self): + ''' + Test keygen + ''' + self.assertEqual(len(self.pk), 44) + self.assertEqual(len(self.sk), 44) + + def test_enc_dec(self): + ''' + Generate keys, encrypt, then decrypt. + ''' + # Encrypt with pk + encrypted_data = nacl.enc(data=self.unencrypted_data, pk=self.pk) + + # Decrypt with sk + decrypted_data = nacl.dec(data=encrypted_data, sk=self.sk) + self.assertEqual(self.unencrypted_data, decrypted_data) + + def test_sealedbox_enc_dec(self): + ''' + Generate keys, encrypt, then decrypt. + ''' + # Encrypt with pk + encrypted_data = nacl.sealedbox_encrypt( + data=self.unencrypted_data, pk=self.pk) + + # Decrypt with sk + decrypted_data = nacl.sealedbox_decrypt( + data=encrypted_data, sk=self.sk) + + self.assertEqual(self.unencrypted_data, decrypted_data) + + def test_secretbox_enc_dec(self): + ''' + Generate keys, encrypt, then decrypt. + ''' + # Encrypt with sk + encrypted_data = nacl.secretbox_encrypt( + data=self.unencrypted_data, sk=self.sk) + + # Decrypt with sk + decrypted_data = nacl.secretbox_decrypt( + data=encrypted_data, sk=self.sk) + + self.assertEqual(self.unencrypted_data, decrypted_data) From 30cf8664b0ffac4337995cb9b329738bfd978cb6 Mon Sep 17 00:00:00 2001 From: twangboy Date: Thu, 15 Nov 2018 14:52:54 -0700 Subject: [PATCH 2/2] Fix some lint --- tests/unit/modules/test_nacl.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/modules/test_nacl.py b/tests/unit/modules/test_nacl.py index be4a9a0963..6d7505d3c2 100644 --- a/tests/unit/modules/test_nacl.py +++ b/tests/unit/modules/test_nacl.py @@ -49,7 +49,6 @@ class NaclTest(TestCase, LoaderModuleMockMixin): self.pk = ret['pk'] self.sk = ret['sk'] - def test_keygen(self): ''' Test keygen