Skip to content

Commit 1cf3e27

Browse files
committed
Add fuzzer
Signed-off-by: Alexey Palazhchenko <[email protected]>
1 parent cd232db commit 1cf3e27

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

protocol/handler_fuzz.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,63 @@
22

33
package protocol
44

5+
import (
6+
"bytes"
7+
"encoding/hex"
8+
"io"
9+
"log"
10+
"strings"
11+
12+
"github.com/GopherConRu/pb-fuzz-workshop/kv"
13+
)
14+
515
func Fuzz(data []byte) int {
6-
return 0
16+
kv, err := kv.NewInMemoryBadgerKV()
17+
if err != nil {
18+
panic(err)
19+
}
20+
defer func() {
21+
if err := kv.Close(); err != nil {
22+
panic(err)
23+
}
24+
}()
25+
26+
h := NewHandler(kv)
27+
input, output := h.NewConn()
28+
29+
cmds := strings.Split(string(data), "\n")
30+
cmds = append(cmds, "PING")
31+
32+
readDone := make(chan int)
33+
go func() {
34+
b, err := io.ReadAll(output)
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
if !bytes.HasSuffix(b, []byte("+PONG\n")) {
40+
log.Printf("Input: %q", cmds)
41+
log.Printf("Output: %q", strings.Split(string(b), "\n"))
42+
panic("Raw output:\n" + hex.Dump(b))
43+
}
44+
45+
if bytes.Contains(b, []byte("-ERR")) {
46+
readDone <- 0
47+
return
48+
}
49+
50+
readDone <- 1
51+
}()
52+
53+
for _, cmd := range cmds {
54+
if _, err := input.Write([]byte(cmd + "\n")); err != nil {
55+
panic(err)
56+
}
57+
}
58+
59+
if err := input.Close(); err != nil {
60+
panic(err)
61+
}
62+
63+
return <-readDone
764
}

0 commit comments

Comments
 (0)