2017-07-30 14:30:18 +02:00
|
|
|
package restserver
|
|
|
|
|
|
|
|
import (
|
2018-04-02 12:50:45 +02:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2020-05-04 01:28:13 +08:00
|
|
|
"fmt"
|
2018-04-02 12:50:45 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2022-08-29 22:45:07 +02:00
|
|
|
"path"
|
2017-07-30 14:30:18 +02:00
|
|
|
"path/filepath"
|
2020-05-04 01:28:13 +08:00
|
|
|
"reflect"
|
2018-04-02 12:50:45 +02:00
|
|
|
"strings"
|
2021-01-31 16:15:57 +01:00
|
|
|
"sync"
|
2017-07-30 14:30:18 +02:00
|
|
|
"testing"
|
2021-08-27 18:00:50 +02:00
|
|
|
|
|
|
|
"github.com/minio/sha256-simd"
|
2017-07-30 14:30:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestJoin(t *testing.T) {
|
|
|
|
var tests = []struct {
|
2020-09-13 12:08:46 +02:00
|
|
|
base string
|
|
|
|
names []string
|
|
|
|
result string
|
2017-07-30 14:30:18 +02:00
|
|
|
}{
|
2020-09-13 12:08:46 +02:00
|
|
|
{"/", []string{"foo", "bar"}, "/foo/bar"},
|
|
|
|
{"/srv/server", []string{"foo", "bar"}, "/srv/server/foo/bar"},
|
|
|
|
{"/srv/server", []string{"foo", "..", "bar"}, "/srv/server/foo/bar"},
|
|
|
|
{"/srv/server", []string{"..", "bar"}, "/srv/server/bar"},
|
|
|
|
{"/srv/server", []string{".."}, "/srv/server"},
|
|
|
|
{"/srv/server", []string{"..", ".."}, "/srv/server"},
|
|
|
|
{"/srv/server", []string{"repo", "data"}, "/srv/server/repo/data"},
|
|
|
|
{"/srv/server", []string{"repo", "data", "..", ".."}, "/srv/server/repo/data"},
|
|
|
|
{"/srv/server", []string{"repo", "data", "..", "data", "..", "..", ".."}, "/srv/server/repo/data/data"},
|
2017-07-30 14:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
2020-09-13 12:08:46 +02:00
|
|
|
got, err := join(filepath.FromSlash(test.base), test.names...)
|
2017-07-30 14:30:18 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := filepath.FromSlash(test.result)
|
|
|
|
if got != want {
|
|
|
|
t.Fatalf("wrong result returned, want %v, got %v", want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 20:45:59 +01:00
|
|
|
|
2018-04-02 12:50:45 +02:00
|
|
|
// declare a few helper functions
|
|
|
|
|
|
|
|
// wantFunc tests the HTTP response in res and calls t.Error() if something is incorrect.
|
|
|
|
type wantFunc func(t testing.TB, res *httptest.ResponseRecorder)
|
|
|
|
|
|
|
|
// newRequest returns a new HTTP request with the given params. On error, t.Fatal is called.
|
|
|
|
func newRequest(t testing.TB, method, path string, body io.Reader) *http.Request {
|
|
|
|
req, err := http.NewRequest(method, path, body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
// wantCode returns a function which checks that the response has the correct HTTP status code.
|
|
|
|
func wantCode(code int) wantFunc {
|
|
|
|
return func(t testing.TB, res *httptest.ResponseRecorder) {
|
2021-01-31 16:13:05 +01:00
|
|
|
t.Helper()
|
2018-04-02 12:50:45 +02:00
|
|
|
if res.Code != code {
|
|
|
|
t.Errorf("wrong response code, want %v, got %v", code, res.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// wantBody returns a function which checks that the response has the data in the body.
|
|
|
|
func wantBody(body string) wantFunc {
|
|
|
|
return func(t testing.TB, res *httptest.ResponseRecorder) {
|
2021-01-31 16:13:05 +01:00
|
|
|
t.Helper()
|
2018-04-02 12:50:45 +02:00
|
|
|
if res.Body == nil {
|
|
|
|
t.Errorf("body is nil, want %q", body)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(res.Body.Bytes(), []byte(body)) {
|
|
|
|
t.Errorf("wrong response body, want:\n %q\ngot:\n %q", body, res.Body.Bytes())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkRequest uses f to process the request and runs the checker functions on the result.
|
|
|
|
func checkRequest(t testing.TB, f http.HandlerFunc, req *http.Request, want []wantFunc) {
|
2021-01-31 16:13:05 +01:00
|
|
|
t.Helper()
|
2018-04-02 12:50:45 +02:00
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
f(rr, req)
|
|
|
|
|
|
|
|
for _, fn := range want {
|
|
|
|
fn(t, rr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestRequest is a sequence of HTTP requests with (optional) tests for the response.
|
|
|
|
type TestRequest struct {
|
|
|
|
req *http.Request
|
|
|
|
want []wantFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// createOverwriteDeleteSeq returns a sequence which will create a new file at
|
|
|
|
// path, and then try to overwrite and delete it.
|
2021-08-09 15:35:13 +02:00
|
|
|
func createOverwriteDeleteSeq(t testing.TB, path string, data string) []TestRequest {
|
2018-04-02 12:50:45 +02:00
|
|
|
// add a file, try to overwrite and delete it
|
|
|
|
req := []TestRequest{
|
|
|
|
{
|
|
|
|
req: newRequest(t, "GET", path, nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusNotFound)},
|
|
|
|
},
|
2021-08-09 15:35:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(path, "/config") {
|
|
|
|
req = append(req, TestRequest{
|
|
|
|
// broken upload must fail
|
|
|
|
req: newRequest(t, "POST", path, strings.NewReader(data+"broken")),
|
|
|
|
want: []wantFunc{wantCode(http.StatusBadRequest)},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
req = append(req,
|
|
|
|
TestRequest{
|
|
|
|
req: newRequest(t, "POST", path, strings.NewReader(data)),
|
2018-04-02 12:50:45 +02:00
|
|
|
want: []wantFunc{wantCode(http.StatusOK)},
|
|
|
|
},
|
2021-08-09 15:35:13 +02:00
|
|
|
TestRequest{
|
2018-04-02 12:50:45 +02:00
|
|
|
req: newRequest(t, "GET", path, nil),
|
|
|
|
want: []wantFunc{
|
|
|
|
wantCode(http.StatusOK),
|
2021-08-09 15:35:13 +02:00
|
|
|
wantBody(data),
|
2018-04-02 12:50:45 +02:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 15:35:13 +02:00
|
|
|
TestRequest{
|
|
|
|
req: newRequest(t, "POST", path, strings.NewReader(data+"other stuff")),
|
2018-04-02 12:50:45 +02:00
|
|
|
want: []wantFunc{wantCode(http.StatusForbidden)},
|
|
|
|
},
|
2021-08-09 15:35:13 +02:00
|
|
|
TestRequest{
|
2018-04-02 12:50:45 +02:00
|
|
|
req: newRequest(t, "GET", path, nil),
|
|
|
|
want: []wantFunc{
|
|
|
|
wantCode(http.StatusOK),
|
2021-08-09 15:35:13 +02:00
|
|
|
wantBody(data),
|
2018-04-02 12:50:45 +02:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 15:35:13 +02:00
|
|
|
TestRequest{
|
2018-04-02 12:50:45 +02:00
|
|
|
req: newRequest(t, "DELETE", path, nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusForbidden)},
|
|
|
|
},
|
2021-08-09 15:35:13 +02:00
|
|
|
TestRequest{
|
2018-04-02 12:50:45 +02:00
|
|
|
req: newRequest(t, "GET", path, nil),
|
|
|
|
want: []wantFunc{
|
|
|
|
wantCode(http.StatusOK),
|
2021-08-09 15:35:13 +02:00
|
|
|
wantBody(data),
|
2018-04-02 12:50:45 +02:00
|
|
|
},
|
|
|
|
},
|
2021-08-09 15:35:13 +02:00
|
|
|
)
|
2018-04-02 12:50:45 +02:00
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
2022-09-02 23:41:05 +02:00
|
|
|
func createTestHandler(t *testing.T, conf Server) (http.Handler, string, string, string, func()) {
|
2018-04-02 12:50:45 +02:00
|
|
|
buf := make([]byte, 32)
|
|
|
|
_, err := io.ReadFull(rand.Reader, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-09 15:35:13 +02:00
|
|
|
data := "random data file " + hex.EncodeToString(buf)
|
|
|
|
dataHash := sha256.Sum256([]byte(data))
|
|
|
|
fileID := hex.EncodeToString(dataHash[:])
|
2018-04-02 12:50:45 +02:00
|
|
|
|
2022-09-02 23:41:05 +02:00
|
|
|
// setup the server with a local backend in a temporary directory
|
|
|
|
tempdir, err := ioutil.TempDir("", "rest-server-test-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the tempdir is properly removed
|
|
|
|
cleanup := func() {
|
|
|
|
err := os.RemoveAll(tempdir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Path = tempdir
|
|
|
|
mux, err := NewHandler(&conf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error from NewHandler: %v", err)
|
|
|
|
}
|
|
|
|
return mux, data, fileID, tempdir, cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestResticAppendOnlyHandler runs tests on the restic handler code, especially in append-only mode.
|
|
|
|
func TestResticAppendOnlyHandler(t *testing.T) {
|
|
|
|
mux, data, fileID, _, cleanup := createTestHandler(t, Server{
|
|
|
|
AppendOnly: true,
|
|
|
|
NoAuth: true,
|
|
|
|
Debug: true,
|
|
|
|
PanicOnError: true,
|
|
|
|
})
|
|
|
|
defer cleanup()
|
|
|
|
|
2018-04-02 12:50:45 +02:00
|
|
|
var tests = []struct {
|
|
|
|
seq []TestRequest
|
|
|
|
}{
|
2021-08-09 15:35:13 +02:00
|
|
|
{createOverwriteDeleteSeq(t, "/config", data)},
|
|
|
|
{createOverwriteDeleteSeq(t, "/data/"+fileID, data)},
|
2018-04-02 12:50:45 +02:00
|
|
|
{
|
|
|
|
// ensure we can add and remove lock files
|
|
|
|
[]TestRequest{
|
|
|
|
{
|
2021-08-09 15:35:13 +02:00
|
|
|
req: newRequest(t, "GET", "/locks/"+fileID, nil),
|
2018-04-02 12:50:45 +02:00
|
|
|
want: []wantFunc{wantCode(http.StatusNotFound)},
|
|
|
|
},
|
|
|
|
{
|
2021-08-09 15:35:13 +02:00
|
|
|
req: newRequest(t, "POST", "/locks/"+fileID, strings.NewReader(data+"broken")),
|
|
|
|
want: []wantFunc{wantCode(http.StatusBadRequest)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "POST", "/locks/"+fileID, strings.NewReader(data)),
|
2018-04-02 12:50:45 +02:00
|
|
|
want: []wantFunc{wantCode(http.StatusOK)},
|
|
|
|
},
|
|
|
|
{
|
2021-08-09 15:35:13 +02:00
|
|
|
req: newRequest(t, "GET", "/locks/"+fileID, nil),
|
2018-04-02 12:50:45 +02:00
|
|
|
want: []wantFunc{
|
|
|
|
wantCode(http.StatusOK),
|
2021-08-09 15:35:13 +02:00
|
|
|
wantBody(data),
|
2018-04-02 12:50:45 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-08-09 15:35:13 +02:00
|
|
|
req: newRequest(t, "POST", "/locks/"+fileID, strings.NewReader(data+"other data")),
|
2018-04-02 12:50:45 +02:00
|
|
|
want: []wantFunc{wantCode(http.StatusForbidden)},
|
|
|
|
},
|
|
|
|
{
|
2021-08-09 15:35:13 +02:00
|
|
|
req: newRequest(t, "DELETE", "/locks/"+fileID, nil),
|
2018-04-02 12:50:45 +02:00
|
|
|
want: []wantFunc{wantCode(http.StatusOK)},
|
|
|
|
},
|
|
|
|
{
|
2021-08-09 15:35:13 +02:00
|
|
|
req: newRequest(t, "GET", "/locks/"+fileID, nil),
|
2018-04-02 12:50:45 +02:00
|
|
|
want: []wantFunc{wantCode(http.StatusNotFound)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-01-04 15:51:23 +08:00
|
|
|
|
|
|
|
// Test subrepos
|
2021-08-09 15:35:13 +02:00
|
|
|
{createOverwriteDeleteSeq(t, "/parent1/sub1/config", "foobar")},
|
|
|
|
{createOverwriteDeleteSeq(t, "/parent1/sub1/data/"+fileID, data)},
|
|
|
|
{createOverwriteDeleteSeq(t, "/parent1/config", "foobar")},
|
|
|
|
{createOverwriteDeleteSeq(t, "/parent1/data/"+fileID, data)},
|
|
|
|
{createOverwriteDeleteSeq(t, "/parent2/config", "foobar")},
|
|
|
|
{createOverwriteDeleteSeq(t, "/parent2/data/"+fileID, data)},
|
2018-04-02 12:50:45 +02:00
|
|
|
}
|
|
|
|
|
2021-01-04 15:51:23 +08:00
|
|
|
// create the repos
|
|
|
|
for _, path := range []string{"/", "/parent1/sub1/", "/parent1/", "/parent2/"} {
|
|
|
|
checkRequest(t, mux.ServeHTTP,
|
|
|
|
newRequest(t, "POST", path+"?create=true", nil),
|
|
|
|
[]wantFunc{wantCode(http.StatusOK)})
|
|
|
|
}
|
2018-04-02 12:50:45 +02:00
|
|
|
|
2022-09-02 23:22:42 +02:00
|
|
|
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)
|
|
|
|
checkRequest(t, mux.ServeHTTP, seq.req, seq.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// createOverwriteDeleteSeq returns a sequence which will create a new file at
|
|
|
|
// path, and then deletes it twice.
|
|
|
|
func createIdempotentDeleteSeq(t testing.TB, path string, data string) []TestRequest {
|
|
|
|
return []TestRequest{
|
|
|
|
{
|
|
|
|
req: newRequest(t, "POST", path, strings.NewReader(data)),
|
|
|
|
want: []wantFunc{wantCode(http.StatusOK)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "DELETE", path, nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusOK)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "GET", path, nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusNotFound)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
req: newRequest(t, "DELETE", path, nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusOK)},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestResticHandler runs tests on the restic handler code, especially in append-only mode.
|
|
|
|
func TestResticHandler(t *testing.T) {
|
2022-09-02 23:41:05 +02:00
|
|
|
mux, data, fileID, _, cleanup := createTestHandler(t, Server{
|
|
|
|
NoAuth: true,
|
|
|
|
Debug: true,
|
|
|
|
PanicOnError: true,
|
|
|
|
})
|
|
|
|
defer cleanup()
|
2022-09-02 23:22:42 +02:00
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
seq []TestRequest
|
|
|
|
}{
|
|
|
|
{createIdempotentDeleteSeq(t, "/config", data)},
|
|
|
|
{createIdempotentDeleteSeq(t, "/data/"+fileID, data)},
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the repo
|
|
|
|
checkRequest(t, mux.ServeHTTP,
|
|
|
|
newRequest(t, "POST", "/?create=true", nil),
|
|
|
|
[]wantFunc{wantCode(http.StatusOK)})
|
|
|
|
|
2018-04-02 12:50:45 +02:00
|
|
|
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)
|
|
|
|
checkRequest(t, mux.ServeHTTP, seq.req, seq.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 01:28:13 +08:00
|
|
|
|
2022-08-29 22:45:07 +02:00
|
|
|
// TestResticErrorHandler runs tests on the restic handler error handling.
|
|
|
|
func TestResticErrorHandler(t *testing.T) {
|
2022-09-02 23:41:05 +02:00
|
|
|
mux, _, _, tempdir, cleanup := createTestHandler(t, Server{
|
|
|
|
AppendOnly: true,
|
|
|
|
NoAuth: true,
|
|
|
|
Debug: true,
|
|
|
|
})
|
|
|
|
defer cleanup()
|
|
|
|
|
2022-08-29 22:45:07 +02:00
|
|
|
var tests = []struct {
|
|
|
|
seq []TestRequest
|
|
|
|
}{
|
|
|
|
// Test inaccessible file
|
|
|
|
{
|
|
|
|
[]TestRequest{{
|
|
|
|
req: newRequest(t, "GET", "/config", nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusInternalServerError)},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]TestRequest{{
|
|
|
|
req: newRequest(t, "GET", "/parent4/config", nil),
|
|
|
|
want: []wantFunc{wantCode(http.StatusNotFound)},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the repo
|
|
|
|
checkRequest(t, mux.ServeHTTP,
|
|
|
|
newRequest(t, "POST", "/?create=true", nil),
|
|
|
|
[]wantFunc{wantCode(http.StatusOK)})
|
|
|
|
// create inaccessible config
|
|
|
|
checkRequest(t, mux.ServeHTTP,
|
|
|
|
newRequest(t, "POST", "/config", strings.NewReader("example")),
|
|
|
|
[]wantFunc{wantCode(http.StatusOK)})
|
2022-09-02 23:41:05 +02:00
|
|
|
err := os.Chmod(path.Join(tempdir, "config"), 0o000)
|
2022-08-29 22:45:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
checkRequest(t, mux.ServeHTTP, seq.req, seq.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:28:13 +08:00
|
|
|
func TestSplitURLPath(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
// Params
|
|
|
|
urlPath string
|
|
|
|
maxDepth int
|
|
|
|
// Expected result
|
|
|
|
folderPath []string
|
|
|
|
remainder string
|
|
|
|
}{
|
|
|
|
{"/", 0, nil, "/"},
|
|
|
|
{"/", 2, nil, "/"},
|
|
|
|
{"/foo/bar/locks/0123", 0, nil, "/foo/bar/locks/0123"},
|
|
|
|
{"/foo/bar/locks/0123", 1, []string{"foo"}, "/bar/locks/0123"},
|
|
|
|
{"/foo/bar/locks/0123", 2, []string{"foo", "bar"}, "/locks/0123"},
|
|
|
|
{"/foo/bar/locks/0123", 3, []string{"foo", "bar"}, "/locks/0123"},
|
|
|
|
{"/foo/bar/zzz/locks/0123", 2, []string{"foo", "bar"}, "/zzz/locks/0123"},
|
|
|
|
{"/foo/bar/zzz/locks/0123", 3, []string{"foo", "bar", "zzz"}, "/locks/0123"},
|
|
|
|
{"/foo/bar/locks/", 2, []string{"foo", "bar"}, "/locks/"},
|
2020-05-31 21:36:39 +08:00
|
|
|
{"/foo/locks/", 2, []string{"foo"}, "/locks/"},
|
|
|
|
{"/foo/data/", 2, []string{"foo"}, "/data/"},
|
|
|
|
{"/foo/index/", 2, []string{"foo"}, "/index/"},
|
|
|
|
{"/foo/keys/", 2, []string{"foo"}, "/keys/"},
|
|
|
|
{"/foo/snapshots/", 2, []string{"foo"}, "/snapshots/"},
|
|
|
|
{"/foo/config", 2, []string{"foo"}, "/config"},
|
|
|
|
{"/foo/", 2, []string{"foo"}, "/"},
|
2020-05-04 01:28:13 +08:00
|
|
|
{"/foo/bar/", 2, []string{"foo", "bar"}, "/"},
|
|
|
|
{"/foo/bar", 2, []string{"foo"}, "/bar"},
|
|
|
|
{"/locks/", 2, nil, "/locks/"},
|
|
|
|
// This function only splits, it does not check the path components!
|
2020-05-31 22:09:41 +08:00
|
|
|
{"/././locks/", 2, []string{".", "."}, "/locks/"},
|
2020-05-04 01:28:13 +08:00
|
|
|
{"/../../locks/", 2, []string{"..", ".."}, "/locks/"},
|
|
|
|
{"///locks/", 2, []string{"", ""}, "/locks/"},
|
|
|
|
{"////locks/", 2, []string{"", ""}, "//locks/"},
|
|
|
|
// Robustness against broken input
|
|
|
|
{"/", -42, nil, "/"},
|
|
|
|
{"foo", 2, nil, "foo"},
|
|
|
|
{"foo/bar", 2, nil, "foo/bar"},
|
|
|
|
{"", 2, nil, ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) {
|
|
|
|
folderPath, remainder := splitURLPath(test.urlPath, test.maxDepth)
|
|
|
|
|
|
|
|
var fpEqual bool
|
|
|
|
if len(test.folderPath) == 0 && len(folderPath) == 0 {
|
|
|
|
fpEqual = true // this check allows for nil vs empty slice
|
|
|
|
} else {
|
|
|
|
fpEqual = reflect.DeepEqual(test.folderPath, folderPath)
|
|
|
|
}
|
|
|
|
if !fpEqual {
|
|
|
|
t.Errorf("wrong folderPath: want %v, got %v", test.folderPath, folderPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.remainder != remainder {
|
|
|
|
t.Errorf("wrong remainder: want %v, got %v", test.remainder, remainder)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 16:15:57 +01:00
|
|
|
|
|
|
|
// delayErrorReader blocks until Continue is closed, closes the channel FirstRead and then returns Err.
|
|
|
|
type delayErrorReader struct {
|
|
|
|
FirstRead chan struct{}
|
|
|
|
firstReadOnce sync.Once
|
|
|
|
|
|
|
|
Err error
|
|
|
|
|
|
|
|
Continue chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDelayedErrorReader(err error) *delayErrorReader {
|
|
|
|
return &delayErrorReader{
|
|
|
|
Err: err,
|
|
|
|
Continue: make(chan struct{}),
|
|
|
|
FirstRead: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *delayErrorReader) Read(p []byte) (int, error) {
|
|
|
|
d.firstReadOnce.Do(func() {
|
|
|
|
// close the channel to signal that the first read has happened
|
|
|
|
close(d.FirstRead)
|
|
|
|
})
|
|
|
|
<-d.Continue
|
|
|
|
return 0, d.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAbortedRequest runs tests with concurrent upload requests for the same file.
|
|
|
|
func TestAbortedRequest(t *testing.T) {
|
2022-09-02 23:41:05 +02:00
|
|
|
// the race condition doesn't happen for append-only repositories
|
|
|
|
mux, _, _, _, cleanup := createTestHandler(t, Server{
|
2021-01-31 16:15:57 +01:00
|
|
|
NoAuth: true,
|
|
|
|
Debug: true,
|
|
|
|
PanicOnError: true,
|
|
|
|
})
|
2022-09-02 23:41:05 +02:00
|
|
|
defer cleanup()
|
2021-01-31 16:15:57 +01:00
|
|
|
|
|
|
|
// create the repo
|
|
|
|
checkRequest(t, mux.ServeHTTP,
|
|
|
|
newRequest(t, "POST", "/?create=true", nil),
|
|
|
|
[]wantFunc{wantCode(http.StatusOK)})
|
|
|
|
|
|
|
|
var (
|
|
|
|
id = "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"
|
|
|
|
wg sync.WaitGroup
|
|
|
|
)
|
|
|
|
|
|
|
|
// the first request is an upload to a file which blocks while reading the
|
|
|
|
// body and then after some data returns an error
|
2021-09-06 22:13:33 +02:00
|
|
|
rd := newDelayedErrorReader(io.ErrUnexpectedEOF)
|
2021-01-31 16:15:57 +01:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
// first, read some string, then read from rd (which blocks and then
|
|
|
|
// returns an error)
|
|
|
|
dataReader := io.MultiReader(strings.NewReader("invalid data from aborted request\n"), rd)
|
|
|
|
|
|
|
|
t.Logf("start first upload")
|
|
|
|
req := newRequest(t, "POST", "/data/"+id, dataReader)
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
mux.ServeHTTP(rr, req)
|
|
|
|
t.Logf("first upload done, response %v (%v)", rr.Code, rr.Result().Status)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait until the first request starts reading from the body
|
|
|
|
<-rd.FirstRead
|
|
|
|
|
|
|
|
// then while the first request is blocked we send a second request to
|
|
|
|
// delete the file and a third request to upload to the file again, only
|
|
|
|
// then the first request is unblocked.
|
|
|
|
|
|
|
|
t.Logf("delete file")
|
|
|
|
checkRequest(t, mux.ServeHTTP,
|
|
|
|
newRequest(t, "DELETE", "/data/"+id, nil),
|
|
|
|
nil) // don't check anything, restic also ignores errors here
|
|
|
|
|
|
|
|
t.Logf("upload again")
|
|
|
|
checkRequest(t, mux.ServeHTTP,
|
|
|
|
newRequest(t, "POST", "/data/"+id, strings.NewReader("foo\n")),
|
|
|
|
[]wantFunc{wantCode(http.StatusOK)})
|
|
|
|
|
|
|
|
// unblock the reader for the first request now so it can continue
|
|
|
|
close(rd.Continue)
|
|
|
|
|
|
|
|
// wait for the first request to continue
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// request the file again, it must exist and contain the string from the
|
|
|
|
// second request
|
|
|
|
checkRequest(t, mux.ServeHTTP,
|
|
|
|
newRequest(t, "GET", "/data/"+id, nil),
|
|
|
|
[]wantFunc{
|
|
|
|
wantCode(http.StatusOK),
|
|
|
|
wantBody("foo\n"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|