From 195d9d18a90d88ff2cb0530d832c59d98934fd1f Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Fri, 20 Aug 2021 10:57:26 +0200 Subject: Implement -deterministic-names: extended -zerodiriv -deterministc-names uses all-zero dirivs but does not write them to disk anymore. --- .../deterministic_names_test.go | 79 ++++++++++++++++++++ tests/matrix/matrix_test.go | 13 +++- tests/test_helpers/helpers.go | 2 +- tests/zerodiriv/zerodiriv_test.go | 85 ---------------------- 4 files changed, 91 insertions(+), 88 deletions(-) create mode 100644 tests/deterministic_names/deterministic_names_test.go delete mode 100644 tests/zerodiriv/zerodiriv_test.go (limited to 'tests') diff --git a/tests/deterministic_names/deterministic_names_test.go b/tests/deterministic_names/deterministic_names_test.go new file mode 100644 index 0000000..1b355df --- /dev/null +++ b/tests/deterministic_names/deterministic_names_test.go @@ -0,0 +1,79 @@ +package deterministic_names + +// integration tests that target "-deterministic-names" specifically + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/rfjakob/gocryptfs/tests/test_helpers" +) + +var cDir string +var pDir string + +var testPw = []byte("test") + +// Create and mount "-deterministic-names" fs +func TestMain(m *testing.M) { + cDir = test_helpers.InitFS(nil, "-deterministic-names") + pDir = cDir + ".mnt" + test_helpers.MountOrExit(cDir, pDir, "-deterministic-names", "-extpass", "echo test") + r := m.Run() + test_helpers.UnmountPanic(pDir) + os.Exit(r) +} + +// TestDeterministicNames checks that a file with the same plaintext name +// always encrypts to the same ciphertext name +func TestDeterministicNames(t *testing.T) { + // "foo" should encrypt to the same name in both directories + if err := os.MkdirAll(pDir+"/x/foo", 0700); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(pDir+"/y/foo", 0700); err != nil { + t.Fatal(err) + } + matches, err := filepath.Glob(cDir + "/*/*") + if err != nil || len(matches) != 2 { + t.Fatal(matches, err) + } + if filepath.Base(matches[0]) != filepath.Base(matches[1]) { + t.Error(matches) + } + fooEncrypted := filepath.Base(matches[0]) + + // "foo" should also encrypt to the same name in the root directory + if err := os.Mkdir(pDir+"/foo", 0700); err != nil { + t.Fatal(err) + } + _, err = os.Stat(cDir + "/" + fooEncrypted) + if err != nil { + t.Error(err) + } + + // Replace directory with file + if err := os.RemoveAll(pDir + "/foo"); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(pDir+"/foo", nil, 0700); err != nil { + t.Fatal(err) + } + _, err = os.Stat(cDir + "/" + fooEncrypted) + if err != nil { + t.Error(err) + } + + // Rename back and forth, name should stay the same + if err := os.Rename(pDir+"/foo", pDir+"/foo.tmp"); err != nil { + t.Fatal(err) + } + if err := os.Rename(pDir+"/foo.tmp", pDir+"/foo"); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(cDir + "/" + fooEncrypted); err != nil { + t.Error(err) + } +} diff --git a/tests/matrix/matrix_test.go b/tests/matrix/matrix_test.go index 5cc5251..c51b391 100644 --- a/tests/matrix/matrix_test.go +++ b/tests/matrix/matrix_test.go @@ -55,20 +55,28 @@ var matrix = []testcaseMatrix{ // -serialize_reads {false, "auto", false, false, []string{"-serialize_reads"}}, {false, "auto", false, false, []string{"-sharedstorage"}}, + {false, "auto", false, false, []string{"-deterministic-names"}}, } // This is the entry point for the tests func TestMain(m *testing.M) { // Make "testing.Verbose()" return the correct value flag.Parse() - for _, testcase = range matrix { + var i int + for i, testcase = range matrix { if testcase.openssl == "true" && stupidgcm.BuiltWithoutOpenssl { continue } if testing.Verbose() { fmt.Printf("matrix: testcase = %#v\n", testcase) } - test_helpers.ResetTmpDir(!testcase.plaintextnames) + createDirIV := true + if testcase.plaintextnames { + createDirIV = false + } else if len(testcase.extraArgs) == 1 && testcase.extraArgs[0] == "-deterministic-names" { + createDirIV = false + } + test_helpers.ResetTmpDir(createDirIV) opts := []string{"-zerokey"} //opts = append(opts, "-fusedebug") opts = append(opts, fmt.Sprintf("-openssl=%v", testcase.openssl)) @@ -90,6 +98,7 @@ func TestMain(m *testing.M) { } test_helpers.UnmountPanic(test_helpers.DefaultPlainDir) if r != 0 { + fmt.Printf("TestMain: matrix[%d] = %#v failed\n", i, testcase) os.Exit(r) } } diff --git a/tests/test_helpers/helpers.go b/tests/test_helpers/helpers.go index f78c59c..87dba0a 100644 --- a/tests/test_helpers/helpers.go +++ b/tests/test_helpers/helpers.go @@ -110,7 +110,7 @@ func ResetTmpDir(createDirIV bool) { // Open cipherdir (following symlinks) dirfd, err := syscall.Open(DefaultCipherDir, syscall.O_DIRECTORY|syscallcompat.O_PATH, 0) if err == nil { - err = nametransform.WriteDirIVAt(dirfd, true) + err = nametransform.WriteDirIVAt(dirfd) syscall.Close(dirfd) } if err != nil { diff --git a/tests/zerodiriv/zerodiriv_test.go b/tests/zerodiriv/zerodiriv_test.go deleted file mode 100644 index 3fbbf47..0000000 --- a/tests/zerodiriv/zerodiriv_test.go +++ /dev/null @@ -1,85 +0,0 @@ -package zerodiriv - -// integration tests that target zerodiriv specifically - -import ( - "bytes" - "path/filepath" - "io/ioutil" - "os" - "testing" - - "github.com/rfjakob/gocryptfs/tests/test_helpers" -) - -var cDir string -var pDir string - -var testPw = []byte("test") - -// Create and mount "-zerodiriv" fs -func TestMain(m *testing.M) { - cDir = test_helpers.InitFS(nil, "-zerodiriv") - pDir = cDir + ".mnt" - test_helpers.MountOrExit(cDir, pDir, "-zerodiriv", "-extpass", "echo test") - r := m.Run() - test_helpers.UnmountPanic(pDir) - os.Exit(r) -} - -// diriv should be all-zero on newly created dirs -func TestZeroDirIV(t *testing.T) { - // Create /dir1, move it and create it again - var dirPath = pDir+"/dir1" - var err = os.Mkdir(dirPath, 0777) - if err != nil { - t.Error(err) - } - err = os.Rename(dirPath, dirPath + ".bak") - if err != nil { - t.Error(err) - } - err = os.Mkdir(dirPath, 0777) - if err != nil { - t.Error(err) - } - - var matches []string - matches, err = filepath.Glob(cDir+"/*/gocryptfs.diriv") - if err != nil { - t.Error(err) - } - - // The contents of the both diriv files must be the same - var diriv0 []byte - diriv0, err = ioutil.ReadFile(matches[0]) - if err != nil { - t.Error(err) - } - var diriv1 []byte - diriv1, err = ioutil.ReadFile(matches[1]) - if err != nil { - t.Error(err) - } - if !bytes.Equal(diriv0, diriv1) { - t.Errorf("both dirivs should have the same value") - } - // And equal to zero - zerodiriv := make([]byte, len(diriv0)) - if !bytes.Equal(diriv0, zerodiriv) { - t.Errorf("both dirivs should be all-zero") - } -} - -// root diriv should be all-zero -func TestZeroRootDirIV(t *testing.T) { - // The contents of the diriv file must be zero - diriv, err := ioutil.ReadFile(cDir+"/gocryptfs.diriv") - if err != nil { - t.Error(err) - } - zerodiriv := make([]byte, len(diriv)) - if !bytes.Equal(diriv, zerodiriv) { - t.Errorf("root diriv should be all-zero") - } -} -- cgit v1.2.3