text: from os import path
  
  from OpenSSL.crypto import FILETYPE_PEM, load_certificate
  
  from firefox_cert_override import (
      CertOverrideEntry,
      generate_cert_override,
      OverrideMask,
      parse_cert_override,
  )
  from firefox_cert_override.base import SHA256_OID
  
  
  BASE_DIR = path.abspath(path.dirname(__file__))
  
  # Load the test certificate
  with open(path.join(BASE_DIR, 'test.cert')) as f:
      certificate = load_certificate(FILETYPE_PEM, f.read())
  
  # Load the test override file
  with open(path.join(BASE_DIR, 'cert_override.txt')) as f:
      cert_override = f.read()
  
  # These values for the test certificate were pulled from a
  # cert_override.txt generated by Firefox 51.0a2.
  certificate_fingerprint = (
      '70:28:3B:D2:63:F8:E7:2C:B4:AB:87:EA:AE:7C:FB:8D:65:B8:D1:12:8B:07:'
      'E8:82:C3:E2:20:4F:0A:8C:66:97'
  )
  certificate_db_key = (
      'AAAAAAAAAAAAAAAJAAAAZACXyKXkIlbiUzBiMQswCQYDVQQGEwJVUzENMAsGA1UECB'
      'MEVGVzdDENMAsGA1UEBxMEVGVzdDEQMA4GA1UEChMHTW96aWxsYTEjMCEGA1UEAxMa'
      'ZmlyZWZveC1jZXJ0LW92ZXJyaWRlLXRlc3Q='
  )
  
  
  def test_from_certificate():
      # Test values were generated from Firefox 51.0a2
      override = CertOverrideEntry.from_certificate('test.com:443', certificate)
      assert override.host == 'test.com'
      assert override.port == '443'
      assert override.fingerprint_algorithm == SHA256_OID
      assert override.fingerprint == certificate_fingerprint
      assert override.mask == OverrideMask(False, False, False)
      assert override.db_key == certificate_db_key
  
  
  def test_parse_cert_override():
      overrides = parse_cert_override(cert_override)
      assert len(overrides) == 2
  
      assert overrides[0].host == 'localhost'
      assert overrides[0].port == '8000'
      assert overrides[0].fingerprint_algorithm == SHA256_OID
      assert overrides[0].fingerprint == certificate_fingerprint
      assert overrides[0].mask == OverrideMask(True, True, False)
      assert overrides[0].db_key == certificate_db_key
  
      assert overrides[1].host == 'example.com'
      assert overrides[1].port == '443'
      assert overrides[1].fingerprint_algorithm == SHA256_OID
      assert overrides[1].fingerprint == certificate_fingerprint
      assert overrides[1].mask == OverrideMask(False, True, True)
      assert overrides[1].db_key == certificate_db_key
  
  
  def test_generate_cert_override():
      generated_cert_override = generate_cert_override([
          CertOverrideEntry.from_certificate(
              'localhost:8000',
              certificate,
              mask='MU'
          ),
          CertOverrideEntry.from_certificate(
              'example.com:443',
              certificate,
              mask='UT',
          ),
      ])
      assert generated_cert_override == cert_override
  