-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
67 lines (57 loc) · 1.45 KB
/
server_test.go
File metadata and controls
67 lines (57 loc) · 1.45 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
// Package main_test is used for by fuzz tests that need to access the embed data.
package main_test
import (
"embed"
"strings"
"sync"
"testing"
"github.com/Defacto2/server/handler/fulltext"
"github.com/Defacto2/server/handler/tidbit"
)
var (
ts fulltext.Tidbits //nolint:gochecknoglobals
once sync.Once //nolint:gochecknoglobals
//go:embed public/**/*
publicFS embed.FS
)
// setupOnce populates the index once for the session.
func setupOnce() {
once.Do(func() {
if err := ts.NewIndex(publicFS, tidbit.Dir); err != nil {
panic(err)
}
})
}
// go test -fuzz=FuzzSearch -fuzztime=30s
//
func FuzzSearch(f *testing.F) {
setupOnce()
f.Add("razor")
f.Add("defacto2")
f.Add("razor 1911")
f.Add("razor 🚀 1911!")
f.Add(" ")
f.Add("invalid\x9c\xadbytes")
f.Fuzz(func(t *testing.T, query string) {
const avoidExcessRAM = 20
results := ts.Search(query, avoidExcessRAM)
if strings.TrimSpace(query) == "" {
if len(results) != 0 {
t.Errorf("expected 0 results for empty query, got %d", len(results))
}
return
}
for _, res := range results {
if res.Name == "error" {
t.Errorf("Index Desync: engine returned DocID that doesn't exist in store. Query: %q", query)
}
if res.Score <= 0 {
t.Errorf("Invalid Score: %f for query %q", res.Score, query)
}
if res.Name != "" && res.ID <= 0 {
// Adjust if 0 is a valid ID in your system
t.Errorf("ID generation failed for Name: %s", res.Name)
}
}
})
}