1
0

test_nacl.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the salt-run command
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. # Import Salt Testing libs
  9. from tests.support.case import ShellCase
  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. log = logging.getLogger(__name__)
  18. @skipIf(not HAS_LIBNACL, 'skipping test_nacl, libnacl is unavailable')
  19. class NaclTest(ShellCase):
  20. '''
  21. Test the nacl runner
  22. '''
  23. def test_keygen(self):
  24. '''
  25. Test keygen
  26. '''
  27. # Store the data
  28. ret = self.run_run_plus(
  29. 'nacl.keygen',
  30. )
  31. self.assertIn('pk', ret['return'])
  32. self.assertIn('sk', ret['return'])
  33. def test_enc(self):
  34. '''
  35. Test keygen
  36. '''
  37. # Store the data
  38. ret = self.run_run_plus(
  39. 'nacl.keygen',
  40. )
  41. self.assertIn('pk', ret['return'])
  42. self.assertIn('sk', ret['return'])
  43. pk = ret['return']['pk']
  44. sk = ret['return']['sk']
  45. unencrypted_data = 'hello'
  46. # Encrypt with pk
  47. ret = self.run_run_plus(
  48. 'nacl.enc',
  49. data=unencrypted_data,
  50. pk=pk,
  51. )
  52. self.assertIn('return', ret)
  53. def test_enc_dec(self):
  54. '''
  55. Store, list, fetch, then flush data
  56. '''
  57. # Store the data
  58. ret = self.run_run_plus(
  59. 'nacl.keygen',
  60. )
  61. self.assertIn('pk', ret['return'])
  62. self.assertIn('sk', ret['return'])
  63. pk = ret['return']['pk']
  64. sk = ret['return']['sk']
  65. unencrypted_data = b'hello'
  66. # Encrypt with pk
  67. ret = self.run_run_plus(
  68. 'nacl.enc',
  69. data=unencrypted_data,
  70. pk=pk,
  71. )
  72. self.assertIn('return', ret)
  73. encrypted_data = ret['return']
  74. # Decrypt with sk
  75. ret = self.run_run_plus(
  76. 'nacl.dec',
  77. data=encrypted_data,
  78. sk=sk,
  79. )
  80. self.assertIn('return', ret)
  81. self.assertEqual(unencrypted_data, ret['return'])
  82. def test_sealedbox_enc_dec(self):
  83. '''
  84. Generate keys, encrypt, then decrypt.
  85. '''
  86. # Store the data
  87. ret = self.run_run_plus(
  88. 'nacl.keygen',
  89. )
  90. self.assertIn('pk', ret['return'])
  91. self.assertIn('sk', ret['return'])
  92. pk = ret['return']['pk']
  93. sk = ret['return']['sk']
  94. unencrypted_data = b'hello'
  95. # Encrypt with pk
  96. ret = self.run_run_plus(
  97. 'nacl.sealedbox_encrypt',
  98. data=unencrypted_data,
  99. pk=pk,
  100. )
  101. encrypted_data = ret['return']
  102. # Decrypt with sk
  103. ret = self.run_run_plus(
  104. 'nacl.sealedbox_decrypt',
  105. data=encrypted_data,
  106. sk=sk,
  107. )
  108. self.assertEqual(unencrypted_data, ret['return'])
  109. def test_secretbox_enc_dec(self):
  110. '''
  111. Generate keys, encrypt, then decrypt.
  112. '''
  113. # Store the data
  114. ret = self.run_run_plus(
  115. 'nacl.keygen',
  116. )
  117. self.assertIn('pk', ret['return'])
  118. self.assertIn('sk', ret['return'])
  119. pk = ret['return']['pk']
  120. sk = ret['return']['sk']
  121. unencrypted_data = b'hello'
  122. # Encrypt with pk
  123. ret = self.run_run_plus(
  124. 'nacl.secretbox_encrypt',
  125. data=unencrypted_data,
  126. sk=sk,
  127. )
  128. encrypted_data = ret['return']
  129. # Decrypt with sk
  130. ret = self.run_run_plus(
  131. 'nacl.secretbox_decrypt',
  132. data=encrypted_data,
  133. sk=sk,
  134. )
  135. self.assertEqual(unencrypted_data, ret['return'])
  136. def test_enc_dec_no_pk_no_sk(self):
  137. '''
  138. Store, list, fetch, then flush data
  139. '''
  140. # Store the data
  141. ret = self.run_run_plus(
  142. 'nacl.keygen',
  143. )
  144. self.assertIn('pk', ret['return'])
  145. self.assertIn('sk', ret['return'])
  146. pk = ret['return']['pk']
  147. sk = ret['return']['sk']
  148. unencrypted_data = b'hello'
  149. # Encrypt with pk
  150. ret = self.run_run_plus(
  151. 'nacl.enc',
  152. data=unencrypted_data,
  153. pk=None,
  154. )
  155. self.assertIn('Exception: no pubkey or pk_file found', ret['return'])
  156. self.assertIn('return', ret)
  157. encrypted_data = ret['return']
  158. # Decrypt with sk
  159. ret = self.run_run_plus(
  160. 'nacl.dec',
  161. data=encrypted_data,
  162. sk=None,
  163. )
  164. self.assertIn('Exception: no key or sk_file found', ret['return'])