tests: error out properly on mount failure

In TestMain we call os.Exit as before, but inside actual tests
we now call t.Fatal().
This commit is contained in:
Jakob Unterwurzacher 2016-06-06 22:30:39 +02:00
parent 0c80cca674
commit cc5d5a3fcd
3 changed files with 32 additions and 21 deletions

View File

@ -76,10 +76,10 @@ func TestExampleFSv04(t *testing.T) {
if err != nil {
t.Fatal(err)
}
mount(cDir, pDir, "-extpass", "echo test")
mountOrFatal(t, cDir, pDir, "-extpass", "echo test")
checkExampleFS(t, pDir)
unmount(pDir)
mount(cDir, pDir, "-masterkey", "74676e34-0b47c145-00dac61a-17a92316-"+
mountOrFatal(t, cDir, pDir, "-masterkey", "74676e34-0b47c145-00dac61a-17a92316-"+
"bb57044c-e205b71f-65f4fdca-7cabd4b3", "-diriv=false", "-emenames=false", "-gcmiv128=false")
checkExampleFS(t, pDir)
unmount(pDir)
@ -98,10 +98,10 @@ func TestExampleFSv05(t *testing.T) {
if err != nil {
t.Fatal(err)
}
mount(cDir, pDir, "-extpass", "echo test")
mountOrFatal(t, cDir, pDir, "-extpass", "echo test")
checkExampleFS(t, pDir)
unmount(pDir)
mount(cDir, pDir, "-masterkey", "199eae55-36bff4af-83b9a3a2-4fa16f65-"+
mountOrFatal(t, cDir, pDir, "-masterkey", "199eae55-36bff4af-83b9a3a2-4fa16f65-"+
"1549ccdb-2d08d1f0-b1b26965-1b61f896", "-emenames=false", "-gcmiv128=false")
checkExampleFS(t, pDir)
unmount(pDir)
@ -120,10 +120,10 @@ func TestExampleFSv06(t *testing.T) {
if err != nil {
t.Fatal(err)
}
mount(cDir, pDir, "-extpass", "echo test")
mountOrFatal(t, cDir, pDir, "-extpass", "echo test")
checkExampleFS(t, pDir)
unmount(pDir)
mount(cDir, pDir, "-masterkey", "7bc8deb0-5fc894ef-a093da43-61561a81-"+
mountOrFatal(t, cDir, pDir, "-masterkey", "7bc8deb0-5fc894ef-a093da43-61561a81-"+
"0e8dee83-fdc056a4-937c37dd-9df5c520", "-gcmiv128=false")
checkExampleFS(t, pDir)
unmount(pDir)
@ -144,10 +144,10 @@ func TestExampleFSv06PlaintextNames(t *testing.T) {
if err != nil {
t.Fatal(err)
}
mount(cDir, pDir, "-extpass", "echo test")
mountOrFatal(t, cDir, pDir, "-extpass", "echo test")
checkExampleFS(t, pDir)
unmount(pDir)
mount(cDir, pDir, "-masterkey", "f4690202-595e4593-64c4f7e0-4dddd7d1-"+
mountOrFatal(t, cDir, pDir, "-masterkey", "f4690202-595e4593-64c4f7e0-4dddd7d1-"+
"303147f9-0ca8aea2-966341a7-52ea8ae9", "-plaintextnames", "-gcmiv128=false")
checkExampleFS(t, pDir)
unmount(pDir)
@ -167,10 +167,10 @@ func TestExampleFSv07(t *testing.T) {
if err != nil {
t.Fatal(err)
}
mount(cDir, pDir, "-extpass", "echo test")
mountOrFatal(t, cDir, pDir, "-extpass", "echo test")
checkExampleFS(t, pDir)
unmount(pDir)
mount(cDir, pDir, "-masterkey", "ed7f6d83-40cce86c-0e7d79c2-a9438710-"+
mountOrFatal(t, cDir, pDir, "-masterkey", "ed7f6d83-40cce86c-0e7d79c2-a9438710-"+
"575221bf-30a0eb60-2821fa8f-7f3123bf")
checkExampleFS(t, pDir)
unmount(pDir)
@ -189,10 +189,10 @@ func TestExampleFSv09(t *testing.T) {
if err != nil {
t.Fatal(err)
}
mount(cDir, pDir, "-extpass", "echo test")
mountOrFatal(t, cDir, pDir, "-extpass", "echo test")
checkExampleFSLongnames(t, pDir)
unmount(pDir)
mount(cDir, pDir, "-masterkey", "1cafe3f4-bc316466-2214c47c-ecd89bf3-"+
mountOrFatal(t, cDir, pDir, "-masterkey", "1cafe3f4-bc316466-2214c47c-ecd89bf3-"+
"4e078fe4-f5faeea7-8b7cab02-884f5e1c")
checkExampleFSLongnames(t, pDir)
unmount(pDir)

View File

@ -60,7 +60,7 @@ func resetTmpDir(plaintextNames bool) {
}
// mount CIPHERDIR "c" on PLAINDIR "p"
func mount(c string, p string, extraArgs ...string) {
func mount(c string, p string, extraArgs ...string) error {
var args []string
args = append(args, extraArgs...)
args = append(args, "-nosyslog", "-q", "-wpanic")
@ -70,16 +70,27 @@ func mount(c string, p string, extraArgs ...string) {
args = append(args, p)
cmd := exec.Command(gocryptfsBinary, args...)
cmd.Stderr = os.Stderr
if testing.Verbose() {
cmd.Stdout = os.Stdout
}
err := cmd.Run()
cmd.Stdout = os.Stdout
return cmd.Run()
}
// mountOrExit calls mount() and exits on failure.
func mountOrExit(c string, p string, extraArgs ...string) {
err := mount(c, p, extraArgs...)
if err != nil {
fmt.Println(err)
fmt.Printf("mount failed: %v", err)
os.Exit(1)
}
}
// mountOrFatal calls mount() and calls t.Fatal() on failure.
func mountOrFatal(t *testing.T, c string, p string, extraArgs ...string) {
err := mount(c, p, extraArgs...)
if err != nil {
t.Fatal(fmt.Errorf("mount failed: %v", err))
}
}
// unmount PLAINDIR "p"
func unmount(p string) error {
fu := exec.Command("fusermount", "-u", "-z", p)

View File

@ -28,7 +28,7 @@ func TestMain(m *testing.M) {
fmt.Println("***** Testing with OpenSSL")
}
resetTmpDir(false) // <- this also create gocryptfs.diriv
mount(defaultCipherDir, defaultPlainDir, "--zerokey")
mountOrExit(defaultCipherDir, defaultPlainDir, "--zerokey")
r := m.Run()
unmount(defaultPlainDir)
@ -44,7 +44,7 @@ func TestMain(m *testing.M) {
fmt.Println("***** Testing with native Go crypto")
}
resetTmpDir(false)
mount(defaultCipherDir, defaultPlainDir, "--zerokey", "--openssl=false")
mountOrExit(defaultCipherDir, defaultPlainDir, "--zerokey", "--openssl=false")
r = m.Run()
unmount(defaultPlainDir)
@ -56,7 +56,7 @@ func TestMain(m *testing.M) {
fmt.Println("***** Testing \"--plaintextnames\"")
}
resetTmpDir(true) // do not create gocryptfs.diriv
mount(defaultCipherDir, defaultPlainDir, "--zerokey", "--plaintextnames")
mountOrExit(defaultCipherDir, defaultPlainDir, "--zerokey", "--plaintextnames")
plaintextNames = true
r = m.Run()
unmount(defaultPlainDir)