2017-04-25 15:57:59 +02:00
|
|
|
// Serve webdav tests set up a server and run the integration tests
|
|
|
|
// for the webdav remote against it.
|
|
|
|
//
|
|
|
|
// We skip tests on platforms with troublesome character mappings
|
|
|
|
|
2021-09-09 14:25:25 +02:00
|
|
|
//go:build !windows && !darwin
|
|
|
|
// +build !windows,!darwin
|
2017-04-25 15:57:59 +02:00
|
|
|
|
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
2020-11-05 16:18:51 +01:00
|
|
|
"context"
|
2019-06-12 14:01:48 +02:00
|
|
|
"flag"
|
2022-08-20 16:38:02 +02:00
|
|
|
"io"
|
2019-06-12 14:01:48 +02:00
|
|
|
"net/http"
|
2017-04-25 15:57:59 +02:00
|
|
|
"os"
|
2019-06-12 14:01:48 +02:00
|
|
|
"strings"
|
2017-04-25 15:57:59 +02:00
|
|
|
"testing"
|
2019-06-12 14:01:48 +02:00
|
|
|
"time"
|
2017-04-25 15:57:59 +02:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
_ "github.com/rclone/rclone/backend/local"
|
2019-08-02 11:56:27 +02:00
|
|
|
"github.com/rclone/rclone/cmd/serve/servetest"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2019-08-02 11:56:27 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/filter"
|
2019-08-02 11:56:27 +02:00
|
|
|
"github.com/rclone/rclone/fs/hash"
|
2017-04-25 15:57:59 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-06-12 14:01:48 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-10 12:54:44 +02:00
|
|
|
"golang.org/x/net/webdav"
|
2017-04-25 15:57:59 +02:00
|
|
|
)
|
|
|
|
|
2018-02-14 21:39:11 +01:00
|
|
|
const (
|
2019-05-01 12:16:22 +02:00
|
|
|
testBindAddress = "localhost:0"
|
2019-08-02 11:56:27 +02:00
|
|
|
testUser = "user"
|
|
|
|
testPass = "pass"
|
2020-05-08 17:15:21 +02:00
|
|
|
testTemplate = "../http/testdata/golden/testindex.html"
|
2018-02-14 21:39:11 +01:00
|
|
|
)
|
|
|
|
|
2018-06-10 12:54:44 +02:00
|
|
|
// check interfaces
|
|
|
|
var (
|
2022-12-11 19:53:55 +01:00
|
|
|
_ os.FileInfo = FileInfo{nil, nil}
|
|
|
|
_ webdav.ETager = FileInfo{nil, nil}
|
|
|
|
_ webdav.ContentTyper = FileInfo{nil, nil}
|
2018-06-10 12:54:44 +02:00
|
|
|
)
|
|
|
|
|
2017-04-25 15:57:59 +02:00
|
|
|
// TestWebDav runs the webdav server then runs the unit tests for the
|
|
|
|
// webdav remote against it.
|
|
|
|
func TestWebDav(t *testing.T) {
|
2019-08-02 11:56:27 +02:00
|
|
|
// Configure and start the server
|
|
|
|
start := func(f fs.Fs) (configmap.Simple, func()) {
|
2022-12-11 19:53:55 +01:00
|
|
|
opt := DefaultOpt
|
|
|
|
opt.HTTP.ListenAddr = []string{testBindAddress}
|
2022-12-22 16:42:47 +01:00
|
|
|
opt.HTTP.BaseURL = "/prefix"
|
2022-12-11 19:53:55 +01:00
|
|
|
opt.Auth.BasicUser = testUser
|
|
|
|
opt.Auth.BasicPass = testPass
|
|
|
|
opt.Template.Path = testTemplate
|
|
|
|
opt.HashType = hash.MD5
|
2019-08-02 11:56:27 +02:00
|
|
|
|
|
|
|
// Start the server
|
2022-12-11 19:53:55 +01:00
|
|
|
w, err := newWebDAV(context.Background(), f, &opt)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, w.serve())
|
2019-08-02 11:56:27 +02:00
|
|
|
|
|
|
|
// Config for the backend we'll use to connect to the server
|
|
|
|
config := configmap.Simple{
|
|
|
|
"type": "webdav",
|
|
|
|
"vendor": "other",
|
2022-12-11 19:53:55 +01:00
|
|
|
"url": w.Server.URLs()[0],
|
2019-08-02 11:56:27 +02:00
|
|
|
"user": testUser,
|
|
|
|
"pass": obscure.MustObscure(testPass),
|
|
|
|
}
|
2017-04-25 15:57:59 +02:00
|
|
|
|
2019-08-02 11:56:27 +02:00
|
|
|
return config, func() {
|
2022-12-11 19:53:55 +01:00
|
|
|
assert.NoError(t, w.Shutdown())
|
2019-08-02 11:56:27 +02:00
|
|
|
w.Wait()
|
|
|
|
}
|
2017-04-25 15:57:59 +02:00
|
|
|
}
|
2019-08-02 11:56:27 +02:00
|
|
|
|
|
|
|
servetest.Run(t, "webdav", start)
|
2017-04-25 15:57:59 +02:00
|
|
|
}
|
2019-06-12 14:01:48 +02:00
|
|
|
|
|
|
|
// Test serve http functionality in serve webdav
|
|
|
|
// While similar to http serve, there are some inconsistencies
|
|
|
|
// in the handling of some requests such as POST requests
|
|
|
|
|
|
|
|
var (
|
|
|
|
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTPFunction(t *testing.T) {
|
2020-11-26 18:10:41 +01:00
|
|
|
ctx := context.Background()
|
2019-06-12 14:01:48 +02:00
|
|
|
// exclude files called hidden.txt and directories called hidden
|
2020-11-26 18:10:41 +01:00
|
|
|
fi := filter.GetConfig(ctx)
|
|
|
|
require.NoError(t, fi.AddRule("- hidden.txt"))
|
|
|
|
require.NoError(t, fi.AddRule("- hidden/**"))
|
2019-06-12 14:01:48 +02:00
|
|
|
|
|
|
|
// Uses the same test files as http tests but with different golden.
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := fs.NewFs(context.Background(), "../http/testdata/files")
|
2019-06-12 14:01:48 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-12-11 19:53:55 +01:00
|
|
|
opt := DefaultOpt
|
|
|
|
opt.HTTP.ListenAddr = []string{testBindAddress}
|
|
|
|
opt.Template.Path = testTemplate
|
2019-06-12 14:01:48 +02:00
|
|
|
|
|
|
|
// Start the server
|
2022-12-11 19:53:55 +01:00
|
|
|
w, err := newWebDAV(context.Background(), f, &opt)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
require.NoError(t, w.serve())
|
2019-06-12 14:01:48 +02:00
|
|
|
defer func() {
|
2022-12-11 19:53:55 +01:00
|
|
|
assert.NoError(t, w.Shutdown())
|
2019-06-12 14:01:48 +02:00
|
|
|
w.Wait()
|
|
|
|
}()
|
2022-12-11 19:53:55 +01:00
|
|
|
testURL := w.Server.URLs()[0]
|
2019-06-12 14:01:48 +02:00
|
|
|
pause := time.Millisecond
|
|
|
|
i := 0
|
|
|
|
for ; i < 10; i++ {
|
|
|
|
resp, err := http.Head(testURL)
|
|
|
|
if err == nil {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// t.Logf("couldn't connect, sleeping for %v: %v", pause, err)
|
|
|
|
time.Sleep(pause)
|
|
|
|
pause *= 2
|
|
|
|
}
|
|
|
|
if i >= 10 {
|
|
|
|
t.Fatal("couldn't connect to server")
|
|
|
|
}
|
|
|
|
|
|
|
|
HelpTestGET(t, testURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check body against the file, or re-write body if -updategolden is
|
|
|
|
// set.
|
|
|
|
func checkGolden(t *testing.T, fileName string, got []byte) {
|
|
|
|
if *updateGolden {
|
|
|
|
t.Logf("Updating golden file %q", fileName)
|
2022-08-20 16:38:02 +02:00
|
|
|
err := os.WriteFile(fileName, got, 0666)
|
2019-06-12 14:01:48 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
2022-08-20 16:38:02 +02:00
|
|
|
want, err := os.ReadFile(fileName)
|
2019-06-12 14:01:48 +02:00
|
|
|
require.NoError(t, err, "problem")
|
|
|
|
wants := strings.Split(string(want), "\n")
|
|
|
|
gots := strings.Split(string(got), "\n")
|
|
|
|
assert.Equal(t, wants, gots, fileName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HelpTestGET(t *testing.T, testURL string) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
URL string
|
|
|
|
Status int
|
|
|
|
Golden string
|
|
|
|
Method string
|
|
|
|
Range string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
URL: "",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/index.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "notfound",
|
|
|
|
Status: http.StatusNotFound,
|
|
|
|
Golden: "testdata/golden/notfound.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "dirnotfound/",
|
|
|
|
Status: http.StatusNotFound,
|
|
|
|
Golden: "testdata/golden/dirnotfound.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "hidden/",
|
|
|
|
Status: http.StatusNotFound,
|
|
|
|
Golden: "testdata/golden/hiddendir.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "one%25.txt",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/one.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "hidden.txt",
|
|
|
|
Status: http.StatusNotFound,
|
|
|
|
Golden: "testdata/golden/hidden.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "three/",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/three.html",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "three/a.txt",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/a.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "",
|
|
|
|
Method: "HEAD",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/indexhead.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "one%25.txt",
|
|
|
|
Method: "HEAD",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/onehead.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "",
|
|
|
|
Method: "POST",
|
|
|
|
Status: http.StatusMethodNotAllowed,
|
|
|
|
Golden: "testdata/golden/indexpost.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "one%25.txt",
|
|
|
|
Method: "POST",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/onepost.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "two.txt",
|
|
|
|
Status: http.StatusOK,
|
|
|
|
Golden: "testdata/golden/two.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "two.txt",
|
|
|
|
Status: http.StatusPartialContent,
|
|
|
|
Range: "bytes=2-5",
|
|
|
|
Golden: "testdata/golden/two2-5.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "two.txt",
|
|
|
|
Status: http.StatusPartialContent,
|
|
|
|
Range: "bytes=0-6",
|
|
|
|
Golden: "testdata/golden/two-6.txt",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: "two.txt",
|
|
|
|
Status: http.StatusPartialContent,
|
|
|
|
Range: "bytes=3-",
|
|
|
|
Golden: "testdata/golden/two3-.txt",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
method := test.Method
|
|
|
|
if method == "" {
|
|
|
|
method = "GET"
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(method, testURL+test.URL, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
if test.Range != "" {
|
|
|
|
req.Header.Add("Range", test.Range)
|
|
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.Status, resp.StatusCode, test.Golden)
|
2022-08-20 16:38:02 +02:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2019-06-12 14:01:48 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkGolden(t, test.Golden, body)
|
|
|
|
}
|
|
|
|
}
|