-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachefs_test.go
86 lines (82 loc) · 1.51 KB
/
cachefs_test.go
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
package cachefs
import (
"bytes"
"io"
"os"
"sync"
"testing"
)
func TestHttpCacheFs(t *testing.T) {
hfs := NewHttpCacheFs("./")
fs1, err := hfs.Open("cachefs.go")
if err != nil {
t.Fatal(err)
}
defer fs1.Close()
fs2, err := hfs.Open("cachefs.go")
if err != nil {
t.Fatal(err)
}
fs2.Close()
}
func TestCacheFsRead(t *testing.T) {
hfs := NewHttpCacheFs("./")
fs1, err := hfs.Open("cachefs.go")
if err != nil {
t.Fatal(err)
}
defer fs1.Close()
p := make([]byte, 10240)
_, err = fs1.Read(p)
if err != nil && err != io.EOF {
t.Fatal(err)
}
_, err = fs1.Read(p)
if err != nil && err != io.EOF {
t.Fatal(err)
}
_, err = fs1.Read(p)
if err != nil && err != io.EOF {
t.Fatal(err)
}
_, err = fs1.Read(p)
if err != nil && err != io.EOF {
t.Fatal(err)
}
_, err = fs1.Read(p)
if err != nil && err != io.EOF {
t.Fatal(err)
}
_, err = fs1.Read(p)
if err != nil && err != io.EOF {
t.Fatal(err)
}
}
func FuzzHttpCacheFs(f *testing.F) {
osvalue, err := os.ReadFile("cachefs.go")
if err != nil {
f.Fatal(err)
}
f.Fuzz(func(t *testing.T, i uint) {
var wg sync.WaitGroup
wg.Add(int(i))
hs := NewHttpCacheFs("./")
for n := uint(0); n < i; n++ {
go func() {
defer wg.Done()
fs, err := hs.Open("cachefs.go")
if err != nil {
t.Fatal(err)
}
body, err := io.ReadAll(fs)
if err != nil && err != io.EOF {
t.Fatal(err)
}
if bytes.Compare(osvalue, body) != 0 {
t.Fatalf("%s \n!= %s", string(osvalue), string(body))
}
}()
}
wg.Wait()
})
}