test_helper: VerifyExistence: don't panic

Instead bubble up the error to the testing object.
This commit is contained in:
Jakob Unterwurzacher 2020-08-15 15:39:08 +02:00
parent 1ea1b179c2
commit f270135c16
5 changed files with 24 additions and 23 deletions

View File

@ -200,7 +200,7 @@ func TestExampleFSv11reverse(t *testing.T) {
dirA = tmpFsPath + dirA dirA = tmpFsPath + dirA
test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-extpass", "echo test", opensslOpt) test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-extpass", "echo test", opensslOpt)
c := dirB + "/gocryptfs.conf" c := dirB + "/gocryptfs.conf"
if !test_helpers.VerifyExistence(c) { if !test_helpers.VerifyExistence(t, c) {
t.Errorf("%s missing", c) t.Errorf("%s missing", c)
} }
test_helpers.MountOrFatal(t, dirB, dirC, "-extpass", "echo test", opensslOpt) test_helpers.MountOrFatal(t, dirB, dirC, "-extpass", "echo test", opensslOpt)
@ -211,7 +211,7 @@ func TestExampleFSv11reverse(t *testing.T) {
m := "68b51855-042abd80-635ae1ba-90152a78-2ec2d243-832ac72a-eab0561a-f2d37913" m := "68b51855-042abd80-635ae1ba-90152a78-2ec2d243-832ac72a-eab0561a-f2d37913"
test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-masterkey", m, test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-masterkey", m,
"-raw64=false", "-hkdf=false", opensslOpt) "-raw64=false", "-hkdf=false", opensslOpt)
if !test_helpers.VerifyExistence(c) { if !test_helpers.VerifyExistence(t, c) {
t.Errorf("%s missing", c) t.Errorf("%s missing", c)
} }
test_helpers.MountOrFatal(t, dirB, dirC, "-aessiv", "-masterkey", m, test_helpers.MountOrFatal(t, dirB, dirC, "-aessiv", "-masterkey", m,
@ -237,7 +237,7 @@ func TestExampleFSv11reversePlaintextnames(t *testing.T) {
dirA = tmpFsPath + dirA dirA = tmpFsPath + dirA
test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-extpass", "echo test", opensslOpt) test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-extpass", "echo test", opensslOpt)
c := dirB + "/gocryptfs.conf" c := dirB + "/gocryptfs.conf"
if !test_helpers.VerifyExistence(c) { if !test_helpers.VerifyExistence(t, c) {
t.Errorf("%s missing", c) t.Errorf("%s missing", c)
} }
test_helpers.MountOrFatal(t, dirB, dirC, "-extpass", "echo test", opensslOpt) test_helpers.MountOrFatal(t, dirB, dirC, "-extpass", "echo test", opensslOpt)
@ -248,7 +248,7 @@ func TestExampleFSv11reversePlaintextnames(t *testing.T) {
m := "e7fb8f0d-2a81df9e-26611e4b-5540b218-e48aa458-c2a623af-d0c82637-1466b5f2" m := "e7fb8f0d-2a81df9e-26611e4b-5540b218-e48aa458-c2a623af-d0c82637-1466b5f2"
test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-masterkey", m, test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-masterkey", m,
"-raw64=false", "-hkdf=false", opensslOpt) "-raw64=false", "-hkdf=false", opensslOpt)
if !test_helpers.VerifyExistence(c) { if !test_helpers.VerifyExistence(t, c) {
t.Errorf("%s missing", c) t.Errorf("%s missing", c)
} }
test_helpers.MountOrFatal(t, dirB, dirC, "-aessiv", "-masterkey", m, test_helpers.MountOrFatal(t, dirB, dirC, "-aessiv", "-masterkey", m,
@ -335,7 +335,7 @@ func TestExampleFSv13reverse(t *testing.T) {
// Mount using password // Mount using password
test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-extpass", "echo test", opensslOpt) test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-extpass", "echo test", opensslOpt)
c := dirB + "/gocryptfs.conf" c := dirB + "/gocryptfs.conf"
if !test_helpers.VerifyExistence(c) { if !test_helpers.VerifyExistence(t, c) {
t.Errorf("%s missing", c) t.Errorf("%s missing", c)
} }
test_helpers.MountOrFatal(t, dirB, dirC, "-extpass", "echo test", opensslOpt) test_helpers.MountOrFatal(t, dirB, dirC, "-extpass", "echo test", opensslOpt)
@ -367,7 +367,7 @@ func TestExampleFSv13reverse(t *testing.T) {
// Mount using masterkey // Mount using masterkey
m := "2290a7f4-3e1908fb-b006f7d9-261bdaf1-4b72bc38-3b24956c-db7d8a8d-d996076a" m := "2290a7f4-3e1908fb-b006f7d9-261bdaf1-4b72bc38-3b24956c-db7d8a8d-d996076a"
test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-masterkey", m, opensslOpt) test_helpers.MountOrFatal(t, dirA, dirB, "-reverse", "-masterkey", m, opensslOpt)
if !test_helpers.VerifyExistence(c) { if !test_helpers.VerifyExistence(t, c) {
t.Errorf("%s missing", c) t.Errorf("%s missing", c)
} }
test_helpers.MountOrFatal(t, dirB, dirC, "-aessiv", "-masterkey", m, opensslOpt) test_helpers.MountOrFatal(t, dirB, dirC, "-aessiv", "-masterkey", m, opensslOpt)

View File

@ -493,7 +493,7 @@ func TestLongNames(t *testing.T) {
t.Fatalf("Could not create n255x: %v", err) t.Fatalf("Could not create n255x: %v", err)
} }
f.Close() f.Close()
if !test_helpers.VerifyExistence(wd + n255x) { if !test_helpers.VerifyExistence(t, wd+n255x) {
t.Errorf("n255x is not in directory listing") t.Errorf("n255x is not in directory listing")
} }
// Rename long to long (target does not exist) // Rename long to long (target does not exist)
@ -502,7 +502,7 @@ func TestLongNames(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Could not rename n255x to n255y: %v", err) t.Fatalf("Could not rename n255x to n255y: %v", err)
} }
if !test_helpers.VerifyExistence(wd + n255y) { if !test_helpers.VerifyExistence(t, wd+n255y) {
t.Errorf("n255y is not in directory listing") t.Errorf("n255y is not in directory listing")
} }
// Rename long to long (target exists) // Rename long to long (target exists)
@ -515,7 +515,7 @@ func TestLongNames(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Could not rename n255x to n255y: %v", err) t.Fatalf("Could not rename n255x to n255y: %v", err)
} }
if !test_helpers.VerifyExistence(wd + n255y) { if !test_helpers.VerifyExistence(t, wd+n255y) {
t.Errorf("n255y is not in directory listing") t.Errorf("n255y is not in directory listing")
} }
// Rename long to short (target does not exist) // Rename long to short (target does not exist)
@ -523,7 +523,7 @@ func TestLongNames(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Could not rename n255y to short: %v", err) t.Fatalf("Could not rename n255y to short: %v", err)
} }
if !test_helpers.VerifyExistence(wd + "short") { if !test_helpers.VerifyExistence(t, wd+"short") {
t.Errorf("short is not in directory listing") t.Errorf("short is not in directory listing")
} }
// Rename long to short (target exists) // Rename long to short (target exists)
@ -536,7 +536,7 @@ func TestLongNames(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Could not rename n255y to short: %v", err) t.Fatalf("Could not rename n255y to short: %v", err)
} }
if !test_helpers.VerifyExistence(wd + "short") { if !test_helpers.VerifyExistence(t, wd+"short") {
t.Errorf("short is not in directory listing") t.Errorf("short is not in directory listing")
} }
// Rename short to long (target does not exist) // Rename short to long (target does not exist)
@ -544,7 +544,7 @@ func TestLongNames(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Could not rename short to n255x: %v", err) t.Fatalf("Could not rename short to n255x: %v", err)
} }
if !test_helpers.VerifyExistence(wd + n255x) { if !test_helpers.VerifyExistence(t, wd+n255x) {
t.Errorf("255x is not in directory listing II") t.Errorf("255x is not in directory listing II")
} }
// Rename short to long (target exists) // Rename short to long (target exists)
@ -557,7 +557,7 @@ func TestLongNames(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Could not rename short to n255x: %v", err) t.Fatalf("Could not rename short to n255x: %v", err)
} }
if !test_helpers.VerifyExistence(wd + n255x) { if !test_helpers.VerifyExistence(t, wd+n255x) {
t.Errorf("n255x is not in directory listing") t.Errorf("n255x is not in directory listing")
} }
// Unlink // Unlink
@ -565,7 +565,7 @@ func TestLongNames(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Could not unlink n255x: %v", err) t.Fatalf("Could not unlink n255x: %v", err)
} }
if test_helpers.VerifyExistence(wd + n255x) { if test_helpers.VerifyExistence(t, wd+n255x) {
t.Errorf("n255x still there after unlink") t.Errorf("n255x still there after unlink")
} }
// Long symlink // Long symlink
@ -574,7 +574,7 @@ func TestLongNames(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !test_helpers.VerifyExistence(wd + n255s) { if !test_helpers.VerifyExistence(t, wd+n255s) {
t.Errorf("n255s is not in directory listing") t.Errorf("n255s is not in directory listing")
} }
err = syscall.Unlink(wd + n255s) err = syscall.Unlink(wd + n255s)

View File

@ -26,7 +26,7 @@ func TestLongnameStat(t *testing.T) {
} }
fd.Close() fd.Close()
path := dirC + "/" + name path := dirC + "/" + name
if !test_helpers.VerifyExistence(path) { if !test_helpers.VerifyExistence(t, path) {
t.Fatalf("failed to verify %q", path) t.Fatalf("failed to verify %q", path)
} }
test_helpers.VerifySize(t, path, 0) test_helpers.VerifySize(t, path, 0)
@ -106,7 +106,7 @@ func TestSymlinkDentrySize(t *testing.T) {
// gocryptfs.conf // gocryptfs.conf
func TestConfigMapping(t *testing.T) { func TestConfigMapping(t *testing.T) {
c := dirB + "/gocryptfs.conf" c := dirB + "/gocryptfs.conf"
if !test_helpers.VerifyExistence(c) { if !test_helpers.VerifyExistence(t, c) {
t.Errorf("%s missing", c) t.Errorf("%s missing", c)
} }
data, err := ioutil.ReadFile(c) data, err := ioutil.ReadFile(c)

View File

@ -118,7 +118,7 @@ func testExclude(t *testing.T, flag string) {
cExclude := encryptExcludeTestPaths(t, sock, pExclude) cExclude := encryptExcludeTestPaths(t, sock, pExclude)
// Check that "excluded" paths are not there and "ok" paths are there // Check that "excluded" paths are not there and "ok" paths are there
for _, v := range cExclude { for _, v := range cExclude {
if test_helpers.VerifyExistence(mnt + "/" + v) { if test_helpers.VerifyExistence(t, mnt+"/"+v) {
t.Errorf("File %q is visible, but should be excluded", v) t.Errorf("File %q is visible, but should be excluded", v)
} }
if nametransform.IsLongContent(filepath.Base(v)) { if nametransform.IsLongContent(filepath.Base(v)) {
@ -126,7 +126,7 @@ func testExclude(t *testing.T, flag string) {
} }
} }
for _, v := range cOk { for _, v := range cOk {
if !test_helpers.VerifyExistence(mnt + "/" + v) { if !test_helpers.VerifyExistence(t, mnt+"/"+v) {
t.Errorf("File %q is hidden, but should be visible", v) t.Errorf("File %q is hidden, but should be visible", v)
} }
} }

View File

@ -289,7 +289,8 @@ func TestRename(t *testing.T, plainDir string) {
// VerifyExistence checks in 3 ways that "path" exists: // VerifyExistence checks in 3 ways that "path" exists:
// stat, open, readdir. Returns true if the path exists, false otherwise. // stat, open, readdir. Returns true if the path exists, false otherwise.
// Panics if the result is inconsistent. // Panics if the result is inconsistent.
func VerifyExistence(path string) bool { func VerifyExistence(t *testing.T, path string) bool {
t.Helper()
// Check if file can be stat()ed // Check if file can be stat()ed
stat := true stat := true
fi, err := os.Stat(path) fi, err := os.Stat(path)
@ -309,12 +310,12 @@ func VerifyExistence(path string) bool {
name := filepath.Base(path) name := filepath.Base(path)
d, err := os.Open(dir) d, err := os.Open(dir)
if err != nil && open == true { if err != nil && open == true {
log.Panicf("we can open the file but not the parent dir!? err=%v", err) t.Errorf("VerifyExistence: we can open the file but not the parent dir!? err=%v", err)
} else if err == nil { } else if err == nil {
defer d.Close() defer d.Close()
listing, err := d.Readdirnames(0) listing, err := d.Readdirnames(0)
if stat && fi.IsDir() && err != nil { if stat && fi.IsDir() && err != nil {
log.Panicf("It's a directory, but readdirnames failed: %v", err) t.Errorf("VerifyExistence: It's a directory, but readdirnames failed: %v", err)
} }
for _, entry := range listing { for _, entry := range listing {
if entry == name { if entry == name {
@ -326,7 +327,7 @@ func VerifyExistence(path string) bool {
if stat == open && open == readdir { if stat == open && open == readdir {
return stat return stat
} }
log.Panicf("inconsistent result on %q: stat=%v open=%v readdir=%v, path=%q", name, stat, open, readdir, path) t.Errorf("VerifyExistence: inconsistent result on %q: stat=%v open=%v readdir=%v, path=%q", name, stat, open, readdir, path)
return false return false
} }