aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJakob Unterwurzacher2019-01-01 19:44:41 +0100
committerJakob Unterwurzacher2019-01-01 20:05:38 +0100
commit887d5aa8e74e20b065b9a0c9b6e99dd05815112a (patch)
tree98dea76b27151a407b634fd2e6de055baa982669 /tests
parent817c485bb7dbd890a15aca683d6004437772c659 (diff)
tests: teach ListFds() to check other processes
Diffstat (limited to 'tests')
-rw-r--r--tests/cli/cli_test.go4
-rw-r--r--tests/matrix/matrix_test.go4
-rw-r--r--tests/test_helpers/helpers.go21
3 files changed, 20 insertions, 9 deletions
diff --git a/tests/cli/cli_test.go b/tests/cli/cli_test.go
index 314f6ce..48ce303 100644
--- a/tests/cli/cli_test.go
+++ b/tests/cli/cli_test.go
@@ -21,9 +21,9 @@ var testPw = []byte("test")
func TestMain(m *testing.M) {
test_helpers.ResetTmpDir(false)
- before := test_helpers.ListFds()
+ before := test_helpers.ListFds(0)
r := m.Run()
- after := test_helpers.ListFds()
+ after := test_helpers.ListFds(0)
if len(before) != len(after) {
fmt.Printf("fd leak? before, after:\n%v\n%v\n", before, after)
os.Exit(1)
diff --git a/tests/matrix/matrix_test.go b/tests/matrix/matrix_test.go
index 660f9d7..31527f0 100644
--- a/tests/matrix/matrix_test.go
+++ b/tests/matrix/matrix_test.go
@@ -74,11 +74,11 @@ func TestMain(m *testing.M) {
opts = append(opts, fmt.Sprintf("-aessiv=%v", testcase.aessiv))
opts = append(opts, fmt.Sprintf("-raw64=%v", testcase.raw64))
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, opts...)
- before := test_helpers.ListFds()
+ before := test_helpers.ListFds(0)
r := m.Run()
// Catch fd leaks in the tests. NOTE: this does NOT catch leaks in
// the gocryptfs FUSE process, but only in the tests that access it!
- after := test_helpers.ListFds()
+ after := test_helpers.ListFds(0)
if len(before) != len(after) {
fmt.Printf("fd leak? before, after:\n%v\n%v\n", before, after)
os.Exit(1)
diff --git a/tests/test_helpers/helpers.go b/tests/test_helpers/helpers.go
index aac03a8..22f47b2 100644
--- a/tests/test_helpers/helpers.go
+++ b/tests/test_helpers/helpers.go
@@ -14,6 +14,7 @@ import (
"os/exec"
"os/signal"
"path/filepath"
+ "runtime"
"syscall"
"testing"
"time"
@@ -481,10 +482,20 @@ func ExtractCmdExitCode(err error) int {
return 0
}
-// ListFds lists our open file descriptors.
-// We use /dev/fd because it exists on both Linux and MacOS.
-func ListFds() []string {
- f, err := os.Open("/dev/fd")
+// ListFds lists the open file descriptors for process "pid". Pass pid=0 for
+// ourselves.
+func ListFds(pid int) []string {
+ // We need /proc to get the list of fds for other processes. Only exists
+ // on Linux.
+ if runtime.GOOS != "linux" && pid > 0 {
+ return nil
+ }
+ // Both Linux and MacOS have /dev/fd
+ dir := "/dev/fd"
+ if pid > 0 {
+ dir = fmt.Sprintf("/proc/%d/fd", pid)
+ }
+ f, err := os.Open(dir)
if err != nil {
log.Panic(err)
}
@@ -495,7 +506,7 @@ func ListFds() []string {
}
for i, n := range names {
// Note: Readdirnames filters "." and ".."
- target, _ := os.Readlink("/dev/fd/" + n)
+ target, _ := os.Readlink(dir + "/" + n)
names[i] = n + "=" + target
}
return names