-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_crypter.py
More file actions
300 lines (224 loc) · 8.82 KB
/
Copy pathtest_crypter.py
File metadata and controls
300 lines (224 loc) · 8.82 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python3
"""
FileCrypter Test Scripti
Şifreleme sistemini test eder
"""
import os
import tempfile
from pathlib import Path
from file_crypter import FileCrypter
def test_single_file():
"""Tek dosya şifreleme testini çalıştır"""
print("\n" + "="*60)
print("TEST 1: Tek Dosya Şifreleme/Şifre Çözme")
print("="*60)
# Test dosyası oluştur
test_content = "Bu bir test dosyasıdır! 🔐\nİçinde Türkçe karakterler de var: ğüşıöçĞÜŞİÖÇ"
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f:
test_file = Path(f.name)
f.write(test_content)
print(f"\n📝 Test dosyası oluşturuldu: {test_file.name}")
print(f"📏 Boyut: {test_file.stat().st_size} byte")
# Şifrele
password = "test123"
crypter = FileCrypter(password)
print(f"\n🔐 Şifreleme başlıyor...")
encrypted_file = test_file.with_suffix(test_file.suffix + '.encrypted')
if not crypter.encrypt_file(str(test_file), str(encrypted_file)):
print("❌ Şifreleme başarısız!")
return False
print(f"✅ Şifrelendi: {encrypted_file.name}")
print(f"📏 Şifreli boyut: {encrypted_file.stat().st_size} byte")
# Orijinali sil
test_file.unlink()
print(f"🗑️ Orijinal silindi")
# Şifre çöz
print(f"\n🔓 Şifre çözülüyor...")
decrypted_file = encrypted_file.with_suffix('')
if not crypter.decrypt_file(str(encrypted_file), str(decrypted_file)):
print("❌ Şifre çözme başarısız!")
return False
print(f"✅ Şifre çözüldü: {decrypted_file.name}")
# İçeriği kontrol et
with open(decrypted_file, 'r') as f:
decrypted_content = f.read()
if decrypted_content == test_content:
print("✅ İçerik doğrulandı!")
else:
print("❌ İçerik eşleşmiyor!")
return False
# Temizlik
encrypted_file.unlink()
decrypted_file.unlink()
print("\n✅ TEST BAŞARILI")
return True
def test_wrong_password():
"""Yanlış şifre testi"""
print("\n" + "="*60)
print("TEST 2: Yanlış Şifre Testi")
print("="*60)
# Test dosyası oluştur
test_content = "Şifreli içerik"
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.txt') as f:
test_file = Path(f.name)
f.write(test_content)
print(f"\n📝 Test dosyası: {test_file.name}")
# Doğru şifre ile şifrele
correct_password = "correct123"
crypter = FileCrypter(correct_password)
encrypted_file = test_file.with_suffix(test_file.suffix + '.encrypted')
crypter.encrypt_file(str(test_file), str(encrypted_file))
print(f"🔐 Doğru şifre ile şifrelendi: correct123")
# Yanlış şifre ile çözmeye çalış
wrong_password = "wrong456"
wrong_crypter = FileCrypter(wrong_password)
print(f"🔓 Yanlış şifre ile çözülmeye çalışılıyor: wrong456")
decrypted_file = encrypted_file.with_suffix('')
success = wrong_crypter.decrypt_file(str(encrypted_file), str(decrypted_file))
if not success:
print("✅ Yanlış şifre reddedildi (Beklenen davranış)")
else:
print("❌ Yanlış şifre kabul edildi! (HATA)")
return False
# Doğru şifre ile çöz
print(f"🔓 Doğru şifre ile çözülüyor: correct123")
if crypter.decrypt_file(str(encrypted_file), str(decrypted_file)):
print("✅ Doğru şifre ile başarıyla çözüldü")
# Temizlik
test_file.unlink()
encrypted_file.unlink()
if decrypted_file.exists():
decrypted_file.unlink()
print("\n✅ TEST BAŞARILI")
return True
def test_large_file():
"""Büyük dosya testi"""
print("\n" + "="*60)
print("TEST 3: Büyük Dosya Testi (10 MB)")
print("="*60)
# 10 MB test dosyası oluştur
size_mb = 10
size_bytes = size_mb * 1024 * 1024
with tempfile.NamedTemporaryFile(delete=False, suffix='.bin') as f:
test_file = Path(f.name)
# 1 MB chunk'lar halinde yaz
chunk_size = 1024 * 1024
for _ in range(size_mb):
f.write(os.urandom(chunk_size))
print(f"\n📝 {size_mb} MB test dosyası oluşturuldu")
# Şifrele
password = "bigfile123"
crypter = FileCrypter(password)
encrypted_file = test_file.with_suffix('.encrypted')
print(f"🔐 Şifreleniyor...")
if not crypter.encrypt_file(str(test_file), str(encrypted_file)):
print("❌ Şifreleme başarısız!")
return False
print(f"✅ Şifrelendi")
# Dosya boyutunu kontrol et
original_size = test_file.stat().st_size
encrypted_size = encrypted_file.stat().st_size
print(f"📊 Orijinal: {original_size:,} byte")
print(f"📊 Şifreli: {encrypted_size:,} byte")
print(f"📊 Overhead: {encrypted_size - original_size} byte (Salt + IV + Padding)")
# Orijinali sil
test_file.unlink()
# Şifre çöz
decrypted_file = encrypted_file.with_suffix('')
print(f"🔓 Şifre çözülüyor...")
if not crypter.decrypt_file(str(encrypted_file), str(decrypted_file)):
print("❌ Şifre çözme başarısız!")
return False
print(f"✅ Şifre çözüldü")
# Boyutu kontrol et
decrypted_size = decrypted_file.stat().st_size
if decrypted_size == original_size:
print(f"✅ Boyut doğrulandı: {decrypted_size:,} byte")
else:
print(f"❌ Boyut eşleşmiyor! Beklenen: {original_size:,}, Gerçek: {decrypted_size:,}")
return False
# Temizlik
encrypted_file.unlink()
decrypted_file.unlink()
print("\n✅ TEST BAŞARILI")
return True
def test_special_characters():
"""Özel karakter ve binary veri testi"""
print("\n" + "="*60)
print("TEST 4: Özel Karakterler ve Binary Veri")
print("="*60)
# Özel karakterler içeren içerik
test_content = """
Türkçe: ğüşıöçĞÜŞİÖÇ
Emoji: 🔐 🚀 💻 ✅ ❌
Özel: !@#$%^&*()_+-=[]{}|;':",./<>?
Unicode: 你好 مرحبا Привет
"""
with tempfile.NamedTemporaryFile(mode='w', encoding='utf-8', delete=False, suffix='.txt') as f:
test_file = Path(f.name)
f.write(test_content)
print(f"\n📝 Özel karakterli test dosyası oluşturuldu")
# Şifrele
password = "special!@#123"
crypter = FileCrypter(password)
encrypted_file = test_file.with_suffix(test_file.suffix + '.encrypted')
crypter.encrypt_file(str(test_file), str(encrypted_file))
test_file.unlink()
# Şifre çöz
decrypted_file = encrypted_file.with_suffix('')
crypter.decrypt_file(str(encrypted_file), str(decrypted_file))
# İçeriği kontrol et
with open(decrypted_file, 'r', encoding='utf-8') as f:
decrypted_content = f.read()
if decrypted_content == test_content:
print("✅ Özel karakterler doğrulandı!")
else:
print("❌ İçerik eşleşmiyor!")
return False
# Temizlik
encrypted_file.unlink()
decrypted_file.unlink()
print("\n✅ TEST BAŞARILI")
return True
def main():
"""Tüm testleri çalıştır"""
print("""
╔══════════════════════════════════════════════════════╗
║ 🧪 FileCrypter Test Suite 🧪 ║
╚══════════════════════════════════════════════════════╝
""")
tests = [
("Tek Dosya", test_single_file),
("Yanlış Şifre", test_wrong_password),
("Büyük Dosya (10 MB)", test_large_file),
("Özel Karakterler", test_special_characters),
]
results = []
for test_name, test_func in tests:
try:
result = test_func()
results.append((test_name, result))
except Exception as e:
print(f"\n❌ TEST HATASI: {str(e)}")
results.append((test_name, False))
# Sonuçları göster
print("\n" + "="*60)
print("TEST SONUÇLARI")
print("="*60)
for test_name, result in results:
status = "✅ BAŞARILI" if result else "❌ BAŞARISIZ"
print(f"{test_name:.<40} {status}")
# Özet
total = len(results)
passed = sum(1 for _, result in results if result)
print("\n" + "="*60)
print(f"TOPLAM: {passed}/{total} test başarılı")
print("="*60)
if passed == total:
print("\n🎉 TÜM TESTLER BAŞARILI! 🎉")
return 0
else:
print(f"\n⚠️ {total - passed} test başarısız!")
return 1
if __name__ == '__main__':
exit(main())