-
Notifications
You must be signed in to change notification settings - Fork 148
/
main_test.go
289 lines (256 loc) · 7.3 KB
/
main_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
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
package main
import (
"bytes"
"log"
"os"
"regexp"
"testing"
)
var (
testInputFile = "./pb/test.pb.go"
testInputFileTemp = "./pb/test.pb.go_tmp"
)
var testsTagFromComment = []struct {
comment string
tag string
}{
{comment: `//@gotags: valid:"abc"`, tag: `valid:"abc"`},
{comment: `// @gotags: valid:"abcd"`, tag: `valid:"abcd"`},
{comment: `// @gotags: valid:"xyz"`, tag: `valid:"xyz"`},
{comment: `// fdsafsa`, tag: ""},
{comment: `//@gotags:`, tag: ""},
{comment: `// @gotags: json:"abc" yaml:"abc`, tag: `json:"abc" yaml:"abc`},
{comment: `// test @gotags: json:"abc" yaml:"abc`, tag: `json:"abc" yaml:"abc`},
{comment: `// test @inject_tags: json:"abc" yaml:"abc`, tag: `json:"abc" yaml:"abc`},
}
func FuzzTagFromComment(f *testing.F) {
for _, test := range testsTagFromComment {
f.Add(test.comment)
}
f.Fuzz(func(t *testing.T, orig string) {
_ = tagFromComment(orig)
})
}
func TestTagFromComment(t *testing.T) {
for _, test := range testsTagFromComment {
if result := tagFromComment(test.comment); result != test.tag {
t.Errorf("expected tag: %q, got: %q", test.tag, result)
}
}
}
func FuzzParseWriteFile(f *testing.F) {
contents, err := os.ReadFile(testInputFile)
if err != nil {
f.Fatal(err)
}
f.Add(contents)
f.Fuzz(func(t *testing.T, orig []byte) {
areas, err := parseFile("placeholder.pb.go", orig, nil)
if err == nil {
for _, area := range areas {
_ = injectTag(orig, area, false) // Test without annotation removal.
_ = injectTag(orig, area, true) // Test with annotation removal.
}
}
})
}
func TestParseWriteFile(t *testing.T) {
expectedTag := `valid:"ip" yaml:"ip" json:"overrided"`
areas, err := parseFile(testInputFile, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(areas) != 9 {
t.Fatalf("expected 9 areas to replace, got: %d", len(areas))
}
area := areas[0]
if area.InjectTag != expectedTag {
t.Errorf("expected tag: %q, got: %q", expectedTag, area.InjectTag)
}
// make a copy of test file
contents, err := os.ReadFile(testInputFile)
if err != nil {
t.Fatal(err)
}
if err = os.WriteFile(testInputFileTemp, contents, 0o644); err != nil {
t.Fatal(err)
}
defer os.Remove(testInputFileTemp)
if err = writeFile(testInputFileTemp, areas, false); err != nil {
t.Fatal(err)
}
newAreas, err := parseFile(testInputFileTemp, nil, nil)
if len(newAreas) != len(areas) {
t.Errorf("the comment tag has error")
}
if err != nil {
t.Fatal(err)
}
// check if file contains custom tag
contents, err = os.ReadFile(testInputFileTemp)
if err != nil {
t.Fatal(err)
}
expectedExpr := "Address[ \t]+string[ \t]+`protobuf:\"bytes,1,opt,name=Address,proto3\" json:\"overrided\" valid:\"ip\" yaml:\"ip\"`"
matched, err := regexp.Match(expectedExpr, contents)
if err != nil || matched != true {
t.Error("file doesn't contains custom tag after writing")
t.Log(string(contents))
}
}
func TestParseWriteFileClearCommon(t *testing.T) {
expectedTag := `valid:"ip" yaml:"ip" json:"overrided"`
areas, err := parseFile(testInputFile, nil, nil)
if err != nil {
t.Fatal(err)
}
if len(areas) != 9 {
t.Fatalf("expected 9 areas to replace, got: %d", len(areas))
}
area := areas[0]
if area.InjectTag != expectedTag {
t.Errorf("expected tag: %q, got: %q", expectedTag, area.InjectTag)
}
// make a copy of test file
contents, err := os.ReadFile(testInputFile)
if err != nil {
t.Fatal(err)
}
if err = os.WriteFile(testInputFileTemp, contents, 0o644); err != nil {
t.Fatal(err)
}
defer os.Remove(testInputFileTemp)
if err = writeFile(testInputFileTemp, areas, true); err != nil {
t.Fatal(err)
}
newAreas, err := parseFile(testInputFileTemp, nil, nil)
if newAreas != nil {
t.Errorf("not clear tag")
}
if err != nil {
t.Fatal(err)
}
// check if file contains custom tag
contents, err = os.ReadFile(testInputFileTemp)
if err != nil {
t.Fatal(err)
}
expectedExpr := "Address[ \t]+string[ \t]+`protobuf:\"bytes,1,opt,name=Address,proto3\" json:\"overrided\" valid:\"ip\" yaml:\"ip\"`"
matched, err := regexp.Match(expectedExpr, contents)
if err != nil || matched != true {
t.Error("file doesn't contains custom tag after writing")
t.Log(string(contents))
}
}
var testsNewTagItems = []struct {
tag string
items tagItems
}{
{
tag: `valid:"ip" yaml:"ip, required" json:"overrided"`,
items: []tagItem{
{key: "valid", value: `"ip"`},
{key: "yaml", value: `"ip, required"`},
{key: "json", value: `"overrided"`},
},
},
{
tag: `validate:"omitempty,oneof=a b c d"`,
items: []tagItem{
{key: "validate", value: `"omitempty,oneof=a b c d"`},
},
},
}
func FuzzNewTagItems(f *testing.F) {
for _, test := range testsNewTagItems {
f.Add(test.tag)
}
f.Fuzz(func(t *testing.T, orig string) {
_ = newTagItems(orig)
})
}
func TestNewTagItems(t *testing.T) {
for _, test := range testsNewTagItems {
for i, item := range newTagItems(test.tag) {
if item.key != test.items[i].key || item.value != test.items[i].value {
t.Errorf("wrong tag item for tag %s, expected %v, got: %v",
test.tag, test.items[i], item)
}
}
}
}
func TestContinueParsingWhenSkippingFields(t *testing.T) {
expectedTags := []string{
`valid:"ip" yaml:"ip" json:"overrided"`,
`valid:"-"`,
`valid:"http|https"`,
`valid:"nonzero"`,
`validate:"omitempty"`,
`xml:"-"`,
`validate:"omitempty"`,
`tag:"foo_bar"`,
`tag:"foo"`,
`tag:"bar"`,
}
areas, err := parseFile(testInputFile, nil, []string{"xml"})
if err != nil {
t.Fatal(err)
}
if len(areas) != len(expectedTags) {
t.Fatalf("expected %d areas to replace, got: %d", len(expectedTags), len(areas))
}
for i, a := range areas {
if a.InjectTag != expectedTags[i] {
t.Errorf("expected tag: %q, got: %q", expectedTags[i], a.InjectTag)
}
}
// make a copy of test file
contents, err := os.ReadFile(testInputFile)
if err != nil {
t.Fatal(err)
}
if err = os.WriteFile(testInputFileTemp, contents, 0o644); err != nil {
t.Fatal(err)
}
defer os.Remove(testInputFileTemp)
if err = writeFile(testInputFileTemp, areas, false); err != nil {
t.Fatal(err)
}
// check if file contains custom tags
contents, err = os.ReadFile(testInputFileTemp)
if err != nil {
t.Fatal(err)
}
expectedExprs := []string{
"Address[ \t]+string[ \t]+`protobuf:\"[^\"]+\" json:\"overrided\" valid:\"ip\" yaml:\"ip\"`",
"Address[ \t]+string[ \t]+`protobuf:\"[^\"]+\" json:\"overrided\" valid:\"ip\" yaml:\"ip\"`",
"Scheme[ \t]+string[ \t]+`protobuf:\"[^\"]+\" json:\"scheme,omitempty\" valid:\"http|https\"`",
"Port[ \t]+int32[ \t]+`protobuf:\"[^\"]+\" json:\"port,omitempty\" valid:\"nonzero\"`",
"FooBar[ \t]+isOneOfObject_FooBar[ \t]+`protobuf_oneof:\"[^\"]+\" tag:\"foo_bar\"`",
"Foo[ \t]+string[ \t]+`protobuf:\"[^\"]+\" tag:\"foo\"`",
"Bar[ \t]+int64[ \t]+`protobuf:\"[^\"]+\" tag:\"bar\"`",
"XXX_Deprecated[ \t]+string[ \t]+`protobuf:\"[^\"]+\" json:\"XXX__deprecated,omitempty\" xml:\"-\"`",
}
for i, expr := range expectedExprs {
matched, err := regexp.Match(expr, contents)
if err != nil || matched != true {
t.Errorf("file doesn't contains custom tag #%d after writing", i+1)
t.Log(string(contents))
break
}
}
}
func TestVerbose(t *testing.T) {
b := new(bytes.Buffer)
log.SetOutput(b)
verbose = false
logf("test")
if len(b.Bytes()) > 0 {
t.Errorf("verbose should be off")
}
verbose = true
logf("test")
if len(b.Bytes()) == 0 {
t.Errorf("verbose should be on")
}
}