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
omega-mv / tests / osh / e2e / test_osh_e2e.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 | /* |
| 18 | * test_osh_e2e.c -- end-to-end tests for the osh program (aien-architecture#158 cut 4c). |
| 19 | * Runs build/osh/osh as a child process in a mkdtemp fixture, with argprint as the test program, and compares |
| 20 | * standard output, standard error and exit status. Every case runs twice, native and OSH_INTERP=1, and the two |
| 21 | * runs must be identical in all three. No shell is involved: osh is started with execve() through fork. |
| 22 | * Usage: test_osh_e2e OSH ARGPRINT |
| 23 | * "@A" in a case is replaced by the argprint path and "@T" by the fixture directory. Final line OSH_E2E_PASS/FAIL. |
| 24 | */ |
| 25 | #include <fcntl.h> |
| 26 | #include <poll.h> |
| 27 | #include <signal.h> |
| 28 | #include <stdarg.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | static const char *OSH, *ARG, *TMP; |
| 37 | /* osh is fail closed without --caps, so every case runs with this explicit policy (all four effects under /): these |
| 38 | * cases test the language and the execution service; enforcement itself is test_osh_caps.c. */ |
| 39 | static char POL[4096]; |
| 40 | static const char E2E_POLICY[] = "principal 1\nallow spawn /\nallow read /\nallow write /\nallow chdir /\n"; |
| 41 | static unsigned long checks, fails; |
| 42 | |
| 43 | static void check(int ok, const char *fmt, ...) |
| 44 | { |
| 45 | checks++; |
| 46 | if (ok) return; |
| 47 | fails++; |
| 48 | va_list ap; |
| 49 | va_start(ap, fmt); |
| 50 | fprintf(stderr, "FAIL: "); |
| 51 | vfprintf(stderr, fmt, ap); |
| 52 | fprintf(stderr, "\n"); |
| 53 | va_end(ap); |
| 54 | } |
| 55 | |
| 56 | static char *subst(const char *s) |
| 57 | { |
| 58 | size_t cap = strlen(s) * 3 + strlen(ARG) * 8 + strlen(TMP) * 8 + 64, o = 0; |
| 59 | char *r = malloc(cap + 1); |
| 60 | for (; *s; s++) { |
| 61 | if (s[0] == '@' && s[1] == 'A') { o += (size_t)sprintf(r + o, "%s", ARG); s++; } |
| 62 | else if (s[0] == '@' && s[1] == 'T') { o += (size_t)sprintf(r + o, "%s", TMP); s++; } |
| 63 | else r[o++] = *s; |
| 64 | if (o + 4096 > cap) { cap *= 2; r = realloc(r, cap + 1); } |
| 65 | } |
| 66 | r[o] = 0; |
| 67 | return r; |
| 68 | } |
| 69 | |
| 70 | static char *slurp(const char *path) |
| 71 | { |
| 72 | FILE *f = fopen(path, "rb"); |
| 73 | char *b = calloc(1, 1 << 16); |
| 74 | if (f) { |
| 75 | size_t n = fread(b, 1, (1 << 16) - 1, f); |
| 76 | b[n] = 0; |
| 77 | fclose(f); |
| 78 | } |
| 79 | return b; |
| 80 | } |
| 81 | |
| 82 | typedef struct { |
| 83 | char *out, *err; |
| 84 | int status; |
| 85 | } Run; |
| 86 | |
| 87 | /* mode 0: osh -c TEXT [args]; 1: osh SCRIPT [args] (TEXT written to the file); 2: osh with TEXT on standard input; |
| 88 | * 3: osh [args] as given (TEXT unused) */ |
| 89 | static Run run_osh(int mode, const char *text, char *const *args, int interp) |
| 90 | { |
| 91 | char outp[512], errp[512], inp[512], scr[512]; |
| 92 | snprintf(outp, sizeof outp, "%s/.out", TMP); |
| 93 | snprintf(errp, sizeof errp, "%s/.err", TMP); |
| 94 | snprintf(inp, sizeof inp, "%s/.in", TMP); |
| 95 | snprintf(scr, sizeof scr, "%s/script.osh", TMP); |
| 96 | if (mode >= 1) { |
| 97 | const char *dst = mode == 1 ? scr : inp; |
| 98 | FILE *f = fopen(dst, "wb"); |
| 99 | fwrite(text, 1, strlen(text), f); |
| 100 | fclose(f); |
| 101 | } |
| 102 | char *av[32]; |
| 103 | int n = 0; |
| 104 | av[n++] = (char *)OSH; |
| 105 | av[n++] = "--caps"; |
| 106 | av[n++] = POL; |
| 107 | if (mode == 0) { av[n++] = "-c"; av[n++] = (char *)text; } |
| 108 | if (mode == 1) av[n++] = scr; |
| 109 | for (; args && *args; args++) av[n++] = *args; |
| 110 | av[n] = NULL; |
| 111 | pid_t pid = fork(); |
| 112 | if (pid == 0) { |
| 113 | int fo = open(outp, O_WRONLY | O_CREAT | O_TRUNC, 0644), fe = open(errp, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 114 | int fi = open(mode == 2 ? inp : "/dev/null", O_RDONLY); |
| 115 | dup2(fi, 0); dup2(fo, 1); dup2(fe, 2); |
| 116 | close(fi); close(fo); close(fe); |
| 117 | if (chdir(TMP) != 0) _exit(126); |
| 118 | if (interp) setenv("OSH_INTERP", "1", 1); |
| 119 | else unsetenv("OSH_INTERP"); |
| 120 | execv(OSH, av); |
| 121 | _exit(127); |
| 122 | } |
| 123 | int st = 0; |
| 124 | waitpid(pid, &st, 0); |
| 125 | Run r; |
| 126 | r.out = slurp(outp); |
| 127 | r.err = slurp(errp); |
| 128 | r.status = WIFEXITED(st) ? WEXITSTATUS(st) : 128 + WTERMSIG(st); |
| 129 | return r; |
| 130 | } |
| 131 | |
| 132 | typedef struct { |
| 133 | const char *name; |
| 134 | int mode; |
| 135 | const char *text; |
| 136 | char *const *args; |
| 137 | const char *out; /* exact standard output */ |
| 138 | const char *err; /* NULL: standard error must be empty; else it must contain this text */ |
| 139 | int status; |
| 140 | } Case; |
| 141 | |
| 142 | static char *const A_NAME_AB[] = {"name", "a", "b", NULL}; |
| 143 | static char *const A_ONETWO[] = {"zero", "one", "two words", NULL}; |
| 144 | |
| 145 | static const Case CASES[] = { |
| 146 | {"plain words", 0, "@A a b c", NULL, "[a]\n[b]\n[c]\n", NULL, 0}, |
| 147 | {"quoting and empty args", 0, "@A a \"b c\" 'd e' \"\" '' x\\ y \"q'r\" 's\"t'", NULL, "[a]\n[b c]\n[d e]\n[]\n[]\n[x y]\n[q'r]\n[s\"t]\n", NULL, 0}, |
| 148 | {"backslashes", 0, "@A \\$X \"\\$X\" \"a\\\\b\" 'a\\b' \"a\\b\"", NULL, "[$X]\n[$X]\n[a\\b]\n[a\\b]\n[a\\b]\n", NULL, 0}, |
| 149 | {"variable splits unquoted", 0, "X='1 2'; @A $X \"$X\" a$X b${X}c", NULL, "[1]\n[2]\n[1 2]\n[a1]\n[2]\n[b1]\n[2c]\n", NULL, 0}, |
| 150 | {"empty and unset expansions", 0, "E=; @A $E \"$E\" $U \"$U\" '' x", NULL, "[]\n[]\n[]\n[x]\n", NULL, 0}, |
| 151 | {"empty beside text", 0, "E=; @A a${E}b \"$E$E\"", NULL, "[ab]\n[]\n", NULL, 0}, |
| 152 | {"assignment is not split", 0, "X='a b'; Y=$X; @A \"$Y\"", NULL, "[a b]\n", NULL, 0}, |
| 153 | {"later assignments see earlier ones", 0, "A=1 B=$A$A; @A $A $B", NULL, "[1]\n[11]\n", NULL, 0}, |
| 154 | {"assignment stays inside the shell", 0, "Z=1; @A -e Z", NULL, "[(unset)]\n", NULL, 0}, |
| 155 | {"export reaches children", 0, "Z=1; export Z; @A -e Z; export Y=two; @A -e Y", NULL, "[1]\n[two]\n", NULL, 0}, |
| 156 | {"prefix assignment is for one command", 0, "W=5 @A -e W; @A -e W", NULL, "[5]\n[(unset)]\n", NULL, 0}, |
| 157 | {"prefix assignment sees old value", 0, "A=old; A=new B=$A @A -e A; @A -e B", NULL, "[new]\n[(unset)]\n", NULL, 0}, |
| 158 | {"unset", 0, "export Q=1; @A -e Q; unset Q; @A -e Q", NULL, "[1]\n[(unset)]\n", NULL, 0}, |
| 159 | {"environment comes in", 0, "@A -e OSH_E2E_PROBE", NULL, "[(unset)]\n", NULL, 0}, |
| 160 | {"semicolon and and-or", 0, "@A a; @A b && @A c || @A d", NULL, "[a]\n[b]\n[c]\n", NULL, 0}, |
| 161 | {"or runs on failure", 0, "@A -x 1 || @A yes; @A -x 0 || @A no; @A -x 2 && @A no2", NULL, "[yes]\n", NULL, 2}, |
| 162 | {"and skips on failure, or resumes", 0, "@A -x 1 && @A no || @A yes", NULL, "[yes]\n", NULL, 0}, |
| 163 | {"exit status of last command", 0, "@A -x 7", NULL, "", NULL, 7}, |
| 164 | {"dollar question", 0, "@A -x 3; printf '%s\\n' $?; @A -x 0; printf '%s\\n' $?", NULL, "3\n0\n", NULL, 0}, |
| 165 | {"pipeline", 0, "@A a b | @A -cat", NULL, "[a]\n[b]\n", NULL, 0}, |
| 166 | {"three stage pipeline", 0, "@A x | @A -cat | @A -cat", NULL, "[x]\n", NULL, 0}, |
| 167 | {"pipeline status is the last command", 0, "@A -x 4 | @A -cat; printf '%s\\n' $?; @A a | @A -x 5", NULL, "0\n", NULL, 5}, |
| 168 | {"redirect out and in", 0, "@A hi > @T/o1; @A -cat < @T/o1; @A -cat <@T/o1", NULL, "[hi]\n[hi]\n", NULL, 0}, |
| 169 | {"redirect truncates", 0, "@A long line > @T/o2; @A s > @T/o2; @A -cat < @T/o2", NULL, "[s]\n", NULL, 0}, |
| 170 | {"redirect append", 0, "@A 1 > @T/o3; @A 2 >> @T/o3; @A -cat < @T/o3", NULL, "[1]\n[2]\n", NULL, 0}, |
| 171 | {"stderr to file", 0, "@A -io 2> @T/e1; @A -cat < @T/e1", NULL, "OE", NULL, 0}, |
| 172 | {"stderr appended", 0, "@A -io 2> @T/e2; @A -io 2>> @T/e2; @A -cat < @T/e2", NULL, "OOEE", NULL, 0}, |
| 173 | {"2>&1 into a pipe", 0, "@A -io 2>&1 | @A -cat", NULL, "OE", NULL, 0}, |
| 174 | {"2>&1 then file: stderr follows the old stdout", 0, "@A -io 2>&1 > @T/f1; @A -cat < @T/f1", NULL, "EO", NULL, 0}, |
| 175 | {"file then 2>&1: both in the file", 0, "@A -io > @T/f2 2>&1; @A -cat < @T/f2", NULL, "OE", NULL, 0}, |
| 176 | {"1>&2 sends output to stderr", 0, "@A a 1>&2", NULL, "", "[a]", 0}, |
| 177 | {"explicit descriptors", 0, "@A -io 1> @T/f3 2> @T/f4; @A -cat 0< @T/f3; @A -cat 0<@T/f4", NULL, "OE", NULL, 0}, |
| 178 | {"redirect target expands", 0, "F=@T/f5; @A v > \"$F\"; @A -cat < $F", NULL, "[v]\n", NULL, 0}, |
| 179 | {"redirect in a pipeline stage", 0, "@A a b > @T/f6 | @A -cat; @A -cat < @T/f6", NULL, "[a]\n[b]\n", NULL, 0}, |
| 180 | {"missing input file", 0, "@A -cat < @T/nonexistent; printf '%s\\n' $?", NULL, "1\n", "nonexistent", 0}, |
| 181 | {"cd and pwd", 0, "cd @T; pwd; cd /; pwd; cd @T; @A -pwd", NULL, "@T\n/\n[@T]\n", NULL, 0}, |
| 182 | {"cd failure", 0, "cd @T/no/such; printf '%s\\n' $?", NULL, "1\n", "no", 0}, |
| 183 | {"printf", 0, "printf 'a%sb\\n' x; printf '%s|%s\\n' 1 2; printf '%d\\n' 42", NULL, "axb\n1|2\n42\n", NULL, 0}, |
| 184 | {"printf with expansions", 0, "X='a b'; printf '[%s]\\n' $X \"$X\"", NULL, "[a]\n[b]\n[a b]\n", NULL, 0}, |
| 185 | {"builtin redirect", 0, "printf hello > @T/f7; @A -cat < @T/f7", NULL, "hello", NULL, 0}, |
| 186 | {"exit status", 0, "exit 7", NULL, "", NULL, 7}, |
| 187 | {"exit stops the list", 0, "@A a; exit 3; @A b", NULL, "[a]\n", NULL, 3}, |
| 188 | {"exit without argument uses last status", 0, "@A -x 9; exit", NULL, "", NULL, 9}, |
| 189 | {"positional parameters", 0, "@A \"$0\" $1 \"$2\" \"$#\" $3 \"$@\"", A_NAME_AB, "[name]\n[a]\n[b]\n[2]\n[a]\n[b]\n", NULL, 0}, |
| 190 | {"quoted at keeps words, star joins", 0, "@A \"$@\"; @A \"$*\"; @A $*", A_ONETWO, "[one]\n[two words]\n[one two words]\n[one]\n[two]\n[words]\n", NULL, 0}, |
| 191 | {"no positionals", 0, "@A x \"$@\" y $@ \"$#\" \"$1\"", NULL, "[x]\n[y]\n[0]\n[]\n", NULL, 0}, |
| 192 | {"default arg0", 0, "@A \"$0\"", NULL, "[osh]\n", NULL, 0}, |
| 193 | {"comments and blank lines", 1, "# comment\n\n@A a # not b\n \n@A b\n", NULL, "[a]\n[b]\n", NULL, 0}, |
| 194 | {"script with arguments", 1, "@A \"$0\" \"$1\" $#\n", A_NAME_AB, "[@T/script.osh]\n[name]\n[3]\n", NULL, 0}, |
| 195 | {"continuation lines", 1, "@A a \\\nb \\\n c\n", NULL, "[a]\n[b]\n[c]\n", NULL, 0}, |
| 196 | {"-c: trailing backslash is literal (bash 5.2)", 0, "@A a\\", NULL, "[a\\]\n", NULL, 0}, |
| 197 | {"-c: a list ending in ; at end of input runs", 0, "@A a;", NULL, "[a]\n", NULL, 0}, |
| 198 | {"-c: two lists, the last ending in ;", 0, "@A a; @A b; ", NULL, "[a]\n[b]\n", NULL, 0}, |
| 199 | {"script: trailing backslash with no newline joins nothing", 1, "@A a\\", NULL, "[a]\n", NULL, 0}, |
| 200 | {"stdin: trailing backslash with no newline joins nothing", 2, "@A a\\", NULL, "[a]\n", NULL, 0}, |
| 201 | {"script: last line ends in ; with no newline", 1, "@A a\n@A b;", NULL, "[a]\n[b]\n", NULL, 0}, |
| 202 | {"printf option is refused and prints nothing", 0, "printf -v x '%s' hi; @A \"$x\" $?", NULL, "[]\n[2]\n", "option not supported", 0}, |
| 203 | {"exit with two arguments drops the rest of the list (bash)", 1, "exit 1 2; @A same\n@A next $?\n", NULL, "[next]\n[1]\n", "too many arguments", 0}, |
| 204 | {"export NAME=$v is not split", 0, "Y='a b'; export A=$Y; @A -e A", NULL, "[a b]\n", NULL, 0}, |
| 205 | {"quoted empty var then $@ with no arguments gives no argument", 0, "E=; @A x \"$E$@\" y", NULL, "[x]\n[y]\n", NULL, 0}, |
| 206 | {"colon is refused before anything runs", 0, "@A a; : ; @A b", NULL, "[a]\n", "UNSUPPORTED_BUILTIN", 2}, |
| 207 | {"quote across lines", 1, "@A \"a\nb\" 'c\nd'\n", NULL, "[a\nb]\n[c\nd]\n", NULL, 0}, |
| 208 | {"and-or across lines", 1, "@A a &&\n@A b ||\n@A c\n@A -x 1 |\n@A -cat\n", NULL, "[a]\n[b]\n", NULL, 0}, |
| 209 | {"missing newline at end", 1, "@A a\n@A b", NULL, "[a]\n[b]\n", NULL, 0}, |
| 210 | {"state carries between lines", 1, "X=1\nexport X\n@A -e X\ncd @T\n@A -pwd\n", NULL, "[1]\n[@T]\n", NULL, 0}, |
| 211 | {"dollar question carries between lines", 1, "@A -x 6\nprintf '%s\\n' $?\n", NULL, "6\n", NULL, 0}, |
| 212 | {"exit in a script", 1, "@A a\nexit 4\n@A b\n", NULL, "[a]\n", NULL, 4}, |
| 213 | {"script from standard input", 2, "@A a\nX=7\nprintf '%s\\n' $X\n", NULL, "[a]\n7\n", NULL, 0}, |
| 214 | {"standard input leaves the rest to children", 2, "@A -cat\nsecond line\n@A z\n", NULL, "second line\n@A z\n", NULL, 0}, |
| 215 | {"standard input, last status at end", 2, "@A -x 5\n", NULL, "", NULL, 5}, |
| 216 | {"standard input, exit", 2, "exit 8\n@A no\n", NULL, "", NULL, 8}, |
| 217 | {"missing command", 0, "@T/no-such-program; printf '%s\\n' $?", NULL, "127\n", "no-such-program", 0}, |
| 218 | {"not executable", 0, "@T; printf '%s\\n' $?", NULL, "126\n", "Is a directory", 0}, |
| 219 | /* syntax errors: the shell says what and where, exits 2 */ |
| 220 | {"unterminated quote", 0, "@A \"abc", NULL, "", "syntax error: SYNTAX_EOF", 2}, |
| 221 | {"dangling operator", 0, "@A a &&", NULL, "", "syntax error: SYNTAX_EOF", 2}, |
| 222 | {"empty command", 0, "@A a ; ; @A b", NULL, "", "syntax error: SYNTAX_EMPTY_CMD", 2}, |
| 223 | {"missing redirect target", 0, "@A a >", NULL, "", "syntax error", 2}, |
| 224 | {"syntax error stops a script", 1, "@A a\n@A b &&& @A c\n@A d\n", NULL, "[a]\n", "syntax error", 2}, |
| 225 | {"error shows the byte offset", 0, "echo ok > @T/x; $(true)", NULL, "", "at byte", 2}, |
| 226 | /* refused constructs, each with its named code */ |
| 227 | {"refused: glob", 0, "@A *.c", NULL, "", "syntax error: GLOB at byte", 2}, |
| 228 | {"refused: backtick", 0, "@A `x`", NULL, "", "syntax error: BACKTICK at byte", 2}, |
| 229 | {"refused: command substitution", 0, "@A $(x)", NULL, "", "syntax error: CMDSUB at byte", 2}, |
| 230 | {"refused: tilde", 0, "@A ~", NULL, "", "syntax error: TILDE at byte", 2}, |
| 231 | {"refused: parameter operator", 0, "@A ${X:-y}", NULL, "", "syntax error: PARAM_OP at byte", 2}, |
| 232 | {"refused: special parameter", 0, "@A $$", NULL, "", "syntax error: SPECIAL_PARAM at byte", 2}, |
| 233 | {"refused: background", 0, "@A a & @A b", NULL, "", "syntax error: BACKGROUND at byte", 2}, |
| 234 | {"refused: subshell", 0, "(@A a)", NULL, "", "syntax error: SUBSHELL at byte", 2}, |
| 235 | {"refused: here document", 0, "@A -cat << EOF", NULL, "", "syntax error: HEREDOC at byte", 2}, |
| 236 | {"refused: other redirection", 0, "@A a <> f", NULL, "", "syntax error: REDIR_OTHER at byte", 2}, |
| 237 | {"refused: if", 0, "if true", NULL, "", "syntax error: IF_COMPOUND at byte", 2}, |
| 238 | {"refused: loop", 0, "while true", NULL, "", "syntax error: LOOP at byte", 2}, |
| 239 | {"refused: case", 0, "case x", NULL, "", "syntax error: CASE at byte", 2}, |
| 240 | {"refused: group", 0, "{ @A a; }", NULL, "", "syntax error: GROUP at byte", 2}, |
| 241 | {"refused: function", 0, "function f", NULL, "", "syntax error: FUNCTION at byte", 2}, |
| 242 | {"refused: negation", 0, "! @A a", NULL, "", "syntax error: NEGATION at byte", 2}, |
| 243 | {"refused: brace expansion", 0, "@A {a,b}", NULL, "", "syntax error: BRACE_EXPANSION at byte", 2}, |
| 244 | {"refused: positional range", 0, "@A ${10}", NULL, "", "syntax error: POSITIONAL_RANGE at byte", 2}, |
| 245 | {"refused: IFS assignment", 0, "IFS=x", NULL, "", "syntax error: IFS_ASSIGN at byte", 2}, |
| 246 | {"refused: set", 0, "set -e", NULL, "", "refused: UNSUPPORTED_BUILTIN at byte", 2}, |
| 247 | {"refused: eval through a variable", 0, "B=eval; $B x", NULL, "", "refused: UNSUPPORTED_BUILTIN at byte", 2}, |
| 248 | {"refused: glob in a value", 0, "G='a*'; @A $G", NULL, "", "refused: VALUE_GLOB at byte", 2}, |
| 249 | {"quoted glob value is fine", 0, "G='a*'; @A \"$G\" '*' \"?\"", NULL, "[a*]\n[*]\n[?]\n", NULL, 0}, |
| 250 | {"refused: ambiguous redirect", 0, "F='a b'; @A x > $F", NULL, "", "refused: AMBIGUOUS_REDIRECT at byte", 2}, |
| 251 | {"refusal after earlier output keeps it", 1, "@A a\nG='*'; @A $G\n@A b\n", NULL, "[a]\n", "refused: VALUE_GLOB", 2}, |
| 252 | }; |
| 253 | |
| 254 | /* Read the pseudo-terminal into buf until it holds want, for up to secs seconds. 1 found, 0 not. */ |
| 255 | static int pty_wait(int fd, char *buf, size_t cap, size_t *n, const char *want, int secs) |
| 256 | { |
| 257 | for (int i = 0; i < secs * 10; i++) { |
| 258 | buf[*n] = 0; |
| 259 | if (strstr(buf, want)) return 1; |
| 260 | struct pollfd p = {fd, POLLIN, 0}; |
| 261 | if (poll(&p, 1, 100) <= 0) continue; |
| 262 | ssize_t r = read(fd, buf + *n, cap - 1 - *n); |
| 263 | if (r <= 0) break; |
| 264 | *n += (size_t)r; |
| 265 | } |
| 266 | buf[*n] = 0; |
| 267 | return strstr(buf, want) != NULL; |
| 268 | } |
| 269 | |
| 270 | /* ^C typed at an idle interactive prompt (review of #340, BUG-5): the shell drops the half-typed line, prompts again, |
| 271 | * keeps running and sets $? to 130, as bash does. Before the fix the shell died of SIGINT. Needs a pseudo-terminal. */ |
| 272 | /* `osh SCRIPT` with a script that is a directory, missing or unreadable: bash 5.2 exits 126, 127 and 126 with the |
| 273 | * system's message (#344 NIT "osh DIR 127 vs 126"). */ |
| 274 | static void test_script_status(void) |
| 275 | { |
| 276 | char miss[600], unread[600]; |
| 277 | snprintf(miss, sizeof miss, "%s/no-such-script.osh", TMP); |
| 278 | snprintf(unread, sizeof unread, "%s/unreadable.osh", TMP); |
| 279 | FILE *f = fopen(unread, "wb"); |
| 280 | if (f) { fputs("exit 0\n", f); fclose(f); } |
| 281 | chmod(unread, 0); |
| 282 | const struct { const char *path, *msg; int status; } rows[] = { |
| 283 | {TMP, "Is a directory", 126}, {miss, "No such file or directory", 127}, {unread, "Permission denied", 126}}; |
| 284 | for (size_t i = 0; i < sizeof rows / sizeof rows[0]; i++) { |
| 285 | if (i == 2 && geteuid() == 0) continue; /* root can read anything */ |
| 286 | char *const args[] = {(char *)rows[i].path, NULL}; |
| 287 | Run r = run_osh(3, "", args, 0); |
| 288 | check(r.status == rows[i].status && strstr(r.err, rows[i].msg) != NULL, "osh %s: status %d (want %d), stderr '%s' (want '%s')", |
| 289 | rows[i].path, r.status, rows[i].status, r.err, rows[i].msg); |
| 290 | free(r.out); |
| 291 | free(r.err); |
| 292 | } |
| 293 | chmod(unread, 0644); |
| 294 | } |
| 295 | |
| 296 | static void test_prompt_ctrl_c(void) |
| 297 | { |
| 298 | int master = posix_openpt(O_RDWR | O_NOCTTY); |
| 299 | if (master < 0 || grantpt(master) || unlockpt(master) || !ptsname(master)) { |
| 300 | printf("note: no pseudo-terminal available, prompt ^C test skipped\n"); |
| 301 | return; |
| 302 | } |
| 303 | char sname[256]; |
| 304 | snprintf(sname, sizeof sname, "%s", ptsname(master)); |
| 305 | pid_t pid = fork(); |
| 306 | if (pid == 0) { |
| 307 | setsid(); |
| 308 | int s = open(sname, O_RDWR); /* the new session leader acquires the terminal */ |
| 309 | if (s < 0) _exit(126); |
| 310 | dup2(s, 0); dup2(s, 1); dup2(s, 2); |
| 311 | if (s > 2) close(s); |
| 312 | close(master); |
| 313 | if (chdir(TMP) != 0) _exit(126); |
| 314 | unsetenv("OSH_INTERP"); |
| 315 | char *av[] = {(char *)OSH, "--caps", POL, NULL}; /* fail closed without a policy, as in run_osh() */ |
| 316 | execv(OSH, av); |
| 317 | _exit(127); |
| 318 | } |
| 319 | char buf[16384], line[1200]; |
| 320 | size_t n = 0; |
| 321 | int ok = pty_wait(master, buf, sizeof buf, &n, "osh$ ", 5); |
| 322 | check(ok, "prompt ^C: first prompt"); |
| 323 | snprintf(line, sizeof line, "%s half", ARG); |
| 324 | if (write(master, line, strlen(line)) < 0) {} |
| 325 | usleep(200000); |
| 326 | size_t mark = n; |
| 327 | if (write(master, "\003", 1) < 0) {} |
| 328 | ok = pty_wait(master, buf + mark, sizeof buf - mark, &(size_t){0}, "osh$ ", 5); |
| 329 | n = strlen(buf); |
| 330 | check(ok, "prompt ^C: a new prompt after ^C"); |
| 331 | snprintf(line, sizeof line, "%s \"st=$?\"\n", ARG); |
| 332 | if (write(master, line, strlen(line)) < 0) {} |
| 333 | ok = pty_wait(master, buf, sizeof buf, &n, "[st=130]", 5); |
| 334 | check(ok, "prompt ^C: the shell still runs commands and $? is 130"); |
| 335 | check(strstr(buf, "[half]") == NULL, "prompt ^C: the abandoned line did not run"); |
| 336 | if (write(master, "exit 0\n", 7) < 0) {} |
| 337 | int st = -1; |
| 338 | for (int i = 0; i < 50 && waitpid(pid, &st, WNOHANG) == 0; i++) usleep(100000); |
| 339 | if (st == -1) { kill(pid, SIGKILL); waitpid(pid, &st, 0); } |
| 340 | check(WIFEXITED(st) && WEXITSTATUS(st) == 0, "prompt ^C: shell exits 0 on exit 0 (raw status %d)", st); |
| 341 | if (fails) fprintf(stderr, " prompt ^C transcript:\n%s\n", buf); |
| 342 | close(master); |
| 343 | } |
| 344 | |
| 345 | int main(int argc, char **argv) |
| 346 | { |
| 347 | if (argc < 3) { fprintf(stderr, "usage: %s OSH ARGPRINT\n", argv[0]); return 2; } |
| 348 | char tmpl[] = "/tmp/osh-e2e-XXXXXX"; |
| 349 | if (!mkdtemp(tmpl)) { perror("mkdtemp"); return 2; } |
| 350 | OSH = realpath(argv[1], NULL); |
| 351 | ARG = realpath(argv[2], NULL); |
| 352 | TMP = realpath(tmpl, NULL); |
| 353 | if (!OSH || !ARG || !TMP) { fprintf(stderr, "bad path\n"); return 2; } |
| 354 | snprintf(POL, sizeof POL, "%s.caps", TMP); |
| 355 | FILE *pf = fopen(POL, "w"); |
| 356 | if (!pf || fputs(E2E_POLICY, pf) == EOF || fclose(pf) != 0) { fprintf(stderr, "cannot write %s\n", POL); return 2; } |
| 357 | unsigned long ncases = 0; |
| 358 | for (size_t i = 0; i < sizeof CASES / sizeof CASES[0]; i++) { |
| 359 | const Case *c = &CASES[i]; |
| 360 | char *text = subst(c->text); |
| 361 | char *want_out = subst(c->out); |
| 362 | char *args[8]; |
| 363 | int na = 0; |
| 364 | if (c->args) |
| 365 | for (char *const *a = c->args; *a; a++) args[na++] = *a; |
| 366 | args[na] = NULL; |
| 367 | Run r[2]; |
| 368 | for (int ip = 0; ip < 2; ip++) { |
| 369 | /* a fresh fixture state for each run */ |
| 370 | char cmd[600]; |
| 371 | (void)cmd; |
| 372 | r[ip] = run_osh(c->mode, text, args, ip); |
| 373 | int ok = strcmp(r[ip].out, want_out) == 0 && r[ip].status == c->status; |
| 374 | if (c->err) ok = ok && strstr(r[ip].err, c->err) != NULL; |
| 375 | else ok = ok && r[ip].err[0] == 0; |
| 376 | if (!ok) |
| 377 | fprintf(stderr, " case [%s] %s:\n want status %d out \"%s\" err %s%s\n got status %d out \"%s\" err \"%s\"\n", c->name, ip ? "interp" : "native", c->status, |
| 378 | want_out, c->err ? "containing " : "empty", c->err ? c->err : "", r[ip].status, r[ip].out, r[ip].err); |
| 379 | check(ok, "case [%s] %s", c->name, ip ? "OSH_INTERP=1" : "native"); |
| 380 | } |
| 381 | check(strcmp(r[0].out, r[1].out) == 0 && strcmp(r[0].err, r[1].err) == 0 && r[0].status == r[1].status, "case [%s]: native and OSH_INTERP=1 differ", c->name); |
| 382 | ncases++; |
| 383 | free(text); |
| 384 | free(want_out); |
| 385 | free(r[0].out); free(r[0].err); free(r[1].out); free(r[1].err); |
| 386 | } |
| 387 | test_script_status(); |
| 388 | test_prompt_ctrl_c(); |
| 389 | printf("e2e cases %lu (each run native and OSH_INTERP=1), checks %lu, failures %lu\n", ncases, checks, fails); |
| 390 | /* clean the fixture: only files this test created */ |
| 391 | char rm[600]; |
| 392 | snprintf(rm, sizeof rm, "%s", TMP); |
| 393 | { |
| 394 | const char *names[] = {".out", ".err", ".in", "script.osh", "o1", "o2", "o3", "e1", "e2", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "x"}; |
| 395 | for (size_t i = 0; i < sizeof names / sizeof *names; i++) { |
| 396 | char p[700]; |
| 397 | snprintf(p, sizeof p, "%s/%s", TMP, names[i]); |
| 398 | unlink(p); |
| 399 | } |
| 400 | rmdir(TMP); |
| 401 | } |
| 402 | unlink(POL); |
| 403 | printf(fails ? "OSH_E2E_FAIL\n" : "OSH_E2E_PASS\n"); |
| 404 | return fails != 0; |
| 405 | } |