aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tests / test_agent_cmd.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* test_agent_cmd.c -- core/agent_cmd.c: the card command script of the Pi resident-lifecycle qualification |
| 18 | * image (CK_PI5_LIFECYCLE). A script is a labelled text file of a few lifecycle verbs; the parser is strict |
| 19 | * (header, bytes, line count, verb whitelist, text bounds) and can never let a secret-carrying or |
| 20 | * state-changing control verb (unlock, rotate, update, shutdown) through. */ |
| 21 | #include <string.h> |
| 22 | #include "ck_test.h" |
| 23 | #include "agent_cmd.h" |
| 24 | |
| 25 | #define H CK_AGENT_CMD_HEADER "\n" |
| 26 | |
| 27 | static int parse(const char *s, struct ck_agent_cmd *o, const char **why) |
| 28 | { |
| 29 | return ck_agent_cmd_parse((const uint8_t *)s, strlen(s), o, why); |
| 30 | } |
| 31 | |
| 32 | static int refused(const char *s, const char *want) |
| 33 | { |
| 34 | struct ck_agent_cmd o; |
| 35 | const char *why = 0; |
| 36 | int rc = parse(s, &o, &why); |
| 37 | /* a refused script leaves nothing behind */ |
| 38 | int clean = o.nlines == 0 && o.line[0][0] == 0; |
| 39 | return rc != 0 && why && strstr(why, want) != 0 && clean; |
| 40 | } |
| 41 | |
| 42 | int main(void) |
| 43 | { |
| 44 | struct ck_agent_cmd o; |
| 45 | const char *why = (const char *)1; |
| 46 | CHECK(strlen(CK_AGENT_CMD_HEADER) == 25); |
| 47 | |
| 48 | /* the L1 script of the qualification run */ |
| 49 | CHECK(parse(H "prefer short notes\nstatus\nrecall\nprompt What is the capital of France?\napprove\n", &o, &why) == 0 && why == 0); |
| 50 | CHECK(o.nlines == 5 && o.len[0] == 18 && strcmp(o.line[0], "prefer short notes") == 0); |
| 51 | CHECK(strcmp(o.line[3], "prompt What is the capital of France?") == 0 && strcmp(o.line[4], "approve") == 0); |
| 52 | CHECK(parse(H "recall\nfacts\nevidence\nhelp\ndeny\ncancel\nremember a b c\ntask do it\n", &o, 0) == 0 && o.nlines == 8); |
| 53 | CHECK(ck_agent_cmd_parse(0, 0, &o, &why) != 0 && ck_agent_cmd_parse((const uint8_t *)"x", 1, 0, &why) != 0); |
| 54 | |
| 55 | /* header */ |
| 56 | CHECK(refused("", "size")); |
| 57 | CHECK(refused("status\n", "header")); |
| 58 | CHECK(refused("AIENAGC1 QUALIF-AGENT-CMD", "line feed")); |
| 59 | CHECK(refused("AIENAGC2 QUALIF-AGENT-CMD\nstatus\n", "header")); |
| 60 | CHECK(refused("AIENAGC1 QUALIF-AGENT-CMD \nstatus\n", "header")); |
| 61 | CHECK(refused(H, "no command line")); |
| 62 | |
| 63 | /* the control verbs can never ride in a card script */ |
| 64 | CHECK(refused(H "unlock\n", "not allowed")); |
| 65 | CHECK(refused(H "unlock 0000000000000000000000000000000000000000000000000000000000000000\n", "not allowed")); |
| 66 | CHECK(refused(H "rotate a b\n", "not allowed")); |
| 67 | CHECK(refused(H "update\n", "not allowed")); |
| 68 | CHECK(refused(H "shutdown\n", "not allowed")); |
| 69 | CHECK(refused(H "status\nunlock\n", "not allowed")); |
| 70 | CHECK(refused(H "Status\n", "not allowed")); |
| 71 | CHECK(refused(H "statuses\n", "not allowed")); |
| 72 | CHECK(refused(H "stat\n", "not allowed")); |
| 73 | CHECK(refused(H "frob\n", "not allowed")); |
| 74 | |
| 75 | /* shape of a line */ |
| 76 | CHECK(refused(H "status now\n", "no text")); |
| 77 | CHECK(refused(H "approve please\n", "no text")); |
| 78 | CHECK(refused(H "prefer\n", "text must be")); |
| 79 | CHECK(refused(H "prefer two blanks\n", "text must be")); |
| 80 | CHECK(refused(H " status\n", "blank")); |
| 81 | CHECK(refused(H "status \n", "blank")); |
| 82 | CHECK(refused(H "prefer short \n", "blank")); |
| 83 | CHECK(refused(H "status\n\nrecall\n", "empty line")); |
| 84 | CHECK(refused(H "prefer 0123456789012345678901234567890123456789012345678\n", "text must be")); /* 49 */ |
| 85 | CHECK(parse(H "prefer 012345678901234567890123456789012345678901234567\n", &o, &why) == 0 && o.len[0] == 7 + 48); /* 48 */ |
| 86 | |
| 87 | /* bytes */ |
| 88 | CHECK(refused(H "status\r\n", "printable")); |
| 89 | CHECK(refused(H "pre\tfer x\n", "printable")); |
| 90 | CHECK(refused("AIENAGC1 QUALIF-AGENT-CMD\nprefer caf\xc3\xa9\n", "printable")); |
| 91 | { |
| 92 | char z[64]; |
| 93 | strcpy(z, H "status\n"); |
| 94 | z[strlen(H) + 2] = 0; |
| 95 | CHECK(ck_agent_cmd_parse((const uint8_t *)z, strlen(H) + 7, &o, &why) != 0 && o.nlines == 0); |
| 96 | } |
| 97 | |
| 98 | /* count and size bounds */ |
| 99 | { |
| 100 | char big[CK_AGENT_CMD_FILE_MAX + 64]; |
| 101 | strcpy(big, H); |
| 102 | for (int i = 0; i < 16; i++) strcat(big, "status\n"); |
| 103 | CHECK(parse(big, &o, &why) == 0 && o.nlines == 16); |
| 104 | strcat(big, "status\n"); |
| 105 | CHECK(refused(big, "more than 16")); |
| 106 | memset(big, 'a', sizeof big); |
| 107 | CHECK(ck_agent_cmd_parse((const uint8_t *)big, CK_AGENT_CMD_FILE_MAX + 1, &o, &why) != 0 && strstr(why, "size")); |
| 108 | } |
| 109 | return ck_t_verdict("test_agent_cmd"); |
| 110 | } |