test_nacl.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the nacl execution module
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import salt.utils.stringutils
  8. # Import Salt Testing libs
  9. from tests.support.case import ModuleCase
  10. from tests.support.unit import skipIf
  11. try:
  12. import libnacl.secret # pylint: disable=unused-import
  13. import libnacl.sealed # pylint: disable=unused-import
  14. HAS_LIBNACL = True
  15. except ImportError:
  16. HAS_LIBNACL = False
  17. @skipIf(not HAS_LIBNACL, 'skipping test_nacl, libnacl is unavailable')
  18. class NaclTest(ModuleCase):
  19. '''
  20. Test the nacl runner
  21. '''
  22. def test_keygen(self):
  23. '''
  24. Test keygen
  25. '''
  26. # Store the data
  27. ret = self.run_function(
  28. 'nacl.keygen',
  29. )
  30. self.assertIn('pk', ret)
  31. self.assertIn('sk', ret)
  32. def test_enc_dec(self):
  33. '''
  34. Generate keys, encrypt, then decrypt.
  35. '''
  36. # Store the data
  37. ret = self.run_function(
  38. 'nacl.keygen',
  39. )
  40. self.assertIn('pk', ret)
  41. self.assertIn('sk', ret)
  42. pk = ret['pk']
  43. sk = ret['sk']
  44. unencrypted_data = salt.utils.stringutils.to_str('hello')
  45. # Encrypt with pk
  46. ret = self.run_function(
  47. 'nacl.enc',
  48. data=unencrypted_data,
  49. pk=pk,
  50. )
  51. encrypted_data = ret
  52. # Decrypt with sk
  53. ret = self.run_function(
  54. 'nacl.dec',
  55. data=encrypted_data,
  56. sk=sk,
  57. )
  58. self.assertEqual(unencrypted_data, ret)