transfer.sh/server/server_fuzz.go

83 lines
1.5 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)))
2020-01-06 17:19:52 +01:00
filename := Encode(10000000+int64(rand.Intn(1000000000))) + ".bin"
2019-08-31 11:41:34 +02:00
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
2020-03-15 20:09:12 +01:00
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 contentLength != fuzzLength {
panic("incorrect content length")
2019-08-29 07:15:04 +02:00
}
2019-08-31 11:41:34 +02:00
2020-03-15 20:09:12 +01:00
output, 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 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
2020-03-15 20:09:12 +01:00
_, err = storage.Head(token, filename)
2019-08-31 11:41:34 +02:00
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
}