2022-01-20 02:40:32 +00:00
|
|
|
package files_test
|
2022-01-15 18:07:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2022-01-19 02:10:50 +00:00
|
|
|
"strings"
|
2022-01-15 18:07:33 +00:00
|
|
|
"testing"
|
2022-01-19 02:10:50 +00:00
|
|
|
"time"
|
2022-01-15 18:07:33 +00:00
|
|
|
|
2022-01-20 02:40:32 +00:00
|
|
|
"git.t-juice.club/torjus/gpaste/files"
|
2022-01-19 02:10:50 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2022-01-15 18:07:33 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2022-01-20 02:40:32 +00:00
|
|
|
func RunFilestoreTest(s files.FileStore, t *testing.T) {
|
2022-01-15 18:07:33 +00:00
|
|
|
t.Run("Basic", func(t *testing.T) {
|
|
|
|
// Create
|
|
|
|
dataString := "TEST_LOL_OMG"
|
|
|
|
id := uuid.Must(uuid.NewRandom()).String()
|
|
|
|
bodyBuf := &bytes.Buffer{}
|
|
|
|
bodyBuf.Write([]byte(dataString))
|
|
|
|
body := io.NopCloser(bodyBuf)
|
2022-01-20 02:40:32 +00:00
|
|
|
f := &files.File{
|
2022-01-24 15:38:17 +00:00
|
|
|
ID: id,
|
|
|
|
MaxViews: 99,
|
|
|
|
Body: body,
|
|
|
|
ExpiresOn: time.Now().Add(99 * time.Second),
|
2022-01-15 18:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := s.Store(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to store file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve
|
|
|
|
retrieved, err := s.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to retrieve file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
retrievedBuf := &bytes.Buffer{}
|
|
|
|
_, err = retrievedBuf.ReadFrom(retrieved.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to read retrieved body: %s", err)
|
|
|
|
}
|
|
|
|
_ = retrieved.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error reading from retrieved file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if retrievedBuf.String() != dataString {
|
|
|
|
t.Fatalf("Data from retrieved body mismatch. Got %s want %s", retrievedBuf.String(), dataString)
|
|
|
|
}
|
2022-01-24 15:38:17 +00:00
|
|
|
expected := &files.File{
|
|
|
|
ID: f.ID,
|
|
|
|
MaxViews: f.MaxViews,
|
|
|
|
ExpiresOn: f.ExpiresOn,
|
|
|
|
FileSize: int64(len(dataString)),
|
|
|
|
}
|
|
|
|
|
|
|
|
ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
|
|
|
|
if diff := cmp.Diff(retrieved, expected, ignoreBody); diff != "" {
|
|
|
|
t.Errorf("File comparison failed: %s", diff)
|
|
|
|
}
|
2022-01-15 18:07:33 +00:00
|
|
|
|
|
|
|
// List
|
|
|
|
ids, err := s.List()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error doing list: %s", err)
|
|
|
|
}
|
|
|
|
if len(ids) != 1 {
|
|
|
|
t.Fatalf("List has wrong length: %d", len(ids))
|
|
|
|
}
|
|
|
|
if ids[0] != id {
|
|
|
|
t.Fatalf("ID is wrong. Got %s want %s", ids[0], id)
|
|
|
|
}
|
2022-01-15 20:31:32 +00:00
|
|
|
|
|
|
|
// Delete
|
|
|
|
if err := s.Delete(id); err != nil {
|
|
|
|
t.Fatalf("Error deleting file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ids, err = s.List()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error listing after delete: %s", err)
|
|
|
|
}
|
|
|
|
if len(ids) != 0 {
|
|
|
|
t.Fatalf("List after delete has wrong length: %d", len(ids))
|
|
|
|
}
|
2022-01-15 18:07:33 +00:00
|
|
|
})
|
|
|
|
}
|
2022-01-19 02:10:50 +00:00
|
|
|
|
2022-01-20 02:40:32 +00:00
|
|
|
func RunPersistentFilestoreTest(newStoreFunc func() files.FileStore, t *testing.T) {
|
2022-01-19 02:10:50 +00:00
|
|
|
s := newStoreFunc()
|
|
|
|
|
|
|
|
files := []struct {
|
2022-01-20 02:40:32 +00:00
|
|
|
File *files.File
|
2022-01-19 02:10:50 +00:00
|
|
|
ExpectedData string
|
|
|
|
}{
|
|
|
|
{
|
2022-01-20 02:40:32 +00:00
|
|
|
File: &files.File{
|
2022-01-19 02:10:50 +00:00
|
|
|
ID: uuid.NewString(),
|
|
|
|
OriginalFilename: "testfile.txt",
|
|
|
|
MaxViews: 5,
|
|
|
|
ExpiresOn: time.Now().Add(10 * time.Minute),
|
|
|
|
Body: io.NopCloser(strings.NewReader("cocks!")),
|
2022-01-24 14:52:15 +00:00
|
|
|
FileSize: 6,
|
2022-01-19 02:10:50 +00:00
|
|
|
},
|
|
|
|
ExpectedData: "cocks!",
|
|
|
|
},
|
|
|
|
{
|
2022-01-20 02:40:32 +00:00
|
|
|
File: &files.File{
|
2022-01-19 02:10:50 +00:00
|
|
|
ID: uuid.NewString(),
|
|
|
|
OriginalFilename: "testfile2.txt",
|
|
|
|
MaxViews: 5,
|
|
|
|
ExpiresOn: time.Now().Add(10 * time.Minute),
|
|
|
|
Body: io.NopCloser(strings.NewReader("derps!")),
|
2022-01-24 14:52:15 +00:00
|
|
|
FileSize: 6,
|
2022-01-19 02:10:50 +00:00
|
|
|
},
|
|
|
|
ExpectedData: "derps!",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
err := s.Store(f.File)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error storing file: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
retrieved, err := s.Get(f.File.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to retrieve file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
|
|
|
|
if !cmp.Equal(retrieved, f.File, ignoreBody) {
|
|
|
|
t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File))
|
|
|
|
}
|
|
|
|
buf := new(strings.Builder)
|
|
|
|
if _, err := io.Copy(buf, retrieved.Body); err != nil {
|
|
|
|
t.Fatalf("Error reading from body: %s", err)
|
|
|
|
}
|
|
|
|
retrieved.Body.Close()
|
|
|
|
if buf.String() != f.ExpectedData {
|
|
|
|
t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reopen store, and fetch again
|
|
|
|
s = newStoreFunc()
|
|
|
|
for _, f := range files {
|
|
|
|
retrieved, err := s.Get(f.File.ID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to retrieve file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ignoreBody := cmp.FilterPath(func(p cmp.Path) bool { return p.String() == "Body" }, cmp.Ignore())
|
|
|
|
if !cmp.Equal(retrieved, f.File, ignoreBody) {
|
|
|
|
t.Errorf("Mismatch: %s", cmp.Diff(retrieved, f.File))
|
|
|
|
}
|
|
|
|
buf := new(strings.Builder)
|
|
|
|
if _, err := io.Copy(buf, retrieved.Body); err != nil {
|
|
|
|
t.Fatalf("Error reading from body: %s", err)
|
|
|
|
}
|
|
|
|
retrieved.Body.Close()
|
|
|
|
if buf.String() != f.ExpectedData {
|
|
|
|
t.Fatalf("Data does not match. %s", cmp.Diff(buf.String(), f.ExpectedData))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|