Skip to content

Commit 765e5b6

Browse files
committed
Add more test coverage for searialization and deserialization
1 parent 3b54170 commit 765e5b6

File tree

3 files changed

+251
-3
lines changed

3 files changed

+251
-3
lines changed

fieldpath/serialize-pe_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ limitations under the License.
1616

1717
package fieldpath
1818

19-
import "testing"
19+
import (
20+
"testing"
21+
)
2022

2123
func TestPathElementRoundTrip(t *testing.T) {
2224
tests := []string{
@@ -25,8 +27,10 @@ func TestPathElementRoundTrip(t *testing.T) {
2527
`f:`,
2628
`f:spec`,
2729
`f:more-complicated-string`,
30+
`f: string-with-spaces `,
2831
`f:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`,
2932
`k:{"name":"my-container"}`,
33+
`k:{"name":" name with spaces "}`,
3034
`k:{"port":"8080","protocol":"TCP"}`,
3135
`k:{"optionalField":null}`,
3236
`k:{"jsonField":{"A":1,"B":null,"C":"D","E":{"F":"G"}}}`,
@@ -35,6 +39,7 @@ func TestPathElementRoundTrip(t *testing.T) {
3539
`v:"some-string"`,
3640
`v:1234`,
3741
`v:{"some":"json"}`,
42+
`v:{"some":" some with spaces "}`,
3843
`k:{"name":"app-🚀"}`,
3944
`k:{"name":"app-💻"}`,
4045
`k:{"name":"app with-unicøde"}`,
@@ -79,6 +84,7 @@ func TestDeserializePathElementError(t *testing.T) {
7984
`v:`,
8085
`k:invalid json`,
8186
`k:{"name":invalid}`,
87+
`v:{"some":" \x41"}`, // This is an invalid JSON string because \x41 is not a valid escape sequence.
8288
}
8389

8490
for _, test := range tests {

fieldpath/serialize_test.go

Lines changed: 243 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"fmt"
2222
"strings"
2323
"testing"
24+
25+
"github.com/google/go-cmp/cmp"
2426
)
2527

2628
func TestSerializeV1(t *testing.T) {
@@ -51,7 +53,7 @@ func TestSerializeV1GoldenData(t *testing.T) {
5153
examples := []string{
5254
`{"f:aaa":{},"f:aab":{},"f:aac":{},"f:aad":{},"f:aae":{},"f:aaf":{},"k:{\"name\":\"first\"}":{},"k:{\"name\":\"second\"}":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{},"k:{\"port\":443,\"protocol\":\"udp\"}":{},"v:1":{},"v:2":{},"v:3":{},"v:\"aa\"":{},"v:\"ab\"":{},"v:true":{},"i:1":{},"i:2":{},"i:3":{},"i:4":{}}`,
5355
`{"f:aaa":{"k:{\"name\":\"second\"}":{"v:3":{"f:aab":{}}},"v:3":{},"v:true":{}},"f:aab":{"f:aaa":{},"f:aaf":{"k:{\"port\":443,\"protocol\":\"udp\"}":{"k:{\"port\":443,\"protocol\":\"tcp\"}":{}}},"k:{\"name\":\"first\"}":{}},"f:aac":{"f:aaa":{"v:1":{}},"f:aac":{},"v:3":{"k:{\"name\":\"second\"}":{}}},"f:aad":{"f:aac":{"v:1":{}},"f:aaf":{"k:{\"name\":\"first\"}":{"k:{\"name\":\"first\"}":{}}},"i:1":{"i:1":{},"i:3":{"v:true":{}}}},"f:aae":{"f:aae":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{"k:{\"port\":443,\"protocol\":\"udp\"}":{}},"i:4":{"f:aaf":{}}},"f:aaf":{"i:1":{"f:aac":{}},"i:2":{},"i:3":{}},"k:{\"name\":\"first\"}":{"f:aad":{"f:aaf":{}}},"k:{\"port\":443,\"protocol\":\"tcp\"}":{"f:aaa":{"f:aad":{}}},"k:{\"port\":443,\"protocol\":\"udp\"}":{"f:aac":{},"k:{\"name\":\"first\"}":{"i:3":{}},"k:{\"port\":443,\"protocol\":\"udp\"}":{"i:4":{}}},"v:1":{"f:aac":{"i:4":{}},"f:aaf":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{}},"v:2":{"f:aad":{"f:aaf":{}},"i:1":{}},"v:3":{"f:aaa":{},"k:{\"name\":\"first\"}":{},"i:2":{}},"v:\"aa\"":{"f:aab":{"f:aaf":{}},"f:aae":{},"k:{\"name\":\"first\"}":{"f:aad":{}},"i:2":{}},"v:\"ab\"":{"f:aaf":{"i:4":{}},"k:{\"port\":443,\"protocol\":\"tcp\"}":{},"k:{\"port\":443,\"protocol\":\"udp\"}":{},"v:1":{"k:{\"port\":443,\"protocol\":\"udp\"}":{}},"i:1":{"f:aae":{"i:4":{}}}},"v:true":{"k:{\"name\":\"second\"}":{"f:aaa":{}},"i:2":{"k:{\"port\":443,\"protocol\":\"tcp\"}":{}}},"i:1":{"i:3":{"f:aaf":{}}},"i:2":{"f:aae":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{"v:1":{}}},"i:3":{"f:aab":{"v:true":{"v:\"aa\"":{}}},"f:aaf":{},"i:1":{}},"i:4":{"v:\"aa\"":{"f:aab":{"k:{\"name\":\"second\"}":{}}}}}`,
54-
`{"f:spec":{".":{},"f:apps":{".":{},"k:{\"name\":\"app-💻\"}":{".":{},"f:container":{".":{},"f:image":{},"f:name":{},"f:ports":{".":{},"k:{\"name\":\"port-🔑\"}":{".":{},"f:containerPort":{},"f:name":{}}}},"f:name":{}},"k:{\"name\":\"app-🚀\"}":{".":{},"f:container":{".":{},"f:image":{},"f:name":{},"f:ports":{".":{},"k:{\"name\":\"port-✅\"}":{".":{},"f:containerPort":{},"f:name":{}}}},"f:name":{}}}}}`,
56+
`{"f:spec":{".":{},"f:apps":{".":{},"k:{\"name\":\" app-💻\"}":{".":{},"f:container":{".":{},"f:image":{},"f:name":{},"f:ports":{".":{},"k:{\"name\":\"port 🔑\"}":{".":{},"f:containerPort":{},"f:name":{}}}},"f:name":{}},"k:{\"name\":\" app-🚀\"}":{".":{},"f:container":{".":{},"f:image":{},"f:name":{},"f:ports":{".":{},"k:{\"name\":\"port-✅ \"}":{".":{},"f:containerPort":{},"f:name":{}}}},"f:name":{}}}}}`,
5557
}
5658
for i, str := range examples {
5759
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
@@ -71,6 +73,246 @@ func TestSerializeV1GoldenData(t *testing.T) {
7173
}
7274
}
7375

76+
func TestDeserializeForValidNonNormalized(t *testing.T) {
77+
testCases := []struct {
78+
nonNormalizedString string
79+
normalizedString string
80+
}{
81+
{
82+
nonNormalizedString: `{
83+
"f:aad": {},
84+
"f:aaa": {},
85+
"f:aab": {},
86+
"f:aac": {},
87+
"f:aae": {},
88+
"f:aaf": {},
89+
90+
91+
"k:{\"name\":\"first\"}": {},
92+
"k:{\"name\":\"second\"}": {},
93+
"k:{\"port\":443,\"protocol\":\"tcp\"}": {},
94+
"k:{ \"protocol\":\"udp\",\"port\":443}": {},
95+
"v:1": {},
96+
97+
"v:2": {},
98+
"v: 3": {},
99+
"v:\"aa\"": {},
100+
101+
"v:\"ab\"": {},
102+
"v:true": {},
103+
"i:1": {},
104+
"i:2": {},
105+
"i:3": {} ,
106+
"i:4": {}
107+
}`,
108+
normalizedString: `{"f:aaa":{},"f:aab":{},"f:aac":{},"f:aad":{},"f:aae":{},"f:aaf":{},"k:{\"name\":\"first\"}":{},"k:{\"name\":\"second\"}":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{},"k:{\"port\":443,\"protocol\":\"udp\"}":{},"v:1":{},"v:2":{},"v:3":{},"v:\"aa\"":{},"v:\"ab\"":{},"v:true":{},"i:1":{},"i:2":{},"i:3":{},"i:4":{}}`,
109+
}, {
110+
nonNormalizedString: `{
111+
"f:aaa": {
112+
"k:{\"name\":\"second\"}": {
113+
"v:3": {
114+
"f:aab": {}
115+
}
116+
},
117+
"v:3": {},
118+
"v:true": {}
119+
},
120+
"f:aab": {
121+
"f:aaa": {},
122+
"f:aaf": {
123+
"k:{\"port\":443,\"protocol\":\"udp\"}": {
124+
"k:{\"port\":443,\"protocol\":\"tcp\"}": {}
125+
}
126+
},
127+
"k:{\"name\":\"first\"}": {}
128+
},
129+
"f:aac": {
130+
"f:aaa": {
131+
"v:1": {}
132+
},
133+
"f:aac": {},
134+
"v:3": {
135+
"k:{\"name\":\"second\"}": {}
136+
}
137+
},
138+
"f:aad": {
139+
"f:aac": {
140+
"v:1": {}
141+
},
142+
"f:aaf": {
143+
"k:{\"name\":\"first\"}": {
144+
"k:{\"name\":\"first\"}": {}
145+
}
146+
},
147+
"i:1": {
148+
"i:1": {},
149+
"i:3": {
150+
"v:true": {}
151+
}
152+
}
153+
},
154+
"f:aae": {
155+
"f:aae": {},
156+
"k:{\"port\":443,\"protocol\":\"tcp\"}": {
157+
"k:{\"port\":443,\"protocol\":\"udp\"}": {}
158+
},
159+
"i:4": {
160+
"f:aaf": {}
161+
}
162+
},
163+
"f:aaf": {
164+
"i:1": {
165+
"f:aac": {}
166+
},
167+
"i:3": {},
168+
"i:2": {}
169+
},
170+
"k:{\"name\":\"first\"}": {
171+
"f:aad": {
172+
"f:aaf": {}
173+
}
174+
},
175+
"k:{\"port\":443,\"protocol\":\"tcp\"}": {
176+
"f:aaa": {
177+
"f:aad": {}
178+
}
179+
},
180+
"k:{\"port\":443,\"protocol\":\"udp\"}": {
181+
"f:aac": {},
182+
"k:{\"name\":\"first\"}": {
183+
"i:3": {}
184+
},
185+
"k:{\"port\":443,\"protocol\":\"udp\"}": {
186+
"i:4": {}
187+
}
188+
},
189+
"v:2": {
190+
"f:aad": {
191+
"f:aaf": {}
192+
},
193+
"i:1": {}
194+
},
195+
"v:1": {
196+
"f:aac": {
197+
"i:4": {}
198+
},
199+
"f:aaf": {},
200+
"k:{\"port\":443,\"protocol\":\"tcp\"}": {}
201+
},
202+
"v:3": {
203+
"f:aaa": {},
204+
"k:{\"name\":\"first\"}": {},
205+
"i:2": {}
206+
},
207+
"v:\"aa\"": {
208+
"f:aab": {
209+
"f:aaf": {}
210+
},
211+
"f:aae": {},
212+
"k:{\"name\":\"first\"}": {
213+
"f:aad": {}
214+
},
215+
"i:2": {}
216+
},
217+
"v:\"ab\"": {
218+
"f:aaf": {
219+
"i:4": {}
220+
},
221+
"k:{\"port\":443,\"protocol\":\"tcp\"}": {},
222+
"k:{\"port\":443,\"protocol\":\"udp\"}": {},
223+
"v:1": {
224+
"k:{\"port\":443,\"protocol\":\"udp\"}": {}
225+
},
226+
"i:1": {
227+
"f:aae": {
228+
"i:4": {}
229+
}
230+
}
231+
},
232+
233+
"v:true": {
234+
"k:{\"name\":\"second\"}": {
235+
"f:aaa": {}
236+
},
237+
"i:2": {
238+
"k:{\"port\":443,\"protocol\":\"tcp\"}": {}
239+
}
240+
},
241+
242+
243+
"i:1": {
244+
"i:3": {
245+
"f:aaf": {}
246+
}
247+
},
248+
249+
"i:3": {
250+
"f:aab": {
251+
"v:true": {
252+
"v:\"aa\"": {}
253+
}
254+
},
255+
"f:aaf": {},
256+
"i:1": {}
257+
},
258+
259+
"i:2": {
260+
"f:aae": {},
261+
"k:{\"port\":443,\"protocol\":\"tcp\"}": {
262+
"v:1": {}
263+
}
264+
},
265+
266+
"i:4": {
267+
"v:\"aa\"": {
268+
"f:aab": {
269+
"k:{\"name\":\"second\"}": {}
270+
}
271+
}
272+
}
273+
}`,
274+
normalizedString: `{"f:aaa":{"k:{\"name\":\"second\"}":{"v:3":{"f:aab":{}}},"v:3":{},"v:true":{}},"f:aab":{"f:aaa":{},"f:aaf":{"k:{\"port\":443,\"protocol\":\"udp\"}":{"k:{\"port\":443,\"protocol\":\"tcp\"}":{}}},"k:{\"name\":\"first\"}":{}},"f:aac":{"f:aaa":{"v:1":{}},"f:aac":{},"v:3":{"k:{\"name\":\"second\"}":{}}},"f:aad":{"f:aac":{"v:1":{}},"f:aaf":{"k:{\"name\":\"first\"}":{"k:{\"name\":\"first\"}":{}}},"i:1":{"i:1":{},"i:3":{"v:true":{}}}},"f:aae":{"f:aae":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{"k:{\"port\":443,\"protocol\":\"udp\"}":{}},"i:4":{"f:aaf":{}}},"f:aaf":{"i:1":{"f:aac":{}},"i:2":{},"i:3":{}},"k:{\"name\":\"first\"}":{"f:aad":{"f:aaf":{}}},"k:{\"port\":443,\"protocol\":\"tcp\"}":{"f:aaa":{"f:aad":{}}},"k:{\"port\":443,\"protocol\":\"udp\"}":{"f:aac":{},"k:{\"name\":\"first\"}":{"i:3":{}},"k:{\"port\":443,\"protocol\":\"udp\"}":{"i:4":{}}},"v:1":{"f:aac":{"i:4":{}},"f:aaf":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{}},"v:2":{"f:aad":{"f:aaf":{}},"i:1":{}},"v:3":{"f:aaa":{},"k:{\"name\":\"first\"}":{},"i:2":{}},"v:\"aa\"":{"f:aab":{"f:aaf":{}},"f:aae":{},"k:{\"name\":\"first\"}":{"f:aad":{}},"i:2":{}},"v:\"ab\"":{"f:aaf":{"i:4":{}},"k:{\"port\":443,\"protocol\":\"tcp\"}":{},"k:{\"port\":443,\"protocol\":\"udp\"}":{},"v:1":{"k:{\"port\":443,\"protocol\":\"udp\"}":{}},"i:1":{"f:aae":{"i:4":{}}}},"v:true":{"k:{\"name\":\"second\"}":{"f:aaa":{}},"i:2":{"k:{\"port\":443,\"protocol\":\"tcp\"}":{}}},"i:1":{"i:3":{"f:aaf":{}}},"i:2":{"f:aae":{},"k:{\"port\":443,\"protocol\":\"tcp\"}":{"v:1":{}}},"i:3":{"f:aab":{"v:true":{"v:\"aa\"":{}}},"f:aaf":{},"i:1":{}},"i:4":{"v:\"aa\"":{"f:aab":{"k:{\"name\":\"second\"}":{}}}}}`,
275+
},
276+
}
277+
278+
for i, tc := range testCases {
279+
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
280+
nonNormSet := NewSet()
281+
err := nonNormSet.FromJSON(strings.NewReader(tc.nonNormalizedString))
282+
if err != nil {
283+
t.Fatalf("Failed to deserialize non normalized string %s : %v\n%#v", tc.nonNormalizedString, err, nonNormSet)
284+
}
285+
286+
normSet := NewSet()
287+
err = normSet.FromJSON(strings.NewReader(tc.normalizedString))
288+
if err != nil {
289+
t.Fatalf("Failed to deserialize non normalized string %s : %v\n%#v", tc.normalizedString, err, normSet)
290+
}
291+
292+
nonNormSetJson, err := nonNormSet.ToJSON()
293+
if err != nil {
294+
t.Fatalf("Failed to serialize non-normalized set: %v\n%#v", err, nonNormSet)
295+
296+
}
297+
298+
normSetJson, err := normSet.ToJSON()
299+
if err != nil {
300+
t.Fatalf("Failed to serialize normalized set: %v\n%#v", err, normSet)
301+
302+
}
303+
304+
if diff := cmp.Diff(nonNormSetJson, normSetJson); diff != "" {
305+
t.Errorf("diff should be non empty, diff %s", diff)
306+
}
307+
308+
// this should exceeds if the diff was "".
309+
if !normSet.Equals(nonNormSet) {
310+
t.Errorf("expected both set to be equal.")
311+
}
312+
313+
})
314+
}
315+
}
74316
func TestDropUnknown(t *testing.T) {
75317
input := `{"f:aaa":{},"r:aab":{}}`
76318
expect := `{"f:aaa":{}}`

fieldpath/set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var randomPathMaker = randomPathAlphabet(MakePathOrDie(
6161
KeyByFields("name", "second"),
6262
KeyByFields("port", 443, "protocol", "tcp"),
6363
KeyByFields("port", 443, "protocol", "udp"),
64-
KeyByFields("key", "value"),
64+
KeyByFields("key", " value with spaces "),
6565
KeyByFields("lang", "en-US"),
6666
KeyByFields("unicode-key", "unicode-value-🔥"),
6767
// Values

0 commit comments

Comments
 (0)