-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_script.py
More file actions
232 lines (189 loc) · 7.79 KB
/
test_script.py
File metadata and controls
232 lines (189 loc) · 7.79 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
#!/usr/bin/env python3
"""
Basic test script for TrackShred
Creates test files and runs TrackShred operations on them
"""
import os
import shutil
import subprocess
import tempfile
import time
from pathlib import Path
class TrackShredTester:
def __init__(self):
self.test_dir = None
self.trackshred_path = None
self.results = []
def setup(self):
"""Setup test environment"""
print("🧪 Setting up test environment...")
# Find trackshred executable
self.trackshred_path = shutil.which('trackshred')
if not self.trackshred_path:
# Try local installation
local_path = Path.home() / '.local' / 'bin' / 'trackshred'
if local_path.exists():
self.trackshred_path = str(local_path)
else:
# Try current directory
current_path = Path('./trackshred.py')
if current_path.exists():
self.trackshred_path = str(current_path)
else:
raise FileNotFoundError("TrackShred executable not found")
print(f"Using TrackShred at: {self.trackshred_path}")
# Create temporary test directory
self.test_dir = Path(tempfile.mkdtemp(prefix='trackshred_test_'))
print(f"Test directory: {self.test_dir}")
def create_test_files(self):
"""Create test files with various content"""
print("📝 Creating test files...")
test_files = {
'text_file.txt': "This is sensitive text content\nLine 2\nLine 3",
'binary_file.bin': bytes(range(256)),
'script_file.sh': "#!/bin/bash\necho 'secret command'\n",
'config_file.json': '{"secret": "value", "password": "123456"}',
}
for filename, content in test_files.items():
file_path = self.test_dir / filename
if isinstance(content, str):
file_path.write_text(content)
else:
file_path.write_bytes(content)
print(f"Created: {filename}")
# Create a subdirectory with files
subdir = self.test_dir / 'subdir'
subdir.mkdir()
(subdir / 'nested_file.txt').write_text("Nested sensitive content")
print("Created: subdir/nested_file.txt")
def run_test(self, test_name, command_args, expect_success=True):
"""Run a single test"""
print(f"\n🔍 Running test: {test_name}")
print(f"Command: python3 {self.trackshred_path} {' '.join(command_args)}")
try:
# Run the command
cmd = ['python3', self.trackshred_path] + command_args
result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=30
)
success = (result.returncode == 0) == expect_success
if success:
print(f"✅ {test_name}: PASSED")
else:
print(f"❌ {test_name}: FAILED")
print(f"Exit code: {result.returncode}")
print(f"STDOUT:\n{result.stdout}")
print(f"STDERR:\n{result.stderr}")
self.results.append({
'test': test_name,
'success': success,
'exit_code': result.returncode,
'stdout': result.stdout,
'stderr': result.stderr
})
return success
except subprocess.TimeoutExpired:
print(f"❌ {test_name}: TIMEOUT")
self.results.append({
'test': test_name,
'success': False,
'error': 'timeout'
})
return False
except Exception as e:
print(f"❌ {test_name}: ERROR - {e}")
self.results.append({
'test': test_name,
'success': False,
'error': str(e)
})
return False
def run_all_tests(self):
"""Run all tests"""
print("\n🚀 Running TrackShred tests...")
# Test 1: Version check
self.run_test("Version Check", ["--version"])
# Test 2: Help output
self.run_test("Help Output", ["--help"])
# Test 3: Dry run on single file
test_file = str(self.test_dir / 'text_file.txt')
self.run_test("Dry Run Single File", ["--target", test_file, "--dry-run"])
# Test 4: Metadata-only mode
self.run_test("Metadata Only Mode", ["--target", test_file, "--metadata-only", "--dry-run"])
# Test 5: Directory dry run
self.run_test("Directory Dry Run", ["--target", str(self.test_dir), "--dry-run"])
# Test 6: Deep clean dry run
self.run_test("Deep Clean Dry Run", ["--deep", "--dry-run"])
# Test 7: Combined operations dry run
self.run_test("Combined Operations Dry Run", ["--target", test_file, "--deep", "--dry-run"])
# Test 8: Custom shred passes
self.run_test("Custom Shred Passes", ["--target", test_file, "--shred-passes", "5", "--dry-run"])
# Test 9: Invalid target (should fail)
self.run_test("Invalid Target", ["--target", "/nonexistent/file"], expect_success=False)
# Test 10: No arguments (should fail)
self.run_test("No Arguments", [], expect_success=False)
# Test 11: Actual file shredding (create a disposable file)
disposable_file = self.test_dir / 'disposable.txt'
disposable_file.write_text("This file will be shredded")
self.run_test("Actual File Shredding", ["--target", str(disposable_file)])
# Verify the file was actually deleted
if not disposable_file.exists():
print("✅ File was successfully deleted")
else:
print("❌ File still exists after shredding")
def generate_report(self):
"""Generate test report"""
print("\n📊 Test Report")
print("=" * 50)
passed = sum(1 for r in self.results if r['success'])
total = len(self.results)
print(f"Tests passed: {passed}/{total}")
print(f"Success rate: {passed/total*100:.1f}%")
print("\nDetailed Results:")
for result in self.results:
status = "✅ PASS" if result['success'] else "❌ FAIL"
print(f" {status} - {result['test']}")
if not result['success'] and 'error' in result:
print(f" Error: {result['error']}")
return passed == total
def cleanup(self):
"""Clean up test environment"""
if self.test_dir and self.test_dir.exists():
print(f"\n🧹 Cleaning up test directory: {self.test_dir}")
shutil.rmtree(self.test_dir)
def run(self):
"""Run the complete test suite"""
try:
self.setup()
self.create_test_files()
self.run_all_tests()
success = self.generate_report()
return success
finally:
self.cleanup()
def main():
"""Main test function"""
print("🔐 TrackShred Test Suite")
print("=" * 30)
tester = TrackShredTester()
try:
success = tester.run()
if success:
print("\n🎉 All tests passed!")
exit(0)
else:
print("\n⚠️ Some tests failed!")
exit(1)
except KeyboardInterrupt:
print("\n\n⏹️ Tests interrupted by user")
tester.cleanup()
exit(130)
except Exception as e:
print(f"\n💥 Test suite error: {e}")
tester.cleanup()
exit(1)
if __name__ == "__main__":
main()