lib/fs, lib/model: Cover more windowsyness sanitizing paths (fixes #7075) (#7158)

This commit is contained in:
Simon Frei
2020-11-25 22:52:46 +01:00
committed by GitHub
parent c6f2ec9400
commit 54b50e3d52
4 changed files with 109 additions and 94 deletions
-46
View File
@@ -25,8 +25,6 @@ import (
"sync/atomic"
"testing"
"time"
"unicode"
"unicode/utf8"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/db"
@@ -3283,50 +3281,6 @@ func TestRequestLimit(t *testing.T) {
}
}
func TestSanitizePath(t *testing.T) {
cases := [][2]string{
{"", ""},
{"foo", "foo"},
{`\*/foo\?/bar[{!@$%^&*#()}]`, "foo bar ()"},
{"Räksmörgås", "Räksmörgås"},
{`Räk \/ smörgås`, "Räk smörgås"},
{"هذا هو *\x07?اسم الملف", "هذا هو اسم الملف"},
{`../foo.txt`, `.. foo.txt`},
{" \t \n filename in \t space\r", "filename in space"},
{"你\xff好", `你 好`},
{"\000 foo", "foo"},
}
for _, tc := range cases {
res := sanitizePath(tc[0])
if res != tc[1] {
t.Errorf("sanitizePath(%q) => %q, expected %q", tc[0], res, tc[1])
}
}
}
// Fuzz test: sanitizePath must always return strings of printable UTF-8
// characters when fed random data.
//
// Note that space is considered printable, but other whitespace runes are not.
func TestSanitizePathFuzz(t *testing.T) {
buf := make([]byte, 128)
for i := 0; i < 100; i++ {
rand.Read(buf)
path := sanitizePath(string(buf))
if !utf8.ValidString(path) {
t.Errorf("sanitizePath(%q) => %q, not valid UTF-8", buf, path)
continue
}
for _, c := range path {
if !unicode.IsPrint(c) {
t.Errorf("non-printable rune %q in sanitized path", c)
}
}
}
}
// TestConnCloseOnRestart checks that there is no deadlock when calling Close
// on a protocol connection that has a blocking reader (blocking writer can't
// be done as the test requires clusterconfigs to go through).