-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_tests.py
247 lines (182 loc) · 7.94 KB
/
unit_tests.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import time
from src import my_files_io
from src import my_cli
from src import encode
from src import decode
from src import inspect
# Compare two binary files to see if they are equal
def compare_binaries(file1, file2):
with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
return f1.read() == f2.read()
#------------------------------------------------------------------------------
def generic_test(test_id, image_path, file_path, image_output_path, file_output_path, bit_planes):
print("\nRunning test: ", test_id)
my_files_io.create_folder_if_not_exists(image_output_path)
my_files_io.create_folder_if_not_exists(file_output_path)
# Encoding
start_time = time.time()
print("Encoding...")
image_output_path = encode.encode_file(image_path, file_path, bit_planes, image_output_path)
print("Encoding time: %s seconds" % (time.time() - start_time))
print("Image encoded and saved at: ", image_output_path)
# Decoding
print("Decoding...")
start_time = time.time()
file_output_path = decode.decode_file(image_output_path, file_output_path, bit_planes)
print("Decoding time: %s seconds" % (time.time() - start_time))
print("File decoded and saved at: ", file_output_path)
# Compare original and decoded files
result = compare_binaries(file_path, file_output_path)
if result:
print("\033[2;32;40m [OK] \033[0;0m")
else:
print("\033[2;31;40m [FAIL] \033[0;0m")
#------------------------------------------------------------------------------
# Test small txt encoded into small png
def test1():
# Encoding variables
image_path = "data/images/baboon.png"
file_path = "data/messages/highway.txt"
image_output_path = "data/encoded_images"
bit_planes = [0, 1, 2]
bit_planes = list(dict.fromkeys(bit_planes))
# Decoding variables
file_output_path = "data/decoded_messages"
# Compare original and decoded files
generic_test(1, image_path, file_path, image_output_path, file_output_path, bit_planes)
#------------------------------------------------------------------------------
# Test big txt encoded into small png using more bit planes
def test2():
# Encoding variables
image_path = "data/images/lenna.png"
file_path = "data/messages/frankenstein.txt"
image_output_path = "data/encoded_images"
bit_planes = [0, 1, 2, 3, 4]
bit_planes = list(dict.fromkeys(bit_planes))
# Decoding variables
file_output_path = "data/decoded_messages"
# Compare original and decoded files
generic_test(2, image_path, file_path, image_output_path, file_output_path, bit_planes)
#------------------------------------------------------------------------------
# Test big txt encoded into small png using more bit planes but different order
def test3():
# Encoding variables
image_path = "data/images/watch.png"
file_path = "data/messages/frankenstein.txt"
image_output_path = "data/encoded_images"
bit_planes = [2, 0, 5, 1]
bit_planes = list(dict.fromkeys(bit_planes))
# Decoding variables
file_output_path = "data/decoded_messages"
# Compare original and decoded files
generic_test(3, image_path, file_path, image_output_path, file_output_path, bit_planes)
#------------------------------------------------------------------------------
# Test big txt encoded into big png different
def test4():
# Encoding variables
image_path = "data/images/big.png"
file_path = "data/messages/big.txt"
image_output_path = "data/encoded_images"
bit_planes = [0, 1, 2]
bit_planes = list(dict.fromkeys(bit_planes))
# Decoding variables
file_output_path = "data/decoded_messages"
# Compare original and decoded files
generic_test(4, image_path, file_path, image_output_path, file_output_path, bit_planes)
#------------------------------------------------------------------------------
# Test small image encoded into big image
def test5():
# Encoding variables
image_path = "data/images/big.png"
file_path = "data/messages/monalisa.png"
image_output_path = "data/encoded_images"
bit_planes = [0, 1, 2]
bit_planes = list(dict.fromkeys(bit_planes))
# Decoding variables
file_output_path = "data/decoded_messages"
# Compare original and decoded files
generic_test(5, image_path, file_path, image_output_path, file_output_path, bit_planes)
#------------------------------------------------------------------------------
# Test audio encoded into big image
def test6():
# Encoding variables
image_path = "data/images/big.png"
file_path = "data/messages/music.mp3"
image_output_path = "data/encoded_images"
bit_planes = [1, 3]
bit_planes = list(dict.fromkeys(bit_planes))
# Decoding variables
file_output_path = "data/decoded_messages"
# Compare original and decoded files
generic_test(6, image_path, file_path, image_output_path, file_output_path, bit_planes)
#------------------------------------------------------------------------------
# Test text with different characters encoded into big image
def test7():
# Encoding variables
image_path = "data/images/big.png"
file_path = "data/messages/01 - The Fellowship Of The Ring.txt"
image_output_path = "data/encoded_images"
bit_planes = [0, 1, 2]
bit_planes = list(dict.fromkeys(bit_planes))
# Decoding variables
file_output_path = "data/decoded_messages"
# Compare original and decoded files
generic_test(7, image_path, file_path, image_output_path, file_output_path, bit_planes)
#------------------------------------------------------------------------------
# Test gif encoded into big image
def test8():
# Encoding variables
image_path = "data/images/big.png"
file_path = "data/messages/mario.gif"
image_output_path = "data/encoded_images"
bit_planes = [0, 1, 2]
bit_planes = list(dict.fromkeys(bit_planes))
# Decoding variables
file_output_path = "data/decoded_messages"
# Compare original and decoded files
generic_test(8, image_path, file_path, image_output_path, file_output_path, bit_planes)
#------------------------------------------------------------------------------
# Test mp4 encoded into big image
def test9():
# Encoding variables
image_path = "data/images/big.png"
file_path = "data/messages/rick_roll.mp4"
image_output_path = "data/encoded_images"
bit_planes = [3, 1, 2]
bit_planes = list(dict.fromkeys(bit_planes))
# Decoding variables
file_output_path = "data/decoded_messages"
# Compare original and decoded files
generic_test(9, image_path, file_path, image_output_path, file_output_path, bit_planes)
#------------------------------------------------------------------------------
# Test zip encoded into big image
def test10():
# Encoding variables
image_path = "data/images/big.png"
file_path = "data/messages/tcc.zip"
image_output_path = "data/encoded_images"
bit_planes = [2, 4, 0]
bit_planes = list(dict.fromkeys(bit_planes))
# Decoding variables
file_output_path = "data/decoded_messages"
# Compare original and decoded files
generic_test(10, image_path, file_path, image_output_path, file_output_path, bit_planes)
#------------------------------------------------------------------------------
# Run all unit tests
def unit_tests():
my_cli.start()
print("\033[2;31;43m [WARNING] \033[0;0m")
print("Unit tests just test the encoding and decoding functions, not the whole CLI. Edit this file carefully")
test1()
test2()
test3()
test4()
test5()
test6()
test7()
test8()
test9()
test10()
my_cli.end()
if __name__ == "__main__":
unit_tests()