2018-04-04 21:49:13 +08:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/httplib/httpflags"
|
2021-03-10 23:40:34 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/configfile"
|
2018-04-04 21:49:13 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// createOverwriteDeleteSeq returns a sequence which will create a new file at
|
|
|
|
// path, and then try to overwrite and delete it.
|
|
|
|
func createOverwriteDeleteSeq(t testing.TB, path string) []TestRequest {
|
|
|
|
// add a file, try to overwrite and delete it
|
|
|
|
req := []TestRequest{
|
|
|
|
{
|
|
|
|
req: newRequest(t, "GET", path, nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusNotFound)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "POST", path, strings.NewReader("foobar test config")),
|
|
|
|
want: []wantFunc{wantCode(http.StatusOK)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "GET", path, nil),
|
|
|
|
want: []wantFunc{
|
|
|
|
wantCode(http.StatusOK),
|
|
|
|
wantBody("foobar test config"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "POST", path, strings.NewReader("other config")),
|
|
|
|
want: []wantFunc{wantCode(http.StatusForbidden)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "GET", path, nil),
|
|
|
|
want: []wantFunc{
|
|
|
|
wantCode(http.StatusOK),
|
|
|
|
wantBody("foobar test config"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "DELETE", path, nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusForbidden)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "GET", path, nil),
|
|
|
|
want: []wantFunc{
|
|
|
|
wantCode(http.StatusOK),
|
|
|
|
wantBody("foobar test config"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestResticHandler runs tests on the restic handler code, especially in append-only mode.
|
|
|
|
func TestResticHandler(t *testing.T) {
|
2021-04-27 05:37:49 +08:00
|
|
|
configfile.Install()
|
2018-04-04 21:49:13 +08:00
|
|
|
buf := make([]byte, 32)
|
|
|
|
_, err := io.ReadFull(rand.Reader, buf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
randomID := hex.EncodeToString(buf)
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
seq []TestRequest
|
|
|
|
}{
|
|
|
|
{createOverwriteDeleteSeq(t, "/config")},
|
|
|
|
{createOverwriteDeleteSeq(t, "/data/"+randomID)},
|
|
|
|
{
|
|
|
|
// ensure we can add and remove lock files
|
|
|
|
[]TestRequest{
|
|
|
|
{
|
|
|
|
req: newRequest(t, "GET", "/locks/"+randomID, nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusNotFound)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "POST", "/locks/"+randomID, strings.NewReader("lock file")),
|
|
|
|
want: []wantFunc{wantCode(http.StatusOK)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "GET", "/locks/"+randomID, nil),
|
|
|
|
want: []wantFunc{
|
|
|
|
wantCode(http.StatusOK),
|
|
|
|
wantBody("lock file"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "POST", "/locks/"+randomID, strings.NewReader("other lock file")),
|
|
|
|
want: []wantFunc{wantCode(http.StatusForbidden)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "DELETE", "/locks/"+randomID, nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusOK)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "GET", "/locks/"+randomID, nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusNotFound)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup rclone with a local backend in a temporary directory
|
2022-01-30 00:24:56 +08:00
|
|
|
tempdir := t.TempDir()
|
2018-04-04 21:49:13 +08:00
|
|
|
|
|
|
|
// globally set append-only mode
|
|
|
|
prev := appendOnly
|
|
|
|
appendOnly = true
|
|
|
|
defer func() {
|
|
|
|
appendOnly = prev // reset when done
|
|
|
|
}()
|
|
|
|
|
|
|
|
// make a new file system in the temp dir
|
|
|
|
f := cmd.NewFsSrc([]string{tempdir})
|
2020-07-26 19:06:47 +08:00
|
|
|
srv := NewServer(f, &httpflags.Opt)
|
2018-04-04 21:49:13 +08:00
|
|
|
|
|
|
|
// create the repo
|
2020-07-26 19:06:47 +08:00
|
|
|
checkRequest(t, srv.ServeHTTP,
|
2018-04-04 21:49:13 +08:00
|
|
|
newRequest(t, "POST", "/?create=true", nil),
|
|
|
|
[]wantFunc{wantCode(http.StatusOK)})
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
for i, seq := range test.seq {
|
|
|
|
t.Logf("request %v: %v %v", i, seq.req.Method, seq.req.URL.Path)
|
2020-07-26 19:06:47 +08:00
|
|
|
checkRequest(t, srv.ServeHTTP, seq.req, seq.want)
|
2018-04-04 21:49:13 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|