-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathsettings-migration-test.sh
More file actions
executable file
·246 lines (201 loc) · 7.88 KB
/
settings-migration-test.sh
File metadata and controls
executable file
·246 lines (201 loc) · 7.88 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
#!/bin/bash
# Settings encryption & migration testing — runs Go unit tests for the settings
# manager and crypto layer, verifies key rotation, import/export with different
# keys, corrupt keyfile handling, and forward compatibility.
#
# Usage:
# ./scripts/settings-migration-test.sh # Run all tests
#
# Prerequisites:
# - Go installed
#
# Output:
# /tmp/settings-migration-report.json — JSON test results
# /tmp/settings-migration-summary.md — human-readable summary
#
# Exit code:
# 0 — all tests pass
# 1 — one or more tests failed
set -euo pipefail
cd "$(dirname "$0")/.."
# ============================================================================
# Colors
# ============================================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m'
REPORT_JSON="/tmp/settings-migration-report.json"
REPORT_MD="/tmp/settings-migration-summary.md"
TMPDIR_SM=$(mktemp -d)
trap 'rm -rf "$TMPDIR_SM"' EXIT
if ! command -v go &>/dev/null; then
echo -e "${RED}ERROR: Go is not installed${NC}"
exit 1
fi
echo -e "${BOLD}═══════════════════════════════════════════════════${NC}"
echo -e "${BOLD} Settings Encryption & Migration Testing${NC}"
echo -e "${BOLD}═══════════════════════════════════════════════════${NC}"
echo ""
TOTAL=0
PASSED=0
FAILED=0
# ============================================================================
# Phase 1: Crypto unit tests
# ============================================================================
echo -e "${BOLD}Phase 1: Crypto unit tests${NC}"
CRYPTO_OUTPUT="$TMPDIR_SM/crypto-tests.txt"
CRYPTO_EXIT=0
go test ./pkg/settings/... -run "TestEncryptDecrypt|TestDecrypt|TestEnsureKeyFile|TestKeyFingerprint" -v -timeout 30s > "$CRYPTO_OUTPUT" 2>&1 || CRYPTO_EXIT=$?
CRYPTO_PASSED=$(grep -c "^--- PASS:" "$CRYPTO_OUTPUT" 2>/dev/null) || CRYPTO_PASSED=0
CRYPTO_FAILED_COUNT=$(grep -c "^--- FAIL:" "$CRYPTO_OUTPUT" 2>/dev/null) || CRYPTO_FAILED_COUNT=0
TOTAL=$((TOTAL + 1))
if [ "$CRYPTO_EXIT" -eq 0 ]; then
echo -e " ${GREEN}✓${NC} Crypto unit tests passed (${CRYPTO_PASSED} tests)"
PASSED=$((PASSED + 1))
else
echo -e " ${RED}❌${NC} Crypto tests failed (${CRYPTO_FAILED_COUNT} failures)"
grep "^--- FAIL:" "$CRYPTO_OUTPUT" 2>/dev/null | while IFS= read -r line; do
echo -e " ${DIM}${line}${NC}"
done
FAILED=$((FAILED + 1))
fi
echo ""
# ============================================================================
# Phase 2: Settings handler tests (export/import)
# ============================================================================
echo -e "${BOLD}Phase 2: Settings handler tests (export/import)${NC}"
HANDLER_OUTPUT="$TMPDIR_SM/handler-tests.txt"
HANDLER_EXIT=0
go test ./pkg/api/handlers/... -run "TestGetSettings|TestSaveSettings|TestExportImport|TestSettingsFileError" -v -timeout 30s > "$HANDLER_OUTPUT" 2>&1 || HANDLER_EXIT=$?
HANDLER_PASSED=$(grep -c "^--- PASS:" "$HANDLER_OUTPUT" 2>/dev/null) || HANDLER_PASSED=0
HANDLER_FAILED_COUNT=$(grep -c "^--- FAIL:" "$HANDLER_OUTPUT" 2>/dev/null) || HANDLER_FAILED_COUNT=0
TOTAL=$((TOTAL + 1))
if [ "$HANDLER_EXIT" -eq 0 ]; then
echo -e " ${GREEN}✓${NC} Settings handler tests passed (${HANDLER_PASSED} tests)"
PASSED=$((PASSED + 1))
else
echo -e " ${RED}❌${NC} Settings handler tests failed (${HANDLER_FAILED_COUNT} failures)"
grep "^--- FAIL:" "$HANDLER_OUTPUT" 2>/dev/null | while IFS= read -r line; do
echo -e " ${DIM}${line}${NC}"
done
FAILED=$((FAILED + 1))
fi
echo ""
# ============================================================================
# Phase 3: Crypto security pattern verification
# ============================================================================
echo -e "${BOLD}Phase 3: Crypto security pattern verification${NC}"
CRYPTO_FILE="pkg/settings/crypto.go"
# AES-256 key size
TOTAL=$((TOTAL + 1))
if grep -q "keyBytes.*=.*32\|aes.NewCipher" "$CRYPTO_FILE" 2>/dev/null; then
echo -e " ${GREEN}✓${NC} AES-256 (32-byte key) confirmed"
PASSED=$((PASSED + 1))
else
echo -e " ${RED}❌${NC} AES-256 key size not verified"
FAILED=$((FAILED + 1))
fi
# GCM mode (authenticated encryption)
TOTAL=$((TOTAL + 1))
if grep -q "cipher.NewGCM\|gcm.Seal\|gcm.Open" "$CRYPTO_FILE" 2>/dev/null; then
echo -e " ${GREEN}✓${NC} GCM authenticated encryption confirmed"
PASSED=$((PASSED + 1))
else
echo -e " ${RED}❌${NC} GCM authenticated encryption not found"
FAILED=$((FAILED + 1))
fi
# Random nonce generation
TOTAL=$((TOTAL + 1))
if grep -q "rand.Read\|crypto/rand" "$CRYPTO_FILE" 2>/dev/null; then
echo -e " ${GREEN}✓${NC} Cryptographic random nonce generation confirmed"
PASSED=$((PASSED + 1))
else
echo -e " ${RED}❌${NC} Missing cryptographic random nonce"
FAILED=$((FAILED + 1))
fi
# Secure file permissions
TOTAL=$((TOTAL + 1))
if grep -q "0600\|keyFileMode" "$CRYPTO_FILE" 2>/dev/null; then
echo -e " ${GREEN}✓${NC} Keyfile permissions (0600) confirmed"
PASSED=$((PASSED + 1))
else
echo -e " ${RED}❌${NC} Keyfile permissions not set securely"
FAILED=$((FAILED + 1))
fi
# Key fingerprint (no key exposure)
TOTAL=$((TOTAL + 1))
if grep -q "keyFingerprint\|sha256.Sum256" "$CRYPTO_FILE" 2>/dev/null; then
echo -e " ${GREEN}✓${NC} Key fingerprint (SHA-256, no key exposure) confirmed"
PASSED=$((PASSED + 1))
else
echo -e " ${RED}❌${NC} Key fingerprint mechanism not found"
FAILED=$((FAILED + 1))
fi
echo ""
# ============================================================================
# Phase 4: Fuzz test availability
# ============================================================================
echo -e "${BOLD}Phase 4: Fuzz test availability${NC}"
TOTAL=$((TOTAL + 1))
if find pkg/settings -name "*fuzz*" -o -name "*_fuzz_*" 2>/dev/null | grep -q .; then
echo -e " ${GREEN}✓${NC} Crypto fuzz tests present"
PASSED=$((PASSED + 1))
else
echo -e " ${YELLOW}⚠️ ${NC} No crypto fuzz tests found (run api-fuzz-test.sh)"
FAILED=$((FAILED + 1))
fi
echo ""
# ============================================================================
# Generate reports
# ============================================================================
cat > "$REPORT_JSON" << EOF
{
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
"summary": {
"total": ${TOTAL},
"passed": ${PASSED},
"failed": ${FAILED}
},
"phases": {
"cryptoTests": { "exit_code": ${CRYPTO_EXIT}, "passed": ${CRYPTO_PASSED} },
"handlerTests": { "exit_code": ${HANDLER_EXIT}, "passed": ${HANDLER_PASSED} },
"securityPatterns": "checked",
"fuzzTests": "checked"
}
}
EOF
cat > "$REPORT_MD" << EOF
# Settings Encryption & Migration Testing
**Date:** $(date -u +%Y-%m-%dT%H:%M:%SZ)
## Summary
| Metric | Count |
|----------|-------|
| Total | ${TOTAL} |
| Passed | ${PASSED} |
| Failed | ${FAILED} |
## Phases
- **Phase 1:** Crypto unit tests — $([ "$CRYPTO_EXIT" -eq 0 ] && echo "PASS" || echo "FAIL")
- **Phase 2:** Settings handler tests — $([ "$HANDLER_EXIT" -eq 0 ] && echo "PASS" || echo "FAIL")
- **Phase 3:** Crypto security pattern verification
- **Phase 4:** Fuzz test availability
EOF
# ============================================================================
# Summary
# ============================================================================
if [ "$PASSED" -eq 0 ] && [ "$FAILED" -eq 0 ]; then
echo -e "${RED}${BOLD}No tests were executed${NC}"
elif [ "$FAILED" -eq 0 ]; then
echo -e "${GREEN}${BOLD}All ${PASSED} settings migration checks passed${NC}"
else
echo -e "${RED}${BOLD}${FAILED}/${TOTAL} settings migration checks failed${NC}"
fi
echo ""
echo "Reports:"
echo " JSON: $REPORT_JSON"
echo " Summary: $REPORT_MD"
[ "$PASSED" -eq 0 ] && [ "$FAILED" -eq 0 ] && exit 1
[ "$FAILED" -gt 0 ] && exit 1
exit 0