Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,16 @@ Five suites run under CTest:
with no invented ones, no API-documentation section (§9.4), and the §9.8
two-corelib comparison as a subsection of
[Benchmarks](#benchmarks). Skipped in a tree without a README.
- **`test_vendored_provenance`** — the shared vectors and the JSON reader that
loads them are verbatim copies of `corelib-c-cpp`, and `test_vectors` replays
whatever bytes are on disk, pinned or not. What makes a re-sync checkable is
the provenance table in `test/shared/README.md`: the merged upstream commit
each copy came from and the `md5` it hashed to. This check reads that table
and re-hashes the files, so a copy refreshed without re-pinning its row goes
red rather than drifting silently; it also rejects a pin that is not a bare
merged commit — a branch or an unmerged PR head leaves the next re-syncer with
no SHA to diff against — and a vendored file with no row at all. Skipped in a
tree without `test/shared/README.md`.

### Coverage and API docs

Expand Down
15 changes: 15 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ add_test(NAME test_readme_structure
${PROJECT_SOURCE_DIR}/README.md)
set_tests_properties(test_readme_structure PROPERTIES SKIP_RETURN_CODE 77)

# CORELIB_PLAN §8: the shared vectors and the JSON reader are copies of
# corelib-c-cpp, the authoritative source. Their provenance record — which
# merged upstream commit each copy came from, and what it hashed to — is the
# only thing that makes a re-sync checkable, and no C++ test can see it rot:
# test_vectors replays whatever bytes are on disk, pinned or not. This check
# reads test/shared/README.md and fails on a pin that is not a bare merged
# commit (a branch or an unmerged PR head leaves a re-syncer nothing to diff
# against), on a recorded checksum that no longer matches the vendored file, and
# on a vendored file with no row at all.
add_test(NAME test_vendored_provenance
COMMAND bash ${CMAKE_CURRENT_LIST_DIR}/test_vendored_provenance.sh
${CMAKE_CURRENT_LIST_DIR}/shared/README.md
${CMAKE_COMMAND})
set_tests_properties(test_vendored_provenance PROPERTIES SKIP_RETURN_CODE 77)

if(SOFAB_ENABLE_COVERAGE)
target_link_libraries(test_roundtrip PRIVATE sofab_coverage)
target_link_libraries(test_vectors PRIVATE sofab_coverage)
Expand Down
37 changes: 29 additions & 8 deletions test/shared/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@ below).

## Provenance

