Artifact ff30fc187c966da3ea0295ab32ac765a65f1fa51e38e78a9661a3cf136db71f5:
- File
test/context/atomicity.c
— part of check-in
[11f47eadde]
at
2026-11-18 11:58:22
on branch trunk
— Record S0/S1 qualification groundwork before product code
Contract maps and pinned Pi reference, local C qualification fixtures, MC/DC and Fil-C remote lifecycle tooling, and the context transaction substrate probe. No product harness code yet. (user: dmestas, size: 11899) [annotate] [blame] [check-ins using]
#define _POSIX_C_SOURCE 200809L #include "context-test.h" #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #define REQUIRE(condition, message) \ do { \ if (!(condition)) { \ fprintf(stderr, "FAIL: %s (line %d)\n", message, __LINE__); \ exit(1); \ } \ } while (0) static void execute(sqlite3 *db, const char *sql) { char *error = NULL; int rc = sqlite3_exec(db, sql, NULL, NULL, &error); if (rc != SQLITE_OK) { fprintf(stderr, "FAIL: SQL %s: %s\n", sql, error ? error : sqlite3_errmsg(db)); sqlite3_free(error); exit(1); } } static sqlite3_stmt *prepare(sqlite3 *db, const char *sql) { sqlite3_stmt *statement = NULL; REQUIRE(sqlite3_prepare_v2(db, sql, -1, &statement, NULL) == SQLITE_OK, sqlite3_errmsg(db)); return statement; } static int scalar(sqlite3 *db, const char *sql) { sqlite3_stmt *statement = prepare(db, sql); REQUIRE(sqlite3_step(statement) == SQLITE_ROW, sqlite3_errmsg(db)); int value = sqlite3_column_int(statement, 0); REQUIRE(sqlite3_step(statement) == SQLITE_DONE, "one scalar row"); REQUIRE(sqlite3_finalize(statement) == SQLITE_OK, "finalize scalar"); return value; } static void bind_text(sqlite3_stmt *statement, int index, const char *text) { REQUIRE(sqlite3_bind_text(statement, index, text, -1, SQLITE_TRANSIENT) == SQLITE_OK, "bind text"); } static char *save_note(sqlite3 *db, const char *body, const char *id) { sqlite3_stmt *statement = prepare( db, id ? "SELECT fsl_technote_save(?1, json_object('id', ?2))" : "SELECT fsl_technote_save(?1, json_object('comment', " "'Synthetic atomicity fixture', 'mimetype', 'text/x-markdown'))"); bind_text(statement, 1, body); if (id) bind_text(statement, 2, id); REQUIRE(sqlite3_step(statement) == SQLITE_ROW, sqlite3_errmsg(db)); const unsigned char *value = sqlite3_column_text(statement, 0); REQUIRE(value != NULL && value[0] != '\0', "technote returns its identity"); char *result = strdup((const char *)value); REQUIRE(result != NULL, "allocate note identity"); REQUIRE(sqlite3_step(statement) == SQLITE_DONE, "finish technote save"); REQUIRE(sqlite3_finalize(statement) == SQLITE_OK, "finalize technote save"); REQUIRE(!sqlite3_get_autocommit(db), "technote save leaves host transaction open"); return result; } static void assert_note(sqlite3 *db, const char *id, const char *body) { sqlite3_stmt *statement = prepare(db, "SELECT CAST(fsl_technote(?1) AS TEXT)"); bind_text(statement, 1, id); REQUIRE(sqlite3_step(statement) == SQLITE_ROW, sqlite3_errmsg(db)); const unsigned char *value = sqlite3_column_text(statement, 0); REQUIRE(value != NULL && strcmp((const char *)value, body) == 0, "technote body matches expected committed version"); REQUIRE(sqlite3_step(statement) == SQLITE_DONE, "finish technote read"); REQUIRE(sqlite3_finalize(statement) == SQLITE_OK, "finalize technote read"); } static sqlite3 *reopen(sqlite3 *db) { REQUIRE(sqlite3_close(db) == SQLITE_OK, "close host and extension statements"); db = context_test_open(); REQUIRE(sqlite3_get_autocommit(db), "reopened connection has no transaction"); return db; } static void require_private_environment(void) { const char *home = getenv("HOME"); const char *path = getenv("CTX_DB"); struct stat status; REQUIRE(home != NULL && home[0] == '/', "explicit absolute temporary HOME"); REQUIRE(path != NULL && strncmp(path, home, strlen(home)) == 0 && strcmp(path + strlen(home), "/ctx.sqlite") == 0, "CTX_DB is inside the private HOME"); REQUIRE(lstat(path, &status) != 0 && errno == ENOENT, "database must be fresh"); FILE *marker = fopen(".context-probe-private", "r"); REQUIRE(marker != NULL, "run from a marked private directory"); REQUIRE(fclose(marker) == 0, "close private marker"); } int main(void) { (void)alarm(30); require_private_environment(); context_test_initialize(); sqlite3 *db = context_test_open(); REQUIRE(strcmp(sqlite3_db_filename(db, "main"), getenv("CTX_DB")) == 0, "context opened the explicitly isolated database file"); REQUIRE(scalar(db, "SELECT journal_mode='wal' FROM pragma_journal_mode") == 1, "probe preserves current context WAL mode"); REQUIRE(scalar(db, "PRAGMA foreign_keys") == 0, "libfossil requires FK OFF"); REQUIRE(scalar(db, "SELECT count(*) FROM pragma_database_list WHERE name NOT " "IN ('main', 'temp')") == 0, "embedded repository is not a separately attached database"); REQUIRE(scalar(db, "SELECT count(*) FROM main.sqlite_master WHERE " "name IN ('fslrepo_me_blob', 'fslrepo_me_config', 'ctx_doc', " "'ctx_chunk', 'ctx_capture')") == 5, "real context and embedded Fossil tables exist in main"); execute(db, "CREATE TABLE probe_receipt(key TEXT PRIMARY KEY, body TEXT NOT " "NULL) STRICT"); execute(db, "BEGIN IMMEDIATE"); char *id = save_note(db, "committed fixture", NULL); execute(db, "INSERT INTO ctx_capture(session,path,project) " "VALUES('synthetic-atomicity','fixture-only','probe');" "INSERT INTO probe_receipt VALUES('committed','committed fixture')"); execute(db, "COMMIT"); REQUIRE(sqlite3_get_autocommit(db), "commit completes host transaction"); assert_note(db, id, "committed fixture"); REQUIRE(scalar(db, "SELECT count(*) FROM ctx_capture WHERE " "session='synthetic-atomicity'") == 1, "actual context row committed"); REQUIRE( scalar(db, "SELECT count(*) FROM probe_receipt WHERE key='committed'") == 1, "host fixture row committed"); db = reopen(db); assert_note(db, id, "committed fixture"); REQUIRE(scalar(db, "SELECT count(*) FROM ctx_capture WHERE " "session='synthetic-atomicity'") == 1, "context row survives reopen"); REQUIRE( scalar(db, "SELECT count(*) FROM probe_receipt WHERE key='committed'") == 1, "host fixture row survives reopen"); puts("PASS: context commit survives reopen"); int committed_blobs = scalar(db, "SELECT count(*) FROM main.fslrepo_me_blob"); execute(db, "BEGIN IMMEDIATE"); char *edited = save_note(db, "rolled back fixture", id); REQUIRE(strcmp(edited, id) == 0, "edit preserves technote identity"); free(edited); execute( db, "UPDATE ctx_capture SET offset=123 WHERE session='synthetic-atomicity';" "UPDATE probe_receipt SET body='rolled back fixture' WHERE " "key='committed';" "INSERT INTO probe_receipt VALUES('rollback-only','not committed')"); assert_note(db, id, "rolled back fixture"); REQUIRE(scalar(db, "SELECT offset FROM ctx_capture WHERE " "session='synthetic-atomicity'") == 123, "context mutation is visible inside host transaction"); REQUIRE( scalar(db, "SELECT count(*) FROM probe_receipt WHERE key='rollback-only'") == 1, "fixture mutation is visible inside host transaction"); #ifdef CTX_PROBE_MUTANT_COMMIT_ROLLBACK execute(db, "COMMIT"); #else execute(db, "ROLLBACK"); #endif REQUIRE(sqlite3_get_autocommit(db), "rollback completes host transaction"); assert_note(db, id, "committed fixture"); REQUIRE(scalar(db, "SELECT offset FROM ctx_capture WHERE " "session='synthetic-atomicity'") == 0, "context mutation rolled back"); REQUIRE( scalar(db, "SELECT count(*) FROM probe_receipt WHERE key='rollback-only'") == 0, "fixture insert rolled back"); REQUIRE(scalar(db, "SELECT count(*) FROM probe_receipt WHERE body='committed " "fixture'") == 1, "fixture update rolled back"); REQUIRE(scalar(db, "SELECT count(*) FROM main.fslrepo_me_blob") == committed_blobs, "rolled back Fossil artifact did not leak into main"); db = reopen(db); assert_note(db, id, "committed fixture"); REQUIRE(scalar(db, "SELECT offset FROM ctx_capture WHERE " "session='synthetic-atomicity'") == 0, "context rollback survives reopen"); REQUIRE(scalar(db, "SELECT count(*) FROM probe_receipt") == 1, "fixture rollback survives reopen"); REQUIRE(scalar(db, "SELECT count(*) FROM main.fslrepo_me_blob") == committed_blobs, "Fossil artifact rollback survives reopen"); puts("PASS: context rollback survives reopen"); execute(db, "BEGIN IMMEDIATE"); edited = save_note(db, "failed statement fixture", id); free(edited); execute( db, "UPDATE ctx_capture SET offset=456 WHERE session='synthetic-atomicity';" "UPDATE probe_receipt SET body='failed statement fixture' WHERE " "key='committed';" "INSERT INTO probe_receipt VALUES('failure-only','not committed')"); char *error = NULL; int rc = sqlite3_exec( db, "INSERT INTO probe_receipt VALUES('committed','duplicate')", NULL, NULL, &error); REQUIRE(rc == SQLITE_CONSTRAINT && sqlite3_extended_errcode(db) == SQLITE_CONSTRAINT_PRIMARYKEY, "injected duplicate key produces the expected statement failure"); sqlite3_free(error); REQUIRE(!sqlite3_get_autocommit(db), "statement ABORT does not implicitly roll back the host transaction"); assert_note(db, id, "failed statement fixture"); REQUIRE(scalar(db, "SELECT offset FROM ctx_capture WHERE " "session='synthetic-atomicity'") == 456, "earlier context mutation remains staged after statement failure"); REQUIRE( scalar(db, "SELECT count(*) FROM probe_receipt WHERE key='failure-only'") == 1, "earlier fixture mutation remains staged after statement failure"); execute(db, "ROLLBACK"); REQUIRE(sqlite3_get_autocommit(db), "explicit failure rollback completes"); assert_note(db, id, "committed fixture"); REQUIRE(scalar(db, "SELECT offset FROM ctx_capture WHERE " "session='synthetic-atomicity'") == 0, "context mutation explicitly rolled back after failure"); REQUIRE(scalar(db, "SELECT count(*) FROM probe_receipt WHERE body='committed " "fixture'") == 1 && scalar(db, "SELECT count(*) FROM probe_receipt") == 1, "fixture mutations explicitly rolled back after failure"); REQUIRE(scalar(db, "SELECT count(*) FROM main.fslrepo_me_blob") == committed_blobs, "Fossil artifact explicitly rolled back after failure"); db = reopen(db); assert_note(db, id, "committed fixture"); REQUIRE(scalar(db, "SELECT offset FROM ctx_capture WHERE " "session='synthetic-atomicity'") == 0, "failure rollback survives reopen on context side"); REQUIRE(scalar(db, "SELECT count(*) FROM probe_receipt WHERE body='committed " "fixture'") == 1 && scalar(db, "SELECT count(*) FROM probe_receipt") == 1, "failure rollback survives reopen on fixture side"); REQUIRE(scalar(db, "SELECT count(*) FROM main.fslrepo_me_blob") == committed_blobs, "failure rollback survives reopen on Fossil side"); puts("PASS: context statement failure explicitly rolled back across reopen"); free(id); REQUIRE(sqlite3_close(db) == SQLITE_OK, "close final connection"); context_test_shutdown(); return 0; }