aboutsummaryrefslogtreecommitdiff
path: root/internal/syscallcompat/main_test.go
blob: ddf6bc4037d5dac2eedd571fb1a12349a969b24d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package syscallcompat

import (
	"fmt"
	"io/ioutil"
	"os"
	"testing"
)

var tmpDir string
var tmpDirFd int

func TestMain(m *testing.M) {
	origWd, err := os.Getwd()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	// Cannot import test_helpers because of import cycle
	parent := fmt.Sprintf("/tmp/gocryptfs-test-parent-%d", os.Getuid())
	err = os.MkdirAll(parent, 0700)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	tmpDir, err = ioutil.TempDir(parent, "syscallcompat")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	dirf, err := os.Open(tmpDir)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer dirf.Close()
	tmpDirFd = int(dirf.Fd())
	// Run the tests
	r := m.Run()
	// Check that we are in the same directory again (the emulated syscalls
	// use Fchdir a lot)
	cwd, _ := os.Getwd()
	if cwd != origWd {
		fmt.Printf("working dir has changed from %q to %q", origWd, cwd)
		os.Exit(1)
	}
	os.Exit(r)
}