transfer.sh/server/server_fuzz.go

91 lines
1.7 KiB
Go
Raw Normal View History

2019-08-29 07:15:04 +02:00
// +build gofuzz
package server
import (
2019-08-31 11:41:34 +02:00
"bytes"
"io"
"math/rand"
"reflect"
2019-08-29 07:15:04 +02:00
)
2019-08-31 11:41:34 +02:00
const applicationOctetStream = "application/octet-stream"
// FuzzLocalStorage tests the Local Storage.
func FuzzLocalStorage(fuzz []byte) int {
var fuzzLength = uint64(len(fuzz))
if fuzzLength == 0 {
2019-08-29 07:15:04 +02:00
return -1
}
2019-08-31 11:41:34 +02:00
storage, err := NewLocalStorage("/tmp", nil)
2019-08-29 07:15:04 +02:00
if err != nil {
2019-08-31 11:41:34 +02:00
panic("unable to create local storage")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
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)
2019-08-29 07:15:04 +02:00
if err != nil {
2019-08-31 11:41:34 +02:00
panic("unable to save file")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
contentType, contentLength, err := storage.Head(token, filename)
2019-08-29 07:15:04 +02:00
if err != nil {
2019-08-31 11:41:34 +02:00
panic("not visible through head")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
if contentType != applicationOctetStream {
panic("incorrect content type")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
if contentLength != fuzzLength {
panic("incorrect content length")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
output, contentType, contentLength, err := storage.Get(token, filename)
2019-08-29 07:15:04 +02:00
if err != nil {
2019-08-31 11:41:34 +02:00
panic("not visible through get")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
if contentType != applicationOctetStream {
panic("incorrect content type")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
if contentLength != fuzzLength {
panic("incorrect content length")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
var length uint64
b := make([]byte, len(fuzz))
for {
n, err := output.Read(b)
length += uint64(n)
if err == io.EOF {
break
}
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
if !reflect.DeepEqual(b, fuzz) {
panic("incorrect content body")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
if length != fuzzLength {
panic("incorrect content length")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
err = storage.Delete(token, filename)
2019-08-29 07:15:04 +02:00
if err != nil {
2019-08-31 11:41:34 +02:00
panic("unable to delete file")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
_, _, err = storage.Head(token, filename)
if !storage.IsNotExist(err) {
panic("file not deleted")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
2019-08-29 07:15:04 +02:00
return 1
}