cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / import / run.sh
| 1 | #!/bin/sh |
| 2 | # tests/import/run.sh - SqliteImport module checks (crc32c.om, sqlite.om, import_main.om). |
| 3 | # Oracles: /usr/bin/sqlite3 3.45.1 (oracle.sh dump with hex()/typeof()/ieee754_to_blob()/rowid), |
| 4 | # Python 3 walfix.py / realcheck.py (independent WAL + record parsers, fixture edits only), and a |
| 5 | # Python CRC-32C reference. Omega programs are built with the pinned omc (omcbuild.sh). |
| 6 | # usage: LIVE=SRC.db sh tests/import/run.sh [WORKDIR] (default build/import-work; needs ~400 MB) |
| 7 | # LIVE is a read-only copy of a cortex-rs database (required). |
| 8 | # Prints PASS/FAIL per check, NEG lines are negative controls that must observe a failure. |
| 9 | set -u |
| 10 | # host rule: run at lowest CPU and I/O priority (re-exec once under nice -n 19 / ionice -c3). |
| 11 | # Delete WORKDIR afterwards (about 1.2 GB); the cited evidence is evidence/import/run.log. |
| 12 | if [ -z "${CXO_NICED:-}" ]; then CXO_NICED=1 exec nice -n 19 ionice -c3 sh "$0" "$@"; fi |
| 13 | T=$(cd "$(dirname "$0")" && pwd) |
| 14 | R=$(cd "$T/../.." && pwd) |
| 15 | W=${1:-$R/build/import-work} |
| 16 | LIVE=${LIVE:?set LIVE to a copy of a cortex-rs database} |
| 17 | rm -rf "$W"; mkdir -p "$W"; cd "$W" || exit 1 |
| 18 | pass=0; fail=0 |
| 19 | ok() { pass=$((pass + 1)); echo "PASS $*"; } |
| 20 | bad() { fail=$((fail + 1)); echo "FAIL $*"; } |
| 21 | check() { name=$1; shift; if "$@"; then ok "$name"; else bad "$name"; fi; } |
| 22 | # same: importer dump of DB equals the sqlite3 oracle dump of a scratch copy of DB (+ its -wal) |
| 23 | imp_dump() { # imp_dump NAME DBPATH -> NAME.log NAME.dump NAME.imperr |
| 24 | rm -f "$1.log" |
| 25 | "$W/cortex-import" "$2" "$1.log" > "$1.impout" 2> "$1.imperr"; echo $? > "$1.rc" |
| 26 | [ "$(cat "$1.rc")" = 0 ] && "$W/logdump" "$1.log" > "$1.dump" |
| 27 | } |
| 28 | oracle_dump() { # oracle_dump NAME DBPATH -> NAME.oracle (on a scratch copy) |
| 29 | rm -rf "o_$1"; mkdir "o_$1"; cp "$2" "o_$1/x.db" |
| 30 | [ -e "$2-wal" ] && cp "$2-wal" "o_$1/x.db-wal" |
| 31 | sh "$T/oracle.sh" "o_$1/x.db" > "$1.oracle" |
| 32 | } |
| 33 | nobad() { ! grep -q 'BAD:' "$1"; } |
| 34 | |
| 35 | echo "== toolchain" |
| 36 | OMC=$(sh "$R/tools/omc.sh") || exit 1 |
| 37 | export OMC |
| 38 | echo "omc $(sha256sum "$OMC" | cut -d' ' -f1) ($(awk '$1 == "omega-mv" { print "omega-mv " substr($2, 1, 8) }' "$R/PIN"), PIN, tools/omc.sh)" |
| 39 | echo "sqlite3 $(/usr/bin/sqlite3 --version | cut -d' ' -f1-2)" |
| 40 | echo "python $(python3 --version 2>&1)" |
| 41 | for f in om/crc32c.om om/sqlite.om om/import_main.om tests/import/logdump.om tests/import/crctest.om; do |
| 42 | echo "src $(sha256sum "$R/$f" | cut -d' ' -f1) $f fns=$(grep -c '^fn ' "$R/$f")" |
| 43 | done |
| 44 | echo "fns library+program (crc32c+sqlite+import_main): $(cat "$R/om/crc32c.om" "$R/om/sqlite.om" "$R/om/import_main.om" | grep -c '^fn ')" |
| 45 | |
| 46 | echo "== build" |
| 47 | sh "$T/omcbuild.sh" "$W/cortex-import" "$R/om/crc32c.om" "$R/om/sqlite.om" "$R/om/import_main.om" || exit 1 |
| 48 | sh "$T/omcbuild.sh" "$W/logdump" "$R/om/crc32c.om" "$T/logdump.om" || exit 1 |
| 49 | sh "$T/omcbuild.sh" "$W/crctest" "$R/om/crc32c.om" "$T/crctest.om" || exit 1 |
| 50 | |
| 51 | echo "== crc32c" |
| 52 | check crc_check_value [ "$(printf 123456789 | ./crctest)" = e3069283 ] |
| 53 | check crc_empty [ "$(printf '' | ./crctest)" = 00000000 ] |
| 54 | head -c 8388608 /dev/urandom > r8m.bin |
| 55 | refcrc() { python3 -c " |
| 56 | import sys |
| 57 | t=[] |
| 58 | for n in range(256): |
| 59 | c=n |
| 60 | for _ in range(8): c=(c>>1)^0x82F63B78 if c&1 else c>>1 |
| 61 | t.append(c) |
| 62 | c=0xFFFFFFFF |
| 63 | for b in open(sys.argv[1],'rb').read(): c=t[(c^b)&255]^(c>>8) |
| 64 | print('%08x'%(c^0xFFFFFFFF))" "$1"; } |
| 65 | for n in 1 7 8 65535 65536 65537 1000003 8388608; do |
| 66 | head -c $n r8m.bin > r.bin |
| 67 | check "crc_random_${n}B_vs_python" [ "$(./crctest < r.bin)" = "$(refcrc r.bin)" ] |
| 68 | done |
| 69 | sed 's/2197175160/2197175161/' "$R/om/crc32c.om" > crc_mutant.om |
| 70 | sh "$T/omcbuild.sh" "$W/crcmut" crc_mutant.om "$T/crctest.om" > /dev/null || exit 1 |
| 71 | if [ "$(printf 123456789 | ./crcmut)" = e3069283 ]; then bad "NEG crc_poly_mutant_detected"; else ok "NEG crc_poly_mutant_detected (mutant gives $(printf 123456789 | ./crcmut))"; fi |
| 72 | |
| 73 | echo "== on-disk REAL storage (independent parser)" |
| 74 | # expected serial types from sqlite3.c 3.45 OP_MakeRecord: an integral REAL is stored as the |
| 75 | # smallest integer type (0 -> 8, 1 -> 9) unless it needs 8 bytes (> 2^47-1), then as float (7). |
| 76 | rm -f real.db; /usr/bin/sqlite3 real.db "CREATE TABLE t(c REAL); INSERT INTO t VALUES (1.0),(0.5),(-3.0),(0.0),(1e14),(1e15);" |
| 77 | st=$(python3 "$T/realcheck.py" real.db); echo "serial types rowid:type = $st" |
| 78 | check integral_reals_stored_as_integers [ "$st" = "1:9 2:7 3:1 4:8 5:5 6:7" ] |
| 79 | |
| 80 | echo "== live copy ($LIVE)" |
| 81 | cp "$LIVE" live.db |
| 82 | [ -e "$LIVE-wal" ] && cp "$LIVE-wal" live.db-wal |
| 83 | sha256sum live.db |
| 84 | for i in 1 2 3; do |
| 85 | rm -f live.log |
| 86 | t0=$(date +%s%N); ./cortex-import live.db live.log > live.impout 2> live.imperr; rc=$?; t1=$(date +%s%N) |
| 87 | echo "live import run $i rc=$rc wall_ms=$(( (t1 - t0) / 1000000 )) $(cat live.impout)" |
| 88 | done |
| 89 | cat live.imperr |
| 90 | "$W/logdump" live.log > live.dump |
| 91 | oracle_dump live live.db |
| 92 | check live_oracle_has_no_BAD nobad live.oracle |
| 93 | # expected counts from SQLite itself, so the check follows whatever snapshot LIVE is |
| 94 | want=$(/usr/bin/sqlite3 -readonly live.db "SELECT 'IMPORT OK spaces=' || (SELECT count(*) FROM spaces) || ' entities=' || (SELECT count(*) FROM entities) || ' claims=' || (SELECT count(*) FROM claims) || ' fts=' || (SELECT count(*) FROM entities_fts)") |
| 95 | echo "live expected: $want" |
| 96 | check live_summary [ "$(cat live.impout)" = "$want" ] |
| 97 | check live_dump_equals_sqlite3 cmp live.dump live.oracle |
| 98 | echo "live log $(stat -c %s live.log) bytes, dump lines $(wc -l < live.dump), sha256 $(sha256sum live.log | cut -d' ' -f1)" |
| 99 | cp live.oracle live.oracle.mut |
| 100 | python3 - live.oracle.mut <<'EOF' |
| 101 | import sys |
| 102 | p = sys.argv[1]; b = bytearray(open(p, 'rb').read()) |
| 103 | i = b.index(b'|f:') + 3; b[i] = ord('0') if b[i] != ord('0') else ord('1') |
| 104 | open(p, 'wb').write(b) |
| 105 | EOF |
| 106 | if cmp -s live.dump live.oracle.mut; then bad "NEG diff_detects_one_f64_digit"; else ok "NEG diff_detects_one_f64_digit"; fi |
| 107 | |
| 108 | echo "== synthetic databases (schema.sql = cortex-rs init_schema, fill.sql)" |
| 109 | for v in s512:512:0 s4k:4096:0 s64k:65536:0 sres:1024:40; do |
| 110 | n=${v%%:*}; r=${v##*:}; p=${v#*:}; p=${p%%:*} |
| 111 | rm -f $n.db; { echo "PRAGMA page_size=$p;"; cat "$T/schema.sql" "$T/fill.sql"; } | /usr/bin/sqlite3 -bail $n.db |
| 112 | [ "$r" != 0 ] && /usr/bin/sqlite3 $n.db ".filectrl reserve_bytes $r" "VACUUM" |
| 113 | echo "$n page_size=$(/usr/bin/sqlite3 $n.db 'pragma page_size') reserved=$(od -An -tu1 -j20 -N1 $n.db | tr -d ' ') pages=$(/usr/bin/sqlite3 $n.db 'pragma page_count') maxpayload=$(/usr/bin/sqlite3 $n.db 'select max(length(cast(content as blob))) from entities')" |
| 114 | imp_dump $n $n.db; cat $n.impout |
| 115 | oracle_dump $n $n.db |
| 116 | check "${n}_oracle_has_no_BAD" nobad $n.oracle |
| 117 | check "${n}_dump_equals_sqlite3" cmp $n.dump $n.oracle |
| 118 | done |
| 119 | check s512_has_nulls_overflow_claims sh -c "grep -q '|null|' s512.dump && grep -c '^CLAIM' s512.dump | grep -qx 294 && awk 'length > 600000' s512.dump | grep -q ENTITY" |
| 120 | |
| 121 | echo "== WAL" |
| 122 | sh "$T/mkwal.sh" "$W/wal" || exit 1 |
| 123 | for s in snap1 snap3; do |
| 124 | echo "$s walfix: $(python3 "$T/walfix.py" info wal/$s/w.db-wal)" |
| 125 | imp_dump $s wal/$s/w.db; cat $s.imperr $s.impout |
| 126 | oracle_dump $s wal/$s/w.db |
| 127 | check "${s}_dump_equals_sqlite3" cmp $s.dump $s.oracle |
| 128 | done |
| 129 | cp wal/w.db final.db; oracle_dump final final.db |
| 130 | check snap3_equals_committed_state_after_close cmp snap3.dump final.oracle |
| 131 | inf=$(python3 "$T/walfix.py" info wal/snap3/w.db-wal) |
| 132 | V=$(echo "$inf" | sed 's/.*valid=\([0-9]*\).*/\1/'); M=$(echo "$inf" | sed 's/.*committed=\([0-9]*\).*/\1/') |
| 133 | C1=$(echo "$inf" | sed 's/.*commits=\([0-9]*\),.*/\1/') |
| 134 | check snap3_has_uncommitted_trailing_frames [ "$V" -gt "$M" ] |
| 135 | check snap3_importer_reports_same_frames grep -q "valid=$V committed=$M" snap3.imperr |
| 136 | U=$(printf UNCOMMITTED | od -An -tx1 | tr -d ' \n' | tr a-f A-F) |
| 137 | check snap3_dump_lacks_uncommitted_rows sh -c "! grep -q '$U' snap3.dump && ! grep -q 't:756E632D' snap3.dump" |
| 138 | check snap3_differs_from_snap1 sh -c "! cmp -s snap3.dump snap1.dump" |
| 139 | |
| 140 | mkdir -p flip; cp wal/snap3/w.db wal/snap3/w.db-wal flip/ |
| 141 | python3 "$T/walfix.py" flipck flip/w.db-wal $((C1 + 1)) |
| 142 | echo "flip frame $((C1 + 1)) walfix: $(python3 "$T/walfix.py" info flip/w.db-wal)" |
| 143 | imp_dump flip flip/w.db; cat flip.imperr |
| 144 | oracle_dump flip flip/w.db |
| 145 | check "NEG flipped_cksum_stops_overlay_at_frame_$((C1 + 1))" grep -q "valid=$C1 committed=$C1" flip.imperr |
| 146 | check flip_dump_equals_sqlite3 cmp flip.dump flip.oracle |
| 147 | check flip_dump_equals_txn1_state cmp flip.dump snap1.dump |
| 148 | |
| 149 | mkdir -p be; cp wal/snap3/w.db wal/snap3/w.db-wal be/ |
| 150 | python3 "$T/walfix.py" be be/w.db-wal |
| 151 | echo "be walfix: $(python3 "$T/walfix.py" info be/w.db-wal) magic=$(od -An -tx1 -N4 be/w.db-wal | tr -d ' ')" |
| 152 | imp_dump be be/w.db; cat be.imperr |
| 153 | oracle_dump be be/w.db |
| 154 | check bigendian_cksum_dump_equals_sqlite3 cmp be.dump be.oracle |
| 155 | check bigendian_cksum_equals_native cmp be.dump snap3.dump |
| 156 | |
| 157 | mkdir -p magic; cp wal/snap3/w.db wal/snap3/w.db-wal magic/ |
| 158 | printf '\377' | dd of=magic/w.db-wal bs=1 seek=3 conv=notrunc 2> /dev/null |
| 159 | imp_dump magic magic/w.db; cat magic.imperr |
| 160 | oracle_dump magic magic/w.db |
| 161 | check badmagic_wal_ignored_like_sqlite grep -q 'WAL ignored' magic.imperr |
| 162 | check badmagic_dump_equals_sqlite3 cmp magic.dump magic.oracle |
| 163 | |
| 164 | mkdir -p ver; cp wal/snap3/w.db wal/snap3/w.db-wal ver/ |
| 165 | python3 "$T/walfix.py" version ver/w.db-wal 3007001 |
| 166 | rm -f ver.log; ./cortex-import ver/w.db ver.log 2> ver.imperr; rc=$?; cat ver.imperr |
| 167 | check "wal_version_3007001_refused(rc=$rc)" sh -c "[ $rc = 14 ] && [ ! -e ver.log ] && [ ! -e ver.log.tmp ]" |
| 168 | |
| 169 | echo "== refusals" |
| 170 | rm -f u16.db; /usr/bin/sqlite3 u16.db "PRAGMA encoding='UTF-16le'; CREATE TABLE spaces(x);" |
| 171 | ./cortex-import u16.db u16.log 2> u16.err; rc=$?; cat u16.err |
| 172 | check "utf16_refused(rc=$rc)" sh -c "[ $rc = 9 ] && [ ! -e u16.log ]" |
| 173 | h0=$(sha256sum s512.log | cut -d' ' -f1) |
| 174 | ./cortex-import s512.db s512.log 2> exist.err; rc=$?; cat exist.err |
| 175 | check "existing_out_refused_and_untouched(rc=$rc)" sh -c "[ $rc = 53 ] && [ $h0 = \$(sha256sum s512.log | cut -d' ' -f1) ]" |
| 176 | touch t.log.tmp; ./cortex-import s512.db t.log 2> tmp.err; rc=$?; cat tmp.err |
| 177 | check "existing_out_tmp_refused(rc=$rc)" sh -c "[ $rc = 54 ] && [ ! -e t.log ]" |
| 178 | head -c 4096 r8m.bin > notdb.db; ./cortex-import notdb.db nd.log 2> nd.err; rc=$?; cat nd.err |
| 179 | check "non_sqlite_refused(rc=$rc)" [ $rc = 5 ] |
| 180 | head -c 100000 s4k.db > trunc.db; ./cortex-import trunc.db tr.log 2> tr.err; rc=$?; cat tr.err |
| 181 | check "truncated_db_refused(rc=$rc)" sh -c "[ $rc != 0 ] && [ ! -e tr.log ] && [ ! -e tr.log.tmp ]" |
| 182 | cp s4k.db badpg.db; croot=$(/usr/bin/sqlite3 s4k.db "select rootpage from sqlite_master where name='claims'") |
| 183 | ctype=$(od -An -tx1 -j$(( (croot - 1) * 4096 )) -N1 s4k.db | tr -d ' ') |
| 184 | printf '\002' | dd of=badpg.db bs=1 seek=$(( (croot - 1) * 4096 )) conv=notrunc 2> /dev/null |
| 185 | ./cortex-import badpg.db bp.log 2> bp.err; rc=$?; cat bp.err |
| 186 | check "late_refusal_removes_OUT_tmp(claims root $croot type $ctype->02, rc=$rc)" sh -c "[ $rc = 18 ] && [ ! -e bp.log ] && [ ! -e bp.log.tmp ]" |
| 187 | ./cortex-import s512.db 2> usage.err; rc=$?; cat usage.err |
| 188 | check "usage(rc=$rc)" [ $rc = 2 ] |
| 189 | |
| 190 | echo "== log decoder negative controls" |
| 191 | cp s512.log crc.log |
| 192 | flip() { python3 -c "import sys; p=sys.argv[1]; o=int(sys.argv[2]); b=bytearray(open(p,'rb').read()); b[o]^=1; open(p,'wb').write(b)" "$1" "$2"; } |
| 193 | flip crc.log $(( 8 + 8 + 20 )) |
| 194 | ./logdump crc.log > crc.dump; rc=$?; tail -1 crc.dump |
| 195 | check "NEG corrupted_payload_byte_detected_CRCFAIL(rc=$rc)" [ $rc = 3 ] |
| 196 | cp s512.log crc2.log |
| 197 | flip crc2.log 12 |
| 198 | ./logdump crc2.log > crc2.dump; rc=$?; tail -1 crc2.dump |
| 199 | check "NEG corrupted_crc_field_detected(rc=$rc)" [ $rc = 3 ] |
| 200 | head -c $(( $(stat -c %s s512.log) - 5 )) s512.log > torn.log |
| 201 | ./logdump torn.log > torn.dump; rc=$?; tail -1 torn.dump |
| 202 | check "NEG torn_tail_detected(rc=$rc)" [ $rc = 4 ] |
| 203 | |
| 204 | echo "== result: PASS=$pass FAIL=$fail" |
| 205 | [ $fail = 0 ] |