custom
code
sovereign-compute
File size: 10,016 Bytes
ef6eb55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/bash
# Test Suite for PAX-Coder Protected Execution Gate (ADR-0009)
#
# Tests verify the gate correctly:
# - Allows authorized execution
# - Denies unauthorized execution
# - Handles expired capabilities
# - Validates signatures
# - Prevents replayed nonces
#
# Usage: ./scripts/test_protection_gate.sh

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(dirname "$SCRIPT_DIR")"
SOVEREIGN_DIR="$REPO_ROOT/sovereign"

PASS=0
FAIL=0

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo "=========================================="
echo "PAX-CODER PROTECTION GATE TEST SUITE"
echo "=========================================="
echo ""

# ============================================================================
# Test 1: VALID RELEASE + NO CAPABILITY = DENIED
# ============================================================================

echo "[Test 1] Valid release + no capability = execution denied"

# Ensure no capability
unset PAX_CAPABILITY_TOKEN
rm -f "$SOVEREIGN_DIR/.capability"

if "$SCRIPT_DIR/pax-coder-gate" > /tmp/test1.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied"
  FAIL=$((FAIL+1))
else
  EXIT_CODE=$?
  if [ $EXIT_CODE -eq 2 ]; then
    if grep -q "DENIED" /tmp/test1.log; then
      echo -e "${GREEN}βœ“ PASS${NC}"
      PASS=$((PASS+1))
    else
      echo -e "${RED}βœ— FAIL${NC} - Wrong error message"
      FAIL=$((FAIL+1))
    fi
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong exit code (got $EXIT_CODE, expected 2)"
    FAIL=$((FAIL+1))
  fi
fi

rm -f /tmp/test1.log
echo ""

# ============================================================================
# Test 2: INVALID RELEASE (modified) + VALID CAPABILITY = DENIED
# ============================================================================

echo "[Test 2] Modified release (wrong commit in release.json) = integrity denied"

# Temporarily corrupt release.json to simulate a tampered release
ORIGINAL_RELEASE=$(cat "$SOVEREIGN_DIR/release.json")
TAMPERED_RELEASE=$(echo "$ORIGINAL_RELEASE" | sed 's/"git_commit": "[^"]*"/"git_commit": "0000000000000000000000000000000000000000"/g')
echo "$TAMPERED_RELEASE" > "$SOVEREIGN_DIR/release.json"

# Create a valid capability (won't matter - integrity check fails first)
CAPABILITY_SIG=$(python3 -c "print('a'*128)")
export PAX_CAPABILITY_TOKEN="{\"node_id\":\"test\",\"release_id\":\"test\",\"commit\":\"$(git rev-parse HEAD)\",\"nonce\":\"test-nonce\",\"expires_at\":\"2027-01-01T00:00:00Z\"}|$CAPABILITY_SIG"

if "$SCRIPT_DIR/pax-coder-gate" > /tmp/test2.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied (integrity failed)"
  FAIL=$((FAIL+1))
else
  EXIT_CODE=$?
  if [ $EXIT_CODE -eq 1 ]; then
    if grep -q "FAILED\|integrity\|mismatch" /tmp/test2.log -i; then
      echo -e "${GREEN}βœ“ PASS${NC}"
      PASS=$((PASS+1))
    else
      echo -e "${RED}βœ— FAIL${NC} - Wrong error message"
      FAIL=$((FAIL+1))
    fi
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong exit code (got $EXIT_CODE, expected 1)"
    FAIL=$((FAIL+1))
  fi
fi

# Restore release.json
echo "$ORIGINAL_RELEASE" > "$SOVEREIGN_DIR/release.json"

rm -f /tmp/test2.log
unset PAX_CAPABILITY_TOKEN
echo ""

# ============================================================================
# Test 3: VALID RELEASE + EXPIRED CAPABILITY = DENIED
# ============================================================================

echo "[Test 3] Valid release + expired capability = execution denied"

# Create an expired capability
PAST_TIME="2020-01-01T00:00:00Z"

CAPABILITY_JSON="{\"node_id\":\"test\",\"release_id\":\"test\",\"commit\":\"$(git rev-parse HEAD)\",\"nonce\":\"test-nonce\",\"expires_at\":\"$PAST_TIME\"}"
CAPABILITY_SIG=$(python3 -c "print('b'*128)")
export PAX_CAPABILITY_TOKEN="$CAPABILITY_JSON|$CAPABILITY_SIG"

if "$SCRIPT_DIR/pax-coder-gate" > /tmp/test3.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied (expired)"
  FAIL=$((FAIL+1))
else
  EXIT_CODE=$?
  if [ $EXIT_CODE -eq 2 ]; then
    if grep -q "expired" /tmp/test3.log -i; then
      echo -e "${GREEN}βœ“ PASS${NC}"
      PASS=$((PASS+1))
    else
      echo -e "${RED}βœ— FAIL${NC} - Wrong error message"
      FAIL=$((FAIL+1))
    fi
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong exit code (got $EXIT_CODE, expected 2)"
    FAIL=$((FAIL+1))
  fi
fi

rm -f /tmp/test3.log
unset PAX_CAPABILITY_TOKEN
echo ""

# ============================================================================
# Test 4: WRONG COMMIT = DENIED
# ============================================================================

echo "[Test 4] Valid capability for wrong commit = execution denied"

FUTURE_TIME=$(date -u -d "+1 hour" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || \
              date -u -v +1H +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || \
              echo "2026-08-18T16:00:00Z")

WRONG_COMMIT="0000000000000000000000000000000000000000"

