1
0

test_nacl.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import
  4. import os
  5. # Import Salt libs
  6. import salt.modules.config as config
  7. import salt.utils.files
  8. from tests.support.helpers import with_tempfile
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. # Import Salt Testing libs
  11. from tests.support.unit import TestCase, skipIf
  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(self.key)
  43. # test sk_file
  44. ret = nacl.keygen(sk_file=fpath)
  45. assert "saved pk_file: {}.pub".format(fpath) == ret
  46. @with_tempfile()
  47. def test_keygen_keyfile(self, fpath):
  48. """
  49. test nacl.keygen function
  50. with keyfile set
  51. """
  52. with salt.utils.files.fopen(fpath, "w") as wfh:
  53. wfh.write(self.key)
  54. ret = nacl.keygen(keyfile=fpath)
  55. assert "saved pk_file: {}.pub".format(fpath) == ret
  56. @with_tempfile()
  57. def test_enc_keyfile(self, fpath):
  58. """
  59. test nacl.enc function
  60. with keyfile and pk_file set
  61. """
  62. with salt.utils.files.fopen(fpath, "w") as wfh:
  63. wfh.write(self.key)
  64. with salt.utils.files.fopen(fpath + ".pub", "w") as wfh:
  65. wfh.write(self.pub)
  66. kwargs = {
  67. "opts": {"pki_dir": os.path.dirname(fpath)},
  68. "keyfile": fpath,
  69. "pk_file": fpath + ".pub",
  70. }
  71. ret = nacl.enc("blah", **kwargs)
  72. assert isinstance(ret, bytes)
  73. @with_tempfile()
  74. def test_enc_sk_file(self, fpath):
  75. """
  76. test nacl.enc function
  77. with sk_file and pk_file set
  78. """
  79. with salt.utils.files.fopen(fpath, "w") as wfh:
  80. wfh.write(self.key)
  81. with salt.utils.files.fopen(fpath + ".pub", "w") as wfh:
  82. wfh.write(self.pub)
  83. kwargs = {
  84. "opts": {"pki_dir": os.path.dirname(fpath)},
  85. "sk_file": fpath,
  86. "pk_file": fpath + ".pub",
  87. }
  88. ret = nacl.enc("blah", **kwargs)
  89. assert isinstance(ret, bytes)
  90. @with_tempfile()
  91. def test_dec_keyfile(self, fpath):
  92. """
  93. test nacl.dec function
  94. with keyfile and pk_file set
  95. """
  96. with salt.utils.files.fopen(fpath, "w") as wfh:
  97. wfh.write(self.key)
  98. with salt.utils.files.fopen(fpath + ".pub", "w") as wfh:
  99. wfh.write(self.pub)
  100. kwargs = {
  101. "opts": {"pki_dir": os.path.dirname(fpath)},
  102. "keyfile": fpath,
  103. "pk_file": fpath + ".pub",
  104. }
  105. enc_data = nacl.enc("blah", **kwargs)
  106. ret = nacl.dec(enc_data, **kwargs)
  107. assert isinstance(ret, bytes)
  108. assert ret == b"blah"
  109. @with_tempfile()
  110. def test_dec_sk_file(self, fpath):
  111. """
  112. test nacl.dec function
  113. with sk_file and pk_file set
  114. """
  115. with salt.utils.files.fopen(fpath, "w") as wfh:
  116. wfh.write(self.key)
  117. with salt.utils.files.fopen(fpath + ".pub", "w") as wfh:
  118. wfh.write(self.pub)
  119. kwargs = {
  120. "opts": {"pki_dir": os.path.dirname(fpath)},
  121. "sk_file": fpath,
  122. "pk_file": fpath + ".pub",
  123. }
  124. enc_data = nacl.enc("blah", **kwargs)
  125. ret = nacl.dec(enc_data, **kwargs)
  126. assert isinstance(ret, bytes)
  127. assert ret == b"blah"