-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorpus_test.go
194 lines (179 loc) · 4.13 KB
/
corpus_test.go
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
package bsony
import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"path"
"strings"
"testing"
)
var testDir = "testdata/bson-corpus"
var fct = New()
type validCase struct {
Description string
CanonicalBSON string `json:"canonical_bson"`
CanonicalExtJSON string `json:"canonical_extjson"`
RelaxedExtJSON string `json:"relaxed_extjson"`
DegenerateBSON string `json:"degenerate_bson"`
DegenerateExtJSON string `json:"degenerate_extjson"`
ConvertedBSON string `json:"converted_bson"`
ConvertedExtJSON string `json:"converted_extjson"`
}
type errorCase struct {
Description string
Bson string
}
type corpusData struct {
Description string
BsonType string `json:"bson_type"`
TestKey string `json:"test_key"`
Valid []validCase
DecodeErrors []errorCase
}
func TestCorpus(t *testing.T) {
files, err := ioutil.ReadDir(testDir)
if err != nil {
t.Fatal("couldn't read corpus directory")
}
for _, f := range files {
if f.IsDir() || path.Ext(f.Name()) != ".json" {
continue
}
file := path.Join(testDir, f.Name())
t.Run(f.Name(), func(t *testing.T) { testCorpusFile(t, file) })
}
}
func shouldSkip(description string) bool {
// we don't validate UTF-8 within string-like types
patterns := []string{"invalid UTF-8", "bad UTF-8"}
for _, p := range patterns {
if strings.Contains(description, p) {
return true
}
}
return false
}
func testCorpusFile(t *testing.T, file string) {
t.Helper()
guts, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("couldn't read %s", file)
}
cases := &corpusData{}
json.Unmarshal(guts, cases)
for _, c := range cases.Valid {
t.Run("valid "+c.Description, func(t *testing.T) { testValidCase(t, c, cases.TestKey) })
}
for _, c := range cases.DecodeErrors {
if shouldSkip(c.Description) {
continue
}
t.Run("invalid "+c.Description, func(t *testing.T) { testErrorCase(t, c) })
}
}
func testValidCase(t *testing.T, c validCase, k string) {
t.Helper()
cB := strings.ToLower(c.CanonicalBSON)
cB2 := strings.ToLower(BSONToBSON(t, cB))
if cB != cB2 {
t.Errorf("native_to_bson( bson_to_native(cB) ) != cB\n Got: %s\nWant: %s", cB2, cB)
}
}
func testErrorCase(t *testing.T, c errorCase) {
t.Helper()
doc, err := docFromHex(t, c.Bson)
if err != nil {
return
}
err = visitDoc(t, doc)
if err != nil {
return
}
t.Fatal("expected error, but got none")
}
func visitDoc(t *testing.T, d *Doc) error {
iter := d.Iter()
for iter.Next() {
if iter.Err() != nil {
return iter.Err()
}
switch iter.Type() {
case TypeEmbeddedDocument:
d, _ := iter.ValueUnsafe().Get().(*Doc)
err := visitDoc(t, d)
if err != nil {
return err
}
case TypeArray:
a, _ := iter.ValueUnsafe().Get().(*Array)
err := visitArray(t, a)
if err != nil {
return err
}
case TypeCodeWithScope:
cs, _ := iter.ValueUnsafe().Get().(CodeWithScope)
err := visitDoc(t, cs.Scope)
if err != nil {
return err
}
}
}
return nil
}
func visitArray(t *testing.T, a *Array) error {
iter := a.Iter()
for iter.Next() {
if iter.Err() != nil {
return iter.Err()
}
switch iter.Type() {
case TypeEmbeddedDocument:
d, _ := iter.ValueUnsafe().Get().(*Doc)
err := visitDoc(t, d)
if err != nil {
return err
}
case TypeArray:
a, _ := iter.ValueUnsafe().Get().(*Array)
err := visitArray(t, a)
if err != nil {
return err
}
case TypeCodeWithScope:
cs, _ := iter.ValueUnsafe().Get().(CodeWithScope)
err := visitDoc(t, cs.Scope)
if err != nil {
return err
}
}
}
return nil
}
func docFromHex(t *testing.T, s string) (*Doc, error) {
t.Helper()
raw, err := hex.DecodeString(s)
if err != nil {
t.Fatalf("error decoding %s", s)
}
return fct.NewDocFromBytes(raw)
}
func BSONToBSON(t *testing.T, s string) string {
t.Helper()
cB, err := docFromHex(t, s)
if err != nil {
t.Fatal(err)
}
defer func() { cB.Release() }()
cB2 := fct.NewDoc()
defer func() { cB2.Release() }()
iter := cB.Iter()
for iter.Next() {
cB2.Add(iter.Key(), iter.Get())
}
if cB2.Err() != nil {
t.Fatalf("error copying BSON doc: %v", cB2.Err())
}
buf := make([]byte, cB2.Len())
cB2.CopyTo(buf)
return hex.EncodeToString(buf)
}