Debian Codegraph @

cache_test.go

raw symbols SCIP
1 package schema
2
3 import (
4 "reflect"
5 "testing"
6
7 utils "github.com/gofiber/utils/v2"
8 )
9
10 func TestNextPathSegment(t *testing.T) {
11 cases := []struct {
12 path string
13 start int
14 end int
15 segment string
16 wantErr bool
17 }{
18 {"a.b", 0, 1, "a", false},
19 {"a.b", 2, 3, "b", false},
20 {"abc.defgh", 0, 3, "abc", false},
21 // dot exactly on the SWAR word boundary
22 {"abcdefgh.x", 0, 8, "abcdefgh", false},
23 // segments longer than one SWAR word
24 {"averylongsegmentname.next", 0, 20, "averylongsegmentname", false},
25 {"averylongsegmentname.next", 21, 25, "next", false},
26 {"noseparatorhere", 0, 15, "noseparatorhere", false},
27 // non-ASCII bytes must pass through the word scan untouched
28 {"héllo.wörld", 0, 6, "héllo", false},
29 {"", 0, 0, "", true},
30 {".abc", 0, 0, "", true},
31 {"a..b", 2, 0, "", true},
32 }
33 for _, c := range cases {
34 end, segment, err := nextPathSegment(c.path, c.start)
35 if c.wantErr {
36 if err == nil {
37 t.Errorf("nextPathSegment(%q, %d): expected error", c.path, c.start)
38 }
39 continue
40 }
41 if err != nil {
42 t.Errorf("nextPathSegment(%q, %d): unexpected error %v", c.path, c.start, err)
43 continue
44 }
45 if end != c.end || segment != c.segment {
46 t.Errorf("nextPathSegment(%q, %d) = (%d, %q), want (%d, %q)",
47 c.path, c.start, end, segment, c.end, c.segment)
48 }
49 }
50 }
51
52 func BenchmarkNextPathSegment(b *testing.B) {
53 const path = "averylongsegmentname.middle.tail"
54 for b.Loop() {
55 for start := 0; ; {
56 end, _, err := nextPathSegment(path, start)
57 if err != nil || end == len(path) {
58 break
59 }
60 start = end + 1
61 }
62 }
63 }
64
65 // The setDefaults fast path must not be defeated by anonymous pointers the
66 // walk can never allocate (unexported ones).
67 func TestNeedsDefaultsWalk(t *testing.T) {
68 type hidden struct {
69 X string `schema:"x"`
70 }
71 type onlyUnexported struct {
72 *hidden
73 A string `schema:"a"`
74 }
75
76 d := NewDecoder()
77 if d.cache.get(reflect.TypeOf(onlyUnexported{})).needsDefaultsWalk {
78 t.Error("unexported anonymous pointer must not force the defaults walk")
79 }
80
81 type Inner struct {
82 Y string `schema:"y"`
83 }
84 type withExported struct {
85 *Inner
86 A string `schema:"a"`
87 }
88 if !d.cache.get(reflect.TypeOf(withExported{})).needsDefaultsWalk {
89 t.Error("exported anonymous pointer must keep the defaults walk (it allocates)")
90 }
91
92 type withDefaults struct {
93 A string `schema:"a,default:z"`
94 }
95 if !d.cache.get(reflect.TypeOf(withDefaults{})).needsDefaultsWalk {
96 t.Error("default tags must keep the defaults walk")
97 }
98 }
99
100 func BenchmarkParsePathCacheMiss(b *testing.B) {
101 type Nested struct {
102 Value string `schema:"value"`
103 }
104 type Outer struct {
105 Items []Nested `schema:"items"`
106 }
107 d := NewDecoder()
108 typ := reflect.TypeOf(Outer{})
109 b.ReportAllocs()
110 info := d.cache.get(typ)
111 for b.Loop() {
112 info.paths.Clear()
113 if _, err := d.cache.parsePath("items.0.value", typ); err != nil {
114 b.Fatal(err)
115 }
116 }
117 }
118
119 // Decode callers (e.g. Fiber binders) pass map keys aliasing reused request
120 // buffers; pathCache must clone the key so later buffer reuse cannot poison it.
121 func TestParsePathDetachesCacheKey(t *testing.T) {
122 // Use a slice-index path: statically-resolvable keys are served from the
123 // direct map and never stored in the sync.Map path cache.
124 type Item struct {
125 Value string `schema:"value"`
126 }
127 type S struct {
128 Items []Item `schema:"items"`
129 }
130 d := NewDecoder()
131 buf := []byte("items.0.value")
132 var s S
133 if err := d.Decode(&s, map[string][]string{utils.UnsafeString(buf): {"x"}}); err != nil {
134 t.Fatal(err)
135 }
136 copy(buf, "xtems.9.qalue") // simulate fasthttp buffer reuse mutating the key bytes
137
138 count := 0
139 d.cache.get(reflect.TypeOf(s)).paths.Range(func(key, _ any) bool {
140 count++
141 if k := key.(string); k != "items.0.value" {
142 t.Fatalf("path cache key mutated to %q; key must be cloned before caching", k)
143 }
144 return true
145 })
146 if count != 1 {
147 t.Fatalf("expected 1 cached path, got %d", count)
148 }
149 }