fileserver: Improve and clarify file hiding logic (#3844)

* fileserver: Improve and clarify file hiding logic

* Oops, forgot to run integration tests

* Make this one integration test OS-agnostic

* See if this appeases the Windows gods

* D'oh
This commit is contained in:
Matt Holt
2020-11-02 14:20:12 -07:00
committed by GitHub
parent 937ec34201
commit 8d038ca515
5 changed files with 119 additions and 39 deletions
@@ -17,6 +17,8 @@ package fileserver
import (
"net/url"
"path/filepath"
"runtime"
"strings"
"testing"
)
@@ -44,7 +46,7 @@ func TestSanitizedPathJoin(t *testing.T) {
},
{
inputPath: "/foo/",
expect: "foo" + string(filepath.Separator),
expect: "foo" + separator,
},
{
inputPath: "/foo/bar",
@@ -77,7 +79,7 @@ func TestSanitizedPathJoin(t *testing.T) {
{
inputRoot: "/a/b",
inputPath: "/%2e%2e%2f%2e%2e%2f",
expect: filepath.Join("/", "a", "b") + string(filepath.Separator),
expect: filepath.Join("/", "a", "b") + separator,
},
{
inputRoot: "C:\\www",
@@ -154,6 +156,26 @@ func TestFileHidden(t *testing.T) {
inputPath: "/foo/asdf/bar",
expect: true,
},
{
inputHide: []string{"*.txt"},
inputPath: "/foo/bar.txt",
expect: true,
},
{
inputHide: []string{"/foo/bar/*.txt"},
inputPath: "/foo/bar/baz.txt",
expect: true,
},
{
inputHide: []string{"/foo/bar/*.txt"},
inputPath: "/foo/bar.txt",
expect: false,
},
{
inputHide: []string{"/foo/bar/*.txt"},
inputPath: "/foo/bar/index.html",
expect: false,
},
{
inputHide: []string{"/foo"},
inputPath: "/foo",
@@ -164,17 +186,29 @@ func TestFileHidden(t *testing.T) {
inputPath: "/foobar",
expect: false,
},
{
inputHide: []string{"first", "second"},
inputPath: "/second",
expect: true,
},
} {
// for Windows' sake
tc.inputPath = filepath.FromSlash(tc.inputPath)
for i := range tc.inputHide {
tc.inputHide[i] = filepath.FromSlash(tc.inputHide[i])
if runtime.GOOS == "windows" {
if strings.HasPrefix(tc.inputPath, "/") {
tc.inputPath, _ = filepath.Abs(tc.inputPath)
}
tc.inputPath = filepath.FromSlash(tc.inputPath)
for i := range tc.inputHide {
if strings.HasPrefix(tc.inputHide[i], "/") {
tc.inputHide[i], _ = filepath.Abs(tc.inputHide[i])
}
tc.inputHide[i] = filepath.FromSlash(tc.inputHide[i])
}
}
actual := fileHidden(tc.inputPath, tc.inputHide)
if actual != tc.expect {
t.Errorf("Test %d: Is %s hidden in %v? Got %t but expected %t",
i, tc.inputPath, tc.inputHide, actual, tc.expect)
t.Errorf("Test %d: Does %v hide %s? Got %t but expected %t",
i, tc.inputHide, tc.inputPath, actual, tc.expect)
}
}
}