-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
332 lines (258 loc) · 11.8 KB
/
test.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import os
import shutil
import subprocess
import tempfile
import gzip
import json
import time
CURRENT_DIR = os.getcwd()
TEST_FILES_DIR = os.path.join(CURRENT_DIR, "test")
BUILD_COMMAND = ["cargo", "build"]
TEST_COMMAND = ["cargo", "test", "--", "--nocapture"]
def create_test_files():
"""Set up test files and get known hashes"""
os.makedirs(TEST_FILES_DIR, exist_ok=True)
# Create some test files
files = {
"empty.txt": b"",
"small.txt": b"test data",
"medium.txt": b"test data" * 1000,
"large.txt": b"test data" * 100000
}
for name, content in files.items():
path = os.path.join(TEST_FILES_DIR, name)
with open(path, "wb") as f:
f.write(content)
# Create compressed version
gz_path = path + ".gz"
with gzip.open(gz_path, "wb") as f:
f.write(content)
print(f"Created test files in {TEST_FILES_DIR}")
def run_hasher(*args, check_output=True):
"""Run hasher with given args and return output"""
cmd = ["./target/debug/hasher"] + list(args)
print(f"\nExecuting: {' '.join(cmd)}")
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
if result.returncode != 0 and check_output:
print(f"\nCommand failed with return code {result.returncode}")
print("\nSTDOUT:")
print(result.stdout)
print("\nSTDERR:")
print(result.stderr)
return result
def test_hash():
"""Test basic hashing functionality"""
print("\nTesting hash command...")
result = run_hasher("hash", TEST_FILES_DIR)
assert result.returncode == 0, "Basic hash failed"
result = run_hasher("hash", "--json-only", TEST_FILES_DIR)
assert result.returncode == 0, "JSON-only hash failed"
assert result.stdout.strip(), "JSON output is empty"
# Validate JSON output
try:
for line in result.stdout.splitlines():
if line.strip():
json.loads(line)
except json.JSONDecodeError:
raise AssertionError("Invalid JSON output")
result = run_hasher("hash", "--sql-only", TEST_FILES_DIR)
assert result.returncode == 0, "SQL-only hash failed"
# Test compressed file handling
result = run_hasher("hash", "--decompress", TEST_FILES_DIR)
assert result.returncode == 0, "Decompressed hash failed"
# Verify both compressed and decompressed hashes
result = run_hasher("hash", "--decompress", "--hash-both", TEST_FILES_DIR)
assert result.returncode == 0, "Hash both failed"
# Parse output to verify we got both hashes
outputs = []
for line in result.stdout.splitlines():
if line.strip():
try:
data = json.loads(line)
outputs.append(data["file_path"])
except json.JSONDecodeError:
continue
compressed = [p for p in outputs if p.endswith(".gz")]
decompressed = [p for p in outputs if not p.endswith(".gz")]
assert compressed and decompressed, "Missing compressed or decompressed outputs"
def test_verify():
"""Test verification of hashed files"""
print("\nTesting verify command...")
# First hash the files
result = run_hasher("hash", TEST_FILES_DIR)
assert result.returncode == 0, "Initial hash before verify failed"
# Verify all files
result = run_hasher("verify")
assert result.returncode == 0, "Verify all files failed"
# Modify a file and verify again
test_file = os.path.join(TEST_FILES_DIR, "small.txt")
print(f"\nModifying test file: {test_file}")
with open(test_file, "wb") as f:
f.write(b"modified data")
result = run_hasher("verify", "--mismatches-only")
assert result.returncode == 0, "Verify with mismatches failed"
assert "small.txt" in result.stdout, "Modified file not detected"
def test_copy():
"""Test copying functionality"""
print("\nTesting copy command...")
with tempfile.TemporaryDirectory() as tmpdir:
print(f"\nCopying to temp dir: {tmpdir}")
# Test without compression
result = run_hasher("copy", TEST_FILES_DIR, tmpdir)
assert result.returncode == 0, f"Copy failed\nStdout: {result.stdout}\nStderr: {result.stderr}"
# Verify files were copied correctly
missing_files = []
for f in os.listdir(TEST_FILES_DIR):
if not f.endswith(".gz"):
dest_file = os.path.join(tmpdir, f)
if not os.path.exists(dest_file):
missing_files.append(f)
assert not missing_files, f"Files not copied: {missing_files}"
# Test with compression
compressed_dir = os.path.join(tmpdir, "compressed")
os.makedirs(compressed_dir, exist_ok=True)
result = run_hasher("copy", "--compress", TEST_FILES_DIR, compressed_dir)
assert result.returncode == 0, f"Compressed copy failed\nStdout: {result.stdout}\nStderr: {result.stderr}"
# Verify compressed files exist
missing_compressed = []
for f in os.listdir(TEST_FILES_DIR):
if not f.endswith(".gz"):
compressed_file = os.path.join(compressed_dir, f + ".gz")
if not os.path.exists(compressed_file):
missing_compressed.append(f + ".gz")
assert not missing_compressed, f"Compressed files not created: {missing_compressed}"
def test_download():
"""Test downloading functionality"""
print("\nTesting download command...")
with tempfile.TemporaryDirectory() as tmpdir:
print(f"\nDownloading to temp dir: {tmpdir}")
# Create necessary subdirectories
raw_github_dir = os.path.join(tmpdir, "raw.githubusercontent.com")
os.makedirs(raw_github_dir, exist_ok=True)
# Test single file download from GitHub raw
url = "https://raw.githubusercontent.com/rust-lang/rust/master/README.md"
result = run_hasher("download", url, tmpdir, check_output=True)
assert result.returncode == 0, f"Single file download failed\nStdout: {result.stdout}\nStderr: {result.stderr}"
# The file should be in raw.githubusercontent.com/rust-lang/rust/master/README.md
expected_path = os.path.join(raw_github_dir, "rust-lang", "rust", "master", "README.md")
assert os.path.exists(expected_path), f"Downloaded file not found at: {expected_path}"
assert os.path.getsize(expected_path) > 0, "Downloaded file is empty"
# Test multiple file download from list
url_file = os.path.join(tmpdir, "urls.txt")
with open(url_file, "w") as f:
f.write("https://raw.githubusercontent.com/rust-lang/rust/master/CONTRIBUTING.md\n")
f.write("https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT\n")
result = run_hasher("download", url_file, tmpdir)
assert result.returncode == 0, "Multiple file download failed"
# Verify downloaded files exist in their proper paths
for fname in ["CONTRIBUTING.md", "COPYRIGHT"]:
expected = os.path.join(raw_github_dir, "rust-lang", "rust", "master", fname)
assert os.path.exists(expected), f"Downloaded file not found at: {expected}"
assert os.path.getsize(expected) > 0, f"Downloaded file is empty: {expected}"
# Test downloading with compression
compressed_dir = os.path.join(tmpdir, "compressed")
os.makedirs(compressed_dir, exist_ok=True)
# Create compressed github subdirectory
compressed_gh_dir = os.path.join(compressed_dir, "raw.githubusercontent.com", "rust-lang", "rust", "master")
os.makedirs(compressed_gh_dir, exist_ok=True)
result = run_hasher("download", "--compress", url, compressed_dir)
assert result.returncode == 0, "Compressed download failed"
compressed_path = os.path.join(compressed_gh_dir, "README.md.gz")
assert os.path.exists(compressed_path), f"Compressed file not found at: {compressed_path}"
assert os.path.getsize(compressed_path) > 0, "Compressed file is empty"
# Test --no-clobber
result = run_hasher("download", "--no-clobber", url, tmpdir)
assert result.returncode == 0, "No-clobber download failed"
def test_download_failures():
"""Test download failure handling"""
print("\nTesting download failure handling...")
with tempfile.TemporaryDirectory() as tmpdir:
# Test file with mix of valid and invalid URLs
url_file = os.path.join(tmpdir, "urls.txt")
with open(url_file, "w") as f:
f.write("https://raw.githubusercontent.com/rust-lang/rust/master/README.md\n")
f.write("https://invalid.example.com/nonexistent\n") # Should fail
f.write("https://raw.githubusercontent.com/rust-lang/rust/master/COPYRIGHT\n")
# Test with skip-failures
result = run_hasher("download", "--skip-failures", url_file, tmpdir)
assert result.returncode == 0, "Download with skip-failures failed"
# Verify some files were downloaded despite failures
gh_dir = os.path.join(tmpdir, "raw.githubusercontent.com")
successful_files = 0
for root, _, files in os.walk(gh_dir):
successful_files += len(files)
assert successful_files > 0, "No files downloaded with skip-failures"
# Test retry count
start_time = time.time()
result = run_hasher(
"download",
"--skip-failures",
"--retry-count", "2",
"--retry-delay", "1",
"https://invalid.example.com/nonexistent",
tmpdir,
check_output=False
)
end_time = time.time()
assert 1 < end_time - start_time < 4, "Retry timing incorrect"
# Test without skip-failures
result = run_hasher(
"download",
url_file,
tmpdir,
check_output=False
)
assert result.returncode != 0, "Download should fail without skip-failures"
def test_download_list():
"""Test downloading from a list of URLs"""
print("\nTesting download from URL list...")
with tempfile.TemporaryDirectory() as tmpdir:
# Create a file with multiple URLs
url_file = os.path.join(tmpdir, "urls.txt")
with open(url_file, "w") as f:
f.write("https://raw.githubusercontent.com/rust-lang/rust/master/README.md\n")
f.write("https://raw.githubusercontent.com/rust-lang/rust/master/LICENSE-APACHE\n")
f.write("https://raw.githubusercontent.com/rust-lang/rust/master/LICENSE-MIT\n")
# Test downloading the list
result = run_hasher("download", url_file, tmpdir)
assert result.returncode == 0, "Download from URL list failed"
# Verify directory structure
gh_dir = os.path.join(tmpdir, "raw.githubusercontent.com", "rust-lang", "rust", "master")
expected_files = ["README.md", "LICENSE-APACHE", "LICENSE-MIT"]
for fname in expected_files:
fpath = os.path.join(gh_dir, fname)
assert os.path.exists(fpath), f"Missing downloaded file: {fname}"
assert os.path.getsize(fpath) > 0, f"Empty downloaded file: {fname}"
def main():
print("\nRunning hasher tests...")
if subprocess.run(BUILD_COMMAND).returncode != 0:
print("Build failed")
exit(1)
try:
# Set up test environment
create_test_files()
# Run individual test functions
test_hash()
test_verify()
test_copy()
test_download()
test_download_failures()
test_download_list()
except AssertionError as e:
print(f"\nTest failed: {str(e)}")
exit(1)
except Exception as e:
print(f"\nUnexpected error: {str(e)}")
exit(1)
finally:
print(f"\nCleaning up {TEST_FILES_DIR}")
shutil.rmtree(TEST_FILES_DIR, ignore_errors=True)
print("\nAll tests passed!")
exit(0)
if __name__ == "__main__":
main()