tests: teach ListFds() to check other processes
This commit is contained in:
parent
817c485bb7
commit
887d5aa8e7
@ -21,9 +21,9 @@ var testPw = []byte("test")
|
|||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
test_helpers.ResetTmpDir(false)
|
test_helpers.ResetTmpDir(false)
|
||||||
before := test_helpers.ListFds()
|
before := test_helpers.ListFds(0)
|
||||||
r := m.Run()
|
r := m.Run()
|
||||||
after := test_helpers.ListFds()
|
after := test_helpers.ListFds(0)
|
||||||
if len(before) != len(after) {
|
if len(before) != len(after) {
|
||||||
fmt.Printf("fd leak? before, after:\n%v\n%v\n", before, after)
|
fmt.Printf("fd leak? before, after:\n%v\n%v\n", before, after)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -74,11 +74,11 @@ func TestMain(m *testing.M) {
|
|||||||
opts = append(opts, fmt.Sprintf("-aessiv=%v", testcase.aessiv))
|
opts = append(opts, fmt.Sprintf("-aessiv=%v", testcase.aessiv))
|
||||||
opts = append(opts, fmt.Sprintf("-raw64=%v", testcase.raw64))
|
opts = append(opts, fmt.Sprintf("-raw64=%v", testcase.raw64))
|
||||||
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, opts...)
|
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, opts...)
|
||||||
before := test_helpers.ListFds()
|
before := test_helpers.ListFds(0)
|
||||||
r := m.Run()
|
r := m.Run()
|
||||||
// Catch fd leaks in the tests. NOTE: this does NOT catch leaks in
|
// 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!
|
// 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) {
|
if len(before) != len(after) {
|
||||||
fmt.Printf("fd leak? before, after:\n%v\n%v\n", before, after)
|
fmt.Printf("fd leak? before, after:\n%v\n%v\n", before, after)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -14,6 +14,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -481,10 +482,20 @@ func ExtractCmdExitCode(err error) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListFds lists our open file descriptors.
|
// ListFds lists the open file descriptors for process "pid". Pass pid=0 for
|
||||||
// We use /dev/fd because it exists on both Linux and MacOS.
|
// ourselves.
|
||||||
func ListFds() []string {
|
func ListFds(pid int) []string {
|
||||||
f, err := os.Open("/dev/fd")
|
// 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 {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
@ -495,7 +506,7 @@ func ListFds() []string {
|
|||||||
}
|
}
|
||||||
for i, n := range names {
|
for i, n := range names {
|
||||||
// Note: Readdirnames filters "." and ".."
|
// Note: Readdirnames filters "." and ".."
|
||||||
target, _ := os.Readlink("/dev/fd/" + n)
|
target, _ := os.Readlink(dir + "/" + n)
|
||||||
names[i] = n + "=" + target
|
names[i] = n + "=" + target
|
||||||
}
|
}
|
||||||
return names
|
return names
|
||||||
|
Loading…
x
Reference in New Issue
Block a user