1
0

test_mac_keychain.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. """
  3. Validate the mac-keychain module
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import os
  7. from salt.exceptions import CommandExecutionError
  8. from salt.ext import six
  9. from tests.support.case import ModuleCase
  10. from tests.support.helpers import destructiveTest, runs_on, skip_if_not_root, slowTest
  11. from tests.support.runtests import RUNTIME_VARS
  12. @destructiveTest
  13. @skip_if_not_root
  14. @runs_on(kernel="Darwin")
  15. class MacKeychainModuleTest(ModuleCase):
  16. """
  17. Integration tests for the mac_keychain module
  18. """
  19. @classmethod
  20. def setUpClass(cls):
  21. cls.cert = os.path.join(
  22. RUNTIME_VARS.FILES, "file", "base", "certs", "salttest.p12"
  23. )
  24. cls.cert_alias = "Salt Test"
  25. cls.passwd = "salttest"
  26. def tearDown(self):
  27. """
  28. Clean up after tests
  29. """
  30. # Remove the salttest cert, if left over.
  31. certs_list = self.run_function("keychain.list_certs")
  32. if self.cert_alias in certs_list:
  33. self.run_function("keychain.uninstall", [self.cert_alias])
  34. @slowTest
  35. def test_mac_keychain_install(self):
  36. """
  37. Tests that attempts to install a certificate
  38. """
  39. install_cert = self.run_function("keychain.install", [self.cert, self.passwd])
  40. self.assertTrue(install_cert)
  41. # check to ensure the cert was installed
  42. certs_list = self.run_function("keychain.list_certs")
  43. self.assertIn(self.cert_alias, certs_list)
  44. @slowTest
  45. def test_mac_keychain_uninstall(self):
  46. """
  47. Tests that attempts to uninstall a certificate
  48. """
  49. self.run_function("keychain.install", [self.cert, self.passwd])
  50. certs_list = self.run_function("keychain.list_certs")
  51. if self.cert_alias not in certs_list:
  52. self.run_function("keychain.uninstall", [self.cert_alias])
  53. self.skipTest("Failed to install keychain")
  54. # uninstall cert
  55. self.run_function("keychain.uninstall", [self.cert_alias])
  56. certs_list = self.run_function("keychain.list_certs")
  57. # check to ensure the cert was uninstalled
  58. try:
  59. self.assertNotIn(self.cert_alias, six.text_type(certs_list))
  60. except CommandExecutionError:
  61. self.run_function("keychain.uninstall", [self.cert_alias])
  62. @slowTest
  63. def test_mac_keychain_get_friendly_name(self):
  64. """
  65. Test that attempts to get friendly name of a cert
  66. """
  67. self.run_function("keychain.install", [self.cert, self.passwd])
  68. certs_list = self.run_function("keychain.list_certs")
  69. if self.cert_alias not in certs_list:
  70. self.run_function("keychain.uninstall", [self.cert_alias])
  71. self.skipTest("Failed to install keychain")
  72. get_name = self.run_function(
  73. "keychain.get_friendly_name", [self.cert, self.passwd]
  74. )
  75. self.assertEqual(get_name, self.cert_alias)
  76. @slowTest
  77. def test_mac_keychain_get_default_keychain(self):
  78. """
  79. Test that attempts to get the default keychain
  80. """
  81. salt_get_keychain = self.run_function("keychain.get_default_keychain")
  82. sys_get_keychain = self.run_function(
  83. "cmd.run", ["security default-keychain -d user"]
  84. )
  85. self.assertEqual(salt_get_keychain, sys_get_keychain)
  86. def test_mac_keychain_list_certs(self):
  87. """
  88. Test that attempts to list certs
  89. """
  90. cert_default = "com.apple.systemdefault"
  91. certs = self.run_function("keychain.list_certs")
  92. self.assertIn(cert_default, certs)