forked from dvyukov/go-fuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (50 loc) · 1.01 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
// 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 websocketclient
import (
"golang.org/x/net/websocket"
"io"
)
func Fuzz(data []byte) int {
cfg, err := websocket.NewConfig("http://golang.org/", "http://golang.org/")
if err != nil {
panic(err)
}
// Header: http.Header{},
conn := &Conn{data, false}
ws, err := websocket.NewClient(cfg, conn)
if err != nil {
return 0
}
ws.Write([]byte("abc"))
var buf [16]byte
ws.Read(buf[:])
ws.Read(buf[:])
ws.Close()
if !conn.closed {
panic("conn is not closed")
}
return 1
}
type Conn struct {
data []byte
closed bool
}
func (c *Conn) 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 *Conn) Write(b []byte) (n int, err error) {
return len(b), nil
}
func (c *Conn) Close() error {
if c.closed {
panic("already closed")
}
c.closed = true
return nil
}