forked from HyperSafeD/Sanctifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-runtime-guards.sh
More file actions
481 lines (392 loc) · 13.2 KB
/
Copy pathvalidate-runtime-guards.sh
File metadata and controls
481 lines (392 loc) · 13.2 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/bin/bash
#======================================================================
# Runtime Guard Wrapper Validation Harness
#======================================================================
# This script provides comprehensive testing and validation for
# deployed runtime guard wrapper contracts on Soroban testnet
#======================================================================
set -euo pipefail
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
VALIDATION_LOG="${PROJECT_ROOT}/.validation.log"
VALIDATION_RESULTS="${PROJECT_ROOT}/.validation-results.json"
# Test configuration
TEST_COUNT=0
PASSED_COUNT=0
FAILED_COUNT=0
NETWORK="testnet"
#======================================================================
# Utility Functions
#======================================================================
log_info() {
echo -e "${BLUE}[INFO]${NC} $*" | tee -a "$VALIDATION_LOG"
}
log_success() {
echo -e "${GREEN}[✓]${NC} $*" | tee -a "$VALIDATION_LOG"
}
log_error() {
echo -e "${RED}[✗]${NC} $*" | tee -a "$VALIDATION_LOG" >&2
}
log_test() {
echo -e "${CYAN}[TEST]${NC} $*" | tee -a "$VALIDATION_LOG"
}
start_test() {
local test_name=$1
(( TEST_COUNT++ ))
log_test "[$TEST_COUNT] Running: $test_name"
}
pass_test() {
local test_name=$1
(( PASSED_COUNT++ ))
log_success "Test passed: $test_name"
}
fail_test() {
local test_name=$1
local reason=${2:-"Unknown error"}
(( FAILED_COUNT++ ))
log_error "Test failed: $test_name - $reason"
}
#======================================================================
# Validation Tests
#======================================================================
test_health_check() {
start_test "Health Check"
local contract_id=$1
local result
result=$(soroban contract invoke \
--id "$contract_id" \
--network "$NETWORK" \
-- health_check 2>&1 || echo "failed")
if echo "$result" | grep -q "true"; then
pass_test "Health Check"
return 0
else
fail_test "Health Check" "Contract health check returned false"
return 1
fi
}
test_get_stats() {
start_test "Get Statistics"
local contract_id=$1
local result
result=$(soroban contract invoke \
--id "$contract_id" \
--network "$NETWORK" \
-- get_stats 2>&1 || echo "failed")
if echo "$result" | grep -qE "[0-9]+"; then
pass_test "Get Statistics"
return 0
else
fail_test "Get Statistics" "Could not retrieve statistics"
return 1
fi
}
test_execution_monitoring() {
start_test "Execution Monitoring"
local contract_id=$1
# Execute a guarded function (simulated)
local result
result=$(soroban contract invoke \
--id "$contract_id" \
--network "$NETWORK" \
-- execute_guarded test_function 2>&1 || echo "failed")
if [ "$result" != "failed" ]; then
pass_test "Execution Monitoring"
return 0
else
fail_test "Execution Monitoring" "Could not execute guarded function"
return 1
fi
}
test_event_emission() {
start_test "Event Emission"
local contract_id=$1
# Check that contract emits guard events
local result
result=$(soroban events read \
--network "$NETWORK" \
--id "$contract_id" 2>&1 || echo "")
if [ -n "$result" ] && echo "$result" | grep -q "guard"; then
pass_test "Event Emission"
return 0
else
log_test "Event Emission - Note: No events found yet (may be expected)"
pass_test "Event Emission"
return 0
fi
}
test_storage_accessibility() {
start_test "Storage Accessibility"
local contract_id=$1
# Verify that contract storage is accessible
if soroban contract read \
--id "$contract_id" \
--network "$NETWORK" \
--key instance 2>&1 | grep -q "0x"; then
pass_test "Storage Accessibility"
return 0
else
fail_test "Storage Accessibility" "Could not access contract storage"
return 1
fi
}
test_performance_baseline() {
start_test "Performance Baseline"
local contract_id=$1
log_test "Measuring execution time for health check..."
local start_time
local end_time
start_time=$(date +%s%N)
soroban contract invoke \
--id "$contract_id" \
--network "$NETWORK" \
-- health_check &>/dev/null
end_time=$(date +%s%N)
local duration_ms=$(( (end_time - start_time) / 1000000 ))
# Baseline: Health checks should complete in reasonable time
if [ "$duration_ms" -lt 30000 ]; then
log_test "Health check completed in ${duration_ms}ms"
pass_test "Performance Baseline"
return 0
else
fail_test "Performance Baseline" "Health check took ${duration_ms}ms (expected < 30000ms)"
return 1
fi
}
test_error_handling() {
start_test "Error Handling"
local contract_id=$1
# Test that contract properly handles invalid inputs
local result
result=$(soroban contract invoke \
--id "$contract_id" \
--network "$NETWORK" \
-- execute_guarded invalid_function 2>&1 || echo "handled")
if echo "$result" | grep -q "handled\|error"; then
pass_test "Error Handling"
return 0
else
fail_test "Error Handling" "Contract did not handle invalid input properly"
return 1
fi
}
test_concurrent_operations() {
start_test "Concurrent Operations"
local contract_id=$1
# Simulate concurrent health checks
local success_count=0
for i in {1..3}; do
if (soroban contract invoke \
--id "$contract_id" \
--network "$NETWORK" \
-- health_check &>/dev/null); then
(( success_count++ ))
fi
sleep 1
done
if [ "$success_count" -ge 2 ]; then
log_test "Concurrent operations: $success_count/3 succeeded"
pass_test "Concurrent Operations"
return 0
else
fail_test "Concurrent Operations" "Only $success_count/3 operations succeeded"
return 1
fi
}
#======================================================================
# Validation Suite Orchestration
#======================================================================
run_full_validation() {
local contract_id=$1
log_info "Running full validation suite for contract: $contract_id"
log_info "Network: $NETWORK"
echo "" | tee -a "$VALIDATION_LOG"
# Run all tests
test_health_check "$contract_id" || true
test_get_stats "$contract_id" || true
test_execution_monitoring "$contract_id" || true
test_event_emission "$contract_id" || true
test_storage_accessibility "$contract_id" || true
test_performance_baseline "$contract_id" || true
test_error_handling "$contract_id" || true
test_concurrent_operations "$contract_id" || true
echo "" | tee -a "$VALIDATION_LOG"
}
generate_validation_report() {
local contract_id=$1
local pass_rate=0
if [ "$TEST_COUNT" -gt 0 ]; then
pass_rate=$(( (PASSED_COUNT * 100) / TEST_COUNT ))
fi
cat > "$VALIDATION_RESULTS" << EOF
{
"contract_id": "$contract_id",
"network": "$NETWORK",
"timestamp": "$(date -u +'%Y-%m-%dT%H:%M:%SZ')",
"test_results": {
"total_tests": $TEST_COUNT,
"passed": $PASSED_COUNT,
"failed": $FAILED_COUNT,
"pass_rate": $pass_rate
},
"status": "$([ $FAILED_COUNT -eq 0 ] && echo 'PASS' || echo 'FAIL')"
}
EOF
log_info "Validation report generated: $VALIDATION_RESULTS"
}
print_validation_summary() {
local contract_id=$1
echo "" | tee -a "$VALIDATION_LOG"
echo "======================================================================" | tee -a "$VALIDATION_LOG"
echo "VALIDATION SUMMARY" | tee -a "$VALIDATION_LOG"
echo "======================================================================" | tee -a "$VALIDATION_LOG"
echo "Contract ID: $contract_id" | tee -a "$VALIDATION_LOG"
echo "Network: $NETWORK" | tee -a "$VALIDATION_LOG"
echo "Total Tests: $TEST_COUNT" | tee -a "$VALIDATION_LOG"
echo -e "Passed: ${GREEN}$PASSED_COUNT${NC}" | tee -a "$VALIDATION_LOG"
echo -e "Failed: ${RED}$FAILED_COUNT${NC}" | tee -a "$VALIDATION_LOG"
if [ "$TEST_COUNT" -gt 0 ]; then
local pass_rate=$(( (PASSED_COUNT * 100) / TEST_COUNT ))
echo "Pass Rate: $pass_rate%" | tee -a "$VALIDATION_LOG"
if [ "$pass_rate" -ge 80 ]; then
echo -e "${GREEN}✓ Validation Status: PASS${NC}" | tee -a "$VALIDATION_LOG"
elif [ "$pass_rate" -ge 50 ]; then
echo -e "${YELLOW}⚠ Validation Status: PARTIAL PASS${NC}" | tee -a "$VALIDATION_LOG"
else
echo -e "${RED}✗ Validation Status: FAIL${NC}" | tee -a "$VALIDATION_LOG"
fi
fi
echo "======================================================================" | tee -a "$VALIDATION_LOG"
echo "" | tee -a "$VALIDATION_LOG"
}
#======================================================================
# CLI Argument Parsing
#======================================================================
parse_arguments() {
while [[ $# -gt 0 ]]; do
case $1 in
--contract-id)
CONTRACT_ID="$2"
shift 2
;;
--network)
NETWORK="$2"
shift 2
;;
--help)
print_help
exit 0
;;
*)
log_error "Unknown argument: $1"
print_help
exit 1
;;
esac
done
}
print_help() {
cat << 'EOF'
Usage: validate-runtime-guards.sh [OPTIONS]
Options:
--contract-id <ID> Contract ID to validate (required)
--network <NETWORK> Target network (testnet, futurenet, mainnet)
Default: testnet
--help Show this help message
Environment Variables:
SOROBAN_SECRET_KEY Your Soroban secret key (required for some tests)
Examples:
# Validate a contract on testnet
./validate-runtime-guards.sh --contract-id C...
# Validate on a different network
./validate-runtime-guards.sh --contract-id C... --network futurenet
EOF
}
#======================================================================
# Validation Functions
#======================================================================
validate_contract_id() {
local id=$1
if [[ "$id" =~ ^C[A-Z0-9]{55}$ ]]; then
return 0
else
return 1
fi
}
validate_secret_key() {
local key=$1
if [[ "$key" =~ ^S[A-Z0-9]{55}$ ]]; then
return 0
else
return 1
fi
}
validate_tools() {
local required_tools=("soroban" "jq")
for tool in "${required_tools[@]}"; do
if ! command -v "$tool" &> /dev/null; then
log_error "Required tool not found: $tool"
log_info "Please install $tool to proceed."
return 1
fi
done
return 0
}
#======================================================================
# Main Entry Point
#======================================================================
main() {
echo -e "${CYAN}"
cat << "EOF"
╔═══════════════════════════════════════════════════════════════╗
║ Sanctifier: Runtime Guard Validation Harness ║
║ ║
║ Comprehensive testing of deployed wrapper contracts ║
╚═══════════════════════════════════════════════════════════════╝
EOF
echo -e "${NC}"
if ! validate_tools; then
exit 1
fi
CONTRACT_ID=""
parse_arguments "$@"
if [ -z "$CONTRACT_ID" ]; then
log_error "Contract ID is required"
log_info "Specify with --contract-id C..."
print_help
exit 1
fi
if ! validate_contract_id "$CONTRACT_ID"; then
log_error "Invalid Contract ID format: $CONTRACT_ID"
log_info "Contract IDs should start with 'C' and be 56 characters long."
exit 1
fi
if [[ -n "${SOROBAN_SECRET_KEY:-}" ]] && ! validate_secret_key "$SOROBAN_SECRET_KEY"; then
log_error "Invalid SOROBAN_SECRET_KEY format"
log_info "Secret keys should start with 'S' and be 56 characters long."
exit 1
fi
# Initialize logging
mkdir -p "$(dirname "$VALIDATION_LOG")"
:> "$VALIDATION_LOG"
# Run validation
run_full_validation "$CONTRACT_ID"
# Generate report
generate_validation_report "$CONTRACT_ID"
# Print summary
print_validation_summary "$CONTRACT_ID"
# Exit with appropriate code
if [ "$FAILED_COUNT" -eq 0 ]; then
exit 0
else
exit 1
fi
}
main "$@"