code wiki / _hdl_build / nx_cms_snapshot_test.nx

nx_cms_snapshot_test.nx source

↩ module page · 63 lines · 2770 B

1// nx_cms_snapshot_test.nx -- KAT for the snapshot archive: pack 2 files with BINARY content 2// (newlines, NUL, high bytes), unpack to a fresh dir, assert byte-exact restore + correct count. 3// expect_exit: 0 4import "nx_cms_snapshot.nx" 5import "nx_syscalls.nx" 6 7func ts_mkdir(path: *u8, mode: i64) -> i64 { let nb: *i64 = sys_mmap(8) as *i64; nb[0] = 258; return __syscall(nb[0], 0 - 100, path, mode, 0, 0, 0) } 8func ts_write(path: *u8, buf: *u8, n: i64) -> i64 { let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0 } sys_write(fd, buf, n); sys_close(fd); return 1 } 9func ts_read(path: *u8, out: *u8, cap: i64) -> i64 { 10 let fd: i64 = sys_openat_rd(path) 11 if fd < 0 { return 0 - 1 } 12 var total: i64 = 0 13 var go: i64 = 1 14 while go == 1 { let r: i64 = sys_read(fd, (out + total) as *u8, cap - total); go = 0; if r > 0 { total = total + r; if total < cap { go = 1 } } } 15 sys_close(fd) 16 return total 17} 18 19func main() -> i64 { 20 var bad: i64 = 0 21 ts_mkdir("/tmp/_snap_src" as *u8, 0x1ed) 22 ts_mkdir("/tmp/_snap_dst" as *u8, 0x1ed) 23 // file A: text with newlines; file B: binary with NUL + high bytes 24 let a: *u8 = "line one\nline two\n@key value\n" as *u8 25 var an: i64 = 0 26 while a[an] != (0 as u8) { an = an + 1 } 27 ts_write("/tmp/_snap_src/content.txt" as *u8, a, an) 28 let b: *u8 = sys_mmap(300) 29 var i: i64 = 0 30 while i < 256 { b[i] = i as u8; i = i + 1 } // every byte 0..255 incl NUL + high 31 ts_write("/tmp/_snap_src/image.bin" as *u8, b, 256) 32 33 let names: *i64 = sys_mmap(8*4) as *i64 34 names[0] = "content.txt" as *u8 as i64 35 names[1] = "image.bin" as *u8 as i64 36 names[2] = "missing.txt" as *u8 as i64 // absent -> skipped 37 let arc: *u8 = sys_mmap(65536) 38 let alen: i64 = snap_pack("/tmp/_snap_src" as *u8, names, 3, arc, 65535) 39 if alen < 0 { bad = bad + 1 } 40 41 let cnt: i64 = snap_unpack(arc, alen, "/tmp/_snap_dst" as *u8) 42 if cnt != 2 { bad = bad + 1 } // 2 present, 1 skipped 43 44 // byte-exact compare A 45 let ra: *u8 = sys_mmap(1024) 46 let ran: i64 = ts_read("/tmp/_snap_dst/content.txt" as *u8, ra, 1023) 47 if ran != an { bad = bad + 1 } 48 var q: i64 = 0 49 while q < an { if ra[q] != a[q] { bad = bad + 1; q = an } q = q + 1 } 50 // byte-exact compare B (all 256 byte values) 51 let rb: *u8 = sys_mmap(1024) 52 let rbn: i64 = ts_read("/tmp/_snap_dst/image.bin" as *u8, rb, 1023) 53 if rbn != 256 { bad = bad + 1 } 54 var w: i64 = 0 55 while w < 256 { if (rb[w] as i64) != w { bad = bad + 1; w = 256 } w = w + 1 } 56 // a corrupt archive (bad magic) is rejected 57 let bogus: *u8 = "XXXXXXXX....." as *u8 58 if snap_unpack(bogus, 13, "/tmp/_snap_dst" as *u8) >= 0 { bad = bad + 1 } 59 60 if bad == 0 { sys_exit(0) } 61 sys_exit(1) 62 return 1 63}