Files
ziran/tests/bundle_assertions.zi
T
waozi cb0a762fd4 Migrate to one ZX format and delete the old route
zi2zx bundle and ziran run always write and run revision-28 executable
ZX. The semantic bundle reader and writer, the portable VM, the
TinyCC-compiled and linked native bundles with the vendored TinyCC,
bundle libraries, the VM profile, --vm, BundleInstantiatePortable,
libziran-vm.a and the ZIR-based Wasm lowering with zi2wasm are removed;
the Wasm tier, the embedding API and every tool run revision 28. Tests
of the removed VM internals and libraries are removed, and the rest run
on revision 28. Engine problems read as the diagnostics users rely on.
2026-11-14 14:03:01 -03:00

75 lines
4.0 KiB
Ziran

// Independent byte-level assertions and corruptions of actual compiler output.
#import "native_test"
#import "check_support"
#import "std/byte_text_linux"
#import "std/text_tools"
using Vectors :: #import "std/vec"
BundleTestU32 :: (test: *NativeTest, data: string, at: s64) -> u32 {
if !NativeAssert(test, at >= 0 && at <= data.count && data.count - at >= 4, "bundle field leaves its input") { return 0 }
value: u32
for index: cast(s64)0..3 { value |= cast(u32)data[at + index] << cast(u32)(index * 8) }
return value
}
BundleTestReplace :: (test: *NativeTest, path: string, data: string, at: s64, count: s64, replacement: string) -> bool {
if !NativeAssert(test, at >= 0 && count >= 0 && at <= data.count && count <= data.count - at, "bundle mutation leaves its input") { return false }
parts := string.[data[:at], replacement, data[at + count:]]
return NativeWrite(test, CheckPath(test.work, path), TextJoin(parts[:], ""))
}
// The capability count is in the manifest, section kind 1.
BundleTestManifestCapabilities :: (test: *NativeTest, data: string) -> u32 {
sections := BundleTestU32(test, data, 32)
for index: cast(s64)0..cast(s64)sections - 1 {
record := 64 + index * 32
if BundleTestU32(test, data, record) == 1 {
return BundleTestU32(test, data, cast(s64)BundleTestU32(test, data, record + 8) + 12)
}
}
unused NativeAssert(test, false, "executable bundle has no manifest")
return 0
}
BundleTestCapabilities :: (test: *NativeTest, mutate: bool) -> bool {
data := NativeRead(test, CheckPath(test.work, "source.zx"))
if !NativeAssert(test, data.count >= 8 && data[:8] == "ZX\x00\x00\x1c\x00\x00\x00", "host bundle header differs") { return false }
if mutate {
at := TextFind(data, "AddTenHost")
if !NativeAssert(test, at >= 0, "bundle is missing the capability being corrupted") { return false }
return BundleTestReplace(test, "tampered.zx", data, at, 1, "X")
}
count := BundleTestManifestCapabilities(test, data)
return NativeAssert(test, !test.failed && count == 7 && !TextContains(data, "UnusedHost"), "bundle capability closure includes unused imports or omits a reachable import")
}
BundleTestAssets :: (test: *NativeTest) -> bool {
data := NativeRead(test, CheckPath(test.work, "source.zx")); path := "assets/a.bin"; at := TextFind(data, path)
if !NativeAssert(test, at >= 8, "bundle asset path is missing") { return false }
names := string.["traversal", "absolute", "backslash", "nul"]
values := string.["../bad/a.bin", "/ssets/a.bin", "assets\\a.bin", "assets\x00a.bin"]
for index: cast(s64)0..names.count - 1 {
if !NativeAssert(test, values[index].count == path.count, "asset corruption must preserve field length") ||
!BundleTestReplace(test, CheckConcat(names[index], ".zx"), data, at, path.count, values[index]) { return false }
}
if !NativeWrite(test, CheckPath(test.work, "truncated.zx"), data[:data.count - 1]) { return false }
maximum: [4]u8 = .[255, 255, 255, 255]
if !BundleTestReplace(test, "oversized.zx", data, at - 8, 4, TextFromBytes(maximum[:])) { return false }
nested := "assets/sub/z.bin"; nested_at := TextFind(data, nested)
if !NativeAssert(test, nested_at >= 0, "nested asset is missing") { return false }
return BundleTestReplace(test, "unsorted.zx", data, nested_at, nested.count, "assets/aaa/z.bin")
}
#program_export
main :: (argc: s32, argv: **u8) -> s32 {
test := NativeStart(argc, argv, "bundle-assertions-")
if test.failed { return 1 }
defer NativeCleanup(*test)
if !NativeAssert(*test, argc == 4, "pass a bundle assertion mode") { return 1 }
mode := TextFromCString(argv[3]); valid := false
if mode == "capabilities" { valid = BundleTestCapabilities(*test, false) }
else if mode == "tamper-capabilities" { valid = BundleTestCapabilities(*test, true) }
else if mode == "assets" { valid = BundleTestAssets(*test) }
return ifx valid && !test.failed then 0 else 1
}