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.
 
 
 

91 lines
1.7 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. contentType, contentLength, err := storage.Head(token, filename)
  28. if err != nil {
  29. panic("not visible through head")
  30. }
  31. if contentType != applicationOctetStream {
  32. panic("incorrect content type")
  33. }
  34. if contentLength != fuzzLength {
  35. panic("incorrect content length")
  36. }
  37. output, contentType, contentLength, err := storage.Get(token, filename)
  38. if err != nil {
  39. panic("not visible through get")
  40. }
  41. if contentType != applicationOctetStream {
  42. panic("incorrect content type")
  43. }
  44. if contentLength != fuzzLength {
  45. panic("incorrect content length")
  46. }
  47. var length uint64
  48. b := make([]byte, len(fuzz))
  49. for {
  50. n, err := output.Read(b)
  51. length += uint64(n)
  52. if err == io.EOF {
  53. break
  54. }
  55. }
  56. if !reflect.DeepEqual(b, fuzz) {
  57. panic("incorrect content body")
  58. }
  59. if length != fuzzLength {
  60. panic("incorrect content length")
  61. }
  62. err = storage.Delete(token, filename)
  63. if err != nil {
  64. panic("unable to delete file")
  65. }
  66. _, _, err = storage.Head(token, filename)
  67. if !storage.IsNotExist(err) {
  68. panic("file not deleted")
  69. }
  70. return 1
  71. }