Repository navigation
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·347 lines (290 loc) · 10.5 KB
/
Copy pathtest.sh
File metadata and controls
executable file
·347 lines (290 loc) · 10.5 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
#!/bin/sh
# SPDX-License-Identifier: MPL-2.0
# test.sh - K9 SVC Test Suite
#
# Runs all tests for the K9 Self-Validating Component system.
# Exit codes: 0 = all pass, 1 = failures
set -eu
PASS=0
FAIL=0
SKIP=0
# ─────────────────────────────────────────────────────────────
# Test Utilities
# ─────────────────────────────────────────────────────────────
green() { printf '\033[32m%s\033[0m\n' "$1"; }
red() { printf '\033[31m%s\033[0m\n' "$1"; }
yellow() { printf '\033[33m%s\033[0m\n' "$1"; }
pass() {
green " ✓ $1"
PASS=$((PASS + 1))
}
fail() {
red " ✗ $1"
FAIL=$((FAIL + 1))
}
skip() {
yellow " ○ $1 (skipped)"
SKIP=$((SKIP + 1))
}
section() {
echo ""
echo "━━━ $1 ━━━"
}
# ─────────────────────────────────────────────────────────────
# Environment Tests
# ─────────────────────────────────────────────────────────────
test_environment() {
section "Environment"
# Test must shim
if ./must status >/dev/null 2>&1; then
pass "must shim executes"
else
fail "must shim failed"
fi
# Test OS detection
os=$(./must status | grep "OS:" | awk '{print $2}')
if [ -n "$os" ]; then
pass "OS detected: $os"
else
fail "OS detection failed"
fi
# Test Nickel
if command -v nickel >/dev/null 2>&1; then
pass "Nickel installed"
else
fail "Nickel not found"
fi
# Test Just
if command -v just >/dev/null 2>&1; then
pass "Just installed"
else
fail "Just not found"
fi
# Test OpenSSL (for signing)
if command -v openssl >/dev/null 2>&1; then
if openssl genpkey -algorithm Ed25519 -out /dev/null 2>/dev/null; then
pass "OpenSSL with Ed25519 support"
else
skip "OpenSSL without Ed25519 (older version)"
fi
else
skip "OpenSSL not installed"
fi
}
# ─────────────────────────────────────────────────────────────
# Schema Tests
# ─────────────────────────────────────────────────────────────
test_schemas() {
section "Schema Validation"
# Test pedigree.ncl
if nickel typecheck pedigree.ncl 2>/dev/null; then
pass "pedigree.ncl typechecks"
else
fail "pedigree.ncl typecheck failed"
fi
# Test register.ncl
if nickel typecheck register.ncl 2>/dev/null; then
pass "register.ncl typechecks"
else
fail "register.ncl typecheck failed"
fi
# Test leash.ncl
if nickel typecheck leash.ncl 2>/dev/null; then
pass "leash.ncl typechecks"
else
fail "leash.ncl typecheck failed"
fi
}
# ─────────────────────────────────────────────────────────────
# Example Tests
# ─────────────────────────────────────────────────────────────
test_examples() {
section "Example Components"
# Test hello.k9 (Kennel level)
if [ -f "examples/hello.k9" ]; then
if head -1 examples/hello.k9 | grep -q "K9!"; then
pass "hello.k9 has magic number"
else
fail "hello.k9 missing magic number"
fi
else
fail "hello.k9 not found"
fi
# Test config.k9.ncl (Yard level)
if [ -f "examples/config.k9.ncl" ]; then
if nickel typecheck examples/config.k9.ncl 2>/dev/null; then
pass "config.k9.ncl typechecks"
else
fail "config.k9.ncl typecheck failed"
fi
else
fail "config.k9.ncl not found"
fi
# Test deploy.k9.ncl (Hunt level)
if [ -f "examples/deploy.k9.ncl" ]; then
if nickel typecheck examples/deploy.k9.ncl 2>/dev/null; then
pass "deploy.k9.ncl typechecks"
else
fail "deploy.k9.ncl typecheck failed"
fi
# Check it declares Hunt level
if grep -q "trust_level.*'Hunt" examples/deploy.k9.ncl; then
pass "deploy.k9.ncl declares Hunt level"
else
fail "deploy.k9.ncl should declare Hunt level"
fi
else
fail "deploy.k9.ncl not found"
fi
}
# ─────────────────────────────────────────────────────────────
# MIME Tests
# ─────────────────────────────────────────────────────────────
test_mime() {
section "MIME Files"
# Test k9.xml
if [ -f "mime/k9.xml" ]; then
if command -v xmllint >/dev/null 2>&1; then
if xmllint --noout mime/k9.xml 2>/dev/null; then
pass "k9.xml is valid XML"
else
fail "k9.xml is invalid XML"
fi
else
skip "xmllint not installed"
fi
# Check for magic number definition
if grep -q 'value="K9!"' mime/k9.xml; then
pass "k9.xml defines magic number"
else
fail "k9.xml missing magic number"
fi
else
fail "mime/k9.xml not found"
fi
# Test k9.uti.plist (macOS)
if [ -f "mime/k9.uti.plist" ]; then
if command -v xmllint >/dev/null 2>&1; then
if xmllint --noout mime/k9.uti.plist 2>/dev/null; then
pass "k9.uti.plist is valid XML"
else
fail "k9.uti.plist is invalid XML"
fi
else
skip "xmllint not installed"
fi
else
fail "mime/k9.uti.plist not found"
fi
# Test k9.magic
if [ -f "mime/k9.magic" ]; then
if grep -q "K9!" mime/k9.magic; then
pass "k9.magic defines magic pattern"
else
fail "k9.magic missing magic pattern"
fi
else
fail "mime/k9.magic not found"
fi
}
# ─────────────────────────────────────────────────────────────
# Signing Tests
# ─────────────────────────────────────────────────────────────
test_signing() {
section "Signing System"
if [ ! -f "sign.sh" ]; then
fail "sign.sh not found"
return
fi
if [ ! -x "sign.sh" ]; then
fail "sign.sh not executable"
return
fi
# Check OpenSSL Ed25519 support
if ! command -v openssl >/dev/null 2>&1; then
skip "OpenSSL not installed"
return
fi
if ! openssl genpkey -algorithm Ed25519 -out /dev/null 2>/dev/null; then
skip "OpenSSL doesn't support Ed25519"
return
fi
# Test key generation (in temp dir)
tmpdir=$(mktemp -d)
export XDG_CONFIG_HOME="$tmpdir"
if ./sign.sh keygen test 2>/dev/null; then
pass "Key generation works"
# Test key listing
if ./sign.sh list 2>/dev/null | grep -q "test"; then
pass "Key listing works"
else
fail "Key listing failed"
fi
# Test signing
echo "test content" > "$tmpdir/testfile"
if ./sign.sh sign "$tmpdir/testfile" test 2>/dev/null; then
pass "Signing works"
# Trust the key (XDG_CONFIG_HOME is $tmpdir, so keys are in $tmpdir/k9/keys/)
./sign.sh trust "$tmpdir/k9/keys/test.pub" 2>/dev/null
# Test verification
if ./sign.sh verify "$tmpdir/testfile" 2>/dev/null; then
pass "Verification works"
else
fail "Verification failed"
fi
else
fail "Signing failed"
fi
else
fail "Key generation failed"
fi
rm -rf "$tmpdir"
unset XDG_CONFIG_HOME
}
# ─────────────────────────────────────────────────────────────
# Container Tests
# ─────────────────────────────────────────────────────────────
test_container() {
section "Container"
if [ -f "Containerfile" ]; then
pass "Containerfile exists"
# Check for multi-stage build
if grep -q "FROM.*AS builder" Containerfile; then
pass "Multi-stage build configured"
else
fail "Multi-stage build not configured"
fi
# Check for non-root user
if grep -q "USER" Containerfile; then
pass "Non-root user configured"
else
fail "Running as root (security issue)"
fi
else
fail "Containerfile not found"
fi
if [ -f "compose.yaml" ]; then
pass "compose.yaml exists"
else
skip "compose.yaml not found"
fi
}
# ─────────────────────────────────────────────────────────────
# Main
# ─────────────────────────────────────────────────────────────
echo "K9 SVC Test Suite"
echo "═════════════════════════════════════"
test_environment
test_schemas
test_examples
test_mime
test_signing
test_container
echo ""
echo "═════════════════════════════════════"
echo "Results: $(green "$PASS passed"), $(red "$FAIL failed"), $(yellow "$SKIP skipped")"
echo ""
if [ "$FAIL" -gt 0 ]; then
exit 1
fi
exit 0