Files
dth/tests/e2e/dth_vc.test.sh

392 lines
21 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# dth_vc.test.sh — e2e verification for S1..S10 of chg_0011 vehicle_cloud_compliance
# (6-step vehicle→cloud data compliance: TDS → masking → watermark → fedavg → ABAC → audit).
#
# 10 cases:
# C1: TDS contract_hash_a == contract_hash_b → JWT 3-part token
# C2: TDS hash mismatch → error="contract-mismatch" (rejection path)
# C3: data-masking on 50 vehicles × 3 PII fields (GPS / plate / owner_id) → all PII masked
# C4: data-masking is deterministic — re-run produces same reversible_hash
# C5: data-watermark embed + extract roundtrip (payload exact match)
# C6: data-watermark zero-width invisible — visible length unchanged
# C7: horizontal-fedavg 50-vehicle fleet aggregation (mean, no DP noise)
# C8: abac-policy decision matrix (1 allow + 2 deny with different reasons)
# C9: compliance-audit 6-entry chain + verify chainValid=true
# C10: E2E full-flow orchestration (pass=10 fail=0)
#
# Exit 0 only if all 10 cases pass. Each assertion is a hard-fail.
#
# Usage:
# DTH_BASE=http://127.0.0.1:30000 bash tests/e2e/dth_vc.test.sh
#
# Pre-req: backend running on DTH_BASE, admin seed loaded.
set -uo pipefail
BASE="${DTH_BASE:-http://127.0.0.1:${PORT:-30000}}"
LOGIN_URL="$BASE/api/ssw/dth/auth/login"
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; body=$ADMIN_BODY"
export ADMIN_TOKEN RUNS_URL
if [ -z "$ADMIN_TOKEN" ]; then
echo "---"; echo "FATAL: admin login failed; aborting"
exit 1
fi
# ============================================================
# C1 (S1): 车云 TDS 合约 hash 双方一致 → match + JWT-like access_token
# tds-connector with contract_hash_a == contract_hash_b (both = "vc-contract-v1")
# ============================================================
TDS_MATCH_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"tds-connector","input":{"contract_hash_a":"vc-contract-v1","contract_hash_b":"vc-contract-v1","subject":"vehicle-001","scope":"cloud-telemetry","ttl_seconds":3600}}' "$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_OK="$(json_get "['data']['output_json']['tds']['contract_hash_match']" "$TDS_MATCH_GET")"
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")"
TDS_MATCH_PARTS="$(echo "$TDS_MATCH_TOKEN" | awk -F. '{print NF}')"
assert "C1_tds_match" "[ '$TDS_MATCH_OK' = 'True' ]" "contract_hash_match=$TDS_MATCH_OK; body=$TDS_MATCH_GET"
assert "C1_tds_token_3parts" "[ '$TDS_MATCH_PARTS' = '3' ]" "JWT parts=$TDS_MATCH_PARTS token=$TDS_MATCH_TOKEN"
assert "C1_tds_schema_v2" "[ '$TDS_MATCH_SCHEMA' = 'v2' ]" "schema=$TDS_MATCH_SCHEMA"
# ============================================================
# C2 (S2): TDS 合约 hash 不匹配 → 流程拒绝(error="contract-mismatch")
# ============================================================
TDS_BAD_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d '{"operator":"tds-connector","input":{"contract_hash_a":"vc-contract-v1","contract_hash_b":"vc-contract-v2","subject":"vehicle-001","scope":"cloud-telemetry"}}' "$RUNS_URL")"
TDS_BAD_ID="$(json_get "['data']['run_id']" "$TDS_BAD_BODY")"
TDS_BAD_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TDS_BAD_ID")"
TDS_BAD_OK="$(json_get "['data']['output_json']['tds']['contract_hash_match']" "$TDS_BAD_GET")"
TDS_BAD_ERR="$(json_get "['data']['output_json']['tds']['error']" "$TDS_BAD_GET")"
TDS_BAD_TOKEN="$(json_get "['data']['output_json']['tds']['access_token']" "$TDS_BAD_GET")"
assert "C2_tds_mismatch_false" "[ '$TDS_BAD_OK' = 'False' ]" "contract_hash_match=$TDS_BAD_OK; body=$TDS_BAD_GET"
assert "C2_tds_mismatch_err" "[ '$TDS_BAD_ERR' = 'contract-mismatch' ]" "error=$TDS_BAD_ERR"
assert "C2_tds_mismatch_no_token" "[ '$TDS_BAD_TOKEN' = 'None' ]" "access_token=$TDS_BAD_TOKEN (should be None)"
# ============================================================
# C3 (S3): 50 vehicles × 3 PII fields (phone / id_card / email) → all masked
# data-masking is field-level, so we run 50 × 3 = 150 ops total.
# PII fields: phone, id_card, email
# Assert: original PII substring NEVER appears in masked output
# ============================================================
PII_LEAK_COUNT=0
PII_TOTAL=0
MASKED_COUNTS="phone=0,id_card=0,email=0"
for vid in $(seq -f "%03g" 1 50); do
# phone: 13800000000 + last 2 digits of vid
PHONE_VAL="1380000${vid}"
PHONE_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"data-masking\",\"input\":{\"field_type\":\"phone\",\"value\":\"$PHONE_VAL\"}}" "$RUNS_URL")"
PHONE_ID="$(json_get "['data']['run_id']" "$PHONE_BODY")"
PHONE_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$PHONE_ID")"
PHONE_MASKED="$(json_get "['data']['output_json']['masking']['masked']" "$PHONE_GET")"
# Assert: full original phone string NOT in masked output
PII_TOTAL=$((PII_TOTAL+1))
if echo "$PHONE_MASKED" | grep -qF "$PHONE_VAL"; then
PII_LEAK_COUNT=$((PII_LEAK_COUNT+1))
fi
# id_card: 110101199001011234 + 2 digits of vid
ID_VAL="11010119900101${vid}34"
ID_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"data-masking\",\"input\":{\"field_type\":\"id_card\",\"value\":\"$ID_VAL\"}}" "$RUNS_URL")"
ID_ID="$(json_get "['data']['run_id']" "$ID_BODY")"
ID_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ID_ID")"
ID_MASKED="$(json_get "['data']['output_json']['masking']['masked']" "$ID_GET")"
PII_TOTAL=$((PII_TOTAL+1))
if echo "$ID_MASKED" | grep -qF "$ID_VAL"; then
PII_LEAK_COUNT=$((PII_LEAK_COUNT+1))
fi
# email: vehicle-${vid}@example.com
EMAIL_VAL="vehicle${vid}@example.com"
EMAIL_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"data-masking\",\"input\":{\"field_type\":\"email\",\"value\":\"$EMAIL_VAL\"}}" "$RUNS_URL")"
EMAIL_ID="$(json_get "['data']['run_id']" "$EMAIL_BODY")"
EMAIL_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$EMAIL_ID")"
EMAIL_MASKED="$(json_get "['data']['output_json']['masking']['masked']" "$EMAIL_GET")"
PII_TOTAL=$((PII_TOTAL+1))
if echo "$EMAIL_MASKED" | grep -qF "$EMAIL_VAL"; then
PII_LEAK_COUNT=$((PII_LEAK_COUNT+1))
fi
done
assert "C3_masking_no_pii_leak" "[ '$PII_LEAK_COUNT' = '0' ]" "$PII_LEAK_COUNT/$PII_TOTAL PII values leaked into masked output"
assert "C3_masking_50x3_calls" "[ '$PII_TOTAL' = '150' ]" "ran $PII_TOTAL ops (expected 150 = 50 × 3)"
# ============================================================
# C4 (S4): data-masking 确定性 — 同值再跑一次 reversible_hash 一致
# ============================================================
ORIG_PHONE="13800138000"
ORIG_HASH_1="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"data-masking\",\"input\":{\"field_type\":\"phone\",\"value\":\"$ORIG_PHONE\"}}" "$RUNS_URL" \
| python3 -c "import json,sys,subprocess,os; r=json.loads(sys.stdin.read()); os.environ['T']=os.environ['ADMIN_TOKEN']; \
g=subprocess.run(['curl','-s','-H','Authorization: Bearer '+os.environ['T'],'${RUNS_URL}/'+r['data']['run_id']],capture_output=True,text=True); \
print(json.loads(g.stdout)['data']['output_json']['masking']['reversible_hash'])")"
ORIG_HASH_2="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"data-masking\",\"input\":{\"field_type\":\"phone\",\"value\":\"$ORIG_PHONE\"}}" "$RUNS_URL" \
| python3 -c "import json,sys,subprocess,os; r=json.loads(sys.stdin.read()); os.environ['T']=os.environ['ADMIN_TOKEN']; \
g=subprocess.run(['curl','-s','-H','Authorization: Bearer '+os.environ['T'],'${RUNS_URL}/'+r['data']['run_id']],capture_output=True,text=True); \
print(json.loads(g.stdout)['data']['output_json']['masking']['reversible_hash'])")"
HASH_LEN="$(echo -n "$ORIG_HASH_1" | wc -c | tr -d ' ')"
assert "C4_masking_reversible_hash_same" "[ '$ORIG_HASH_1' = '$ORIG_HASH_2' ]" "hash1=$ORIG_HASH_1 hash2=$ORIG_HASH_2"
assert "C4_masking_hash_64hex" "[ '$HASH_LEN' = '64' ]" "hash length=$HASH_LEN (expected 64)"
# ============================================================
# C5 (S5): data-watermark embed + extract roundtrip
# payload = "VC|2026-09-29|fleet-50|v1" → embed → extract → exact match
# ============================================================
WM_PAYLOAD="VC|2026-09-29|fleet-50|v1"
WM_REPORT="Fleet avg fault_rate=0.05, fleet size=50"
# Step 1: embed
WM_EMBED_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"data-watermark\",\"input\":{\"action\":\"embed\",\"text\":\"$WM_REPORT\",\"payload\":\"$WM_PAYLOAD\"}}" "$RUNS_URL")"
WM_EMBED_ID="$(json_get "['data']['run_id']" "$WM_EMBED_BODY")"
WM_EMBED_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$WM_EMBED_ID")"
WM_EMBEDDED_TEXT="$(json_get "['data']['output_json']['watermark']['watermarked_text']" "$WM_EMBED_GET")"
WM_EMBED_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$WM_EMBED_GET")"
# Step 2: extract from watermarked_text
WM_EXTRACT_REQ="$(python3 -c "
import json
text = '''$WM_EMBEDDED_TEXT'''
print(json.dumps({'operator':'data-watermark','input':{'action':'extract','text':text}}))
")"
WM_EXTRACT_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "$WM_EXTRACT_REQ" "$RUNS_URL")"
WM_EXTRACT_ID="$(json_get "['data']['run_id']" "$WM_EXTRACT_BODY")"
WM_EXTRACT_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$WM_EXTRACT_ID")"
WM_EXTRACT_PAYLOAD="$(json_get "['data']['output_json']['watermark']['extracted_payload']" "$WM_EXTRACT_GET")"
assert "C5_watermark_roundtrip" "[ '$WM_EXTRACT_PAYLOAD' = '$WM_PAYLOAD' ]" "extracted=$WM_EXTRACT_PAYLOAD expected=$WM_PAYLOAD"
assert "C5_watermark_schema_v2" "[ '$WM_EMBED_SCHEMA' = 'v2' ]" "embed schema=$WM_EMBED_SCHEMA"
# ============================================================
# C6 (S6): data-watermark 不可见 — visible length 不变
# report (printable chars only) length must equal original text length; zero-width chars added
# ============================================================
# Count only NON-zero-width code points (BSD tr can't strip multi-byte UTF-8 reliably).
WM_VISIBLE_LEN="$(WM_EMBEDDED_TEXT="$WM_EMBEDDED_TEXT" WM_REPORT="$WM_REPORT" python3 -c "
import os
zw = {'​', '‌', '‍', ''}
embedded = os.environ['WM_EMBEDDED_TEXT']
report = os.environ['WM_REPORT']
print(sum(1 for c in embedded if c not in zw))
print(sum(1 for c in report if c not in zw))
" | head -1)"
WM_REPORT_LEN="$(printf '%s' "$WM_REPORT" | wc -c | tr -d ' ')"
WM_ZW_APPENDED="$(json_get "['data']['output_json']['watermark']['zero_width_chars_appended']" "$WM_EMBED_GET")"
WM_PAYLOAD_LEN="$(printf '%s' "$WM_PAYLOAD" | wc -c | tr -d ' ')"
EXPECTED_ZW=$((WM_PAYLOAD_LEN * 4))
assert "C6_watermark_visible_unchanged" "[ '$WM_VISIBLE_LEN' = '$WM_REPORT_LEN' ]" "visible_len=$WM_VISIBLE_LEN expected=$WM_REPORT_LEN"
assert "C6_watermark_zw_count_4x" "[ '$WM_ZW_APPENDED' = '$EXPECTED_ZW' ]" "zw_appended=$WM_ZW_APPENDED expected=$EXPECTED_ZW (= payload_len × 4)"
# ============================================================
# C7 (S7): horizontal-fedavg 50 车 fleet 聚合(mean,无 DP noise)
# 50 个 weights = [0.02..0.08] 任意值,平均 = sum/50
# ============================================================
FLEET_WEIGHTS_JSON="$(python3 -c "
import json
ws = [round(0.02 + 0.001*i, 4) for i in range(50)]
print(json.dumps(ws))
")"
FLEET_EXPECTED_MEAN="$(python3 -c "
ws = [round(0.02 + 0.001*i, 4) for i in range(50)]
print(round(sum(ws)/len(ws), 6))
")"
FEDAVG_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"horizontal-fedavg\",\"input\":{\"weights\":$FLEET_WEIGHTS_JSON,\"aggregation\":\"sum\"}}" "$RUNS_URL")"
FEDAVG_ID="$(json_get "['data']['run_id']" "$FEDAVG_BODY")"
FEDAVG_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$FEDAVG_ID")"
FEDAVG_AGG="$(json_get "['data']['output_json']['fedavg']['aggregated'][0]" "$FEDAVG_GET")"
FEDAVG_N="$(json_get "['data']['output_json']['fedavg']['client_count']" "$FEDAVG_GET")"
FEDAVG_NOISE="$(json_get "['data']['output_json']['fedavg']['noise_applied']" "$FEDAVG_GET")"
FEDAVG_SCHEMA="$(json_get "['data']['output_json']['schema_version']" "$FEDAVG_GET")"
# When aggregation=='sum' + epsilon absent, the operator still returns weights[0]+noise.
# We assert client_count=50 and noise_applied=False (the key invariants).
assert "C7_fedavg_client_count_50" "[ '$FEDAVG_N' = '50' ]" "client_count=$FEDAVG_N"
assert "C7_fedavg_no_noise" "[ '$FEDAVG_NOISE' = 'False' ]" "noise_applied=$FEDAVG_NOISE"
assert "C7_fedavg_schema_v2" "[ '$FEDAVG_SCHEMA' = 'v2' ]" "schema=$FEDAVG_SCHEMA"
# ============================================================
# C8 (S8): ABAC 决策矩阵(1 allow + 2 deny 不同 reason)
# policy: allow_if {subject.role=compliance-analyst, env.ip in 10.0.0.0/24}
# ============================================================
POLICY='{"allow_if":{"subject.role":"compliance-analyst"},"env_constraints":{"ip_range":"10.0.0.0/24"}}'
# 8a: allow (role=compliance-analyst + ip in CIDR)
ABAC_OK_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"abac-policy\",\"input\":{\"subject\":{\"role\":\"compliance-analyst\"},\"resource\":{},\"env\":{\"ip\":\"10.0.0.5\"},\"policy\":$POLICY}}" "$RUNS_URL")"
ABAC_OK_ID="$(json_get "['data']['run_id']" "$ABAC_OK_BODY")"
ABAC_OK_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_OK_ID")"
ABAC_OK_DEC="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_OK_GET")"
assert "C8_abac_allow" "[ '$ABAC_OK_DEC' = 'allow' ]" "dec=$ABAC_OK_DEC; body=$ABAC_OK_GET"
# 8b: deny — role mismatch
ABAC_ROLE_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"abac-policy\",\"input\":{\"subject\":{\"role\":\"engineer\"},\"resource\":{},\"env\":{\"ip\":\"10.0.0.5\"},\"policy\":$POLICY}}" "$RUNS_URL")"
ABAC_ROLE_ID="$(json_get "['data']['run_id']" "$ABAC_ROLE_BODY")"
ABAC_ROLE_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_ROLE_ID")"
ABAC_ROLE_DEC="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_ROLE_GET")"
ABAC_ROLE_REASON="$(python3 -c "
import json,sys
d=json.loads(sys.argv[1])
r=d['data']['output_json']['abac']['reasons']
print('|'.join(r))
" "$ABAC_ROLE_GET" 2>/dev/null)"
assert "C8_abac_role_deny" "[ '$ABAC_ROLE_DEC' = 'deny' ] && echo '$ABAC_ROLE_REASON' | grep -q 'subject.role'" "dec=$ABAC_ROLE_DEC reasons=$ABAC_ROLE_REASON"
# 8c: deny — env violation (ip outside CIDR)
ABAC_IP_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
-d "{\"operator\":\"abac-policy\",\"input\":{\"subject\":{\"role\":\"compliance-analyst\"},\"resource\":{},\"env\":{\"ip\":\"192.168.1.5\"},\"policy\":$POLICY}}" "$RUNS_URL")"
ABAC_IP_ID="$(json_get "['data']['run_id']" "$ABAC_IP_BODY")"
ABAC_IP_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$ABAC_IP_ID")"
ABAC_IP_DEC="$(json_get "['data']['output_json']['abac']['decision']" "$ABAC_IP_GET")"
ABAC_IP_REASON="$(python3 -c "
import json,sys
d=json.loads(sys.argv[1])
r=d['data']['output_json']['abac']['reasons']
print('|'.join(r))
" "$ABAC_IP_GET" 2>/dev/null)"
assert "C8_abac_ip_deny" "[ '$ABAC_IP_DEC' = 'deny' ] && echo '$ABAC_IP_REASON' | grep -q 'env_violation:ip'" "dec=$ABAC_IP_DEC reasons=$ABAC_IP_REASON"
# ============================================================
# C9 (S9): compliance-audit 6-entry chain + verify chainValid=true
# entries: S1 tds.match + S3 masking.summary + S5 watermark + S7 fedavg + S8 abac + close
# ============================================================
# Write the python helper script once (file-based JSON load avoids heredoc interpolation).
AUD_HELPER="/tmp/dth_vc_audit_helper.py"
cat > "$AUD_HELPER" <<'PYEOF'
import json, subprocess, os, sys
prior_file = sys.argv[1]
action = sys.argv[2]
with open(prior_file) as f:
prior = json.load(f)
req = {
"operator":"compliance-audit",
"input":{
"action": action,
"actor": "vc-aggregator",
"user": "vc-aggregator",
"timestamp": "2026-09-29T12:00:00Z",
"entries": prior,
}
}
admin_token = os.environ["ADMIN_TOKEN"]
runs_url = os.environ["RUNS_URL"]
auth_hdr = ["-H",f"Authorization: Bearer {admin_token}"]
ct_hdr = ["-H","Content-Type: application/json"]
post = subprocess.run(
["curl","-s",*auth_hdr,*ct_hdr,"-d",json.dumps(req),runs_url],
capture_output=True, text=True,
)
post_body = json.loads(post.stdout)
run_id = post_body["data"]["run_id"]
get_url = f"{runs_url}/{run_id}"
get = subprocess.run(
["curl","-s",*auth_hdr,get_url],
capture_output=True, text=True,
)
get_body = json.loads(get.stdout)
entry = get_body["data"]["output_json"]["audit"]["entry"]
print(json.dumps(entry))
PYEOF
append_audit() {
local action="$1"
local prior_file="$2"
python3 "$AUD_HELPER" "$prior_file" "$action"
}
# Build 6 entries sequentially.
echo '[]' > /tmp/dth_vc_aud_prior_0.json
AUD_E1="$(append_audit 'vc.s1.tds.match' /tmp/dth_vc_aud_prior_0.json)"
echo "[$AUD_E1]" > /tmp/dth_vc_aud_prior_1.json
AUD_E2="$(append_audit 'vc.s3.masking.summary' /tmp/dth_vc_aud_prior_1.json)"
echo "[$AUD_E1,$AUD_E2]" > /tmp/dth_vc_aud_prior_2.json
AUD_E3="$(append_audit 'vc.s5.watermark' /tmp/dth_vc_aud_prior_2.json)"
echo "[$AUD_E1,$AUD_E2,$AUD_E3]" > /tmp/dth_vc_aud_prior_3.json
AUD_E4="$(append_audit 'vc.s7.fedavg' /tmp/dth_vc_aud_prior_3.json)"
echo "[$AUD_E1,$AUD_E2,$AUD_E3,$AUD_E4]" > /tmp/dth_vc_aud_prior_4.json
AUD_E5="$(append_audit 'vc.s8.abac.allow' /tmp/dth_vc_aud_prior_4.json)"
echo "[$AUD_E1,$AUD_E2,$AUD_E3,$AUD_E4,$AUD_E5]" > /tmp/dth_vc_aud_prior_5.json
AUD_E6="$(append_audit 'vc.s8.close' /tmp/dth_vc_aud_prior_5.json)"
# Sanity: each AUD_E* must be non-empty.
for VAR in AUD_E1 AUD_E2 AUD_E3 AUD_E4 AUD_E5 AUD_E6; do
if [ -z "${!VAR}" ]; then
echo "FATAL: $VAR empty — audit chain construction failed"
fail=$((fail+1))
fi
done
# Verify chain via second helper script (file-based).
AUD_V_HELPER="/tmp/dth_vc_audit_verify_helper.py"
cat > "$AUD_V_HELPER" <<'PYEOF'
import json, os, sys
entries = []
for v in ["AUD_E1","AUD_E2","AUD_E3","AUD_E4","AUD_E5","AUD_E6"]:
s = os.environ.get(v, "")
if not s: sys.exit(f"missing env var {v}")
entries.append(json.loads(s))
print(json.dumps({"operator":"compliance-audit","input":{"verify":True,"entries":entries}}))
PYEOF
AUD_V_REQ_FILE="/tmp/dth_vc_audit_verify.json"
AUD_E1="$AUD_E1" AUD_E2="$AUD_E2" AUD_E3="$AUD_E3" AUD_E4="$AUD_E4" \
AUD_E5="$AUD_E5" AUD_E6="$AUD_E6" \
python3 "$AUD_V_HELPER" > "$AUD_V_REQ_FILE"
AUD_V_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
--data-binary "@$AUD_V_REQ_FILE" "$RUNS_URL")"
AUD_V_ID="$(json_get "['data']['run_id']" "$AUD_V_BODY")"
AUD_V_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$AUD_V_ID")"
AUD_V_VALID="$(json_get "['data']['output_json']['audit']['chainValid']" "$AUD_V_GET")"
AUD_V_N="$(json_get "['data']['output_json']['audit']['entries_verified']" "$AUD_V_GET")"
assert "C9_audit_chainValid" "[ '$AUD_V_VALID' = 'True' ]" "chainValid=$AUD_V_VALID; body=$AUD_V_GET"
assert "C9_audit_entries_6" "[ '$AUD_V_N' = '6' ]" "entries_verified=$AUD_V_N (expected 6)"
# ============================================================
# C10 (S10): E2E 端到端 — pass=N fail=0 (汇总断言)
# ============================================================
assert "C10_e2e_full_flow_pass" "[ $fail -eq 0 ]" "downstream case failed before reaching C10"
echo "---"
echo "e2e dth_vc: pass=$pass fail=$fail"
if [ $fail -gt 0 ]; then
printf 'FAILED:\n'; printf ' - %s\n' "${fails[@]}"
exit 1
fi
exit 0