1
0

test_nacl.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for the salt-run command
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import logging
  7. import pytest
  8. from tests.support.case import ShellCase
  9. from tests.support.helpers import slowTest
  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, OSError, AttributeError):
  16. HAS_LIBNACL = False
  17. log = logging.getLogger(__name__)
  18. @skipIf(not HAS_LIBNACL, "skipping test_nacl, libnacl is unavailable")
  19. @pytest.mark.windows_whitelisted
  20. class NaclTest(ShellCase):
  21. """
  22. Test the nacl runner
  23. """
  24. @slowTest
  25. def test_keygen(self):
  26. """
  27. Test keygen
  28. """
  29. # Store the data
  30. ret = self.run_run_plus("nacl.keygen",)
  31. self.assertIn("pk", ret["return"])
  32. self.assertIn("sk", ret["return"])
  33. @slowTest
  34. def test_enc(self):
  35. """
  36. Test keygen
  37. """
  38. # Store the data
  39. ret = self.run_run_plus("nacl.keygen",)
  40. self.assertIn("pk", ret["return"])
  41. self.assertIn("sk", ret["return"])
  42. pk = ret["return"]["pk"]
  43. sk = ret["return"]["sk"]
  44. unencrypted_data = "hello"
  45. # Encrypt with pk
  46. ret = self.run_run_plus("nacl.enc", data=unencrypted_data, pk=pk,)
  47. self.assertIn("return", ret)
  48. @slowTest
  49. def test_enc_dec(self):
  50. """
  51. Store, list, fetch, then flush data
  52. """
  53. # Store the data
  54. ret = self.run_run_plus("nacl.keygen",)
  55. self.assertIn("pk", ret["return"])
  56. self.assertIn("sk", ret["return"])
  57. pk = ret["return"]["pk"]
  58. sk = ret["return"]["sk"]
  59. unencrypted_data = b"hello"
  60. # Encrypt with pk
  61. ret = self.run_run_plus("nacl.enc", data=unencrypted_data, pk=pk,)
  62. self.assertIn("return", ret)
  63. encrypted_data = ret["return"]
  64. # Decrypt with sk
  65. ret = self.run_run_plus("nacl.dec", data=encrypted_data, sk=sk,)
  66. self.assertIn("return", ret)
  67. self.assertEqual(unencrypted_data, ret["return"])
  68. @slowTest
  69. def test_sealedbox_enc_dec(self):
  70. """
  71. Generate keys, encrypt, then decrypt.
  72. """
  73. # Store the data
  74. ret = self.run_run_plus("nacl.keygen",)
  75. self.assertIn("pk", ret["return"])
  76. self.assertIn("sk", ret["return"])
  77. pk = ret["return"]["pk"]
  78. sk = ret["return"]["sk"]
  79. unencrypted_data = b"hello"
  80. # Encrypt with pk
  81. ret = self.run_run_plus("nacl.sealedbox_encrypt", data=unencrypted_data, pk=pk,)
  82. encrypted_data = ret["return"]
  83. # Decrypt with sk
  84. ret = self.run_run_plus("nacl.sealedbox_decrypt", data=encrypted_data, sk=sk,)
  85. self.assertEqual(unencrypted_data, ret["return"])
  86. @slowTest
  87. def test_secretbox_enc_dec(self):
  88. """
  89. Generate keys, encrypt, then decrypt.
  90. """
  91. # Store the data
  92. ret = self.run_run_plus("nacl.keygen",)
  93. self.assertIn("pk", ret["return"])
  94. self.assertIn("sk", ret["return"])
  95. pk = ret["return"]["pk"]
  96. sk = ret["return"]["sk"]
  97. unencrypted_data = b"hello"
  98. # Encrypt with pk
  99. ret = self.run_run_plus("nacl.secretbox_encrypt", data=unencrypted_data, sk=sk,)
  100. encrypted_data = ret["return"]
  101. # Decrypt with sk
  102. ret = self.run_run_plus("nacl.secretbox_decrypt", data=encrypted_data, sk=sk,)
  103. self.assertEqual(unencrypted_data, ret["return"])
  104. @slowTest
  105. def test_enc_dec_no_pk_no_sk(self):
  106. """
  107. Store, list, fetch, then flush data
  108. """
  109. # Store the data
  110. ret = self.run_run_plus("nacl.keygen",)
  111. self.assertIn("pk", ret["return"])
  112. self.assertIn("sk", ret["return"])
  113. pk = ret["return"]["pk"]
  114. sk = ret["return"]["sk"]
  115. unencrypted_data = b"hello"
  116. # Encrypt with pk
  117. ret = self.run_run_plus("nacl.enc", data=unencrypted_data, pk=None,)
  118. self.assertIn("Exception: no pubkey or pk_file found", ret["return"])
  119. self.assertIn("return", ret)
  120. encrypted_data = ret["return"]
  121. # Decrypt with sk
  122. ret = self.run_run_plus("nacl.dec", data=encrypted_data, sk=None,)
  123. self.assertIn("Exception: no key or sk_file found", ret["return"])