aboutsummaryrefslogtreecommitdiff
path: root/tests/cluster/poc_test.go
blob: 6f74ba2db73a8af79146c581ffac035b9e390e5a (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cluster

// poc_test.go contains proof of concept tests for the byte-range locking logic.
// This goes directly to an underlying filesystem without going through gocryptfs.

import (
	"bytes"
	"errors"
	"io"
	"os"
	"sync"
	"sync/atomic"
	"syscall"
	"testing"

	"golang.org/x/sys/unix"

	"github.com/rfjakob/gocryptfs/v2/internal/contentenc"
	"github.com/rfjakob/gocryptfs/v2/internal/syscallcompat"
	"github.com/rfjakob/gocryptfs/v2/tests/test_helpers"
)

// Check that byte-range locks work on an empty file
func TestPoCFcntlFlock(t *testing.T) {
	path := test_helpers.TmpDir + "/" + t.Name()

	fd1, err := syscall.Open(path, syscall.O_CREAT|syscall.O_WRONLY|syscall.O_EXCL, 0600)
	if err != nil {
		t.Fatal(err)
	}
	defer syscall.Close(fd1)

	// F_OFD_SETLK locks on the same fd always succeed, so we have to
	// open a 2nd time.
	fd2, err := syscall.Open(path, syscall.O_RDWR, 0)
	if err != nil {
		t.Fatal(err)
	}
	defer syscall.Close(fd2)

	lk := unix.Flock_t{
		Type:   unix.F_WRLCK,
		Whence: unix.SEEK_SET,
		Start:  0,
		Len:    0,
	}
	err = unix.FcntlFlock(uintptr(fd1), unix.F_OFD_SETLK, &lk)
	if err != nil {
		t.Fatal(err)
	}
	err = unix.FcntlFlock(uintptr(fd2), unix.F_OFD_SETLK, &lk)
	if err == nil {
		t.Fatal("double-lock succeeded but should have failed")
	}
}

// See if we can get garbage data when the file header is read and written concurrently.
// We should get either 0 bytes or 18 correct bytes.
func TestPoCHeaderCreation(t *testing.T) {
	path := test_helpers.TmpDir + "/" + t.Name()
	var wg sync.WaitGroup
	// I ran this with 10000 iteration and no problems to be seen. Let's not waste too
	// much testing time.
	const loops = 100

	var stats struct {
		readOk    int64
		readEmpty int64
		writes    int64
	}

	writeBuf := []byte("123456789012345678")
	if len(writeBuf) != contentenc.HeaderLen {
		t.Fatal("BUG wrong header length")
	}

	writerThread := func() {
		defer wg.Done()
		for i := 0; i < loops; i++ {
			if t.Failed() {
				return
			}
			f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_EXCL, 0600)
			if err != nil {
				t.Errorf("BUG: this should not happen: open err=%v", err)
				return
			}
			// Do like gocryptfs does and prealloc the 18 bytes
			err = syscallcompat.EnospcPrealloc(int(f.Fd()), 0, contentenc.HeaderLen)

			_, err = f.WriteAt(writeBuf, 0)
			if err != nil {
				t.Errorf("iteration %d: Pwrite: %v", i, err)
			}
			atomic.AddInt64(&stats.writes, 1)
			f.Close()
			syscall.Unlink(path)
		}
	}

	readerThread := func() {
		defer wg.Done()
		for i := 0; i < loops; i++ {
			if t.Failed() {
				return
			}
			f, err := os.OpenFile(path, os.O_RDONLY, 0600)
			if errors.Is(err, os.ErrNotExist) {
				continue
			}
			if err != nil {
				t.Error(err)
				return
			}
			readBuf := make([]byte, contentenc.HeaderLen)
			_, err = f.ReadAt(readBuf, 0)
			if errors.Is(err, io.EOF) {
				atomic.AddInt64(&stats.readEmpty, 1)
				goto close
			}
			if err != nil {
				t.Errorf("iteration %d: ReadAt: %v", i, err)
				goto close
			}
			if !bytes.Equal(writeBuf, readBuf) {
				t.Errorf("iteration %d: corrupt data received: %x", i, readBuf)
				goto close
			}
			atomic.AddInt64(&stats.readOk, 1)
		close:
			f.Close()
		}
	}

	wg.Add(2)
	go writerThread()
	go readerThread()
	wg.Wait()

	t.Logf("readEmpty=%d readOk=%d writes=%d", stats.readEmpty, stats.readOk, stats.writes)
}