-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnative_model_cache_fuzz_test.go
More file actions
77 lines (72 loc) · 2.5 KB
/
Copy pathnative_model_cache_fuzz_test.go
File metadata and controls
77 lines (72 loc) · 2.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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func FuzzModelCacheEnvelope(f *testing.F) {
encoded, err := os.ReadFile(filepath.Join(protocolFixtureDir(), "model-cache.json"))
if err != nil {
f.Fatalf("read model-cache fixture: %v", err)
}
var fixture protocolFixture
if err := json.Unmarshal(encoded, &fixture); err != nil {
f.Fatalf("decode model-cache fixture: %v", err)
}
var expected modelCacheFixtureExpected
if err := json.Unmarshal(fixture.Expected, &expected); err != nil {
f.Fatalf("decode model-cache expected fixture: %v", err)
}
officialEnvelope, err := decodeStrictStdBase64(expected.Encrypted)
if err != nil {
f.Fatalf("official encrypted length %d: strict decode failed", len(expected.Encrypted))
}
wrongVersion := append([]byte(nil), officialEnvelope...)
wrongVersion[len(qmcV1Prefix)-1]++
corruptTag := append([]byte(nil), officialEnvelope...)
corruptTag[len(corruptTag)-1] ^= 1
truncatedNonce := append([]byte(qmcV1Prefix), make([]byte, qmcNonceSize-1)...)
truncatedTag := append([]byte(qmcV1Prefix), make([]byte, qmcNonceSize+qmcTagSize-1)...)
missingPadding := strings.TrimRight(expected.Encrypted, "=")
if missingPadding == expected.Encrypted {
f.Fatal("official encrypted fixture unexpectedly lacks padding")
}
for _, seed := range []string{
expected.Encrypted,
"",
base64.StdEncoding.EncodeToString([]byte("QMC")),
base64.StdEncoding.EncodeToString(wrongVersion),
base64.StdEncoding.EncodeToString(truncatedNonce),
base64.StdEncoding.EncodeToString(truncatedTag),
base64.StdEncoding.EncodeToString(corruptTag),
missingPadding,
expected.Encrypted + "\r",
expected.Encrypted + "\n",
} {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, blob string) {
if len(blob) > 1<<20 {
return
}
plain, err := (nativeModelCacheDecryptor{}).Decrypt(t.Context(), blob, protocolFixtureUID)
if err != nil {
return
}
reencrypted, err := encryptQMCV1ForTest(plain, protocolFixtureUID, []byte("fuzz-nonce12"))
if err != nil {
t.Fatalf("successful decrypt plaintext length %d: re-encrypt returned kind %q", len(plain), protocolErrorKindOf(err))
}
roundTrip, err := (nativeModelCacheDecryptor{}).Decrypt(t.Context(), reencrypted, protocolFixtureUID)
if err != nil {
t.Fatalf("re-encrypted length %d: decrypt returned kind %q", len(reencrypted), protocolErrorKindOf(err))
}
if !bytes.Equal(roundTrip, plain) {
t.Fatalf("round-trip plaintext lengths got=%d want=%d", len(roundTrip), len(plain))
}
})
}