Files
dth/tests/e2e/dth_sp1_more.test.sh
T
xstudio agent 2082df948f test(dth:sp1-more): fix BSD wc whitespace in TDS token 3-parts assertion
BSD `wc -l` pads count with leading spaces on macOS (e.g. "       3"),
which made `[ '       3' = '3' ]` always false.

Verified: dth_sp1_more.test.sh 27/27 PASS on host after fix.
2026-09-28 21:10:05 +08:00

404 lines
23 KiB
Bash

#!/usr/bin/env bash
# dth_sp1_more.test.sh — e2e verification for S1..S12 of chg_0009
# (10 new real-algorithm operators + schema_version巡检 + 算子总数19).
#
# Exit 0 only if all 12 cases pass. Each assertion is a hard-fail.
#
# Usage:
# DTH_BASE=http://127.0.0.1:30000 bash tests/e2e/dth_sp1_more.test.sh
#
# Pre-req: backend is running on DTH_BASE and admin seed is loaded.
set -uo pipefail
BASE="${DTH_BASE:-http://127.0.0.1:${PORT:-30000}}"
LOGIN_URL="$BASE/api/ssw/dth/auth/login"
OPS_URL="$BASE/api/ssw/dth/experiment/operators"
RUNS_URL="$BASE/api/ssw/dth/experiment/runs"
pass=0; fail=0; fails=()
json_get() {
local field="$1"; local body="$2"
python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(d${field})" "$body" 2>/dev/null
}
login() {
curl -s -X POST "$LOGIN_URL" -H 'Content-Type: application/json' -d "{\"username\":\"$1\",\"password\":\"$2\"}"
}
assert() {
local id="$1"; local cond="$2"; local detail="$3"
if eval "$cond"; then
pass=$((pass+1))
printf " ✓ %s\n" "$id"
else
fail=$((fail+1))
fails+=("$id: $detail")
printf " ✗ %s — %s\n" "$id" "$detail"
fi
}
# ----- pre: login admin -----
ADMIN_BODY="$(login admin 123456)"
ADMIN_TOKEN="$(json_get "['data']['token']" "$ADMIN_BODY")"
assert "pre_admin_login" "[ -n '$ADMIN_TOKEN' ]" "admin token empty; login body=$ADMIN_BODY"
# ============================================================
# C1 (S1): paillier-he 加法同态不变性
# values=[10,20,30], pubkey 固定 → sum_decrypted = 60, invariant_ok = True
# ============================================================
PAI_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"paillier-he","input":{"values":[10,20,30],"seed":"paillier-c1"}}' "$RUNS_URL")"
PAI_ID="$(json_get "['data']['run_id']" "$PAI_BODY")"
PAI_STATUS="$(json_get "['data']['status']" "$PAI_BODY")"
assert "C1_paillier_run_status" "[ '$PAI_STATUS' = 'success' ]" "paillier status=$PAI_STATUS; body=$PAI_BODY"
PAI_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$PAI_ID")"
PAI_ASSERT="$(python3 -c "
import json,sys
d=json.loads(sys.argv[1])
p=d['data']['output_json']['paillier']
sv=d['data']['output_json']['schema_version']
ok = (p['sum_decrypted'] == 60 and p['homomorphic_invariant_ok'] is True and sv == 'v2')
print('true' if ok else 'false', p.get('sum_decrypted'), p.get('homomorphic_invariant_ok'), sv)
" "$PAI_GET" 2>/dev/null)"
PAI_OK="$(echo "$PAI_ASSERT" | awk '{print $1}')"
assert "C1_paillier_homomorphic_invariant" "[ '$PAI_OK' = 'true' ]" "paillier invariant: $PAI_ASSERT; body=$PAI_GET"
# ============================================================
# C2 (S2): threshold-sig 2-of-3 Lagrange 重构
# 2 share 重构成功;1 share 失败
# ============================================================
# Generate 3 Shamir shares for secret 0xdeadbeefcafe via Python helper
SHARES="$(python3 -c "
p = 0x1FFFFFFFFFFFFFFF # 2^61-1
secret = 0xdeadbeefcafe
import hashlib
r = int(hashlib.sha256(b'threshold-sig-c2-seed').hexdigest()[:16], 16) % (p - 1) + 1
shares = []
for x in [1, 2, 3]:
y = (secret + r * x) % p
# Use minimal hex (no zero-padding) to match operator's toHex.
shares.append({'x': x, 'y': hex(y)[2:]})
print(shares[0]['x'], shares[0]['y'], shares[1]['x'], shares[1]['y'], shares[2]['x'], shares[2]['y'])
")"
SX1="$(echo "$SHARES" | awk '{print $1}')"
SY1="$(echo "$SHARES" | awk '{print $2}')"
SX2="$(echo "$SHARES" | awk '{print $3}')"
SY2="$(echo "$SHARES" | awk '{print $4}')"
SX3="$(echo "$SHARES" | awk '{print $5}')"
SY3="$(echo "$SHARES" | awk '{print $6}')"
TS_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"threshold-sig\",\"input\":{\"message\":\"hello-threshold\",\"shares\":[{\"x\":$SX1,\"y\":\"$SY1\"},{\"x\":$SX2,\"y\":\"$SY2\"}],\"original_secret\":\"deadbeefcafe\"}}" "$RUNS_URL")"
TS_ID="$(json_get "['data']['run_id']" "$TS_BODY")"
TS_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TS_ID")"
TS_ASSERT="$(python3 -c "
import json,sys
d=json.loads(sys.argv[1])
t=d['data']['output_json']['threshold_sig']
sv=d['data']['output_json']['schema_version']
ok = (t['reconstructed_signature'] is not None and t['signature_match'] is True and sv == 'v2')
print('true' if ok else 'false')
" "$TS_GET" 2>/dev/null)"
assert "C2_threshold_sig_2shares_reconstruct" "[ '$TS_ASSERT' = 'true' ]" "2-share reconstruct: $TS_ASSERT; body=$TS_GET"
# 1 share alone → with_one_share_fails = True
TS_ONE_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"threshold-sig\",\"input\":{\"message\":\"hello-threshold\",\"shares\":[{\"x\":$SX1,\"y\":\"$SY1\"}]}}" "$RUNS_URL")"
TS_ONE_ID="$(json_get "['data']['run_id']" "$TS_ONE_BODY")"
TS_ONE_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TS_ONE_ID")"
TS_ONE_FAILS="$(json_get "['data']['output_json']['threshold_sig']['with_one_share_fails']" "$TS_ONE_GET")"
assert "C2_threshold_sig_1share_fails" "[ '$TS_ONE_FAILS' = 'True' ]" "1-share should fail; got with_one_share_fails=$TS_ONE_FAILS; body=$TS_ONE_GET"
# ============================================================
# C3 (S3): mpc-beaver z == xy (tolerance ≤ 1e-9)
# ============================================================
BEAVER_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"mpc-beaver","input":{"x":7,"y":11,"a":2,"b":3,"c":6}}' "$RUNS_URL")"
BEAVER_ID="$(json_get "['data']['run_id']" "$BEAVER_BODY")"
BEAVER_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$BEAVER_ID")"
BEAVER_ASSERT="$(python3 -c "
import json,sys
d=json.loads(sys.argv[1])
m=d['data']['output_json']['mpc']
sv=d['data']['output_json']['schema_version']
ok = (abs(m['z'] - 77.0) < 1e-9 and m['invariant_ok'] is True and abs(m['xy_ground_truth'] - 77.0) < 1e-9 and sv == 'v2')
print('true' if ok else 'false', m.get('z'), m.get('invariant_ok'))
" "$BEAVER_GET" 2>/dev/null)"
BEAVER_OK="$(echo "$BEAVER_ASSERT" | awk '{print $1}')"
assert "C3_beaver_xy_invariant" "[ '$BEAVER_OK' = 'true' ]" "beaver invariant: $BEAVER_ASSERT; body=$BEAVER_GET"
# ============================================================
# C4 (S4): psi cuckoo PSI intersection = {3,4,5}
# ============================================================
PSI_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"psi","input":{"set_a":[1,2,3,4,5],"set_b":[3,4,5,6,7],"hash_key":"psi-c4"}}' "$RUNS_URL")"
PSI_ID="$(json_get "['data']['run_id']" "$PSI_BODY")"
PSI_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$PSI_ID")"
PSI_ASSERT="$(python3 -c "
import json,sys
d=json.loads(sys.argv[1])
p=d['data']['output_json']['psi']
sv=d['data']['output_json']['schema_version']
ok = (sorted(p['intersection']) == ['3','4','5'] and p['size'] == 3 and p['leaked_non_intersection'] is False and sv == 'v2')
print('true' if ok else 'false', p.get('intersection'), p.get('size'))
" "$PSI_GET" 2>/dev/null)"
PSI_OK="$(echo "$PSI_ASSERT" | awk '{print $1}')"
assert "C4_psi_intersection_correct" "[ '$PSI_OK' = 'true' ]" "psi intersection: $PSI_ASSERT; body=$PSI_GET"
# ============================================================
# C5 (S5): did-identity create + verify
# ============================================================
DID_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"did-identity","input":{"method":"dth","identifier":"alice","secret":"dth-secret-c5"}}' "$RUNS_URL")"
DID_ID="$(json_get "['data']['run_id']" "$DID_BODY")"
DID_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$DID_ID")"
DID_ASSERT="$(python3 -c "
import json,sys
d=json.loads(sys.argv[1])
x=d['data']['output_json']['did']
sv=d['data']['output_json']['schema_version']
ok = (x['did'] == 'did:dth:alice' and x['verify_with_correct_secret'] is True and x['verify_with_wrong_secret'] is False and sv == 'v2')
print('true' if ok else 'false')
" "$DID_GET" 2>/dev/null)"
assert "C5_did_create_verify" "[ '$DID_ASSERT' = 'true' ]" "did verify: $DID_ASSERT; body=$DID_GET"
# ============================================================
# C6 (S6): tds-connector match → token; mismatch → error
# ============================================================
TDS_MATCH_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"tds-connector","input":{"contract_hash_a":"contract-v1","contract_hash_b":"contract-v1","subject":"party-a","scope":"read"}}' "$RUNS_URL")"
TDS_MATCH_ID="$(json_get "['data']['run_id']" "$TDS_MATCH_BODY")"
TDS_MATCH_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TDS_MATCH_ID")"
TDS_MATCH_TOKEN="$(json_get "['data']['output_json']['tds']['access_token']" "$TDS_MATCH_GET")"
TDS_MATCH_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$TDS_MATCH_GET")"
assert "C6_tds_match_token_nonempty" "[ -n '$TDS_MATCH_TOKEN' ]" "tds match token empty; body=$TDS_MATCH_GET"
assert "C6_tds_match_schema_v2" "[ '$TDS_MATCH_SCHEMA' = 'v2' ]" "tds match schema=$TDS_MATCH_SCHEMA"
# Verify JWT-like token structure externally (header.payload.signature)
TDS_TOKEN_PARTS="$(echo "$TDS_MATCH_TOKEN" | tr '.' '\n' | wc -l | tr -d ' ')"
assert "C6_tds_match_token_3parts" "[ '$TDS_TOKEN_PARTS' = '3' ]" "expected 3-part JWT-like token, got $TDS_TOKEN_PARTS parts"
TDS_MISMATCH_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"tds-connector","input":{"contract_hash_a":"contract-v1","contract_hash_b":"contract-different"}}' "$RUNS_URL")"
TDS_MISMATCH_ID="$(json_get "['data']['run_id']" "$TDS_MISMATCH_BODY")"
TDS_MISMATCH_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TDS_MISMATCH_ID")"
TDS_MISMATCH_ERR="$(json_get "['data']['output_json']['tds']['error']" "$TDS_MISMATCH_GET")"
assert "C6_tds_mismatch_error" "[ '$TDS_MISMATCH_ERR' = 'contract-mismatch' ]" "expected contract-mismatch, got $TDS_MISMATCH_ERR; body=$TDS_MISMATCH_GET"
# ============================================================
# C7 (S7): data-masking field-aware
# phone / id_card / email 三种字段
# ============================================================
MASK_PHONE_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"data-masking","input":{"field_type":"phone","value":"13800138000"}}' "$RUNS_URL")"
MASK_PHONE_ID="$(json_get "['data']['run_id']" "$MASK_PHONE_BODY")"
MASK_PHONE_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MASK_PHONE_ID")"
MASK_PHONE_OUT="$(json_get "['data']['output_json']['masking']['masked']" "$MASK_PHONE_GET")"
assert "C7_mask_phone" "[ '$MASK_PHONE_OUT' = '138****8000' ]" "phone masked=$MASK_PHONE_OUT; body=$MASK_PHONE_GET"
MASK_ID_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"data-masking","input":{"field_type":"id_card","value":"110101199001011234"}}' "$RUNS_URL")"
MASK_ID_ID="$(json_get "['data']['run_id']" "$MASK_ID_BODY")"
MASK_ID_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MASK_ID_ID")"
MASK_ID_OUT="$(json_get "['data']['output_json']['masking']['masked']" "$MASK_ID_GET")"
assert "C7_mask_id_card" "[ '$MASK_ID_OUT' = '110101********1234' ]" "id_card masked=$MASK_ID_OUT; body=$MASK_ID_GET"
MASK_EMAIL_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"data-masking","input":{"field_type":"email","value":"[email protected]"}}' "$RUNS_URL")"
MASK_EMAIL_ID="$(json_get "['data']['run_id']" "$MASK_EMAIL_BODY")"
MASK_EMAIL_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MASK_EMAIL_ID")"
MASK_EMAIL_OUT="$(json_get "['data']['output_json']['masking']['masked']" "$MASK_EMAIL_GET")"
assert "C7_mask_email" "[ '$MASK_EMAIL_OUT' = 'al**@example.com' ]" "email masked=$MASK_EMAIL_OUT; body=$MASK_EMAIL_GET"
MASK_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$MASK_PHONE_GET")"
assert "C7_mask_schema_v2" "[ '$MASK_SCHEMA' = 'v2' ]" "schema=$MASK_SCHEMA"
# ============================================================
# C8 (S8): data-watermark zero-width embed + extract roundtrip
# ============================================================
WM_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"data-watermark","input":{"text":"hello world","payload":"secret-001","action":"embed"}}' "$RUNS_URL")"
WM_ID="$(json_get "['data']['run_id']" "$WM_BODY")"
WM_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$WM_ID")"
WM_ASSERT="$(python3 -c "
import json,sys
d=json.loads(sys.argv[1])
w=d['data']['output_json']['watermark']
sv=d['data']['output_json']['schema_version']
expected_len = len('hello world') + len('secret-001') * 4
ok = (w['length_invariant_ok'] is True and len(w['watermarked_text']) == expected_len and w['zero_width_chars_appended'] == len('secret-001') * 4 and sv == 'v2')
print('true' if ok else 'false', len(w['watermarked_text']), w['zero_width_chars_appended'])
" "$WM_GET" 2>/dev/null)"
WM_OK="$(echo "$WM_ASSERT" | awk '{print $1}')"
assert "C8_watermark_embed_length_invariant" "[ '$WM_OK' = 'true' ]" "watermark embed: $WM_ASSERT; body=$WM_GET"
# Extract roundtrip: read watermarked_text via stdin to avoid shell quoting hell.
WM_GET_FILE="/tmp/dth_sp1_more_wm_get.json"
printf '%s' "$WM_GET" > "$WM_GET_FILE"
WM_TEXT_FILE="/tmp/dth_sp1_more_wm_text.txt"
python3 -c "
import json, sys
d=json.load(open(sys.argv[1]))
sys.stdout.write(d['data']['output_json']['watermark']['watermarked_text'])
" "$WM_GET_FILE" > "$WM_TEXT_FILE"
# Build the extract request payload via python (handles unicode correctly)
WM_EXT_REQ_FILE="/tmp/dth_sp1_more_wm_extract.json"
python3 <<PYEOF > "$WM_EXT_REQ_FILE"
import json
with open("$WM_TEXT_FILE",'r') as f:
text = f.read()
print(json.dumps({'operator':'data-watermark','input':{'text':text,'action':'extract'}}))
PYEOF
WM_EXT_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
--data-binary "@$WM_EXT_REQ_FILE" "$RUNS_URL")"
WM_EXT_ID="$(json_get "['data']['run_id']" "$WM_EXT_BODY")"
WM_EXT_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$WM_EXT_ID")"
WM_EXT_OUT="$(json_get "['data']['output_json']['watermark']['extracted_payload']" "$WM_EXT_GET")"
assert "C8_watermark_extract_roundtrip" "[ '$WM_EXT_OUT' = 'secret-001' ]" "watermark extract=$WM_EXT_OUT; body=$WM_EXT_GET"
# ============================================================
# C9 (S9): abac-policy decision matrix (3 cases)
# ============================================================
ABAC_ADMIN_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"abac-policy","input":{"subject":{"role":"admin"},"resource":{"sensitivity":"high"},"env":{},"policy":{"allow_if":{"subject.role":"admin"}}}}' "$RUNS_URL")"
ABAC_ADMIN_ID="$(json_get "['data']['run_id']" "$ABAC_ADMIN_BODY")"
ABAC_ADMIN_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_ADMIN_ID")"
ABAC_ADMIN_DECISION="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_ADMIN_GET")"
assert "C9_abac_admin_allow" "[ '$ABAC_ADMIN_DECISION' = 'allow' ]" "admin should be allowed; got=$ABAC_ADMIN_DECISION; body=$ABAC_ADMIN_GET"
ABAC_GUEST_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"abac-policy","input":{"subject":{"role":"guest"},"resource":{"sensitivity":"high"},"env":{},"policy":{"allow_if":{"subject.role":"admin"}}}}' "$RUNS_URL")"
ABAC_GUEST_ID="$(json_get "['data']['run_id']" "$ABAC_GUEST_BODY")"
ABAC_GUEST_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_GUEST_ID")"
ABAC_GUEST_DECISION="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_GUEST_GET")"
assert "C9_abac_guest_deny" "[ '$ABAC_GUEST_DECISION' = 'deny' ]" "guest should be denied; got=$ABAC_GUEST_DECISION; body=$ABAC_GUEST_GET"
# Env constraint violation: outside time window. Pass env.time="22:00",
# policy has env_constraints.time_window="09:00-09:01" (tiny window).
ABAC_TIME_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"abac-policy","input":{"subject":{"role":"admin"},"resource":{"sensitivity":"high"},"env":{"time":"22:00"},"policy":{"allow_if":{"subject.role":"admin"},"env_constraints":{"time_window":"09:00-09:01"}}}}' "$RUNS_URL")"
ABAC_TIME_ID="$(json_get "['data']['run_id']" "$ABAC_TIME_BODY")"
ABAC_TIME_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_TIME_ID")"
ABAC_TIME_DECISION="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_TIME_GET")"
assert "C9_abac_timewindow_deny" "[ '$ABAC_TIME_DECISION' = 'deny' ]" "outside-window should be denied; got=$ABAC_TIME_DECISION; body=$ABAC_TIME_GET"
ABAC_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$ABAC_ADMIN_GET")"
assert "C9_abac_schema_v2" "[ '$ABAC_SCHEMA' = 'v2' ]" "schema=$ABAC_SCHEMA"
# ============================================================
# C10 (S10): model-watermark LSB embed + extract roundtrip
# payload "wmX" (3 ASCII chars = 24 bits) → 24 positions modified,
# weights length 32, fits 24 bits.
# ============================================================
MW_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"model-watermark","input":{"weights":[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3.0,3.1,3.2],"payload":"wmX","action":"embed"}}' "$RUNS_URL")"
MW_ID="$(json_get "['data']['run_id']" "$MW_BODY")"
MW_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MW_ID")"
MW_ASSERT="$(python3 -c "
import json,sys
d=json.loads(sys.argv[1])
w=d['data']['output_json']['watermark']
sv=d['data']['output_json']['schema_version']
ww = w['watermarked_weights']
expected_mod = len('wmX') * 8 # 24
ok = (w['bit_positions_modified_count'] == expected_mod and len(ww) == 32 and sv == 'v2')
print('true' if ok else 'false', w.get('bit_positions_modified_count'), len(ww), sv)
" "$MW_GET" 2>/dev/null)"
MW_OK="$(echo "$MW_ASSERT" | awk '{print $1}')"
assert "C10_model_watermark_embed_invariant" "[ '$MW_OK' = 'true' ]" "model watermark embed: $MW_ASSERT; body=$MW_GET"
# Extract roundtrip from watermarked_weights with byte_count=3
MW_GET_FILE="/tmp/dth_sp1_more_mw_get.json"
printf '%s' "$MW_GET" > "$MW_GET_FILE"
MW_WW_FILE="/tmp/dth_sp1_more_mw_ww.json"
python3 -c "
import json, sys
d=json.load(open(sys.argv[1]))
sys.stdout.write(json.dumps(d['data']['output_json']['watermark']['watermarked_weights']))
" "$MW_GET_FILE" > "$MW_WW_FILE"
MW_EXT_REQ_FILE="/tmp/dth_sp1_more_mw_extract.json"
python3 <<PYEOF > "$MW_EXT_REQ_FILE"
import json
with open("$MW_WW_FILE",'r') as f:
ww = json.load(f)
print(json.dumps({'operator':'model-watermark','input':{'weights':ww,'action':'extract','byte_count':3}}))
PYEOF
MW_EXT_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
--data-binary "@$MW_EXT_REQ_FILE" "$RUNS_URL")"
MW_EXT_ID="$(json_get "['data']['run_id']" "$MW_EXT_BODY")"
MW_EXT_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MW_EXT_ID")"
MW_EXT_OUT="$(json_get "['data']['output_json']['watermark']['extracted_payload']" "$MW_EXT_GET")"
assert "C10_model_watermark_extract_roundtrip" "[ '$MW_EXT_OUT' = 'wmX' ]" "model extract=$MW_EXT_OUT; body=$MW_EXT_GET"
# ============================================================
# C11 (S11): schema_version = v2 持续 — 全 10 个新算子
# ============================================================
C11_OK=1
C11_DETAIL=""
for OP in paillier-he threshold-sig mpc-beaver psi did-identity tds-connector data-masking data-watermark abac-policy model-watermark; do
case "$OP" in
paillier-he) INPUT='{"values":[1,2],"seed":"c11"}' ;;
threshold-sig) INPUT='{"message":"c11","shares":[{"x":1,"y":"000000000000002a"},{"x":2,"y":"0000000000000054"}]}' ;;
mpc-beaver) INPUT='{"x":2,"y":3,"a":1,"b":1,"c":1}' ;;
psi) INPUT='{"set_a":[1,2],"set_b":[2,3],"hash_key":"c11"}' ;;
did-identity) INPUT='{"method":"dth","identifier":"c11","secret":"c11"}' ;;
tds-connector) INPUT='{"contract_hash_a":"c11","contract_hash_b":"c11"}' ;;
data-masking) INPUT='{"field_type":"phone","value":"13800138000"}' ;;
data-watermark) INPUT='{"text":"c11","payload":"c11","action":"embed"}' ;;
abac-policy) INPUT='{"subject":{"role":"admin"},"resource":{},"env":{},"policy":{"allow_if":{"subject.role":"admin"}}}' ;;
model-watermark) INPUT='{"weights":[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0,2.1,2.2,2.3,2.4],"payload":"c11","action":"embed"}' ;;
esac
C11_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"$OP\",\"input\":$INPUT}" "$RUNS_URL")"
C11_RID="$(json_get "['data']['run_id']" "$C11_BODY")"
C11_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$C11_RID")"
C11_SV="$(json_get "['data']['output_json']['schema_version']" "$C11_GET")"
if [ "$C11_SV" != "v2" ]; then
C11_OK=0
C11_DETAIL="$C11_DETAIL $OP=$C11_SV"
fi
done
assert "C11_schema_version_v2_all_10" "[ '$C11_OK' = '1' ]" "schema_version巡检未全部 v2:$C11_DETAIL"
# ============================================================
# C12 (S12): 算子总数 = 19(含全部 10 个新算子)
# ============================================================
LIST_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$OPS_URL")"
LIST_ASSERT="$(python3 -c "
import json,sys
ops=json.loads(sys.argv[1])['data']
codes={o['code'] for o in ops}
required={'paillier-he','threshold-sig','mpc-beaver','psi','did-identity','tds-connector','data-masking','data-watermark','abac-policy','model-watermark'}
missing=required - codes
ok = (len(ops) >= 19 and not missing)
print('true' if ok else 'false', len(ops), sorted(missing))
" "$LIST_BODY" 2>/dev/null)"
LIST_OK="$(echo "$LIST_ASSERT" | awk '{print $1}')"
assert "C12_operators_list_19" "[ '$LIST_OK' = 'true' ]" "operator list: $LIST_ASSERT"
# Verify all 10 new algorithms have -v2 suffix in their meta.algorithm
ALG_ASSERT="$(python3 -c "
import json,sys
ops=json.loads(sys.argv[1])['data']
new_codes={'paillier-he','threshold-sig','mpc-beaver','psi','did-identity','tds-connector','data-masking','data-watermark','abac-policy','model-watermark'}
bad=[]
for o in ops:
if o['code'] in new_codes:
a=o.get('algorithm','')
if not a.endswith('-v2'):
bad.append(o['code']+'='+a)
print('true' if not bad else 'false', '|'.join(bad))
" "$LIST_BODY" 2>/dev/null)"
ALG_OK="$(echo "$ALG_ASSERT" | awk '{print $1}')"
assert "C12_algorithms_v2_suffix" "[ '$ALG_OK' = 'true' ]" "alg v2 suffix: $ALG_ASSERT"
echo "---"
echo "e2e dth_sp1_more: pass=$pass fail=$fail"
if [ $fail -gt 0 ]; then
printf 'FAILED:\n'; printf ' - %s\n' "${fails[@]}"
exit 1
fi
exit 0