mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 09:23:56 +00:00
Merge pull request #50528 from twangboy/fix_test_nacl
Fix `modules.test_nacl` on Py3
This commit is contained in:
commit
4fcc9277dd
@ -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)
|
96
tests/unit/modules/test_nacl.py
Normal file
96
tests/unit/modules/test_nacl.py
Normal file
@ -0,0 +1,96 @@
|
||||
# -*- 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)
|
Loading…
Reference in New Issue
Block a user