| 1811 | } |
| 1812 | |
| 1813 | static bool cli_scope_fixture_start(cli_scope_fixture_t *fixture, const char *tag) { |
| 1814 | memset(fixture, 0, sizeof(*fixture)); |
| 1815 | fixture->host = -1; |
| 1816 | fixture->release_fd = -1; |
| 1817 | snprintf(fixture->tmpdir, sizeof(fixture->tmpdir), "/tmp/cli-guard-scope-%s-XXXXXX", tag); |
| 1818 | if (!cbm_mkdtemp(fixture->tmpdir)) { |
| 1819 | return cli_scope_setup_failed(fixture, tag, "creating the fixture directory", errno); |
| 1820 | } |
| 1821 | snprintf(fixture->runtime_parent, sizeof(fixture->runtime_parent), "%s/runtime", |
| 1822 | fixture->tmpdir); |
| 1823 | snprintf(fixture->host_home, sizeof(fixture->host_home), "%s/host", fixture->tmpdir); |
| 1824 | snprintf(fixture->host_cache, sizeof(fixture->host_cache), "%s/cache", fixture->host_home); |
| 1825 | snprintf(fixture->conflict_log, sizeof(fixture->conflict_log), "%s/conflicts.ndjson", |
| 1826 | fixture->host_home); |
| 1827 | /* The host identity's cache fingerprint is derived exactly as main.c does |
| 1828 | * for a real daemon: resolved dir -> canonical path -> SHA-256. */ |
| 1829 | char canonical_cache[1024]; |
| 1830 | if (test_mkdirp(fixture->runtime_parent) != 0 || !cbm_mkdir_p(fixture->host_cache, 0700) || |
| 1831 | !cbm_canonical_path(fixture->host_cache, canonical_cache, sizeof(canonical_cache))) { |
| 1832 | return cli_scope_setup_failed(fixture, tag, "creating the host directories", errno); |
| 1833 | } |
| 1834 | cbm_normalize_path_sep(canonical_cache); |
| 1835 | cbm_sha256_hex(canonical_cache, strlen(canonical_cache), fixture->host_cache_fingerprint); |
| 1836 | if (!cbm_daemon_runtime_process_build_fingerprint((uint64_t)getpid(), fixture->self_build)) { |
| 1837 | /* Fails when the runner's file no longer matches its running image, |
| 1838 | * e.g. a rebuild replaced it mid-run. */ |
| 1839 | return cli_scope_setup_failed(fixture, tag, "fingerprinting the runner's own image", 0); |
| 1840 | } |
| 1841 | /* The guard claims the supervisor's captured build; the runner stubs that |
| 1842 | * capture while a runtime service must carry the real image hash. Align |
| 1843 | * the two for this fixture exactly as one production process has them. */ |
| 1844 | const char *previous_supervisor_build = cbm_index_supervisor_build_fingerprint(); |
| 1845 | snprintf(fixture->previous_supervisor_build, sizeof(fixture->previous_supervisor_build), "%s", |
| 1846 | previous_supervisor_build ? previous_supervisor_build : ""); |
| 1847 | cbm_index_supervisor_set_build_fingerprint_for_test(fixture->self_build); |
| 1848 | fixture->identity = (cbm_daemon_build_identity_t){ |
| 1849 | .semantic_version = CBM_VERSION, |
| 1850 | .build_fingerprint = fixture->self_build, |
| 1851 | .cache_fingerprint = fixture->host_cache_fingerprint, |
| 1852 | .protocol_abi = CBM_DAEMON_RUNTIME_WIRE_ABI, |
| 1853 | .store_abi = 1, |
| 1854 | .feature_abi = 1, |
| 1855 | }; |
| 1856 | int ready_pipe[2] = {-1, -1}; |
| 1857 | int release_pipe[2] = {-1, -1}; |
| 1858 | if (pipe(ready_pipe) != 0) { |
| 1859 | return cli_scope_setup_failed(fixture, tag, "creating the readiness pipe", errno); |
| 1860 | } |
| 1861 | if (pipe(release_pipe) != 0) { |
| 1862 | int pipe_error = errno; |
| 1863 | close(ready_pipe[0]); |
| 1864 | close(ready_pipe[1]); |
| 1865 | return cli_scope_setup_failed(fixture, tag, "creating the release pipe", pipe_error); |
| 1866 | } |
| 1867 | pid_t child = fork(); |
| 1868 | int fork_error = child < 0 ? errno : 0; |
| 1869 | if (child == 0) { |
| 1870 | close(ready_pipe[0]); |
no test coverage detected