Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_win32crypt.py 4.2KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Test module for win32crypt
  2. import contextlib
  3. import unittest
  4. from typing import Any, Iterator
  5. import win32crypt
  6. from pywin32_testutil import TestSkipped, find_test_fixture, testmain
  7. from win32cryptcon import *
  8. class Crypt(unittest.TestCase):
  9. def testSimple(self):
  10. data = b"My test data"
  11. entropy = None
  12. desc = "My description"
  13. flags = 0
  14. ps = None
  15. blob = win32crypt.CryptProtectData(data, desc, entropy, None, ps, flags)
  16. got_desc, got_data = win32crypt.CryptUnprotectData(
  17. blob, entropy, None, ps, flags
  18. )
  19. self.assertEqual(data, got_data)
  20. self.assertEqual(desc, got_desc)
  21. def testEntropy(self):
  22. data = b"My test data"
  23. entropy = b"My test entropy"
  24. desc = "My description"
  25. flags = 0
  26. ps = None
  27. blob = win32crypt.CryptProtectData(data, desc, entropy, None, ps, flags)
  28. got_desc, got_data = win32crypt.CryptUnprotectData(
  29. blob, entropy, None, ps, flags
  30. )
  31. self.assertEqual(data, got_data)
  32. self.assertEqual(desc, got_desc)
  33. # via https://github.com/mhammond/pywin32/issues/1859
  34. _LOCAL_MACHINE = "LocalMachine"
  35. _CURRENT_USER = "CurrentUser"
  36. @contextlib.contextmanager
  37. def open_windows_certstore(store_name: str, store_location: str) -> Iterator[Any]:
  38. """Open a windows certificate store
  39. :param store_name: store name
  40. :param store_location: store location
  41. :return: handle to cert store
  42. """
  43. handle = None
  44. try:
  45. handle = win32crypt.CertOpenStore(
  46. CERT_STORE_PROV_SYSTEM,
  47. 0,
  48. None,
  49. CERT_SYSTEM_STORE_LOCAL_MACHINE
  50. if store_location == _LOCAL_MACHINE
  51. else CERT_SYSTEM_STORE_CURRENT_USER,
  52. store_name,
  53. )
  54. yield handle
  55. finally:
  56. if handle is not None:
  57. handle.CertCloseStore()
  58. class TestCerts(unittest.TestCase):
  59. def readCertFile(self, file_name):
  60. with open(find_test_fixture(file_name), "rb") as f:
  61. buf = bytearray(f.read())
  62. return win32crypt.CryptQueryObject(
  63. CERT_QUERY_OBJECT_BLOB,
  64. buf,
  65. CERT_QUERY_CONTENT_FLAG_CERT,
  66. CERT_QUERY_FORMAT_FLAG_ALL,
  67. 0,
  68. )
  69. def testReadCertFiles(self):
  70. # readCertFile has Python read the file and load it as a blob.
  71. # win32crypt can read the file directly - let's check that works too
  72. # (ideally we'd compare the 2 approaches etc, but the objects don't support
  73. # equality checks etc, so this will do for now.)
  74. # No need to do this for different filenames!
  75. filename = "win32crypt_testcert_base64.cer"
  76. cert = win32crypt.CryptQueryObject(
  77. CERT_QUERY_OBJECT_FILE,
  78. find_test_fixture(filename),
  79. CERT_QUERY_CONTENT_FLAG_CERT,
  80. CERT_QUERY_FORMAT_FLAG_ALL,
  81. 0,
  82. )
  83. self.assertEqual(cert["FormatType"], CERT_QUERY_FORMAT_BASE64_ENCODED)
  84. self.assertEqual(cert["ContentType"], CERT_QUERY_CONTENT_CERT)
  85. def checkCertFile(self, filename, expected_format):
  86. cert = self.readCertFile(filename)
  87. self.assertEqual(cert["FormatType"], expected_format)
  88. self.assertEqual(cert["ContentType"], CERT_QUERY_CONTENT_CERT)
  89. with open_windows_certstore(_CURRENT_USER, "Temp") as store:
  90. context = store.CertAddCertificateContextToStore(
  91. cert["Context"], CERT_STORE_ADD_REPLACE_EXISTING
  92. )
  93. # Getting 2 certs here - main thing is we get 1!
  94. self.assertTrue(len(store.CertEnumCertificatesInStore()))
  95. self.assertFalse(len(store.CertEnumCTLsInStore()))
  96. context.CertFreeCertificateContext()
  97. try:
  98. context.CertFreeCertificateContext()
  99. except ValueError:
  100. pass
  101. else:
  102. raise RuntimeError("should not be able to close the context twice")
  103. def testCertBase64(self):
  104. self.checkCertFile(
  105. "win32crypt_testcert_base64.cer", CERT_QUERY_FORMAT_BASE64_ENCODED
  106. )
  107. def testCertBinary(self):
  108. self.checkCertFile("win32crypt_testcert_bin.cer", CERT_QUERY_FORMAT_BINARY)
  109. if __name__ == "__main__":
  110. testmain()