1
0

test_nacl.py 4.6 KB

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