aboutsummaryrefslogtreecommitdiff
path: root/internal/kwcache/fuzz_test.go
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-09-14 20:10:01 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-09-14 20:10:01 +0200
commitaa24cfb344b1b3eaef7217d996359023cd72ba28 (patch)
treeee0d981d3add58451a83de13ac56be4a8b165eea /internal/kwcache/fuzz_test.go
parent00aae60378982902b871a87850e0ed427b28a347 (diff)
downloadkrino-aa24cfb344b1b3eaef7217d996359023cd72ba28.tar.gz
krino-aa24cfb344b1b3eaef7217d996359023cd72ba28.zip
plan 8: fuzz decoders; fold ẞ and invalid UTF-8 correctly, refuse non-UTF-8 paths in krino new
Diffstat (limited to 'internal/kwcache/fuzz_test.go')
-rw-r--r--internal/kwcache/fuzz_test.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/internal/kwcache/fuzz_test.go b/internal/kwcache/fuzz_test.go
new file mode 100644
index 0000000..0b0eced
--- /dev/null
+++ b/internal/kwcache/fuzz_test.go
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package kwcache
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+// FuzzLoad: a cache file of any content loads as a usable cache - never
+// nil, never a panic - and one that reports an error answers nothing. Its
+// answers can then be saved again.
+func FuzzLoad(f *testing.F) {
+ seed := New("fp")
+ seed.Store(a, map[string]bool{"k:acme": true, "k:x": false})
+ path := filepath.Join(f.TempDir(), "seed.cache")
+ if err := seed.Save(path, []ID{a}); err != nil {
+ f.Fatal(err)
+ }
+ good, err := os.ReadFile(path)
+ if err != nil {
+ f.Fatal(err)
+ }
+ f.Add(good)
+ f.Add([]byte(`{"version":1,"fingerprint":"fp","keywords":[["b","a"]],"files":[{"dev":1,"ino":10,"size":100,"mtime":1000,"keywords":0,"hits":[5]}]}`))
+ f.Add([]byte(`{"version":1,"fingerprint":"fp","keywords":[],"files":[{"dev":1,"ino":10,"size":100,"mtime":1000,"keywords":-1,"hits":[]}]}`))
+ f.Add([]byte("{}"))
+ f.Add([]byte("null"))
+ f.Fuzz(func(t *testing.T, data []byte) {
+ p := filepath.Join(t.TempDir(), "dl.cache")
+ if err := os.WriteFile(p, data, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ c, err := Load(p, "fp")
+ if c == nil {
+ t.Fatal("Load returned a nil cache")
+ }
+ hits, ok := c.Lookup(a, []string{"k:acme", "k:x"})
+ if ok && len(hits) != 2 {
+ t.Fatalf("Lookup answered %d of 2 keywords", len(hits))
+ }
+ if err != nil && ok {
+ t.Fatalf("a cache that failed to load (%v) answered", err)
+ }
+ if err := c.Save(filepath.Join(t.TempDir(), "out.cache"), []ID{a, b}); err != nil {
+ t.Fatal(err)
+ }
+ })
+}