test_mac_keychain.py 3.8 KB

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