-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnative_model_cache_test.go
More file actions
295 lines (278 loc) · 12.5 KB
/
Copy pathnative_model_cache_test.go
File metadata and controls
295 lines (278 loc) · 12.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
package main
import (
"bytes"
"context"
"crypto/hkdf"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"strings"
"testing"
)
func TestNativeModelCacheDerives32ByteKey(t *testing.T) {
const oracleUID = "synthetic-user-0001"
got, err := deriveModelCacheKey(oracleUID)
if err != nil {
t.Fatalf("oracle synthetic UID: derive returned error kind %q", protocolErrorKindOf(err))
}
want, err := hkdf.Key(sha256.New, []byte(oracleUID), []byte("qoder-model-cache-enc"), "model-cache-v1", 32)
if err != nil {
t.Fatal("independent HKDF-SHA256 oracle returned an error")
}
if !bytes.Equal(got, want) {
t.Fatal("derived key differs from independent HKDF-SHA256 oracle")
}
first, err := deriveModelCacheKey("synthetic-user-a")
if err != nil {
t.Fatalf("first synthetic UID: derive returned error kind %q", protocolErrorKindOf(err))
}
second, err := deriveModelCacheKey("synthetic-user-a")
if err != nil {
t.Fatalf("second synthetic UID: derive returned error kind %q", protocolErrorKindOf(err))
}
different, err := deriveModelCacheKey("synthetic-user-b")
if err != nil {
t.Fatalf("different synthetic UID: derive returned error kind %q", protocolErrorKindOf(err))
}
if len(first) != 32 || len(second) != 32 || len(different) != 32 {
t.Fatalf("derived key lengths = %d/%d/%d, want 32 each", len(first), len(second), len(different))
}
if !bytes.Equal(first, second) {
t.Fatal("same synthetic UID produced different derived keys")
}
if bytes.Equal(first, different) {
t.Fatal("different synthetic UIDs produced the same derived key")
}
first[0] ^= 1
if bytes.Equal(first, second) {
t.Fatal("derived key calls returned aliased storage")
}
}
func TestNativeModelCacheMatchesOfficialFixture(t *testing.T) {
input, expected, nonce := loadNativeModelCacheFixture(t)
encrypted, err := encryptQMCV1ForTest([]byte(input.Plain), input.UID, nonce)
if err != nil {
t.Fatalf("fixture plaintext length %d nonce length %d: native encrypt returned kind %q", len(input.Plain), len(nonce), protocolErrorKindOf(err))
}
if encrypted != expected.Encrypted {
t.Fatalf("fixture encryption mismatch: native length %d official length %d", len(encrypted), len(expected.Encrypted))
}
decrypted, err := (nativeModelCacheDecryptor{}).Decrypt(context.Background(), expected.Encrypted, input.UID)
if err != nil {
t.Fatalf("fixture encrypted length %d: native decrypt returned kind %q", len(expected.Encrypted), protocolErrorKindOf(err))
}
if string(decrypted) != expected.Decrypted {
t.Fatalf("fixture decryption mismatch: native length %d official length %d", len(decrypted), len(expected.Decrypted))
}
}
func TestNativeModelCacheOfficialFixtureMutationsFailClosed(t *testing.T) {
input, expected, _ := loadNativeModelCacheFixture(t)
envelope, err := decodeStrictStdBase64(expected.Encrypted)
if err != nil {
t.Fatalf("fixture encrypted length %d: strict decode failed", len(expected.Encrypted))
}
indices := []int{0, len(qmcV1Prefix) - 1, len(qmcV1Prefix), len(qmcV1Prefix) + qmcNonceSize, len(envelope) - 1}
for mutation, index := range indices {
changed := append([]byte(nil), envelope...)
changed[index] ^= 1
blob := base64.StdEncoding.EncodeToString(changed)
plain, err := (nativeModelCacheDecryptor{}).Decrypt(context.Background(), blob, input.UID)
if err == nil || protocolErrorKindOf(err) != protocolBackendIncompatible {
t.Fatalf("mutation %d at envelope offset %d: fail-closed category differs", mutation, index)
}
if plain != nil {
t.Fatalf("mutation %d at envelope offset %d: returned plaintext length %d", mutation, index, len(plain))
}
}
plain, err := (nativeModelCacheDecryptor{}).Decrypt(context.Background(), expected.Encrypted, "synthetic-wrong-fixture-user")
if err == nil || protocolErrorKindOf(err) != protocolBackendIncompatible {
t.Fatal("wrong synthetic fixture UID did not fail closed")
}
if plain != nil {
t.Fatalf("wrong synthetic fixture UID returned plaintext length %d", len(plain))
}
}
func loadNativeModelCacheFixture(t *testing.T) (modelCacheFixtureInput, modelCacheFixtureExpected, []byte) {
t.Helper()
_, fixture := loadProtocolFixture(t, "model-cache.json")
var input modelCacheFixtureInput
var expected modelCacheFixtureExpected
decodeExactJSON(t, fixture.Input, &input)
decodeExactJSON(t, fixture.Expected, &expected)
if len(fixture.Transcript.EntropyReads) != 1 {
t.Fatalf("fixture entropy read count = %d, want 1", len(fixture.Transcript.EntropyReads))
}
nonce := append([]byte(nil), fixture.Transcript.EntropyReads[0].Bytes...)
if fixture.Transcript.EntropyReads[0].Length != qmcNonceSize || len(nonce) != qmcNonceSize {
t.Fatalf("fixture nonce declared/decoded lengths = %d/%d, want %d", fixture.Transcript.EntropyReads[0].Length, len(nonce), qmcNonceSize)
}
return input, expected, nonce
}
func TestDecryptQMCV1RoundTripReturnsFreshPlaintext(t *testing.T) {
plain := []byte("synthetic model cache plaintext")
blob, err := encryptQMCV1ForTest(plain, "synthetic-user", []byte("nonce-12byte"))
if err != nil {
t.Fatalf("setup plaintext length %d: encrypt returned kind %q", len(plain), protocolErrorKindOf(err))
}
first, err := decryptQMCV1(blob, "synthetic-user")
if err != nil {
t.Fatalf("ciphertext length %d: decrypt returned kind %q", len(blob), protocolErrorKindOf(err))
}
if !bytes.Equal(first, plain) {
t.Fatalf("round-trip lengths plaintext=%d decrypted=%d", len(plain), len(first))
}
first[0] ^= 1
second, err := decryptQMCV1(blob, "synthetic-user")
if err != nil {
t.Fatalf("second ciphertext length %d: decrypt returned kind %q", len(blob), protocolErrorKindOf(err))
}
if !bytes.Equal(second, plain) {
t.Fatalf("second round-trip lengths plaintext=%d decrypted=%d", len(plain), len(second))
}
}
func TestNativeModelCacheDecryptorHonorsCanceledContextBeforeWork(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
plain, err := (nativeModelCacheDecryptor{}).Decrypt(ctx, "not-base64", "synthetic-user")
if !errors.Is(err, context.Canceled) {
t.Fatalf("cancellation error category = %T, want context cancellation", err)
}
if plain != nil {
t.Fatalf("canceled decrypt returned plaintext length %d", len(plain))
}
}
func TestNativeModelCacheRejectsMalformedEnvelopesSafely(t *testing.T) {
const uid = "SENTINEL-MODEL-CACHE-UID"
plain := []byte("SENTINEL-MODEL-CACHE-PLAINTEXT")
valid, err := encryptQMCV1ForTest(plain, uid, []byte("nonce-12byte"))
if err != nil {
t.Fatalf("setup plaintext length %d: encrypt returned kind %q", len(plain), protocolErrorKindOf(err))
}
validEnvelope, err := decodeStrictStdBase64(valid)
if err != nil {
t.Fatalf("setup encrypted length %d: strict decode failed", len(valid))
}
emptyValid, err := encryptQMCV1ForTest(nil, uid, []byte("nonce-12byte"))
if err != nil {
t.Fatalf("setup empty plaintext: encrypt returned kind %q", protocolErrorKindOf(err))
}
emptyEnvelope, err := decodeStrictStdBase64(emptyValid)
if err != nil {
t.Fatalf("setup empty encrypted length %d: strict decode failed", len(emptyValid))
}
encode := base64.StdEncoding.EncodeToString
wrongMagic := append([]byte(nil), validEnvelope...)
wrongMagic[0] ^= 1
wrongVersion := append([]byte(nil), validEnvelope...)
wrongVersion[len(qmcV1Prefix)-1]++
corruptCiphertext := append([]byte(nil), validEnvelope...)
corruptCiphertext[len(qmcV1Prefix)+qmcNonceSize] ^= 1
corruptTag := append([]byte(nil), validEnvelope...)
corruptTag[len(corruptTag)-1] ^= 1
missingPadding := strings.TrimRight(valid, "=")
if missingPadding == valid {
t.Fatal("setup encrypted envelope unexpectedly lacks padding")
}
tests := []struct {
name string
blob string
uid string
internal string
}{
{name: "empty Base64", blob: "", uid: uid, internal: "model cache envelope is too short"},
{name: "carriage return", blob: valid + "\r", uid: uid, internal: "model cache envelope encoding is invalid"},
{name: "line feed", blob: valid + "\n", uid: uid, internal: "model cache envelope encoding is invalid"},
{name: "URL-safe Base64", blob: "-_8=", uid: uid, internal: "model cache envelope encoding is invalid"},
{name: "missing padding", blob: missingPadding, uid: uid, internal: "model cache envelope encoding is invalid"},
{name: "trailing garbage", blob: valid + "garbage", uid: uid, internal: "model cache envelope encoding is invalid"},
{name: "wrong magic", blob: encode(wrongMagic), uid: uid, internal: "model cache envelope magic or version is unsupported"},
{name: "wrong version", blob: encode(wrongVersion), uid: uid, internal: "model cache envelope magic or version is unsupported"},
{name: "truncated nonce", blob: encode(append([]byte(qmcV1Prefix), make([]byte, qmcNonceSize-1+qmcTagSize)...)), uid: uid, internal: "model cache envelope is too short"},
{name: "truncated tag", blob: encode(emptyEnvelope[:len(emptyEnvelope)-1]), uid: uid, internal: "model cache envelope is too short"},
{name: "corrupt ciphertext", blob: encode(corruptCiphertext), uid: uid, internal: "model cache envelope authentication failed"},
{name: "corrupt tag", blob: encode(corruptTag), uid: uid, internal: "model cache envelope authentication failed"},
{name: "wrong UID", blob: valid, uid: "SENTINEL-WRONG-MODEL-CACHE-UID", internal: "model cache envelope authentication failed"},
}
for decodedLen := 1; decodedLen < len(qmcV1Prefix)+qmcNonceSize+qmcTagSize; decodedLen++ {
tests = append(tests, struct {
name string
blob string
uid string
internal string
}{
name: fmt.Sprintf("decoded length %d", decodedLen),
blob: encode(make([]byte, decodedLen)),
uid: uid,
internal: "model cache envelope is too short",
})
}
decryptor := nativeModelCacheDecryptor{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := decryptor.Decrypt(context.Background(), tt.blob, tt.uid)
if err == nil {
t.Fatalf("case %s blob length %d: decrypt succeeded with plaintext length %d", tt.name, len(tt.blob), len(got))
}
if got != nil {
t.Fatalf("case %s blob length %d: rejected decrypt returned plaintext length %d", tt.name, len(tt.blob), len(got))
}
if kind := protocolErrorKindOf(err); kind != protocolBackendIncompatible {
t.Fatalf("case %s blob length %d: error kind %q, want %q", tt.name, len(tt.blob), kind, protocolBackendIncompatible)
}
if err.Error() != "model cache data is incompatible" {
t.Fatalf("case %s blob length %d: public error category is unsafe", tt.name, len(tt.blob))
}
internal := protocolInternalError(err)
if internal == nil || internal.Error() != tt.internal {
t.Fatalf("case %s blob length %d: internal error category differs", tt.name, len(tt.blob))
}
for _, forbidden := range []string{uid, tt.uid, string(plain), tt.blob} {
if forbidden != "" && (strings.Contains(err.Error(), forbidden) || strings.Contains(internal.Error(), forbidden)) {
t.Fatalf("case %s blob length %d: error leaked a forbidden content marker", tt.name, len(tt.blob))
}
}
})
}
}
func TestEncryptQMCV1ForTestRejectsInvalidNonce(t *testing.T) {
for _, nonceLen := range []int{0, qmcNonceSize - 1, qmcNonceSize + 1} {
_, err := encryptQMCV1ForTest([]byte("synthetic payload"), "synthetic-user", make([]byte, nonceLen))
if err == nil {
t.Fatalf("nonce length %d: encrypt returned nil error", nonceLen)
}
if got := protocolErrorKindOf(err); got != protocolInvalidInput {
t.Fatalf("nonce length %d: error kind %q, want %q", nonceLen, got, protocolInvalidInput)
}
if err.Error() != "model cache input is invalid" {
t.Fatalf("nonce length %d: public error category is unsafe", nonceLen)
}
}
}
func TestEncryptQMCV1ForTestUsesV1Layout(t *testing.T) {
plain := []byte("synthetic model cache payload")
nonce := []byte("nonce-12byte")
plainBefore := append([]byte(nil), plain...)
nonceBefore := append([]byte(nil), nonce...)
blob, err := encryptQMCV1ForTest(plain, "synthetic-user", nonce)
if err != nil {
t.Fatalf("plaintext length %d nonce length %d: encrypt returned kind %q", len(plain), len(nonce), protocolErrorKindOf(err))
}
envelope, err := decodeStrictStdBase64(blob)
if err != nil {
t.Fatalf("encrypted length %d: strict decode failed", len(blob))
}
wantLen := len(qmcV1Prefix) + qmcNonceSize + len(plain) + qmcTagSize
if len(envelope) != wantLen {
t.Fatalf("envelope length = %d, want %d", len(envelope), wantLen)
}
if !bytes.Equal(envelope[:len(qmcV1Prefix)], []byte(qmcV1Prefix)) {
t.Fatal("envelope prefix differs from QMC v1")
}
if !bytes.Equal(envelope[len(qmcV1Prefix):len(qmcV1Prefix)+qmcNonceSize], nonce) {
t.Fatal("envelope nonce differs from caller nonce")
}
if !bytes.Equal(plain, plainBefore) || !bytes.Equal(nonce, nonceBefore) {
t.Fatal("test encrypt mutated caller input")
}
}