forked from dvyukov/go-fuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
136 lines (114 loc) · 2.36 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2015 Dmitry Vyukov. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package httpserver
import (
"bytes"
"fmt"
"io"
"log"
"net"
"net/http"
"runtime"
"time"
)
type MyListener chan *MyConn
func (ln MyListener) Accept() (c net.Conn, err error) {
return <-ln, nil
}
func (ln MyListener) Close() error {
return nil
}
func (ln MyListener) Addr() net.Addr {
return &net.TCPAddr{net.IP{127, 0, 0, 1}, 49706, ""}
}
type MyConn struct {
data []byte
done chan bool
written bool
}
func (c *MyConn) Read(b []byte) (n int, err error) {
if len(c.data) == 0 {
return 0, io.EOF
}
n = copy(b, c.data)
c.data = c.data[n:]
return
}
func (c *MyConn) Write(b []byte) (n int, err error) {
c.written = true
return len(b), nil
}
func (c *MyConn) Close() error {
close(c.done)
return nil
}
func (c *MyConn) LocalAddr() net.Addr {
return &net.TCPAddr{net.IP{127, 0, 0, 1}, 49706, ""}
}
func (c *MyConn) RemoteAddr() net.Addr {
return &net.TCPAddr{net.IP{127, 0, 0, 1}, 49706, ""}
}
func (c *MyConn) SetDeadline(t time.Time) error {
return nil
}
func (c *MyConn) SetReadDeadline(t time.Time) error {
return nil
}
func (c *MyConn) SetWriteDeadline(t time.Time) error {
return nil
}
type LogPipe struct {
c chan []byte
}
func (p *LogPipe) Write(data []byte) (int, error) {
p.c <- append([]byte{}, data...)
return len(data), nil
}
func (p *LogPipe) ReadAll() []byte {
var data []byte
for {
select {
case d := <-p.c:
data = append(data, d...)
default:
return data
}
}
}
func init() {
http.HandleFunc("/", handler)
s := &http.Server{
ErrorLog: log.New(logpipe, "", 0),
}
go func() {
if err := s.Serve(ln); err != nil {
panic(err)
}
}()
}
func handler(w http.ResponseWriter, req *http.Request) {
w.Write(reply)
}
var (
logpipe = &LogPipe{c: make(chan []byte, 1000)}
ln = MyListener(make(chan *MyConn))
reply = []byte("hello")
)
func Fuzz(data []byte) int {
c := &MyConn{data: data, done: make(chan bool)}
ln <- c
<-c.done
http.DefaultTransport.(*http.Transport).CloseIdleConnections()
runtime.Gosched()
if data := logpipe.ReadAll(); bytes.Contains(data, []byte("http: panic serving")) {
fmt.Printf("%s\n", data)
panic("serving panicked")
}
if runtime.NumGoroutine() > 50 {
panic("leaking goroutines")
}
if c.written {
return 1
}
return 0
}