From 305e9c1045f0546967bfbd2d10f13a28b6227a76 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Thu, 16 Jun 2016 21:06:03 +0200 Subject: tests: add InitFS helper --- tests/integration_tests/cli_test.go | 65 +++++++------------------------------ tests/test_helpers/helpers.go | 45 ++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 58 deletions(-) diff --git a/tests/integration_tests/cli_test.go b/tests/integration_tests/cli_test.go index 0e88581..ae6ef7f 100644 --- a/tests/integration_tests/cli_test.go +++ b/tests/integration_tests/cli_test.go @@ -3,7 +3,6 @@ package integration_tests // Test CLI operations like "-init", "-password" etc import ( - "io/ioutil" "os" "os/exec" "path/filepath" @@ -17,18 +16,8 @@ import ( // Test -init flag func TestInit(t *testing.T) { - dir, err := ioutil.TempDir(test_helpers.TmpDir, "TestInit") - if err != nil { - t.Fatal(err) - } - cmd := exec.Command(test_helpers.GocryptfsBinary, "-q", "-init", "-extpass", "echo test", "-scryptn=10", dir) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Run() - if err != nil { - t.Fatal(err) - } - _, err = os.Stat(filepath.Join(dir, configfile.ConfDefaultName)) + dir := test_helpers.InitFS(t) + _, err := os.Stat(filepath.Join(dir, configfile.ConfDefaultName)) if err != nil { t.Fatal(err) } @@ -37,22 +26,12 @@ func TestInit(t *testing.T) { // Test -passwd flag func TestPasswd(t *testing.T) { // Create FS - dir, err := ioutil.TempDir(test_helpers.TmpDir, "TestPasswd") - if err != nil { - t.Fatal(err) - } - cmd := exec.Command(test_helpers.GocryptfsBinary, "-q", "-init", "-extpass", "echo test", "-scryptn=10", dir) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Run() - if err != nil { - t.Fatal(err) - } + dir := test_helpers.InitFS(t) // Change password using "-extpass" - cmd = exec.Command(test_helpers.GocryptfsBinary, "-q", "-passwd", "-extpass", "echo test", dir) + cmd := exec.Command(test_helpers.GocryptfsBinary, "-q", "-passwd", "-extpass", "echo test", dir) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - err = cmd.Run() + err := cmd.Run() if err != nil { t.Error(err) } @@ -81,21 +60,10 @@ func TestPasswd(t *testing.T) { // Test -init & -config flag func TestInitConfig(t *testing.T) { - dir := test_helpers.TmpDir + "TestInitConfig/" config := test_helpers.TmpDir + "TestInitConfig.conf" - err := os.Mkdir(dir, 0777) - if err != nil { - t.Fatal(err) - } - cmd := exec.Command(test_helpers.GocryptfsBinary, "-q", "-init", "-extpass", "echo test", - "-config", config, "-scryptn=10", dir) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Run() - if err != nil { - t.Fatal(err) - } - _, err = os.Stat(config) + dir := test_helpers.InitFS(t, "-config="+config) + + _, err := os.Stat(config) if err != nil { t.Fatal(err) } @@ -113,20 +81,9 @@ func TestInitConfig(t *testing.T) { // Test -init -plaintextnames func TestInitPlaintextNames(t *testing.T) { - dir := test_helpers.TmpDir + "TestInitPlaintextNames/" - err := os.Mkdir(dir, 0777) - if err != nil { - t.Fatal(err) - } - cmd := exec.Command(test_helpers.GocryptfsBinary, "-q", "-init", "-extpass", "echo test", - "-scryptn=10", "-plaintextnames", dir) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err = cmd.Run() - if err != nil { - t.Fatal(err) - } - _, err = os.Stat(dir + configfile.ConfDefaultName) + dir := test_helpers.InitFS(t, "-plaintextnames") + dir = dir + "/" + _, err := os.Stat(dir + configfile.ConfDefaultName) if err != nil { t.Fatal(err) } diff --git a/tests/test_helpers/helpers.go b/tests/test_helpers/helpers.go index 2f3bf0c..c006e8b 100644 --- a/tests/test_helpers/helpers.go +++ b/tests/test_helpers/helpers.go @@ -21,7 +21,12 @@ const DefaultCipherDir = TmpDir + "cipher/" const GocryptfsBinary = "../../gocryptfs" -// ResetTmpDir - delete old tmp dir, create new one, write gocryptfs.diriv +// ResetTmpDir - delete TmpDir, create new dir tree: +// +// TmpDir +// |-- DefaultPlainDir +// *-- DefaultCipherDir +// *-- gocryptfs.diriv func ResetTmpDir(plaintextNames bool) { // Try to unmount everything @@ -59,7 +64,33 @@ func ResetTmpDir(plaintextNames bool) { } } +// InitFS calls "gocryptfs -init" on a new directory in TmpDir, passing +// "extraArgs" in addition to practical defaults. +// The returned "dir" has NO trailing slash. +func InitFS(t *testing.T, extraArgs ...string) string { + dir, err := ioutil.TempDir(TmpDir, "") + if err != nil { + t.Fatal(err) + } + + args := []string{"-q", "-init", "-extpass", "echo test", "-scryptn=10"} + args = append(args, extraArgs...) + args = append(args, dir) + + cmd := exec.Command(GocryptfsBinary, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + err = cmd.Run() + if err != nil { + t.Fatalf("InitFS with args %v failed: %v", args, err) + } + + return dir +} + // Mount CIPHERDIR "c" on PLAINDIR "p" +// Creates "p" if it does not exist. func Mount(c string, p string, extraArgs ...string) error { var args []string args = append(args, extraArgs...) @@ -68,11 +99,17 @@ func Mount(c string, p string, extraArgs ...string) error { //args = append(args, "-d") args = append(args, c) args = append(args, p) + + if _, err := os.Stat(p); err != nil { + err = os.Mkdir(p, 0777) + if err != nil { + return err + } + } + cmd := exec.Command(GocryptfsBinary, args...) cmd.Stderr = os.Stderr - if testing.Verbose() { - cmd.Stdout = os.Stdout - } + cmd.Stdout = os.Stdout return cmd.Run() } -- cgit v1.2.3