certutils_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright 2014 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Test routines to generate dummy certificates."""
  15. import BaseHTTPServer
  16. import os
  17. import shutil
  18. import ssl
  19. import tempfile
  20. import threading
  21. import unittest
  22. import certutils
  23. class Server(BaseHTTPServer.HTTPServer):
  24. def __init__(self, https_root_ca_cert_path):
  25. BaseHTTPServer.HTTPServer.__init__(
  26. self, ('localhost', 0), BaseHTTPServer.BaseHTTPRequestHandler)
  27. self.socket = ssl.wrap_socket(
  28. self.socket, certfile=https_root_ca_cert_path, server_side=True,
  29. do_handshake_on_connect=False)
  30. def __enter__(self):
  31. thread = threading.Thread(target=self.serve_forever)
  32. thread.daemon = True
  33. thread.start()
  34. return self
  35. def cleanup(self):
  36. try:
  37. self.shutdown()
  38. except KeyboardInterrupt:
  39. pass
  40. def __exit__(self, type_, value_, traceback_):
  41. self.cleanup()
  42. class CertutilsTest(unittest.TestCase):
  43. def _check_cert_file(self, cert_file_path, cert_str, key_str=None):
  44. cert_load = open(cert_file_path, 'r').read()
  45. if key_str:
  46. expected_cert = key_str + cert_str
  47. else:
  48. expected_cert = cert_str
  49. self.assertEqual(expected_cert, cert_load)
  50. def setUp(self):
  51. self._temp_dir = tempfile.mkdtemp(prefix='certutils_', dir='/tmp')
  52. def tearDown(self):
  53. if self._temp_dir:
  54. shutil.rmtree(self._temp_dir)
  55. def test_generate_dummy_ca_cert(self):
  56. subject = 'testSubject'
  57. c, _ = certutils.generate_dummy_ca_cert(subject)
  58. c = certutils.load_cert(c)
  59. self.assertEqual(c.get_subject().commonName, subject)
  60. def test_get_host_cert(self):
  61. ca_cert_path = os.path.join(self._temp_dir, 'rootCA.pem')
  62. issuer = 'testCA'
  63. certutils.write_dummy_ca_cert(*certutils.generate_dummy_ca_cert(issuer),
  64. cert_path=ca_cert_path)
  65. with Server(ca_cert_path) as server:
  66. cert_str = certutils.get_host_cert('localhost', server.server_port)
  67. cert = certutils.load_cert(cert_str)
  68. self.assertEqual(issuer, cert.get_subject().commonName)
  69. def test_get_host_cert_gives_empty_for_bad_host(self):
  70. cert_str = certutils.get_host_cert('not_a_valid_host_name_2472341234234234')
  71. self.assertEqual('', cert_str)
  72. def test_write_dummy_ca_cert(self):
  73. base_path = os.path.join(self._temp_dir, 'testCA')
  74. ca_cert_path = base_path + '.pem'
  75. cert_path = base_path + '-cert.pem'
  76. ca_cert_android = base_path + '-cert.cer'
  77. ca_cert_windows = base_path + '-cert.p12'
  78. self.assertFalse(os.path.exists(ca_cert_path))
  79. self.assertFalse(os.path.exists(cert_path))
  80. self.assertFalse(os.path.exists(ca_cert_android))
  81. self.assertFalse(os.path.exists(ca_cert_windows))
  82. c, k = certutils.generate_dummy_ca_cert()
  83. certutils.write_dummy_ca_cert(c, k, ca_cert_path)
  84. self._check_cert_file(ca_cert_path, c, k)
  85. self._check_cert_file(cert_path, c)
  86. self._check_cert_file(ca_cert_android, c)
  87. self.assertTrue(os.path.exists(ca_cert_windows))
  88. def test_generate_cert(self):
  89. ca_cert_path = os.path.join(self._temp_dir, 'testCA.pem')
  90. issuer = 'testIssuer'
  91. certutils.write_dummy_ca_cert(
  92. *certutils.generate_dummy_ca_cert(issuer), cert_path=ca_cert_path)
  93. with open(ca_cert_path, 'r') as root_file:
  94. root_string = root_file.read()
  95. subject = 'testSubject'
  96. cert_string = certutils.generate_cert(
  97. root_string, '', subject)
  98. cert = certutils.load_cert(cert_string)
  99. self.assertEqual(issuer, cert.get_issuer().commonName)
  100. self.assertEqual(subject, cert.get_subject().commonName)
  101. with open(ca_cert_path, 'r') as ca_cert_file:
  102. ca_cert_str = ca_cert_file.read()
  103. cert_string = certutils.generate_cert(ca_cert_str, cert_string,
  104. 'host')
  105. cert = certutils.load_cert(cert_string)
  106. self.assertEqual(issuer, cert.get_issuer().commonName)
  107. self.assertEqual(subject, cert.get_subject().commonName)
  108. if __name__ == '__main__':
  109. unittest.main()