summaryrefslogtreecommitdiff
path: root/tests/reverse/one_file_system_test.go
blob: a3e441fa3ff67d28731e2daec7da03898795a118 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package reverse_test

import (
	"io/ioutil"
	"net/url"
	"os"
	"runtime"
	"syscall"
	"testing"

	"github.com/rfjakob/gocryptfs/tests/test_helpers"
)

func TestOneFileSystem(t *testing.T) {
	if runtime.GOOS != "linux" {
		t.Skip("only works on linux")
	}
	// Let's not explode with "TempDir: pattern contains path separator"
	myEscapedName := url.PathEscape(t.Name())
	mnt, err := ioutil.TempDir(test_helpers.TmpDir, myEscapedName)
	if err != nil {
		t.Fatal(err)
	}
	cliArgs := []string{"-reverse", "-zerokey", "-one-file-system"}
	if plaintextnames {
		cliArgs = append(cliArgs, "-plaintextnames")
	} else if deterministic_names {
		cliArgs = append(cliArgs, "-deterministic-names")
	}
	test_helpers.MountOrFatal(t, "/", mnt, cliArgs...)
	defer test_helpers.UnmountErr(mnt)

	// Copied from inomap
	const maxPassthruIno = 1<<48 - 1

	entries, err := ioutil.ReadDir(mnt)
	if err != nil {
		t.Fatal(err)
	}
	mountpoints := []string{}
	for _, e := range entries {
		if !e.IsDir() {
			// We are only interested in directories
			continue
		}
		st := e.Sys().(*syscall.Stat_t)
		// The inode numbers of files with a different device number are remapped
		// to something above maxPassthruIno
		if st.Ino > maxPassthruIno {
			mountpoints = append(mountpoints, e.Name())
		}
	}
	if len(mountpoints) == 0 {
		t.Skip("no mountpoints found, nothing to test")
	}
	for _, m := range mountpoints {
		dir, err := os.Open(mnt + "/" + m)
		if err != nil {
			t.Error(err)
		}
		defer dir.Close()
		e, err := dir.Readdirnames(-1)
		if err != nil {
			t.Error(err)
		}
		expected := 1
		if plaintextnames || deterministic_names {
			expected = 0
		}
		if len(e) != expected {
			t.Errorf("mountpoint %q should have %d entries, actually has: %v", m, expected, e)
		}
	}
	t.Logf("tested %d mountpoints: %v", len(mountpoints), mountpoints)
}