| 1 |
|
| 2 |
"""Generate test files with various filenames for HTTPDirFS integration tests. |
| 3 |
|
| 4 |
Creates a set of files with known content and checksums to verify filesystem |
| 5 |
correctness after mounting with httpdirfs. Filenames include |
| 6 |
non-alphanumeric characters, spaces, and other edge cases. |
| 7 |
|
| 8 |
File sizes are randomised between 1 KB and 10 MB using a deterministic seed |
| 9 |
so that the manifest is reproducible across runs. |
| 10 |
""" |
| 11 |
|
| 12 |
import hashlib |
| 13 |
import json |
| 14 |
import os |
| 15 |
import random |
| 16 |
import sys |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
RNG_SEED = 20260518 |
| 21 |
|
| 22 |
ut |
| 23 |
CHUNK_SIZE = 64 * 1024 |
| 24 |
|
| 25 |
|
| 26 |
def write_deterministic_file(filepath, name, size): |
| 27 |
"""Write a file with deterministic content and return its SHA-256. |
| 28 |
|
| 29 |
Uses a name-derived seed with Python's Mersenne Twister PRNG. |
| 30 |
Streams to disk in 64 KB chunks so even multi-GB files use constant |
| 31 |
memory. |
| 32 |
""" |
| 33 |
if size == 0: |
| 34 |
with open(filepath, "wb"): |
| 35 |
pass |
| 36 |
return hashlib.sha256(b"").hexdigest() |
| 37 |
|
| 38 |
seed_hash = hashlib.sha256(name.encode()).digest() |
| 39 |
file_rng = random.Random(int.from_bytes(seed_hash[:8], "big")) |
| 40 |
h = hashlib.sha256() |
| 41 |
written = 0 |
| 42 |
|
| 43 |
with open(filepath, "wb") as f: |
| 44 |
while written < size: |
| 45 |
n = min(CHUNK_SIZE, size - written) |
| 46 |
chunk = file_rng.randbytes(n) |
| 47 |
f.write(chunk) |
| 48 |
h.update(chunk) |
| 49 |
written += n |
| 50 |
|
| 51 |
return h.hexdigest() |
| 52 |
|
| 53 |
|
| 54 |
def random_size(rng): |
| 55 |
"""Return a random file size between 1 KB and 10 MB.""" |
| 56 |
return rng.randint(1024, 10 * 1024 * 1024) |
| 57 |
|
| 58 |
|
| 59 |
def format_size(size): |
| 60 |
"""Human-readable size string.""" |
| 61 |
if size >= 1024 * 1024 * 1024: |
| 62 |
return f"{size / (1024**3):.1f} GB" |
| 63 |
if size >= 1024 * 1024: |
| 64 |
return f"{size / (1024**2):.1f} MB" |
| 65 |
if size >= 1024: |
| 66 |
return f"{size / 1024:.1f} KB" |
| 67 |
return f"{size} bytes" |
| 68 |
|
| 69 |
|
| 70 |
def main(): |
| 71 |
if len(sys.argv) < 2: |
| 72 |
print(f"Usage: {sys.argv[0]} <output_directory> [--short | --long | --all]", |
| 73 |
file=sys.stderr) |
| 74 |
sys.exit(1) |
| 75 |
|
| 76 |
output_dir = sys.argv[1] |
| 77 |
|
| 78 |
|
| 79 |
is_short = False |
| 80 |
is_large = False |
| 81 |
|
| 82 |
if "--short" in sys.argv: |
| 83 |
is_short = True |
| 84 |
elif "--long" in sys.argv: |
| 85 |
is_large = True |
| 86 |
elif "--all" in sys.argv: |
| 87 |
is_short = True |
| 88 |
is_large = True |
| 89 |
|
| 90 |
os.makedirs(output_dir, exist_ok=True) |
| 91 |
|
| 92 |
rng = random.Random(RNG_SEED) |
| 93 |
|
| 94 |
test_files = [] |
| 95 |
if is_short: |
| 96 |
|
| 97 |
|
| 98 |
filenames = [ |
| 99 |
|
| 100 |
"simple.txt", |
| 101 |
"UPPERCASE.DAT", |
| 102 |
|
| 103 |
|
| 104 |
"file with spaces.txt", |
| 105 |
"multiple spaces here.bin", |
| 106 |
|
| 107 |
|
| 108 |
"file-with-dashes.txt", |
| 109 |
"file_with_underscores.txt", |
| 110 |
"file.multiple.dots.txt", |
| 111 |
"file~tilde.txt", |
| 112 |
|
| 113 |
|
| 114 |
"parens(1).txt", |
| 115 |
"brackets[2].txt", |
| 116 |
"curly{3}.txt", |
| 117 |
"hash#tag.txt", |
| 118 |
"[email protected]", |
| 119 |
"plus+plus.txt", |
| 120 |
"equals=value.txt", |
| 121 |
"comma,separated.txt", |
| 122 |
"semi;colon.txt", |
| 123 |
"exclaim!mark.txt", |
| 124 |
"single'quote.txt", |
| 125 |
|
| 126 |
|
| 127 |
"CamelCase123.txt", |
| 128 |
"MiXeD_cAsE-456.dat", |
| 129 |
|
| 130 |
|
| 131 |
"a" * 200 + ".txt", |
| 132 |
] |
| 133 |
|
| 134 |
|
| 135 |
test_files = [(name, random_size(rng)) for name in filenames] |
| 136 |
|
| 137 |
|
| 138 |
test_files.append(("empty_file.txt", 0)) |
| 139 |
test_files.append(("tiny.txt", 1)) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
zero_length_names = [ |
| 144 |
"zero_basic.txt", |
| 145 |
"zero file with spaces.txt", |
| 146 |
"zero-dashes.bin", |
| 147 |
"zero_underscores.bin", |
| 148 |
"zero.multiple.dots.bin", |
| 149 |
"zero(parens).txt", |
| 150 |
"zero[brackets].txt", |
| 151 |
"[email protected]", |
| 152 |
"zero" + "a" * 50 + ".txt", |
| 153 |
] |
| 154 |
for name in zero_length_names: |
| 155 |
test_files.append((name, 0)) |
| 156 |
|
| 157 |
|
| 158 |
if is_large: |
| 159 |
test_files.append(("large_1g.bin", 1024 * 1024 * 1024)) |
| 160 |
|
| 161 |
manifest = {} |
| 162 |
|
| 163 |
for filename, size in test_files: |
| 164 |
filepath = os.path.join(output_dir, filename) |
| 165 |
checksum = write_deterministic_file(filepath, filename, size) |
| 166 |
|
| 167 |
manifest[filename] = { |
| 168 |
"size": size, |
| 169 |
"sha256": checksum, |
| 170 |
} |
| 171 |
print(f" Created: {filename} ({format_size(size)}," |
| 172 |
f" sha256={checksum[:16]}...)") |
| 173 |
|
| 174 |
if is_short: |
| 175 |
|
| 176 |
subdir = os.path.join(output_dir, "subdir with spaces") |
| 177 |
os.makedirs(subdir, exist_ok=True) |
| 178 |
|
| 179 |
subdir_filenames = [ |
| 180 |
"nested file.txt", |
| 181 |
"deep-data.bin", |
| 182 |
] |
| 183 |
|
| 184 |
for filename in subdir_filenames: |
| 185 |
size = random_size(rng) |
| 186 |
filepath = os.path.join(subdir, filename) |
| 187 |
seed_name = f"subdir/{filename}" |
| 188 |
checksum = write_deterministic_file(filepath, seed_name, size) |
| 189 |
|
| 190 |
relative = os.path.join("subdir with spaces", filename) |
| 191 |
manifest[relative] = { |
| 192 |
"size": size, |
| 193 |
"sha256": checksum, |
| 194 |
} |
| 195 |
print(f" Created: {relative} ({format_size(size)}," |
| 196 |
f" sha256={checksum[:16]}...)") |
| 197 |
|
| 198 |
|
| 199 |
manifest_path = os.path.join(output_dir, "manifest.json") |
| 200 |
with open(manifest_path, "w") as f: |
| 201 |
json.dump(manifest, f, indent=2, sort_keys=True) |
| 202 |
print(f"\nManifest written to {manifest_path}") |
| 203 |
print(f"Total files: {len(manifest)}") |
| 204 |
|
| 205 |
|
| 206 |
if __name__ == "__main__": |
| 207 |
main() |