1
|
import pytest
|
2
|
from services.cryptography import CryptographyService, CryptographyException
|
3
|
import subprocess
|
4
|
|
5
|
|
6
|
@pytest.fixture
|
7
|
def service():
|
8
|
# provide a CryptographyService fixture
|
9
|
return CryptographyService()
|
10
|
|
11
|
|
12
|
def test_private_key(service):
|
13
|
private_key = service.create_private_key()
|
14
|
|
15
|
# verify the private key
|
16
|
subprocess.check_output(["openssl", "rsa", "-in", "-", "-check"], input=bytes(private_key, encoding="utf-8"),
|
17
|
stderr=subprocess.STDOUT)
|
18
|
|
19
|
|
20
|
def test_encrypted_private_key(service):
|
21
|
private_key = service.create_private_key(passphrase="foobar")
|
22
|
|
23
|
# verify the private key providing a correct passphrase
|
24
|
subprocess.check_output(["openssl", "rsa", "-in", "-", "-passin", "pass:foobar", "-check"],
|
25
|
input=bytes(private_key, encoding="utf-8"), stderr=subprocess.STDOUT)
|
26
|
|
27
|
|
28
|
def test_encrypted_private_key_incorrect_pass(service):
|
29
|
private_key = service.create_private_key(passphrase="foobar")
|
30
|
|
31
|
# incorrect passphrase provided
|
32
|
with pytest.raises(subprocess.CalledProcessError) as e:
|
33
|
subprocess.check_output(["openssl", "rsa", "-in", "-", "-passin", "pass:bazbaz", "-check"],
|
34
|
input=bytes(private_key, encoding="utf-8"), stderr=subprocess.STDOUT)
|
35
|
# no passphrase provided
|
36
|
with pytest.raises(subprocess.CalledProcessError) as e:
|
37
|
subprocess.check_output(["openssl", "rsa", "-in", "-", "-check"],
|
38
|
input=bytes(private_key, encoding="utf-8"), stderr=subprocess.STDOUT)
|