A high-performance Android storage analyzer that bypasses MTP using a native C++ daemon, a Rust TCP bridge, and a React Treemap UI.
Rust
Reject ambiguous transfer benchmark manifests (#163)
Vishnu Srivatsava9389dd115556
diff --git a/tests/benchmark_transfer.py b/tests/benchmark_transfer.pyindex b26a252..6a5c7fc 100644--- a/tests/benchmark_transfer.py+++ b/tests/benchmark_transfer.py@@ -54,12 +54,21 @@ def prepare(workspace, large_mib=64, small_count=1000):def load_manifest(workspace):+ def unique_fields(pairs):+ fields = {}+ for key, value in pairs:+ if key in fields:+ raise ValueError("Manifest contains a duplicate JSON field")+ fields[key] = value+ return fields+with (workspace / "manifest.json").open("rb") as stream:encoded = stream.read(2 * MIB + 1)if len(encoded) > 2 * MIB:raise ValueError("Manifest exceeds 2 MiB")- manifest = json.loads(encoded)- if not isinstance(manifest, dict) or manifest.get("version") != 1:+ manifest = json.loads(encoded, object_pairs_hook=unique_fields)+ if (not isinstance(manifest, dict) or type(manifest.get("version")) is not int+ or manifest["version"] != 1):raise ValueError("Unsupported manifest")if not re.fullmatch(r"SocketSweep-benchmark-[0-9a-f]{32}", str(manifest.get("fixture"))):raise ValueError("Invalid fixture name")diff --git a/tests/test_transfer_benchmark.py b/tests/test_transfer_benchmark.pyindex a7af058..e393bfb 100644--- a/tests/test_transfer_benchmark.py+++ b/tests/test_transfer_benchmark.py@@ -70,7 +70,8 @@ class TransferBenchmarkTests(unittest.TestCase):shutil.rmtree(target)def test_manifest_rejects_traversal_bad_hash_size_and_layout(self):- for change in ("traversal", "absolute", "fixture", "hash", "size", "empty", "layout"):+ for change in ("traversal", "absolute", "fixture", "hash", "size", "empty", "layout",+ "bool-version", "float-version"):manifest = copy.deepcopy(self.manifest)files = manifest["files"]if change in ("traversal", "absolute"):@@ -83,12 +84,25 @@ class TransferBenchmarkTests(unittest.TestCase):files["large/file-000000.bin"]["bytes"] = Trueelif change == "empty":manifest["files"] = {}+ elif change in ("bool-version", "float-version"):+ manifest["version"] = True if change == "bool-version" else 1.0else:del files["small/file-000001.bin"](self.workspace / "manifest.json").write_text(json.dumps(manifest))with self.subTest(change=change), self.assertRaises(ValueError):benchmark.load_manifest(self.workspace)+ def test_manifest_rejects_duplicate_json_fields(self):+ encoded = json.dumps(self.manifest)+ for field, duplicate in [+ ('"version": 1', '"version": 2, "version": 1'),+ ('"small/file-000000.bin":', '"small/file-000000.bin": {}, "small/file-000000.bin":'),+ ('"bytes": 4096', '"bytes": 4097, "bytes": 4096'),+ ]:+ (self.workspace / "manifest.json").write_text(encoded.replace(field, duplicate, 1))+ with self.subTest(field=field), self.assertRaisesRegex(ValueError, "duplicate JSON field"):+ benchmark.load_manifest(self.workspace)+def fake_copy(self, command, **kwargs):workload = command[-2].rsplit("/", 1)[1]shutil.copytree(self.source / workload, Path(command[-1]) / workload)