Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/omegatool_path/physics_dir_test.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / omegatool_path / physics_dir_test.c` 118 lines ยท 6390 bytes Raw Blame History 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 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 /* 3b. a real checkout whose real path holds a quote: refuse (the gate runners single-quote it for the shell) */ 97 snprintf(p, sizeof p, "%s/q'uote", base); mkphys(p); 98 setenv("PHYSICS_DIR", p, 1); 99 CHECK(omega_physics_dir_resolve(out, sizeof out, err, sizeof err) == 0); 100 CHECK(strstr(err, "unusable") && strstr(err, "q'uote")); 101 102 /* 4. env empty or unset: default ../physics relative to cwd; absent -> refuse */ 103 snprintf(p, sizeof p, "%s/omega", base); mkdir(p, 0755); 104 CHECK(chdir(p) == 0); 105 setenv("PHYSICS_DIR", "", 1); 106 CHECK(omega_physics_dir_resolve(out, sizeof out, err, sizeof err) == 0); 107 CHECK(strstr(err, "../physics")); 108 unsetenv("PHYSICS_DIR"); 109 CHECK(omega_physics_dir_resolve(out, sizeof out, err, sizeof err) == 0); 110 snprintf(p, sizeof p, "%s/physics", base); mkphys(p); 111 CHECK(omega_physics_dir_resolve(out, sizeof out, err, sizeof err) == 1); 112 snprintf(p, sizeof p, "%s/physics", want); CHECK(strcmp(out, p) == 0); 113 114 verify_cases(base); 115 char cmd[2 * PATH_MAX]; snprintf(cmd, sizeof cmd, "rm -rf '%s'", base); (void)!system(cmd); 116 puts(fails ? "test-omegatool-path-runtime: FAIL" : "test-omegatool-path-runtime: PASS"); 117 return fails != 0; 118 }