2016-02-02 09:06:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.win_certutil as certutil
|
2016-02-02 09:06:20 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-03-21 23:56:24 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase
|
|
|
|
from tests.support.mock import (
|
2016-02-02 09:06:20 +00:00
|
|
|
MagicMock,
|
|
|
|
patch
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-03-21 23:56:24 +00:00
|
|
|
class CertUtilTestCase(TestCase, LoaderModuleMockMixin):
|
2016-02-02 09:06:20 +00:00
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
return {certutil: {}}
|
2016-02-02 09:06:20 +00:00
|
|
|
|
|
|
|
def test_get_serial(self):
|
|
|
|
'''
|
|
|
|
Test getting the serial number from a certificate
|
|
|
|
'''
|
|
|
|
expected = 'XYZABC'
|
|
|
|
mock = MagicMock(return_value='CertInfo\r\nSerial: XYZABC\r\nOtherStuff')
|
|
|
|
with patch.dict(certutil.__salt__, {'cmd.run': mock}):
|
|
|
|
out = certutil.get_cert_serial('/path/to/cert.cer')
|
|
|
|
mock.assert_called_once_with('certutil.exe -verify /path/to/cert.cer')
|
|
|
|
self.assertEqual(expected, out)
|
|
|
|
|
|
|
|
def test_get_serials(self):
|
|
|
|
'''
|
|
|
|
Test getting the all the serial numbers from a store
|
|
|
|
'''
|
|
|
|
expected = ['XYZABC', '123456']
|
|
|
|
mock = MagicMock(return_value='CertInfo\r\nSerial Number: XYZABC\r\nSerial Number: 123456\r\n')
|
|
|
|
with patch.dict(certutil.__salt__, {'cmd.run': mock}):
|
|
|
|
out = certutil.get_stored_cert_serials('TrustedPublisher')
|
|
|
|
mock.assert_called_once_with('certutil.exe -store TrustedPublisher')
|
|
|
|
self.assertEqual(expected, out)
|
|
|
|
|
|
|
|
def test_add_store(self):
|
|
|
|
'''
|
|
|
|
Test adding a certificate to a specific store
|
|
|
|
'''
|
|
|
|
cmd_mock = MagicMock(return_value='CertInfo\r\nSerial: XYZABC\r\nOtherStuff')
|
|
|
|
cache_mock = MagicMock(return_value='/tmp/cert.cer')
|
|
|
|
with patch.dict(certutil.__salt__, {'cmd.run': cmd_mock,
|
|
|
|
'cp.cache_file': cache_mock}):
|
|
|
|
certutil.add_store('salt://path/to/file', 'TrustedPublisher')
|
|
|
|
cmd_mock.assert_called_once_with('certutil.exe -addstore TrustedPublisher /tmp/cert.cer')
|
|
|
|
cache_mock.assert_called_once_with('salt://path/to/file', 'base')
|
|
|
|
|
|
|
|
@patch('salt.modules.win_certutil.get_cert_serial')
|
|
|
|
def test_del_store(self, cert_serial_mock):
|
|
|
|
'''
|
|
|
|
Test removing a certificate to a specific store
|
|
|
|
'''
|
|
|
|
cmd_mock = MagicMock(return_value='CertInfo\r\nSerial: XYZABC\r\nOtherStuff')
|
|
|
|
cache_mock = MagicMock(return_value='/tmp/cert.cer')
|
|
|
|
cert_serial_mock.return_value = "ABCDEF"
|
|
|
|
with patch.dict(certutil.__salt__, {'cmd.run': cmd_mock,
|
|
|
|
'cp.cache_file': cache_mock}):
|
|
|
|
certutil.del_store('salt://path/to/file', 'TrustedPublisher')
|
|
|
|
cmd_mock.assert_called_once_with('certutil.exe -delstore TrustedPublisher ABCDEF')
|
|
|
|
cache_mock.assert_called_once_with('salt://path/to/file', 'base')
|