nx_http_form_test.nx source
↩ module page · 145 lines · 6219 B
1// nx_http_form_test.nx -- smoke for urlencoded form parser.
2//
3// expect_exit: 0
4//
5// license_tier: ORIGINAL
6
7import "nx_syscalls_x86_64.nx"
8import "nx_http_form.nx"
9
10func bytes_eq(a: *u8, b: *u8, n: i64) -> i64 {
11 var i: i64 = 0
12 while i < n {
13 if a[i] != b[i] { return 0 }
14 i = i + 1
15 }
16 return 1
17}
18
19func main() -> i64 {
20 // ---- Verdict enum ----
21 if nxf_verdict_is_valid(NXF_OK) != 1 { return 1 }
22 if nxf_verdict_is_valid(NXF_NOT_FOUND) != 1 { return 2 }
23 if nxf_verdict_is_valid(NXF_VERDICT_N) != 0 { return 3 }
24 if bytes_eq(nxf_verdict_name(NXF_OK), "OK" as *u8, 2) != 1 { return 4 }
25 if bytes_eq(nxf_verdict_name(NXF_BAD_PCT), "BAD_PCT" as *u8, 7) != 1 { return 5 }
26
27 // ---- Hex nibble decoder ----
28 if nxf_hex_nibble(0x30) != 0 { return 10 } // '0'
29 if nxf_hex_nibble(0x39) != 9 { return 11 } // '9'
30 if nxf_hex_nibble(0x61) != 10 { return 12 } // 'a'
31 if nxf_hex_nibble(0x66) != 15 { return 13 } // 'f'
32 if nxf_hex_nibble(0x41) != 10 { return 14 } // 'A'
33 if nxf_hex_nibble(0x46) != 15 { return 15 } // 'F'
34 if nxf_hex_nibble(0x67) != -1 { return 16 } // 'g'
35 if nxf_hex_nibble(0x40) != -1 { return 17 } // '@'
36
37 let val_buf: *u8 = sys_mmap(256)
38 let val_len: *i64 = sys_mmap(8) as *i64
39
40 // ---- Simple key=value ----
41 let body1: *u8 = "username=alice" as *u8
42 let rc1: i64 = nx_http_form_get_field(body1, 14,
43 "username" as *u8, 8,
44 val_buf, 256, val_len)
45 if rc1 != NXF_OK { return 20 }
46 if val_len[0] != 5 { return 21 }
47 if bytes_eq(val_buf, "alice" as *u8, 5) != 1 { return 22 }
48
49 // ---- Multiple fields; find second ----
50 let body2: *u8 = "username=alice&password=secret123" as *u8
51 let rc2: i64 = nx_http_form_get_field(body2, 33,
52 "password" as *u8, 8,
53 val_buf, 256, val_len)
54 if rc2 != NXF_OK { return 30 }
55 if val_len[0] != 9 { return 31 }
56 if bytes_eq(val_buf, "secret123" as *u8, 9) != 1 { return 32 }
57
58 // ---- Find first field too ----
59 let rc3: i64 = nx_http_form_get_field(body2, 33,
60 "username" as *u8, 8,
61 val_buf, 256, val_len)
62 if rc3 != NXF_OK { return 40 }
63 if val_len[0] != 5 { return 41 }
64 if bytes_eq(val_buf, "alice" as *u8, 5) != 1 { return 42 }
65
66 // ---- `+` decodes to space ----
67 let body3: *u8 = "msg=hello+world" as *u8
68 let rc4: i64 = nx_http_form_get_field(body3, 15,
69 "msg" as *u8, 3,
70 val_buf, 256, val_len)
71 if rc4 != NXF_OK { return 50 }
72 if val_len[0] != 11 { return 51 }
73 if bytes_eq(val_buf, "hello world" as *u8, 11) != 1 { return 52 }
74
75 // ---- %HH decode (lowercase) ----
76 let body4: *u8 = "q=hello%20world%21" as *u8
77 let rc5: i64 = nx_http_form_get_field(body4, 18,
78 "q" as *u8, 1,
79 val_buf, 256, val_len)
80 if rc5 != NXF_OK { return 60 }
81 if val_len[0] != 12 { return 61 }
82 if bytes_eq(val_buf, "hello world!" as *u8, 12) != 1 { return 62 }
83
84 // ---- %HH decode (uppercase) ----
85 let body5: *u8 = "q=A%41B" as *u8
86 let rc6: i64 = nx_http_form_get_field(body5, 7,
87 "q" as *u8, 1,
88 val_buf, 256, val_len)
89 if rc6 != NXF_OK { return 70 }
90 if val_len[0] != 3 { return 71 }
91 if bytes_eq(val_buf, "AAB" as *u8, 3) != 1 { return 72 }
92
93 // ---- NOT_FOUND ----
94 let body6: *u8 = "a=1&b=2&c=3" as *u8
95 let rc7: i64 = nx_http_form_get_field(body6, 11,
96 "missing" as *u8, 7,
97 val_buf, 256, val_len)
98 if rc7 != NXF_NOT_FOUND { return 80 }
99
100 // ---- BAD_PCT (truncated %) ----
101 let body7: *u8 = "x=ab%" as *u8
102 let rc8: i64 = nx_http_form_get_field(body7, 5,
103 "x" as *u8, 1,
104 val_buf, 256, val_len)
105 if rc8 != NXF_BAD_PCT { return 90 }
106
107 // ---- BAD_PCT (non-hex after %) ----
108 let body8: *u8 = "x=%zz" as *u8
109 let rc9: i64 = nx_http_form_get_field(body8, 5,
110 "x" as *u8, 1,
111 val_buf, 256, val_len)
112 if rc9 != NXF_BAD_PCT { return 91 }
113
114 // ---- Empty value ----
115 let body9: *u8 = "empty=&filled=yes" as *u8
116 let rc10: i64 = nx_http_form_get_field(body9, 17,
117 "empty" as *u8, 5,
118 val_buf, 256, val_len)
119 if rc10 != NXF_OK { return 100 }
120 if val_len[0] != 0 { return 101 }
121 // Then "filled" should still be findable
122 let rc11: i64 = nx_http_form_get_field(body9, 17,
123 "filled" as *u8, 6,
124 val_buf, 256, val_len)
125 if rc11 != NXF_OK { return 102 }
126 if val_len[0] != 3 { return 103 }
127 if bytes_eq(val_buf, "yes" as *u8, 3) != 1 { return 104 }
128
129 // ---- Count pairs ----
130 if nx_http_form_count_pairs("a=1" as *u8, 3) != 1 { return 110 }
131 if nx_http_form_count_pairs("a=1&b=2" as *u8, 7) != 2 { return 111 }
132 if nx_http_form_count_pairs("a=1&b=2&c=3" as *u8, 11) != 3 { return 112 }
133 if nx_http_form_count_pairs("a=1&" as *u8, 4) != 1 { return 113 } // trailing & ignored
134 if nx_http_form_count_pairs("" as *u8, 0) != 0 { return 114 }
135
136 // ---- BAD_ARG paths ----
137 if nx_http_form_get_field(0 as *u8, 10, "x" as *u8, 1,
138 val_buf, 256, val_len) != NXF_BAD_ARG { return 120 }
139 if nx_http_form_get_field(body1, 14, 0 as *u8, 1,
140 val_buf, 256, val_len) != NXF_BAD_ARG { return 121 }
141 if nx_http_form_get_field(body1, 14, "x" as *u8, 0,
142 val_buf, 256, val_len) != NXF_BAD_ARG { return 122 }
143
144 return 0
145}