You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.5 KiB

  1. // +build gofuzz
  2. package server
  3. import (
  4. "bytes"
  5. "io"
  6. "math/rand"
  7. "reflect"
  8. )
  9. const applicationOctetStream = "application/octet-stream"
  10. // FuzzLocalStorage tests the Local Storage.
  11. func FuzzLocalStorage(fuzz []byte) int {
  12. var fuzzLength = uint64(len(fuzz))
  13. if fuzzLength == 0 {
  14. return -1
  15. }
  16. storage, err := NewLocalStorage("/tmp", nil)
  17. if err != nil {
  18. panic("unable to create local storage")
  19. }
  20. token := Encode(10000000 + int64(rand.Intn(1000000000)))
  21. filename := Encode(10000000+int64(rand.Intn(1000000000))) + ".bin"
  22. input := bytes.NewReader(fuzz)
  23. err = storage.Put(token, filename, input, applicationOctetStream, fuzzLength)
  24. if err != nil {
  25. panic("unable to save file")
  26. }
  27. contentLength, err := storage.Head(token, filename)
  28. if err != nil {
  29. panic("not visible through head")
  30. }
  31. if contentLength != fuzzLength {
  32. panic("incorrect content length")
  33. }
  34. output, contentLength, err := storage.Get(token, filename)
  35. if err != nil {
  36. panic("not visible through get")
  37. }
  38. if contentLength != fuzzLength {
  39. panic("incorrect content length")
  40. }
  41. var length uint64
  42. b := make([]byte, len(fuzz))
  43. for {
  44. n, err := output.Read(b)
  45. length += uint64(n)
  46. if err == io.EOF {
  47. break
  48. }
  49. }
  50. if !reflect.DeepEqual(b, fuzz) {
  51. panic("incorrect content body")
  52. }
  53. if length != fuzzLength {
  54. panic("incorrect content length")
  55. }
  56. err = storage.Delete(token, filename)
  57. if err != nil {
  58. panic("unable to delete file")
  59. }
  60. _, err = storage.Head(token, filename)
  61. if !storage.IsNotExist(err) {
  62. panic("file not deleted")
  63. }
  64. return 1
  65. }