transfer.sh/server/server_fuzz.go
Andrea Spacca 2e33f7c716 v1.1.3
2020-01-06 17:19:52 +01:00

90 lines
1.7 KiB
Go

// +build gofuzz
package server
import (
"bytes"
"io"
"math/rand"
"reflect"
)
const applicationOctetStream = "application/octet-stream"
// FuzzLocalStorage tests the Local Storage.
func FuzzLocalStorage(fuzz []byte) int {
var fuzzLength = uint64(len(fuzz))
if fuzzLength == 0 {
return -1
}
storage, err := NewLocalStorage("/tmp", nil)
if err != nil {
panic("unable to create local storage")
}
token := Encode(10000000 + int64(rand.Intn(1000000000)))
filename := Encode(10000000+int64(rand.Intn(1000000000))) + ".bin"
input := bytes.NewReader(fuzz)
err = storage.Put(token, filename, input, applicationOctetStream, fuzzLength)
if err != nil {
panic("unable to save file")
}
contentType, contentLength, err := storage.Head(token, filename)
if err != nil {
panic("not visible through head")
}
if contentType != applicationOctetStream {
panic("incorrect content type")
}
if contentLength != fuzzLength {
panic("incorrect content length")
}
output, contentType, contentLength, err := storage.Get(token, filename)
if err != nil {
panic("not visible through get")
}
if contentType != applicationOctetStream {
panic("incorrect content type")
}
if contentLength != fuzzLength {
panic("incorrect content length")
}
var length uint64
b := make([]byte, len(fuzz))
for {
n, err := output.Read(b)
length += uint64(n)
if err == io.EOF {
break
}
}
if !reflect.DeepEqual(b, fuzz) {
panic("incorrect content body")
}
if length != fuzzLength {
panic("incorrect content length")
}
err = storage.Delete(token, filename)
if err != nil {
panic("unable to delete file")
}
_, _, err = storage.Head(token, filename)
if !storage.IsNotExist(err) {
panic("file not deleted")
}
return 1
}