-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-wdcrypt.py
executable file
·107 lines (83 loc) · 3.29 KB
/
test-wdcrypt.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python3
import os
import shutil
import subprocess
from random import randbytes
TEMP_DIR_1 = "/tmp/wdcrypt/1/"
TEMP_DIR_2 = "/tmp/wdcrypt/2/"
SECRET_KEY = ".secret.key"
class TestWdcryptFileOnly:
sha256dict = {}
def __init__(self):
os.makedirs(TEMP_DIR_1, exist_ok=True)
os.chdir(TEMP_DIR_1)
for n in range(10):
file_name = TEMP_DIR_1 + "file-" + str(n)
self.sha256dict[file_name] = new_file_return_hash(file_name)
def run(self):
if os.system("wdcrypt -e >/dev/null 2>&1") != 0:
raise Exception("Wdcrypt -> non zero exit code")
if os.system("wdcrypt -d >/dev/null 2>&1") != 0:
raise Exception("Wdcrypt -> non zero exit code")
for n in range(10):
file_name = TEMP_DIR_1 + "file-" + str(n)
if self.sha256dict[file_name] != get_hash_of_file(file_name):
raise Exception("Hash mismatch!")
print("[PASSED] ✅ TestWdcryptFileOnly")
def __del__(self):
for n in range(10):
file_name = TEMP_DIR_1 + "file-" + str(n)
os.remove(file_name)
os.remove(SECRET_KEY)
shutil.rmtree(TEMP_DIR_1)
class TestWdcryptFileAndDir:
sha256dict = {}
def __init__(self):
os.makedirs(TEMP_DIR_2, exist_ok=True)
os.chdir(TEMP_DIR_2)
for dn in range(10):
dir_name = TEMP_DIR_2 + "dir-" + str(dn) + "/"
os.mkdir(dir_name)
for n in range(10):
file_name = dir_name + "file-" + str(n)
self.sha256dict[file_name] = new_file_return_hash(file_name)
def run(self):
if os.system("wdcrypt -e >/dev/null 2>&1") != 0:
raise Exception("Wdcrypt -> non zero exit code")
if os.system("wdcrypt -d >/dev/null 2>&1") != 0:
raise Exception("Wdcrypt -> non zero exit code")
for dn in range(10):
dir_name = TEMP_DIR_2 + "dir-" + str(dn) + "/"
for n in range(10):
file_name = dir_name + "file-" + str(n)
if self.sha256dict[file_name] != get_hash_of_file(file_name):
raise Exception("Hash mismatch!")
print("[PASSED] ✅ TestWdcryptFileAndDir")
def __del__(self):
for dn in range(10):
dir_name = TEMP_DIR_2 + "dir-" + str(dn) + "/"
shutil.rmtree(dir_name)
os.remove(SECRET_KEY)
shutil.rmtree(TEMP_DIR_2)
def new_file_return_hash(file_name):
with open(file_name, "wb") as file:
num_chars = 1024 * 1024 * 5
file.write(randbytes(num_chars))
return subprocess.check_output(["sha256sum", file.name]).decode("utf-8")[0:64]
def get_hash_of_file(file_name):
return subprocess.check_output(["sha256sum", file_name]).decode("utf-8")[0:64]
def installCurrentWdcrypt():
return os.system("cargo install --path . >/dev/null 2>&1")
def main():
if installCurrentWdcrypt() != 0:
raise Exception("Cannot install wdcrypt...")
try:
TestWdcryptFileOnly().run()
except Exception as exception:
print("[FAILED] ❌ TestWdcryptFileOnly ",exception)
try:
TestWdcryptFileAndDir().run()
except Exception as exception:
print("[FAILED] ❌ TestWdcryptFileAndDir ",exception)
if __name__ == "__main__":
main()