Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_using_codesign(self):
"""Test that an added cert works with codesign."""
with keyper.TemporaryKeychain() as keychain:
certificate = keyper.Certificate(KeyperCertificateTests.TEST_CERT_PATH, password=KeyperCertificateTests.TEST_CERT_PASSWORD)
keychain.install_cert(certificate)
temp_file_path = tempfile.mktemp()
with open(temp_file_path, 'w') as temp_file:
temp_file.write("Test")
try:
subprocess.run(
f"codesign -s TestCertificate_CodeSign --keychain {keychain.path} {temp_file_path}",
shell=True,
check=True
)
except subprocess.CalledProcessError as ex:
self.fail(f"Failed to use codesign: {ex}")
def test_adding_cert(self):
"""Test that we can add a cert to the keychain."""
with keyper.TemporaryKeychain() as keychain:
certificate = keyper.Certificate(KeyperCertificateTests.TEST_CERT_PATH, password=KeyperCertificateTests.TEST_CERT_PASSWORD)
self.assertEqual(certificate.path, KeyperCertificateTests.TEST_CERT_PATH)
self.assertEqual(certificate.password, KeyperCertificateTests.TEST_CERT_PASSWORD)
keychain.install_cert(certificate)
def test_password_write_read(self):
"""Test that passwords written to a keychain can be read back."""
with keyper.TemporaryKeychain() as keychain:
for index, password in enumerate(KeyperPasswordTests.SAMPLE_PASSWORDS):
keyper.set_password(password, account=f"account_{index}", service=f"service_{index}", keychain=keychain)
returned_password = keyper.get_password(account=f"account_{index}", service=f"service_{index}", keychain=keychain)
self.assertEqual(password, returned_password)
def test_temporary_keychain_context(self):
"""Test that a temporary keychain can be created, read and destroyed via the context manager."""
with keyper.TemporaryKeychain() as keychain:
self.assertIsNotNone(keychain.path)
self.assertIsNotNone(keychain.password)
self.assertTrue(os.path.exists(keychain.path))
self.assertTrue(keychain.is_temporary)
self.assertFalse(os.path.exists(keychain.path))