From 19cb6d046aac92f44722c17ba9a371b08ca0be6a Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 17 Feb 2019 17:05:05 +0100 Subject: nametransform: reject names longer than 255 chars Looks like we allowed creating longer names by accident. Fix that, and add a test that verifies it. --- tests/matrix/matrix_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'tests') diff --git a/tests/matrix/matrix_test.go b/tests/matrix/matrix_test.go index be47990..33d2ab2 100644 --- a/tests/matrix/matrix_test.go +++ b/tests/matrix/matrix_test.go @@ -377,6 +377,57 @@ func TestRename(t *testing.T) { test_helpers.TestRename(t, test_helpers.DefaultPlainDir) } +// Test that names of all lengths work +func TestNameLengths(t *testing.T) { + f, err := os.Open(test_helpers.DefaultPlainDir) + if err != nil { + t.Fatal(err) + } + entries, err := f.Readdirnames(0) + if err != nil { + t.Fatal(err) + } + f.Close() + cnt1 := len(entries) + + wd := test_helpers.DefaultPlainDir + "/" + name := "x" + for len(name) < 2000 { + f, err := os.Create(wd + name + "x") + if err != nil { + break + } + name = name + "x" + f.Close() + f, err = os.Open(test_helpers.DefaultPlainDir) + if err != nil { + t.Fatal(err) + } + // In v1.7-rc2, we had a bug that allowed creation of too-long names. + // This threw errors in like this in READDIR: + // + // OpenDir ".": invalid entry "gocryptfs.longname.wrE-izsR9ciEkP7JSCFDrk_d_Nj4mQo1dGY6hjuixAU=": + // Could not read .name: ReadLongName: size=345 > limit=344 + // + entries, err = f.Readdirnames(0) + if err != nil { + t.Fatal(err) + } + f.Close() + cnt2 := len(entries) + if cnt2 != cnt1+1 { + t.Fatalf("len=%d: expected %d dir entries, have %d: %v", len(name), cnt1+1, cnt2, entries) + } + err = syscall.Unlink(wd + name) + if err != nil { + t.Fatal(err) + } + } + if len(name) != 255 { + t.Errorf("maxlen=%d", len(name)) + } +} + func TestLongNames(t *testing.T) { fi, err := ioutil.ReadDir(test_helpers.DefaultCipherDir) if err != nil { -- cgit v1.2.3