nx_asn1_emit_test.nx source
↩ module page · 172 lines · 5785 B
1// nx_asn1_emit_test.nx -- smoke for ASN.1 DER encoder helpers.
2//
3// Exercises length-encoding (every size class), TLV emission,
4// common types (INTEGER, NULL, OCTET STRING, BIT STRING, UTF8String),
5// SEQUENCE + SET headers, OID emission, context-specific tags.
6//
7// expect_exit: 0
8//
9// license_tier: ORIGINAL
10
11import "nx_syscalls_x86_64.nx"
12import "nx_asn1_emit.nx"
13
14func bytes_eq(a: *u8, b: *u8, n: i64) -> i64 {
15 var i: i64 = 0
16 while i < n {
17 if a[i] != b[i] { return 0 }
18 i = i + 1
19 }
20 return 1
21}
22
23func main() -> i64 {
24 let buf: *u8 = sys_mmap(2048)
25 let off: *i64 = sys_mmap(8) as *i64
26
27 // ---- Verdict enum ----
28 if nxae_verdict_is_valid(NXAE_OK) != 1 { return 1 }
29 if nxae_verdict_is_valid(NXAE_OOM_BUFFER) != 1 { return 2 }
30 if nxae_verdict_is_valid(NXAE_VERDICT_N) != 0 { return 3 }
31
32 // ---- Length encoding: 0..127 (single byte) ----
33 off[0] = 0
34 if nxae_put_length(buf, off, 16, 0) != NXAE_OK { return 10 }
35 if off[0] != 1 { return 11 }
36 if buf[0] != 0 as u8 { return 12 }
37
38 off[0] = 0
39 if nxae_put_length(buf, off, 16, 127) != NXAE_OK { return 13 }
40 if off[0] != 1 { return 14 }
41 if buf[0] != 127 as u8 { return 15 }
42
43 // ---- Length encoding: 128..255 ----
44 off[0] = 0
45 if nxae_put_length(buf, off, 16, 200) != NXAE_OK { return 20 }
46 if off[0] != 2 { return 21 }
47 if buf[0] != 0x81 as u8 { return 22 }
48 if buf[1] != 200 as u8 { return 23 }
49
50 // ---- Length encoding: 256..65535 ----
51 off[0] = 0
52 if nxae_put_length(buf, off, 16, 1000) != NXAE_OK { return 30 }
53 if off[0] != 3 { return 31 }
54 if buf[0] != 0x82 as u8 { return 32 }
55 if buf[1] != 0x03 as u8 { return 33 } // 1000 = 0x03E8
56 if buf[2] != 0xe8 as u8 { return 34 }
57
58 // ---- Length encoding: 65536+ ----
59 off[0] = 0
60 if nxae_put_length(buf, off, 16, 100000) != NXAE_OK { return 40 }
61 if off[0] != 4 { return 41 }
62 if buf[0] != 0x83 as u8 { return 42 }
63 // 100000 = 0x0186A0
64 if buf[1] != 0x01 as u8 { return 43 }
65 if buf[2] != 0x86 as u8 { return 44 }
66 if buf[3] != 0xa0 as u8 { return 45 }
67
68 // ---- length_size helper ----
69 if nxae_length_size(0) != 1 { return 50 }
70 if nxae_length_size(127) != 1 { return 51 }
71 if nxae_length_size(128) != 2 { return 52 }
72 if nxae_length_size(65535) != 3 { return 53 }
73 if nxae_length_size(65536) != 4 { return 54 }
74 if nxae_length_size(16777216) != 5 { return 55 }
75
76 // ---- INTEGER u8 ----
77 off[0] = 0
78 if nxae_put_int_u8(buf, off, 16, 0) != NXAE_OK { return 60 }
79 if off[0] != 3 { return 61 }
80 // 0x02 0x01 0x00
81 if buf[0] != 0x02 as u8 { return 62 }
82 if buf[1] != 0x01 as u8 { return 63 }
83 if buf[2] != 0x00 as u8 { return 64 }
84
85 off[0] = 0
86 if nxae_put_int_u8(buf, off, 16, 42) != NXAE_OK { return 65 }
87 if buf[2] != 42 as u8 { return 66 }
88
89 // BAD_ARG: negative or > 255
90 off[0] = 0
91 if nxae_put_int_u8(buf, off, 16, -1) != NXAE_BAD_ARG { return 67 }
92 if nxae_put_int_u8(buf, off, 16, 256) != NXAE_BAD_ARG { return 68 }
93
94 // ---- NULL ----
95 off[0] = 0
96 if nxae_put_null(buf, off, 16) != NXAE_OK { return 70 }
97 if off[0] != 2 { return 71 }
98 if buf[0] != 0x05 as u8 { return 72 }
99 if buf[1] != 0x00 as u8 { return 73 }
100
101 // ---- OCTET STRING ----
102 off[0] = 0
103 if nxae_put_octet_string(buf, off, 16, "abc" as *u8, 3) != NXAE_OK { return 80 }
104 if off[0] != 5 { return 81 }
105 if buf[0] != 0x04 as u8 { return 82 }
106 if buf[1] != 0x03 as u8 { return 83 }
107 if buf[2] != 0x61 as u8 { return 84 } // 'a'
108
109 // ---- BIT STRING ----
110 off[0] = 0
111 if nxae_put_bit_string(buf, off, 16, 0, "ab" as *u8, 2) != NXAE_OK { return 90 }
112 if off[0] != 5 { return 91 }
113 // tag(03) len(03) unused(00) a b
114 if buf[0] != 0x03 as u8 { return 92 }
115 if buf[1] != 0x03 as u8 { return 93 }
116 if buf[2] != 0x00 as u8 { return 94 }
117 if buf[3] != 0x61 as u8 { return 95 }
118
119 // BAD_ARG: unused_bits > 7
120 off[0] = 0
121 if nxae_put_bit_string(buf, off, 16, 8, "x" as *u8, 1) != NXAE_BAD_ARG { return 96 }
122
123 // ---- UTF8String ----
124 off[0] = 0
125 if nxae_put_utf8(buf, off, 16, "Nishi" as *u8, 5) != NXAE_OK { return 100 }
126 if off[0] != 7 { return 101 }
127 if buf[0] != 0x0c as u8 { return 102 }
128 if buf[1] != 0x05 as u8 { return 103 }
129
130 // ---- SEQUENCE header ----
131 off[0] = 0
132 if nxae_put_sequence_header(buf, off, 16, 10) != NXAE_OK { return 110 }
133 if off[0] != 2 { return 111 }
134 if buf[0] != 0x30 as u8 { return 112 }
135 if buf[1] != 10 as u8 { return 113 }
136
137 // ---- SET header ----
138 off[0] = 0
139 if nxae_put_set_header(buf, off, 16, 5) != NXAE_OK { return 120 }
140 if off[0] != 2 { return 121 }
141 if buf[0] != 0x31 as u8 { return 122 }
142
143 // ---- OID emission ----
144 let oid_bytes: *u8 = sys_mmap(8)
145 oid_bytes[0] = 0x2b as u8
146 oid_bytes[1] = 0x65 as u8
147 oid_bytes[2] = 0x70 as u8
148 off[0] = 0
149 if nxae_put_oid(buf, off, 16, oid_bytes, 3) != NXAE_OK { return 130 }
150 // tag(06) len(03) bytes
151 if buf[0] != 0x06 as u8 { return 131 }
152 if buf[1] != 0x03 as u8 { return 132 }
153 if buf[2] != 0x2b as u8 { return 133 }
154 if buf[3] != 0x65 as u8 { return 134 }
155 if buf[4] != 0x70 as u8 { return 135 }
156
157 // ---- Context-specific [0] EXPLICIT ----
158 off[0] = 0
159 if nxae_put_context_explicit(buf, off, 16, 0, "x" as *u8, 1) != NXAE_OK { return 140 }
160 if off[0] != 3 { return 141 }
161 if buf[0] != 0xa0 as u8 { return 142 }
162 if buf[1] != 0x01 as u8 { return 143 }
163 if buf[2] != 0x78 as u8 { return 144 } // 'x'
164
165 // ---- OOM_BUFFER ----
166 let small: *u8 = sys_mmap(2)
167 let s_off: *i64 = sys_mmap(8) as *i64
168 s_off[0] = 0
169 if nxae_put_bytes(small, s_off, 2, "abc" as *u8, 3) != NXAE_OOM_BUFFER { return 150 }
170
171 return 0
172}