-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_securestring.py
62 lines (51 loc) · 2.05 KB
/
test_securestring.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright 2015, Nashwan Azhari.
# Licensed under the GPLv2, see LICENSE file for details.
from codecs import decode
from subprocess import check_output
from securestring import encrypt, decrypt
testinputs = [
"Simple",
"A longer string",
"A!string%with(a4239lot#of$&*special@characters{[]})",
"Quite a very much longer string meant to push the envelope",
"fsdafsgdfgdfgdfgdfgsdfgdgdfgdmmghnh kv dfv dj fkvjjenrwenvfvvslfvnsljfvnlsfvlnsfjlvnssdwoewivdsvmxxvsdvsdv",
]
def test_encryption_decryption_symmetry():
"""Tests whether encryption and decryption is symmetrical."""
for input in testinputs:
try:
assert decrypt(encrypt(input)) == input
except Exception as e:
print(e)
assert False
def runpscommand(command):
"""Helper function which runs the given command with PowerShell
and returns the output.
"""
try:
# NOTE: trailing newline characters must be removed here:
return check_output(["powershell.exe", "-NoProfile",
"-NonInteractive", command]).replace(b"\r\n", b"")
except:
raise Exception("System error has occured.")
def test_decrypt_from_CFSS():
"""Tests whether decrypt() is able to decrypt the
output of ConvertFrom-SecureString."""
for input in testinputs:
try:
psenc = runpscommand("ConvertTo-SecureString \"%s\" -AsPlainText -Force | ConvertFrom-SecureString" % input)
assert decrypt(psenc) == input
except Exception as e:
print(e)
assert False
def test_convert_encrypted_to_securestring():
"""Tests whether the output of encrypt() is compatible with
PowerShell's SecureStrings by feeding its output to ConvertTo-SecureString."""
for input in testinputs:
try:
enc = encrypt(input)
psres = runpscommand("\"%s\" | ConvertTo-SecureString" % enc)
assert decode(psres, "utf-8") == "System.Security.SecureString"
except Exception as e:
print(e)
assert False