1
0

test_nacl.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import
  4. import os
  5. # Import Salt Testing libs
  6. from tests.support.unit import TestCase, skipIf
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.helpers import with_tempfile
  9. # Import Salt libs
  10. import salt.modules.config as config
  11. import salt.utils.files
  12. try:
  13. import libnacl.secret # pylint: disable=unused-import
  14. import libnacl.sealed # pylint: disable=unused-import
  15. import salt.utils.nacl as nacl
  16. HAS_LIBNACL = True
  17. except (ImportError, OSError, AttributeError):
  18. HAS_LIBNACL = False
  19. @skipIf(not HAS_LIBNACL, 'skipping test_nacl, libnacl is unavailable')
  20. class NaclUtilsTests(TestCase, LoaderModuleMockMixin):
  21. def setup_loader_modules(self):
  22. return {
  23. nacl: {'__salt__': {'config.get': config.get}},
  24. config: {'__opts__': {}},
  25. }
  26. def setUp(self):
  27. self.key = 'C16NxgBhw8cqbhvPCDAn2pirwW1A1WEVLUexCsoUD2Y='
  28. self.pub = '+XWFfZXnfItS++a4gQf8Adu1aUlTgHWyTfsglbTdXyg='
  29. def test_keygen(self):
  30. '''
  31. test nacl.keygen function
  32. '''
  33. ret = nacl.keygen()
  34. assert all(key in ret for key in ret.keys())
  35. @with_tempfile()
  36. def test_keygen_sk_file(self, fpath):
  37. '''
  38. test nacl.keygen function
  39. with sk_file set
  40. '''
  41. with salt.utils.files.fopen(fpath, 'w') as wfh:
  42. wfh.write(
  43. self.key
  44. )
  45. # test sk_file
  46. ret = nacl.keygen(sk_file=fpath)
  47. assert 'saved pk_file: {}.pub'.format(fpath) == ret
  48. @with_tempfile()
  49. def test_keygen_keyfile(self, fpath):
  50. '''
  51. test nacl.keygen function
  52. with keyfile set
  53. '''
  54. with salt.utils.files.fopen(fpath, 'w') as wfh:
  55. wfh.write(
  56. self.key
  57. )
  58. ret = nacl.keygen(keyfile=fpath)
  59. assert 'saved pk_file: {}.pub'.format(fpath) == ret
  60. @with_tempfile()
  61. def test_enc_keyfile(self, fpath):
  62. '''
  63. test nacl.enc function
  64. with keyfile and pk_file set
  65. '''
  66. with salt.utils.files.fopen(fpath, 'w') as wfh:
  67. wfh.write(
  68. self.key
  69. )
  70. with salt.utils.files.fopen(fpath + '.pub', 'w') as wfh:
  71. wfh.write(
  72. self.pub
  73. )
  74. kwargs = {'opts': {'pki_dir': os.path.dirname(fpath)},
  75. 'keyfile': fpath,
  76. 'pk_file': fpath + '.pub'}
  77. ret = nacl.enc('blah', **kwargs)
  78. assert isinstance(ret, bytes)
  79. @with_tempfile()
  80. def test_enc_sk_file(self, fpath):
  81. '''
  82. test nacl.enc function
  83. with sk_file and pk_file set
  84. '''
  85. with salt.utils.files.fopen(fpath, 'w') as wfh:
  86. wfh.write(
  87. self.key
  88. )
  89. with salt.utils.files.fopen(fpath + '.pub', 'w') as wfh:
  90. wfh.write(
  91. self.pub
  92. )
  93. kwargs = {'opts': {'pki_dir': os.path.dirname(fpath)},
  94. 'sk_file': fpath,
  95. 'pk_file': fpath + '.pub'}
  96. ret = nacl.enc('blah', **kwargs)
  97. assert isinstance(ret, bytes)
  98. @with_tempfile()
  99. def test_dec_keyfile(self, fpath):
  100. '''
  101. test nacl.dec function
  102. with keyfile and pk_file set
  103. '''
  104. with salt.utils.files.fopen(fpath, 'w') as wfh:
  105. wfh.write(
  106. self.key
  107. )
  108. with salt.utils.files.fopen(fpath + '.pub', 'w') as wfh:
  109. wfh.write(
  110. self.pub
  111. )
  112. kwargs = {'opts': {'pki_dir': os.path.dirname(fpath)},
  113. 'keyfile': fpath,
  114. 'pk_file': fpath + '.pub'}
  115. enc_data = nacl.enc('blah', **kwargs)
  116. ret = nacl.dec(enc_data, **kwargs)
  117. assert isinstance(ret, bytes)
  118. assert ret == b'blah'
  119. @with_tempfile()
  120. def test_dec_sk_file(self, fpath):
  121. '''
  122. test nacl.dec function
  123. with sk_file and pk_file set
  124. '''
  125. with salt.utils.files.fopen(fpath, 'w') as wfh:
  126. wfh.write(
  127. self.key
  128. )
  129. with salt.utils.files.fopen(fpath + '.pub', 'w') as wfh:
  130. wfh.write(
  131. self.pub
  132. )
  133. kwargs = {'opts': {'pki_dir': os.path.dirname(fpath)},
  134. 'sk_file': fpath,
  135. 'pk_file': fpath + '.pub'}
  136. enc_data = nacl.enc('blah', **kwargs)
  137. ret = nacl.dec(enc_data, **kwargs)
  138. assert isinstance(ret, bytes)
  139. assert ret == b'blah'