aboutsummaryrefslogtreecommitdiff
path: root/tests/sharedstorage/sharedstorage_test.go
blob: d2230a023dd77a2f1a112246636630b3498d3432 (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
// test gocryptfs cipherdir mounted multiple times at the same time
package sharedstorage

import (
	"testing"

	"golang.org/x/sys/unix"

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

var flagSharestorage bool

func TestMain(m *testing.M) {
	flagSharestorage = false
	m.Run()
	flagSharestorage = true
	m.Run()
}

// mountSharedstorage mounts `cipherdir` on `mnt` with or without the
// `-sharedstorage` flag, depending on the global var `flagSharestorage`.
func mountSharedstorage(t *testing.T, cipherdir string, mnt string) {
	args := []string{"-extpass=echo test"}
	if flagSharestorage {
		args = append(args, "-sharedstorage")
	}
	test_helpers.MountOrFatal(t, cipherdir, mnt, args...)
}

func TestUnlink(t *testing.T) {
	cipherdir := test_helpers.InitFS(t)
	mnt1 := cipherdir + ".mnt1"
	mnt2 := cipherdir + ".mnt2"
	mountSharedstorage(t, cipherdir, mnt1)
	defer test_helpers.UnmountPanic(mnt1)
	mountSharedstorage(t, cipherdir, mnt2)
	defer test_helpers.UnmountPanic(mnt2)

	// Create dir via mnt1
	if err := unix.Mkdir(mnt1+"/foo", 0700); err != nil {
		t.Fatal(err)
	}
	// Replace dir with file via mnt2
	if err := unix.Rmdir(mnt2 + "/foo"); err != nil {
		t.Fatal(err)
	}
	if fd, err := unix.Creat(mnt2+"/foo", 0600); err != nil {
		t.Fatal(err)
	} else {
		unix.Close(fd)
	}
	// Try to unlink via mnt1
	if err := unix.Unlink(mnt1 + "/foo"); err != nil {
		if flagSharestorage {
			t.Fatal(err)
		} else {
			t.Logf("Unlink failed as expected: errno %d / %v", err, err)
		}
	}
}