-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnative_runtime_fields_fuzz_test.go
More file actions
68 lines (64 loc) · 2.08 KB
/
Copy pathnative_runtime_fields_fuzz_test.go
File metadata and controls
68 lines (64 loc) · 2.08 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
package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func FuzzRuntimeFieldsEnvelope(f *testing.F) {
fixtureBytes, err := os.ReadFile(filepath.Join(protocolFixtureDir(), "runtime-fields.json"))
if err != nil {
f.Fatalf("read official runtime fixture: %v", err)
}
var fixture protocolFixture
if err := decodeStrictJSON(fixtureBytes, &fixture); err != nil {
f.Fatal("decode official runtime fixture")
}
var expected runtimeFixtureExpected
if err := decodeStrictJSON(fixture.Expected, &expected); err != nil {
f.Fatal("decode official runtime fixture output")
}
seeds := [][]byte{
[]byte(expected.Raw),
[]byte(`{}`),
[]byte(`{"key":"synthetic-key"}`),
[]byte(`{"encrypt_user_info":"synthetic-a","encrypt_user_info":"synthetic-b","key":"synthetic-key"}`),
[]byte(`{"encrypt_user_info":"synthetic-encrypted","key":"synthetic-key","unknown":"synthetic"}`),
[]byte(`{"encrypt_user_info":"synthetic-encrypted","key":"synthetic-key"}{}`),
[]byte(`{"encrypt_user_info":`),
{0xff, 0xfe},
[]byte(`{"encrypt_user_info":"a","key":"b"}`),
[]byte(`{"encrypt_user_info":"` + strings.Repeat("a", 64<<10) + `","key":"` + strings.Repeat("b", 64<<10) + `"}`),
[]byte(strings.Repeat("x", (1<<20)+1)),
}
for _, seed := range seeds {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, raw []byte) {
if len(raw) > 1<<20 {
return
}
output, err := decodeRuntimeFieldOutputJSON(raw)
if err != nil {
return
}
if output.EncryptUserInfo == "" || output.Key == "" {
t.Fatalf("successful parse of input length %d returned an empty field", len(raw))
}
canonical, err := json.Marshal(runtimeFieldOutputWire{
EncryptUserInfo: output.EncryptUserInfo,
Key: output.Key,
})
if err != nil {
t.Fatalf("successful parse of input length %d could not be re-marshaled", len(raw))
}
reparsed, err := decodeRuntimeFieldOutputJSON(canonical)
if err != nil {
t.Fatalf("canonical output length %d could not be parsed", len(canonical))
}
if reparsed != output {
t.Fatalf("canonical output length %d changed parsed fields", len(canonical))
}
})
}