| File | Upstream source | Pinned at |
|------|-----------------|-----------|
| `sofab_test_json.c` | [`sofa-buffers/corelib-c-cpp`](https://github.com/sofa-buffers/corelib-c-cpp) → `test/shared/sofab_test_json.c` | commit `e149c218cdbb` (2026-06-25) |
| `sofab_test_json.h` | [`sofa-buffers/corelib-c-cpp`](https://github.com/sofa-buffers/corelib-c-cpp) → `test/shared/sofab_test_json.h` | commit `e149c218cdbb` (2026-06-25) |
| File | Upstream source | Pinned at | `md5` |
|------|-----------------|-----------|-------|
| `sofab_test_json.c` | [`sofa-buffers/corelib-c-cpp`](https://github.com/sofa-buffers/corelib-c-cpp) → `test/shared/sofab_test_json.c` | commit `e149c218cdbb` (2026-06-25) | `4dd57a285e9e3d16b3aced83800d7bff` |
| `sofab_test_json.h` | [`sofa-buffers/corelib-c-cpp`](https://github.com/sofa-buffers/corelib-c-cpp) → `test/shared/sofab_test_json.h` | commit `e149c218cdbb` (2026-06-25) | `dc69a2eab0135b5903f9bafa8785ebad` |

Each row pins a **merged** upstream commit — the last commit on `corelib-c-cpp`'s
`main` that touched that file — and the `md5` the copy hashed to when it was
taken. The checksum is what makes the record self-checking: `test_vendored_provenance`
re-hashes every vendored file on each `ctest` run, so a copy refreshed without
re-pinning its row turns red instead of drifting silently.

`sofab_test_json.{c,h}` is a tiny dependency-free JSON reader. Its only job is to
load the shared conformance vectors so `test/test_vectors.cpp` can replay them
through this repo's pure-C++20 `sofab::OStream` / `sofab::IStream`.

The vectors it loads are **also vendored**, from the same upstream:

| File | Upstream source | Pinned at |
|------|-----------------|-----------|
| `../../assets/test_vectors.json` | [`sofa-buffers/corelib-c-cpp`](https://github.com/sofa-buffers/corelib-c-cpp) → `assets/test_vectors.json` | commit `37e06a829178` (2026-07-26), branch `poc/omit-all-default-sequences` — re-pin to the merged SHA once that lands |
| File | Upstream source | Pinned at | `md5` |
|------|-----------------|-----------|-------|
| `../../assets/test_vectors.json` | [`sofa-buffers/corelib-c-cpp`](https://github.com/sofa-buffers/corelib-c-cpp) → `assets/test_vectors.json` | commit `f30244e3f29e` (2026-07-28) | `f542c66bfa6aecb3636282ec6e802a1b` |

`test_vectors.json` is the cross-language source of truth for the wire format and
is copied verbatim into every SofaBuffers corelib. We track the copy vendored in
Expand Down Expand Up @@ -84,4 +90,19 @@ curl -fsSL https://raw.githubusercontent.com/sofa-buffers/corelib-c-cpp/main/tes
curl -fsSL https://raw.githubusercontent.com/sofa-buffers/corelib-c-cpp/main/assets/test_vectors.json -o assets/test_vectors.json
```

Then update the "Pinned at" commits in the table above to the new upstream SHAs.
Then re-pin the table above. For each file, take the SHA of the last **merged**
commit that touched it upstream — never a branch or an unmerged PR head, which
gives a later reader nothing to diff against — and its new checksum:

```sh
# the merged SHA to pin (repeat per path)
gh api 'repos/sofa-buffers/corelib-c-cpp/commits?path=assets/test_vectors.json&sha=main&per_page=1' \
--jq '.[0] | "\(.sha[0:12]) (\(.commit.committer.date[0:10]))"'

# the checksums to record
md5sum test/shared/sofab_test_json.c test/shared/sofab_test_json.h assets/test_vectors.json
```

Verifying an existing checkout needs neither command nor the network — the
recorded checksums are re-hashed by `ctest -R test_vendored_provenance`, which
also rejects a pin that is not a bare merged commit.
165 changes: 165 additions & 0 deletions test/test_vendored_provenance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/usr/bin/env bash
#
# SofaBuffers pure-C++20 — guards the vendored-file provenance record.
#
# CORELIB_PLAN §8 makes `assets/test_vectors.json` a file "generated by and
# copied from corelib-c-cpp (the authoritative source)", and the same holds for
# the JSON reader under `test/shared/`. None of those files is original to this
# repository: the only thing that makes a re-sync checkable is the provenance
# table in `test/shared/README.md`, which says *which upstream commit* each copy
# came from and *what it hashed to* when it was taken.
#
# Nothing inside the library can notice that record going stale — a pin that
# names an unmerged branch, or a copy that was refreshed without touching the
# table, is invisible to every C++ test and to `test_vectors`, which happily
# replays whatever bytes are on disk. So the check has to read the document and
# the files side by side.
#
# What it enforces, for every row of every provenance table:
#
# 1. The vendored file the row names actually exists (paths are resolved
# relative to the README, so `../../assets/test_vectors.json` is checked
# where it really lives).
# 2. The "Pinned at" cell is a *merged commit* and nothing else: a 12-hex SHA
# plus its date. A branch name, a "re-pin once that lands" caveat or any
# other provisional wording is a failure — a reader re-syncing needs a SHA
# on upstream `main` to diff against, and a branch pin gives them none.
# 3. The row records the file's checksum, and that checksum still matches the
# bytes on disk. This is what turns a re-sync into a one-command check: the
# moment somebody re-copies a file without re-pinning the row, this test
# goes red instead of the record silently drifting out of date.
# 4. Every vendored file is covered by a row, and the table has not shrunk
# below the three files this repo vendors — a reshaped README that drops
# rows must not pass by having nothing left to check.
#
# Usage: test_vendored_provenance.sh <path-to-test/shared/README.md> [cmake]
#
# SPDX-License-Identifier: MIT
set -uo pipefail

README="${1:?usage: test_vendored_provenance.sh <path-to-test/shared/README.md> [cmake]}"
CMAKE="${2:-cmake}"

# CTest maps 77 to "skipped" (SKIP_RETURN_CODE): a consumer tree that vendored
# only the header has no test/shared/README.md to check.
if [ ! -f "$README" ]; then
echo "SKIP: no vendored-file README at $README"
exit 77
fi

BASE="$(cd "$(dirname "$README")" && pwd)"

fails=0
fail() { echo "FAIL: $*" >&2; fails=$((fails + 1)); }
ok() { echo " ok: $*"; }

# md5 of a file, lower-case hex, no filename. `cmake -E md5sum` is the portable
# spelling — CMake is by definition present in this tree, while md5sum (GNU) and
# md5 (BSD) are not both.
md5_of() {
if "$CMAKE" -E md5sum "$1" >/dev/null 2>&1; then
"$CMAKE" -E md5sum "$1" | awk '{ print $1 }'
elif command -v md5sum >/dev/null 2>&1; then
md5sum "$1" | awk '{ print $1 }'
elif command -v md5 >/dev/null 2>&1; then
md5 -q "$1"
else
echo ""
fi
}

# Provenance rows are the table lines whose first cell is a back-ticked path.
# Header and separator rows never start with a backtick, so this picks out
# exactly the data rows of every provenance table in the document.
rows="$(grep -E '^\| *`[^`]+` *\|' "$README")"

if [ -z "$rows" ]; then
fail "no provenance rows found — §8 requires every vendored file to record its upstream"
echo "test_vendored_provenance: $fails failure(s)" >&2
exit 1
fi

seen_files=""
row_count=0

while IFS= read -r row; do
row_count=$((row_count + 1))

# Split the markdown row into its cells (leading/trailing '|' dropped).
body="${row#|}"
body="${body%|}"
IFS='|' read -r c_file c_src c_pin c_sum _rest <<< "$body"

trim() { printf '%s' "$1" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//'; }
c_file="$(trim "${c_file:-}")"
c_src="$(trim "${c_src:-}")"
c_pin="$(trim "${c_pin:-}")"
c_sum="$(trim "${c_sum:-}")"

name="${c_file#\`}"; name="${name%\`}"

# ------------------------------------------------------- 1. the file exists
path="$BASE/$name"
if [ ! -f "$path" ]; then
fail "provenance row names \`$name\`, but no such file exists (looked in $BASE)"
continue
fi
ok "\`$name\` exists"
seen_files="$seen_files $(cd "$(dirname "$path")" && pwd)/$(basename "$path")"

# ---------------------------------------------- 2. the pin is a merged SHA
if [[ "$c_pin" =~ ^commit\ \`[0-9a-f]{12,40}\`\ \([0-9]{4}-[0-9]{2}-[0-9]{2}\)$ ]]; then
ok "\`$name\` is pinned at a bare upstream commit"
else
fail "\`$name\`: \"Pinned at\" is \"$c_pin\"; §8 needs a merged-commit pin and nothing else — 'commit \`<sha>\` (YYYY-MM-DD)'. A branch name or a 're-pin once that lands' note leaves a re-syncer with no SHA to diff against."
fi

# ------------------------------------- 3. the recorded checksum still holds
want="${c_sum#\`}"; want="${want%\`}"
if [ -z "$want" ]; then
fail "\`$name\`: the provenance row records no checksum, so a re-sync cannot be verified without the network"
continue
fi
if ! [[ "$want" =~ ^[0-9a-f]{32}$ ]]; then
fail "\`$name\`: recorded checksum \"$want\" is not a 32-hex md5"
continue
fi
got="$(md5_of "$path")"
if [ -z "$got" ]; then
echo "SKIP: no md5 tool available to verify \`$name\`"
continue
fi
if [ "$got" = "$want" ]; then
ok "\`$name\` matches its recorded md5 ($want)"
else
fail "\`$name\`: recorded md5 $want, file hashes to $got — the copy was refreshed without re-pinning the row (or was hand-edited, which §8 forbids)"
fi

# A row that forgot its upstream link is a row that cannot be re-synced.
case "$c_src" in
*corelib-c-cpp*) ok "\`$name\` names its upstream source" ;;
*) fail "\`$name\`: \"Upstream source\" cell does not name corelib-c-cpp, the authoritative source (§8)" ;;
esac
done <<< "$rows"

# ---------------------------------------- 4. every vendored file has a row

# The files this repo vendors, relative to test/shared/. Kept explicit so that
# deleting a table row cannot make the check pass by having nothing to check.
for required in sofab_test_json.c sofab_test_json.h ../../assets/test_vectors.json; do
abs="$(cd "$(dirname "$BASE/$required")" && pwd)/$(basename "$required")"
case "$seen_files" in
*" $abs"*) ok "$required is covered by a provenance row" ;;
*) fail "$required is vendored but has no provenance row in $README" ;;
esac
done

if [ "$row_count" -lt 3 ]; then
fail "only $row_count provenance row(s) — this repo vendors three files"
fi

if [ "$fails" -ne 0 ]; then
echo "test_vendored_provenance: $fails failure(s)" >&2
exit 1
fi
echo "test_vendored_provenance: all checks passed"