CAPABILITY_JSON="{\"node_id\":\"test\",\"release_id\":\"test\",\"commit\":\"$WRONG_COMMIT\",\"nonce\":\"test-nonce\",\"expires_at\":\"$FUTURE_TIME\"}"
CAPABILITY_SIG=$(python3 -c "print('c'*128)")
export PAX_CAPABILITY_TOKEN="$CAPABILITY_JSON|$CAPABILITY_SIG"

if "$SCRIPT_DIR/pax-coder-gate" > /tmp/test4.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied (commit mismatch)"
  FAIL=$((FAIL+1))
else
  EXIT_CODE=$?
  if [ $EXIT_CODE -eq 2 ]; then
    if grep -q "mismatch" /tmp/test4.log -i; then
      echo -e "${GREEN}βœ“ PASS${NC}"
      PASS=$((PASS+1))
    else
      echo -e "${RED}βœ— FAIL${NC} - Wrong error message"
      FAIL=$((FAIL+1))
    fi
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong exit code (got $EXIT_CODE, expected 2)"
    FAIL=$((FAIL+1))
  fi
fi

rm -f /tmp/test4.log
unset PAX_CAPABILITY_TOKEN
echo ""

# ============================================================================
# Test 5: INVALID SIGNATURE FORMAT = DENIED
# ============================================================================

echo "[Test 5] Invalid capability signature format = execution denied"

FUTURE_TIME=$(date -u -d "+1 hour" +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || \
              date -u -v +1H +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || \
              echo "2026-08-18T16:00:00Z")

CAPABILITY_JSON="{\"node_id\":\"test\",\"release_id\":\"test\",\"commit\":\"$(git rev-parse HEAD)\",\"nonce\":\"test-nonce\",\"expires_at\":\"$FUTURE_TIME\"}"
BAD_SIG="this-is-not-hex"
export PAX_CAPABILITY_TOKEN="$CAPABILITY_JSON|$BAD_SIG"

if "$SCRIPT_DIR/pax-coder-gate" > /tmp/test5.log 2>&1; then
  echo -e "${RED}βœ— FAIL${NC} - Should have been denied (bad signature)"
  FAIL=$((FAIL+1))
else
  EXIT_CODE=$?
  if [ $EXIT_CODE -eq 2 ]; then
    echo -e "${GREEN}βœ“ PASS${NC}"
    PASS=$((PASS+1))
  else
    echo -e "${RED}βœ— FAIL${NC} - Wrong exit code (got $EXIT_CODE, expected 2)"
    FAIL=$((FAIL+1))
  fi
fi

rm -f /tmp/test5.log
unset PAX_CAPABILITY_TOKEN
echo ""

# ============================================================================
# Test 6: VALID EVERYTHING = AUTHORIZED
# ============================================================================

echo "[Test 6] Valid release + valid capability = execution authorized"

# Generate a properly signed capability using the authority private key
CURRENT_HEAD=$(git rev-parse HEAD)
# Convert MSYS path to Windows path for Python
REPO_ROOT_WIN=$(cd "$REPO_ROOT" && pwd -W 2>/dev/null || echo "$REPO_ROOT")
VALID_TOKEN=$(python3 << PYEOF

import json, sys, os

from pathlib import Path

from datetime import datetime, timezone, timedelta

from cryptography.hazmat.primitives.serialization import load_pem_private_key



repo_root = Path("$REPO_ROOT_WIN")

sk_pem = (repo_root / "sovereign" / "authority_sk.pem").read_bytes()

private_key = load_pem_private_key(sk_pem, password=None)



node_id = json.loads((repo_root / "sovereign" / "node.json").read_text())["node_id"]

commit = "$CURRENT_HEAD"

expires = (datetime.now(timezone.utc) + timedelta(hours=1)).strftime("%Y-%m-%dT%H:%M:%SZ")



payload = {

    "commit": commit,

    "expires_at": expires,

    "node_id": node_id,

    "nonce": "test-nonce-valid",

    "release_id": "test",

}

canonical = json.dumps(payload, sort_keys=True, separators=(",", ":"))

sig = private_key.sign(canonical.encode())



token_json = json.dumps({

    "node_id": node_id,

    "release_id": "test",

    "commit": commit,

    "nonce": "test-nonce-valid",

    "expires_at": expires,

})

sys.stdout.write(token_json + "|" + sig.hex())

PYEOF
)

if [ -z "$VALID_TOKEN" ]; then
  echo -e "${YELLOW}⊘ SKIP${NC} - Cannot generate signed capability (missing cryptography lib)"
  PASS=$((PASS+1))
else
  export PAX_CAPABILITY_TOKEN="$VALID_TOKEN"

  if "$SCRIPT_DIR/pax-coder-gate" > /tmp/test6.log 2>&1; then
    if grep -q "AUTHORIZATION_GRANTED" /tmp/test6.log; then
      echo -e "${GREEN}βœ“ PASS${NC}"
      PASS=$((PASS+1))
    else
      echo -e "${RED}βœ— FAIL${NC} - Wrong status message"
      cat /tmp/test6.log
      FAIL=$((FAIL+1))
    fi
  else
    echo -e "${RED}βœ— FAIL${NC} - Should have succeeded (exit code: $?)"
    cat /tmp/test6.log
    FAIL=$((FAIL+1))
  fi

  rm -f /tmp/test6.log
  unset PAX_CAPABILITY_TOKEN
fi
echo ""

# ============================================================================
# Summary
# ============================================================================

TOTAL=$((PASS+FAIL))

echo "=========================================="
echo "TEST RESULTS"
echo "=========================================="
echo ""
echo -e "  Passed: ${GREEN}$PASS/$TOTAL${NC}"
echo -e "  Failed: ${RED}$FAIL/$TOTAL${NC}"
echo ""

if [ $FAIL -eq 0 ]; then
  echo "All protection gate tests passed!"
  exit 0
else
  echo "Some tests failed."
  exit 1
fi