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 / omegatool_path / physics_dir_test.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 | /* Run-time physics resolution (src/omega_physics_dir.h): env wins, ../physics default, |
| 18 | * and clear failure (no guessing) when unresolvable. Host only, no chip. */ |
| 19 | #include "omega_physics_dir.h" |
| 20 | #include <sys/stat.h> |
| 21 | |
| 22 | static int fails; |
| 23 | #define CHECK(c) do { if (!(c)) { fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0) |
| 24 | |
| 25 | static void mkphys(const char *root) { |
| 26 | char p[2 * PATH_MAX]; |
| 27 | mkdir(root, 0755); |
| 28 | snprintf(p, sizeof p, "%s/m16", root); mkdir(p, 0755); |
| 29 | snprintf(p, sizeof p, "%s/m16/m16_native.h", root); |
| 30 | FILE *f = fopen(p, "w"); if (f) { fputs("/* fake */\n", f); fclose(f); } |
| 31 | } |
| 32 | |
| 33 | /* The M17 check before the pin was added (d70bc4a): header exists and git status is empty. */ |
| 34 | static int legacy_m17(const char *dir) { |
| 35 | char c[4 * PATH_MAX], b[256]; |
| 36 | snprintf(c, sizeof c, "cd '%s' && git status --porcelain 2>/dev/null", dir); |
| 37 | FILE *p = popen(c, "r"); if (!p) return 0; |
| 38 | int lines = 0; while (fgets(b, sizeof b, p)) lines++; |
| 39 | pclose(p); return lines == 0; |
| 40 | } |
| 41 | |
| 42 | static void sh(const char *fmt, const char *a) { |
| 43 | char c[4 * PATH_MAX]; snprintf(c, sizeof c, fmt, a, a, a); |
| 44 | if (system(c) != 0) { fprintf(stderr, "setup failed: %s\n", c); fails++; } |
| 45 | } |
| 46 | |
| 47 | /* Pin + clean checks (omega_physics_dir_verify). */ |
| 48 | static void verify_cases(const char *base) { |
| 49 | char good[2 * PATH_MAX], other[2 * PATH_MAX], nogit[2 * PATH_MAX], lock[2 * PATH_MAX], sha[64] = {0}, err[OMEGA_PHYSICS_ERR_SIZE], c[4 * PATH_MAX]; |
| 50 | snprintf(good, sizeof good, "%s/pgood", base); snprintf(other, sizeof other, "%s/pother", base); |
| 51 | snprintf(nogit, sizeof nogit, "%s/pnogit", base); snprintf(lock, sizeof lock, "%s/physics.lock", base); |
| 52 | const char *mk = "mkdir -p '%s/m16' && echo x > '%s/m16/m16_native.h' && cd '%s' && git init -q . && git add . && git -c user.name=t -c user.email=t@t commit -q -m c"; |
| 53 | sh(mk, good); |
| 54 | sh("mkdir -p '%s/m16' && echo y > '%s/m16/m16_native.h' && cd '%s' && git init -q . && git add . && git -c user.name=t -c user.email=t@t commit -q -m c", other); |
| 55 | sh("mkdir -p '%s/m16' && echo x > '%s/m16/m16_native.h' && true", nogit); |
| 56 | snprintf(c, sizeof c, "git -C '%s' rev-parse HEAD", good); |
| 57 | FILE *p = popen(c, "r"); if (!p || !fgets(sha, sizeof sha, p)) { fails++; return; } pclose(p); sha[40] = 0; |
| 58 | FILE *l = fopen(lock, "w"); fprintf(l, "%s\n", sha); fclose(l); |
| 59 | /* red on the old check: an unrelated clean repo and a non-repo both pass it */ |
| 60 | CHECK(legacy_m17(other) == 1); |
| 61 | CHECK(legacy_m17(nogit) == 1); |
| 62 | /* new check fails closed */ |
| 63 | CHECK(omega_physics_dir_verify(other, lock, 1, err, sizeof err) == 0 && strstr(err, "expected commit") && strstr(err, other)); |
| 64 | CHECK(omega_physics_dir_verify(nogit, lock, 1, err, sizeof err) == 0 && strstr(err, nogit)); |
| 65 | CHECK(omega_physics_dir_verify(good, lock, 1, err, sizeof err) == 1); |
| 66 | snprintf(c, sizeof c, "echo z >> '%s/m16/m16_native.h'", good); CHECK(system(c) == 0); |
| 67 | CHECK(omega_physics_dir_verify(good, lock, 1, err, sizeof err) == 0 && strstr(err, "dirty")); |
| 68 | CHECK(omega_physics_dir_verify(good, lock, 0, err, sizeof err) == 1); /* pin-only mode (M18/M19) */ |
| 69 | l = fopen(lock, "w"); fputs("deadbeef\n", l); fclose(l); |
| 70 | CHECK(omega_physics_dir_verify(good, lock, 0, err, sizeof err) == 0 && strstr(err, "malformed")); |
| 71 | CHECK(omega_physics_dir_verify(good, "/nonexistent/physics.lock", 0, err, sizeof err) == 0); |
| 72 | } |
| 73 | |
| 74 | int main(void) { |
| 75 | char base[] = "/tmp/omega_physdir_XXXXXX", out[PATH_MAX], err[OMEGA_PHYSICS_ERR_SIZE], p[2 * PATH_MAX]; |
| 76 | if (!mkdtemp(base)) return 2; |
| 77 | char want[PATH_MAX]; if (!realpath(base, want)) return 2; |
| 78 | |
| 79 | /* 1. env set to a valid checkout */ |
| 80 | snprintf(p, sizeof p, "%s/phys", base); mkphys(p); |
| 81 | setenv("PHYSICS_DIR", p, 1); |
| 82 | CHECK(omega_physics_dir_resolve(out, sizeof out, err, sizeof err) == 1); |
| 83 | snprintf(p, sizeof p, "%s/phys", want); CHECK(strcmp(out, p) == 0); |
| 84 | |
| 85 | /* 2. env set to a missing dir: refuse, message names the value and the variable */ |
| 86 | setenv("PHYSICS_DIR", "/nonexistent/physics-xyz", 1); |
| 87 | CHECK(omega_physics_dir_resolve(out, sizeof out, err, sizeof err) == 0); |
| 88 | CHECK(strstr(err, "/nonexistent/physics-xyz") && strstr(err, "PHYSICS_DIR")); |
| 89 | |
| 90 | /* 3. env set to a dir that is not physics: refuse, never fall back to ../physics */ |
| 91 | snprintf(p, sizeof p, "%s/notphys", base); mkdir(p, 0755); |
| 92 | setenv("PHYSICS_DIR", p, 1); |
| 93 | CHECK(omega_physics_dir_resolve(out, sizeof out, err, sizeof err) == 0); |
| 94 | CHECK(strstr(err, "m16/m16_native.h")); |
| 95 | |
| 96 | /* 4. env empty or unset: default ../physics relative to cwd; absent -> refuse */ |
| 97 | snprintf(p, sizeof p, "%s/omega", base); mkdir(p, 0755); |
| 98 | CHECK(chdir(p) == 0); |
| 99 | setenv("PHYSICS_DIR", "", 1); |
| 100 | CHECK(omega_physics_dir_resolve(out, sizeof out, err, sizeof err) == 0); |
| 101 | CHECK(strstr(err, "../physics")); |
| 102 | unsetenv("PHYSICS_DIR"); |
| 103 | CHECK(omega_physics_dir_resolve(out, sizeof out, err, sizeof err) == 0); |
| 104 | snprintf(p, sizeof p, "%s/physics", base); mkphys(p); |
| 105 | CHECK(omega_physics_dir_resolve(out, sizeof out, err, sizeof err) == 1); |
| 106 | snprintf(p, sizeof p, "%s/physics", want); CHECK(strcmp(out, p) == 0); |
| 107 | |
| 108 | verify_cases(base); |
| 109 | char cmd[2 * PATH_MAX]; snprintf(cmd, sizeof cmd, "rm -rf '%s'", base); (void)!system(cmd); |
| 110 | puts(fails ? "test-omegatool-path-runtime: FAIL" : "test-omegatool-path-runtime: PASS"); |
| 111 | return fails != 0; |
| 112 | } |