mirror of
https://github.com/dutchcoders/transfer.sh.git
synced 2024-11-23 12:40:19 +01:00
Define fuzz targets
This commit is contained in:
parent
6c54ddccef
commit
6b148d3574
1 changed files with 128 additions and 0 deletions
128
server/server_fuzz.go
Normal file
128
server/server_fuzz.go
Normal file
|
@ -0,0 +1,128 @@
|
|||
// +build gofuzz
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FuzzProfile tests the profile server.
|
||||
func FuzzProfile(fuzz []byte) int {
|
||||
if len(fuzz) == 0 {
|
||||
return -1
|
||||
}
|
||||
server, err := New(EnableProfiler())
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
server.Run()
|
||||
defer server.profileListener.Close()
|
||||
defer server.httpListener.Close()
|
||||
address := server.profileListener.Addr
|
||||
connection, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
_, err = connection.Write(fuzz)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
response, err := ioutil.ReadAll(connection)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
err = connection.Close()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
fields := strings.Fields(string(response))
|
||||
if len(fields) < 2 {
|
||||
panic("invalid HTTP response")
|
||||
}
|
||||
code := fields[1]
|
||||
if code == "500" {
|
||||
panic("server panicked")
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// FuzzHTTP tests the HTTP server.
|
||||
func FuzzHTTP(fuzz []byte) int {
|
||||
if len(fuzz) == 0 {
|
||||
return -1
|
||||
}
|
||||
server, err := New(Listener("localhost"))
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
server.Run()
|
||||
defer server.httpListener.Close()
|
||||
address := server.httpListener.Addr
|
||||
connection, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
_, err = connection.Write(fuzz)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
response, err := ioutil.ReadAll(connection)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
err = connection.Close()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
fields := strings.Fields(string(response))
|
||||
if len(fields) < 2 {
|
||||
panic("invalid HTTP response")
|
||||
}
|
||||
code := fields[1]
|
||||
if code == "500" {
|
||||
panic("server panicked")
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// FuzzHTTPS tests the HTTPS server.
|
||||
func FuzzHTTPS(fuzz []byte) int {
|
||||
if len(fuzz) == 0 {
|
||||
return -1
|
||||
}
|
||||
server, err := New(TLSListener("localhost", true))
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
server.Run()
|
||||
defer server.httpsListener.Close()
|
||||
address := server.httpsListener.Addr
|
||||
connection, err := tls.Dial("tcp", address, nil)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
_, err = connection.Write(fuzz)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
response, err := ioutil.ReadAll(connection)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
err = connection.Close()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
fields := strings.Fields(string(response))
|
||||
if len(fields) < 2 {
|
||||
panic("invalid HTTP response")
|
||||
}
|
||||
code := fields[1]
|
||||
if code == "500" {
|
||||
panic("server panicked")
|
||||
}
|
||||
return 1
|
||||
}
|
Loading…
Reference in a new issue