-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdictionary_test.go
More file actions
173 lines (141 loc) · 3.82 KB
/
Copy pathdictionary_test.go
File metadata and controls
173 lines (141 loc) · 3.82 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
package httpsfv
import (
"reflect"
"strings"
"testing"
)
func TestMarshalDictionnary(t *testing.T) {
t.Parallel()
dict := NewDictionary()
add := []struct {
in string
expected Member
valid bool
}{
{"f_o1o3-", NewItem(10.0), true},
{"deleteme", NewItem(""), true},
{"*f0.o*", NewItem(""), true},
{"t", NewItem(true), true},
{"f", NewItem(false), true},
{"b", NewItem([]byte{0, 1}), true},
{"0foo", NewItem(""), false},
{"mAj", NewItem(""), false},
{"_foo", NewItem(""), false},
{"foo", NewItem(Token("é")), false},
}
var b strings.Builder
for _, d := range add {
vDict := NewDictionary()
vDict.Add(d.in, d.expected)
b.Reset()
if valid := vDict.marshalSFV(&b) == nil; valid != d.valid {
t.Errorf("(%v, %v).isValid() = %v; %v expected", d.in, d.expected, valid, d.valid)
}
if d.valid {
dict.Add(d.in, d.expected)
}
}
i := NewItem(123.0)
dict.Add("f_o1o3-", i)
newValue, _ := dict.Get("f_o1o3-")
if newValue != i {
t.Errorf(`Add("f_o1o3-") must overwrite the existing value`)
}
if !dict.Del("deleteme") {
t.Errorf(`Del("deleteme") must return true`)
}
if dict.Del("deleteme") {
t.Errorf(`the second call to Del("deleteme") must return false`)
}
if v, ok := dict.Get("*f0.o*"); v.(Item).Value != "" || !ok {
t.Errorf(`Get("*f0.o*") = %v, %v; "", true expected`, v, ok)
}
if v, ok := dict.Get("notexist"); v != nil || ok {
t.Errorf(`Get("notexist") = %v, %v; nil, false expected`, v, ok)
}
if k := dict.Names(); len(k) != 5 {
t.Errorf(`Names() = %v; {"f_o1o3-", "*f0.o*"} expected`, k)
}
m, _ := dict.Get("f_o1o3-")
i = m.(Item)
i.Params.Add("foo", 9.5)
b.Reset()
_ = dict.marshalSFV(&b)
if b.String() != `f_o1o3-=123.0;foo=9.5, *f0.o*="", t, f=?0, b=:AAE=:` {
t.Errorf(`Dictionnary.marshalSFV(): invalid serialization: %v`, b.String())
}
}
func TestUnmarshalDictionary(t *testing.T) {
t.Parallel()
d1 := NewDictionary()
d1.Add("a", NewItem(false))
d1.Add("b", NewItem(true))
c := NewItem(true)
c.Params.Add("foo", Token("bar"))
d1.Add("c", c)
d2 := NewDictionary()
d2.Add("aa", NewItem(DisplayString("")))
data := []struct {
in []string
expected *Dictionary
valid bool
}{
{[]string{"a=?0, b, c; foo=bar"}, d1, false},
{[]string{"a="}, nil, false},
{[]string{"a=?0, b", "c; foo=bar"}, d1, false},
{[]string{""}, NewDictionary(), false},
{[]string{"é"}, nil, true},
{[]string{`foo="é"`}, nil, true},
{[]string{`foo;é`}, nil, true},
{[]string{`f="foo" é`}, nil, true},
{[]string{`f="foo",`}, nil, true},
{[]string{`aa=%""`}, d2, false},
{[]string{`aa=%"`}, nil, true},
{[]string{`aa=%`}, nil, true},
}
for _, d := range data {
l, err := UnmarshalDictionary(d.in)
if d.valid && err == nil {
t.Errorf("UnmarshalDictionary(%s): error expected", d.in)
}
if !d.valid && !reflect.DeepEqual(d.expected, l) {
t.Errorf("UnmarshalDictionary(%s) = %v, %v; %v, <nil> expected", d.in, l, err, d.expected)
}
}
}
func FuzzUnmarshalDictionary(f *testing.F) {
testCases := []string{
"",
"a=?0, b, c; foo=bar",
"a=(1 2), b=3, c=4;aa=bb, d=(5 6);valid",
`aa=%""`,
`aa=%"K%c3%a9vin"`,
"a=@1659578233",
"a=@",
"a=",
"é",
`foo="é"`,
"a=1.9",
"a=:AAE=:",
}
for _, t := range testCases {
f.Add(t)
}
f.Fuzz(func(t *testing.T, b string) {
unmarshaled, err := UnmarshalDictionary([]string{b})
if err != nil {
return
}
reMarshaled, err := Marshal(unmarshaled)
if err != nil {
t.Errorf("Unexpected marshaling error %q for %q, %#v", err, b, unmarshaled)
}
reUnmarshaled, err := UnmarshalDictionary([]string{reMarshaled})
if err != nil {
t.Errorf("Unexpected remarshaling error %q for %q; original %q", err, reMarshaled, b)
}
if !reflect.DeepEqual(unmarshaled, reUnmarshaled) {
t.Errorf("Unmarshaled and re-unmarshaled doesn't match: %#v; %#v", unmarshaled, reUnmarshaled)
}
})
}