interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / core / sovereign-core / om / serve / test.om

465 lines · 12878 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/sovereign-core/om/serve/test.om
3note Comprehensive behavioral test suite with negative/refusal controls for the
4note pure-Omega HTTP inference server, constant-time bearer auth, read caps, and CLI router.
5note Compose with:
6note si/directives/om2.om core/sovereign-core/om/serve/http_auth.om
7note core/sovereign-core/om/serve/router.om core/sovereign-core/om/serve/cli.om
8note core/sovereign-core/om/serve/test.om
9
10text t_pass "PASS "
11text t_fail "FAIL "
12text t_nl "\n"
13text t_all_ok "SOVEREIGN-CORE SERVE TEST SUITE PASS\n"
14text t_all_bad "SOVEREIGN-CORE SERVE TEST SUITE FAIL\n"
15
16text t_n1 "auth_fail_closed_and_constant_time"
17text t_n2 "http_parse_negative_caps_and_smuggling"
18text t_n3 "router_health_and_models_auth"
19text t_n4 "router_chat_completions_json_and_sse"
20text t_n5 "router_chat_negative_auth_and_json"
21text t_n6 "cli_dispatch_valid_and_negative"
22text t_n7 "http_parse_transfer_encoding_and_content_length_smuggling"
23
24text tok_good "omega_secret_bearer_token_012345"
25text tok_short "too_short"
26text tok_space "omega_secret bearer_token_012345"
27text hdr_good "Bearer omega_secret_bearer_token_012345"
28text hdr_flip "Bearer omega_secret_bearer_token_012344"
29text hdr_basic "Basic omega_secret_bearer_token_012345"
30text hdr_extra "Bearer omega_secret_bearer_token_012345x"
31
32note Request fixtures use LF; t_handle_lf expands LF -> CRLF for wire HTTP/1.1.
33text req_health_get "GET /health HTTP/1.1\nHost: localhost\n\n"
34text req_health_post "POST /health HTTP/1.1\nHost: localhost\n\n"
35text req_models_noauth "GET /v1/models HTTP/1.1\nHost: localhost\n\n"
36text req_models_auth "GET /v1/models HTTP/1.1\nHost: localhost\nAuthorization: Bearer omega_secret_bearer_token_012345\n\n"
37text req_unknown_rt "GET /v1/nonexistent HTTP/1.1\nHost: localhost\n\n"
38text req_trav "GET /v1/../health HTTP/1.1\nHost: localhost\n\n"
39text req_bad_method "DELETE /health HTTP/1.1\nHost: localhost\n\n"
40text req_no_term "GET /health HTTP/1.1\nHost: localhost"
41text req_dup_cl "POST /v1/chat/completions HTTP/1.1\nContent-Length: 2\nContent-Length: 2\n\n{}"
42text req_dup_auth "GET /v1/models HTTP/1.1\nAuthorization: Bearer omega_secret_bearer_token_012345\nAuthorization: Bearer omega_secret_bearer_token_012345\n\n"
43text req_cl_mismatch "POST /v1/chat/completions HTTP/1.1\nContent-Length: 10\n\n{}"
44note Transfer-Encoding is never accepted (the body is framed by Content-Length alone): TE alone, TE with CL in
45note either order, odd casing, tab/space padding, empty value, space before the colon; duplicate/conflicting CL.
46text req_te_alone "GET /health HTTP/1.1\nHost: localhost\nTransfer-Encoding: chunked\n\n"
47text req_te_alone_body "POST /v1/chat/completions HTTP/1.1\nTransfer-Encoding: chunked\n\n2\n{}\n0\n\n"
48text req_te_cl "GET /health HTTP/1.1\nContent-Length: 0\nTransfer-Encoding: chunked\n\n"
49text req_cl_te "GET /health HTTP/1.1\nTransfer-Encoding: chunked\nContent-Length: 0\n\n"
50text req_te_case "GET /health HTTP/1.1\ntRANSFER-encoding: identity \n\n"
51text req_te_tab "GET /health HTTP/1.1\nTRANSFER-ENCODING:\tgzip, chunked\t\n\n"
52text req_te_empty "GET /health HTTP/1.1\nTransfer-Encoding:\n\n"
53text req_te_spcolon "GET /health HTTP/1.1\nTransfer-Encoding : chunked\n\n"
54text req_cl_spcolon "GET /health HTTP/1.1\nContent-Length : 0\n\n"
55text req_cl_conflict "POST /v1/chat/completions HTTP/1.1\nContent-Length: 2\nContent-Length: 3\n\n{}"
56text req_cl_dup_case "GET /health HTTP/1.1\nContent-Length: 0\ncontent-LENGTH: 0\n\n"
57text req_cl_list "POST /v1/chat/completions HTTP/1.1\nContent-Length: 2, 2\n\n{}"
58text req_te_lookalike "GET /health HTTP/1.1\nX-Transfer-Encoding-Note: chunked\nTransfer-Encodings: x\n\n"
59text req_cl_huge "POST /v1/chat/completions HTTP/1.1\nContent-Length: 9999\n\n"
60
61text req_chat_ok "POST /v1/chat/completions HTTP/1.1\nHost: localhost\nAuthorization: Bearer omega_secret_bearer_token_012345\nContent-Length: 71\n\n{\"model\":\"qwen-native-om\",\"messages\":[{\"role\":\"user\"}],\"max_tokens\":64}"
62text req_chat_sse "POST /v1/chat/completions HTTP/1.1\nHost: localhost\nAuthorization: Bearer omega_secret_bearer_token_012345\nContent-Length: 69\n\n{\"model\":\"qwen-native-om\",\"messages\":[{\"role\":\"user\"}],\"stream\":true}"
63text req_chat_badtok "POST /v1/chat/completions HTTP/1.1\nHost: localhost\nAuthorization: Bearer omega_secret_bearer_token_012344\nContent-Length: 55\n\n{\"model\":\"qwen-native-om\",\"messages\":[{\"role\":\"user\"}]}"
64text req_chat_nomodel "POST /v1/chat/completions HTTP/1.1\nAuthorization: Bearer omega_secret_bearer_token_012345\nContent-Length: 30\n\n{\"messages\":[{\"role\":\"user\"}]}"
65text req_chat_nomsgs "POST /v1/chat/completions HTTP/1.1\nAuthorization: Bearer omega_secret_bearer_token_012345\nContent-Length: 26\n\n{\"model\":\"qwen-native-om\"}"
66text req_chat_unbal "POST /v1/chat/completions HTTP/1.1\nAuthorization: Bearer omega_secret_bearer_token_012345\nContent-Length: 25\n\n{\"model\":\"qwen-native-om\""
67text req_chat_badmax "POST /v1/chat/completions HTTP/1.1\nAuthorization: Bearer omega_secret_bearer_token_012345\nContent-Length: 70\n\n{\"model\":\"qwen-native-om\",\"messages\":[{\"role\":\"user\"}],\"max_tokens\":0}"
68text req_chat_get "GET /v1/chat/completions HTTP/1.1\nAuthorization: Bearer omega_secret_bearer_token_012345\n\n"
69
70text s_ok_engine "\"omega-sovereign-core\""
71text s_model_id "\"qwen-native-om\""
72text s_chat_obj "\"chat.completion\""
73text s_sse_done "data: [DONE]"
74
75text cli_v1 "serve --port 9090 --max-tokens 1024 --model qwen-native-om"
76text cli_v2 "chat --model qwen-native-om --max-tokens 256"
77text cli_v3 "models"
78text cli_v4 "health"
79text cli_v5 "version"
80text cli_b1 ""
81text cli_b2 "rmrf --port 80"
82text cli_b3 "serve --port 0"
83text cli_b4 "serve --port 70000"
84text cli_b5 "serve --max-tokens 0"
85text cli_b6 "serve --max-tokens 99999"
86text cli_b7 "chat --max-tokens 128"
87text cli_b8 "serve --evil 1"
88text cli_b9 "serve --port 8080 --port 8081"
89
90global t_fails ulong 1
91
92fn t_w s view uchar * to void
93n = len s
94off = ulong 0
95while lt off n bound 256
96k = write 1 s off sub n off
97if le k 0
98break
99end
100off = add off ulong k
101end
102end
103
104fn t_report name view uchar * ok bool to void
105if ok
106t_w t_pass
107else
108t_w t_fail
109put t_fails 0 add get t_fails 0 1
110end
111t_w name
112t_w t_nl
113end
114
115note Expand LF -> CRLF into a local wire buffer and invoke srv_handle_http.
116fn t_handle_lf src view uchar * to ulong
117buf = new uchar 512 0
118n = len src
119w = ulong 0
120for i ulong n bound 512
121b = get src i
122if eq b uchar 10
123if lt add w 1 512
124put buf w uchar 13
125put buf add w 1 uchar 10
126w = add w 2
127end
128else
129if lt w 512
130put buf w b
131w = add w 1
132end
133end
134end
135ret srv_handle_http part buf 0 w
136end
137
138fn t_resp_contains needle view uchar * to bool
139rlen = srv_resp_len
140nlen = len needle
141if or eq nlen 0 gt nlen rlen
142ret false
143end
144lim = add sub rlen nlen 1
145for i ulong lim bound 2048
146ok = true
147for j ulong nlen bound 64
148if ne get srv_resp_buf add i j get needle j
149ok = false
150break
151end
152end
153if ok
154ret true
155end
156end
157ret false
158end
159
160note Test 1: Fail-closed unconfigured auth + constant-time Bearer token checks.
161fn test_auth to bool
162srv_auth_reset
163if srv_auth_is_configured
164ret false
165end
166if srv_auth_verify_header hdr_good
167ret false
168end
169if srv_auth_set_token tok_short
170ret false
171end
172if srv_auth_set_token tok_space
173ret false
174end
175if not srv_auth_set_token tok_good
176ret false
177end
178if not srv_auth_verify_header hdr_good
179ret false
180end
181if srv_auth_verify_header hdr_flip
182ret false
183end
184if srv_auth_verify_header hdr_basic
185ret false
186end
187if srv_auth_verify_header hdr_extra
188ret false
189end
190if or ne srv_auth_allow_count 1 ne srv_auth_deny_count 4
191ret false
192end
193ret true
194end
195
196note Test 2 (NEGATIVE): Read caps, header-too-large, Content-Length mismatch, smuggling, traversal.
197fn test_http_parse_negative to bool
198srv_auth_reset
199ok_cfg = srv_auth_set_token tok_good
200if not ok_cfg
201ret false
202end
203if ne t_handle_lf req_trav 400
204ret false
205end
206if ne t_handle_lf req_bad_method 405
207ret false
208end
209if ne t_handle_lf req_no_term 400
210ret false
211end
212if ne t_handle_lf req_dup_cl 400
213ret false
214end
215if ne t_handle_lf req_dup_auth 400
216ret false
217end
218if ne t_handle_lf req_cl_mismatch 400
219ret false
220end
221if ne t_handle_lf req_cl_huge 413
222ret false
223end
224note Build an oversized header block (> 1024 bytes) -> must return 431
225big_hdr = new uchar 1200 65
226put big_hdr 0 uchar 71
227put big_hdr 1 uchar 69
228put big_hdr 2 uchar 84
229put big_hdr 3 uchar 32
230put big_hdr 4 uchar 47
231put big_hdr 5 uchar 32
232put big_hdr 1196 uchar 13
233put big_hdr 1197 uchar 10
234put big_hdr 1198 uchar 13
235put big_hdr 1199 uchar 10
236if ne srv_handle_http big_hdr 431
237ret false
238end
239ret true
240end
241
242note Test 3: /health public probe, /v1/models bearer-gated, 405 & 404.
243fn test_router_health_models to bool
244srv_auth_reset
245ok_cfg = srv_auth_set_token tok_good
246if not ok_cfg
247ret false
248end
249if ne t_handle_lf req_health_get 200
250ret false
251end
252if not t_resp_contains s_ok_engine
253ret false
254end
255if ne t_handle_lf req_health_post 405
256ret false
257end
258if ne t_handle_lf req_models_noauth 401
259ret false
260end
261if ne t_handle_lf req_models_auth 200
262ret false
263end
264if not t_resp_contains s_model_id
265ret false
266end
267if ne t_handle_lf req_unknown_rt 404
268ret false
269end
270ret true
271end
272
273note Test 4: /v1/chat/completions JSON and SSE streaming paths.
274fn test_router_chat_ok to bool
275srv_auth_reset
276ok_cfg = srv_auth_set_token tok_good
277if not ok_cfg
278ret false
279end
280if ne t_handle_lf req_chat_ok 200
281ret false
282end
283if not t_resp_contains s_chat_obj
284ret false
285end
286if ne get srv_resp_meta 3 64
287ret false
288end
289if ne t_handle_lf req_chat_sse 200
290ret false
291end
292if not t_resp_contains s_sse_done
293ret false
294end
295ret true
296end
297
298note Test 5 (NEGATIVE): /v1/chat/completions bad auth, malformed JSON, missing fields, bad max_tokens, wrong method.
299fn test_router_chat_negative to bool
300srv_auth_reset
301ok_cfg = srv_auth_set_token tok_good
302if not ok_cfg
303ret false
304end
305if ne t_handle_lf req_chat_badtok 401
306ret false
307end
308if ne t_handle_lf req_chat_nomodel 400
309ret false
310end
311if ne t_handle_lf req_chat_nomsgs 400
312ret false
313end
314if ne t_handle_lf req_chat_unbal 400
315ret false
316end
317if ne t_handle_lf req_chat_badmax 400
318ret false
319end
320if ne t_handle_lf req_chat_get 405
321ret false
322end
323note Also verify fail-closed when token is unconfigured
324srv_auth_reset
325if ne t_handle_lf req_chat_ok 401
326ret false
327end
328ret true
329end
330
331note Test 6: CLI dispatch valid commands and fail-closed negative flag checks.
332fn test_cli_dispatch to bool
333if ne cli_dispatch cli_v1 CLI_OK
334ret false
335end
336if or ne cli_cmd CLI_CMD_SERVE or ne cli_port 9090 or ne cli_max_tokens 1024 ne cli_model_len 14
337ret false
338end
339if ne cli_dispatch cli_v2 CLI_OK
340ret false
341end
342if or ne cli_cmd CLI_CMD_CHAT ne cli_max_tokens 256
343ret false
344end
345if ne cli_dispatch cli_v3 CLI_OK
346ret false
347end
348if ne cli_cmd CLI_CMD_MODELS
349ret false
350end
351if ne cli_dispatch cli_v4 CLI_OK
352ret false
353end
354if ne cli_cmd CLI_CMD_HEALTH
355ret false
356end
357if ne cli_dispatch cli_v5 CLI_OK
358ret false
359end
360if ne cli_cmd CLI_CMD_VERSION
361ret false
362end
363note NEGATIVE controls
364if ne cli_dispatch part cli_b1 0 0 CLI_ERR_EMPTY
365ret false
366end
367if ne cli_dispatch cli_b2 CLI_ERR_UNKNOWN_CMD
368ret false
369end
370if ne cli_dispatch cli_b3 CLI_ERR_BAD_PORT
371ret false
372end
373if ne cli_dispatch cli_b4 CLI_ERR_BAD_PORT
374ret false
375end
376if ne cli_dispatch cli_b5 CLI_ERR_BAD_MAX_TOKENS
377ret false
378end
379if ne cli_dispatch cli_b6 CLI_ERR_BAD_MAX_TOKENS
380ret false
381end
382if ne cli_dispatch cli_b7 CLI_ERR_MISSING_MODEL
383ret false
384end
385if ne cli_dispatch cli_b8 CLI_ERR_UNKNOWN_FLAG
386ret false
387end
388if ne cli_dispatch cli_b9 CLI_ERR_DUP_FLAG
389ret false
390end
391ret true
392end
393
394note NEGATIVE: every Transfer-Encoding shape and duplicate/conflicting Content-Length is 400. POSITIVE
395note controls: header names that only contain or extend "transfer-encoding" still route (200), as does plain GET.
396fn test_http_te_cl to bool
397srv_auth_reset
398if not srv_auth_set_token tok_good
399ret false
400end
401ok = true
402if ne t_handle_lf req_te_alone 400
403ok = false
404end
405if ne t_handle_lf req_te_alone_body 400
406ok = false
407end
408if ne t_handle_lf req_te_cl 400
409ok = false
410end
411if ne t_handle_lf req_cl_te 400
412ok = false
413end
414if ne t_handle_lf req_te_case 400
415ok = false
416end
417if ne t_handle_lf req_te_tab 400
418ok = false
419end
420if ne t_handle_lf req_te_empty 400
421ok = false
422end
423if ne t_handle_lf req_te_spcolon 400
424ok = false
425end
426if ne t_handle_lf req_cl_spcolon 400
427ok = false
428end
429if ne t_handle_lf req_dup_cl 400
430ok = false
431end
432if ne t_handle_lf req_cl_conflict 400
433ok = false
434end
435if ne t_handle_lf req_cl_dup_case 400
436ok = false
437end
438if ne t_handle_lf req_cl_list 400
439ok = false
440end
441if ne t_handle_lf req_te_lookalike 200
442ok = false
443end
444if ne t_handle_lf req_health_get 200
445ok = false
446end
447ret ok
448end
449
450fn main to int
451put t_fails 0 0
452t_report t_n1 test_auth
453t_report t_n2 test_http_parse_negative
454t_report t_n3 test_router_health_models
455t_report t_n4 test_router_chat_ok
456t_report t_n5 test_router_chat_negative
457t_report t_n6 test_cli_dispatch
458t_report t_n7 test_http_te_cl
459if eq get t_fails 0 0
460t_w t_all_ok
461ret 0
462end
463t_w t_all_bad
464ret 1
465end