nx_bgzf_test.nx source
↩ module page · 141 lines · 5555 B
1// nx_bgzf_test.nx -- KAT for BGZF stored-block writer + EOF marker.
2//
3// Section A -- nx_bgzf_eof_marker: byte-exact match against the 28-byte
4// SAMv1 ยง4.1.2 canonical sequence.
5// Section B -- nx_bgzf_write_stored_block: gzip header / XLEN / BC
6// extra subfield / DEFLATE BTYPE=00 / payload / CRC32 /
7// ISIZE for payload "Hi" + length-0 + reject paths.
8// Section C -- round-trip: feed the written block back through
9// nx_gzip_inflate; verify recovered payload matches.
10//
11// expect_exit: 0
12//
13// license_tier: ORIGINAL
14
15import "nx_syscalls.nx"
16import "nx_const.nx"
17import "nx_bgzf.nx"
18import "nx_gzip_wrap.nx"
19import "nx_crc32.nx"
20
21func main() -> i64 {
22
23 // ============================================================
24 // Section A -- canonical 28-byte EOF marker.
25 // ============================================================
26
27 let eof: *u8 = sys_mmap(64)
28 let en: i64 = nx_bgzf_eof_marker(eof, 64)
29 if en != 28 { return 1 }
30
31 // The wire sequence is well-known: bytes match the literal.
32 if (eof[0] & 0xff) != 0x1F { return 2 }
33 if (eof[1] & 0xff) != 0x8B { return 3 }
34 if (eof[2] & 0xff) != 0x08 { return 4 }
35 if (eof[3] & 0xff) != 0x04 { return 5 }
36 if (eof[9] & 0xff) != 0xFF { return 6 }
37 if (eof[10] & 0xff) != 0x06 { return 7 } // XLEN
38 if (eof[12] & 0xff) != 0x42 { return 8 } // 'B'
39 if (eof[13] & 0xff) != 0x43 { return 9 } // 'C'
40 if (eof[14] & 0xff) != 0x02 { return 10 } // SLEN
41 if (eof[16] & 0xff) != 0x1B { return 11 } // BSIZE = 27
42 if (eof[18] & 0xff) != 0x03 { return 12 } // empty fixed-huff
43 if (eof[20] & 0xff) != 0x00 { return 13 } // CRC32 = 0
44 if (eof[24] & 0xff) != 0x00 { return 14 } // ISIZE = 0
45 if (eof[27] & 0xff) != 0x00 { return 15 } // last byte
46
47 // Capacity overflow rejected.
48 if nx_bgzf_eof_marker(eof, 27) != -1 { return 16 }
49
50 // ============================================================
51 // Section B -- stored-block "Hi".
52 // ============================================================
53
54 let hi: *u8 = sys_mmap(8)
55 hi[0] = 0x48 as u8 // 'H'
56 hi[1] = 0x69 as u8 // 'i'
57
58 let block: *u8 = sys_mmap(64)
59 let bn: i64 = nx_bgzf_write_stored_block(hi, 2, block, 64)
60 // total = 31 + 2 = 33; BSIZE = 32
61 if bn != 33 { return 30 }
62
63 // gzip header.
64 if (block[0] & 0xff) != 0x1F { return 31 }
65 if (block[1] & 0xff) != 0x8B { return 32 }
66 if (block[2] & 0xff) != 0x08 { return 33 }
67 if (block[3] & 0xff) != 0x04 { return 34 }
68 if (block[9] & 0xff) != 0xFF { return 35 }
69
70 // XLEN = 6.
71 if (block[10] & 0xff) != 0x06 { return 36 }
72 if (block[11] & 0xff) != 0x00 { return 37 }
73
74 // BC extra subfield + BSIZE = 32 = 0x20.
75 if (block[12] & 0xff) != 0x42 { return 38 }
76 if (block[13] & 0xff) != 0x43 { return 39 }
77 if (block[14] & 0xff) != 0x02 { return 40 }
78 if (block[15] & 0xff) != 0x00 { return 41 }
79 if (block[16] & 0xff) != 0x20 { return 42 } // BSIZE lo
80 if (block[17] & 0xff) != 0x00 { return 43 } // BSIZE hi
81
82 // DEFLATE stored block hdr: BFINAL=1 BTYPE=00 -> 0x01.
83 if (block[18] & 0xff) != 0x01 { return 44 }
84 // LEN = 2 LE
85 if (block[19] & 0xff) != 0x02 { return 45 }
86 if (block[20] & 0xff) != 0x00 { return 46 }
87 // NLEN = ~2 & 0xffff = 0xfffd
88 if (block[21] & 0xff) != 0xFD { return 47 }
89 if (block[22] & 0xff) != 0xFF { return 48 }
90
91 // payload "Hi"
92 if (block[23] & 0xff) != 0x48 { return 49 }
93 if (block[24] & 0xff) != 0x69 { return 50 }
94
95 // CRC32 bytes match value computed from the payload directly.
96 let exp_crc: i64 = nx_crc32(hi, 2)
97 if (block[25] & 0xff) != ((exp_crc ) & 0xff) { return 51 }
98 if (block[26] & 0xff) != ((exp_crc >> 8) & 0xff) { return 52 }
99 if (block[27] & 0xff) != ((exp_crc >> 16) & 0xff) { return 53 }
100 if (block[28] & 0xff) != ((exp_crc >> 24) & 0xff) { return 54 }
101
102 // ISIZE = 2 LE.
103 if (block[29] & 0xff) != 0x02 { return 55 }
104 if (block[30] & 0xff) != 0x00 { return 56 }
105 if (block[31] & 0xff) != 0x00 { return 57 }
106 if (block[32] & 0xff) != 0x00 { return 58 }
107
108 // Length-0 block (just the 31-byte frame, no payload).
109 let zb: i64 = nx_bgzf_write_stored_block(hi, 0, block, 64)
110 if zb != 31 { return 60 }
111 // CRC32 of empty = 0
112 if (block[23] & 0xff) != 0x00 { return 61 }
113 if (block[26] & 0xff) != 0x00 { return 62 }
114 // ISIZE = 0
115 if (block[27] & 0xff) != 0x00 { return 63 }
116 if (block[30] & 0xff) != 0x00 { return 64 }
117
118 // Reject paths.
119 if nx_bgzf_write_stored_block(hi, -1, block, 64) != -1 { return 70 }
120 if nx_bgzf_write_stored_block(hi, 65505, block, 65536) != -1 { return 71 }
121 if nx_bgzf_write_stored_block(hi, 2, block, 30) != -1 { return 72 }
122
123 // ============================================================
124 // Section C -- round-trip via nx_gzip_inflate.
125 //
126 // The BGZF stored block is a valid gzip stream; feed it through
127 // the existing inflate path to confirm we get "Hi" back.
128 // ============================================================
129
130 let bn2: i64 = nx_bgzf_write_stored_block(hi, 2, block, 64)
131 if bn2 != 33 { return 80 }
132
133 let gr: *NxGzipResult = nx_gzip_inflate(block, 33, 64)
134 if gr.error_code != NX_GZ_OK { return 81 }
135 if gr.output_size != 2 { return 82 }
136 if (gr.output_data[0] & 0xff) != 0x48 { return 83 }
137 if (gr.output_data[1] & 0xff) != 0x69 { return 84 }
138 if gr.crc_expected != gr.crc_computed { return 85 }
139
140 return 0
141}