summaryrefslogtreecommitdiff
path: root/tests/matrix/fallocate_test.go
blob: c94c070b17c9602d29d319cabdaba63dee73586c (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package matrix

import (
	"os"
	"runtime"
	"syscall"
	"testing"

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

const (
	// From man statfs
	TMPFS_MAGIC      = 0x01021994
	EXT4_SUPER_MAGIC = 0xef53
)

// isWellKnownFS decides if the backing filesystem is well-known.
// The expected allocated sizes are only valid on tmpfs and ext4. btrfs
// gives different results, but that's not an error.
func isWellKnownFS(fn string) bool {
	var fs syscall.Statfs_t
	err := syscall.Statfs(fn, &fs)
	if err != nil {
		panic(err)
	}
	if fs.Type == EXT4_SUPER_MAGIC || fs.Type == TMPFS_MAGIC {
		return true
	}
	return false
}

const FALLOC_DEFAULT = 0x00
const FALLOC_FL_KEEP_SIZE = 0x01

func TestFallocate(t *testing.T) {
	if runtime.GOOS == "darwin" {
		t.Skipf("OSX does not support fallocate")
	}
	fn := test_helpers.DefaultPlainDir + "/fallocate"
	file, err := os.Create(fn)
	if err != nil {
		t.FailNow()
	}
	defer file.Close()
	wellKnown := isWellKnownFS(test_helpers.DefaultCipherDir)
	fd := int(file.Fd())
	nBytes := test_helpers.Du(t, fd)
	if nBytes != 0 {
		t.Fatalf("Empty file has %d bytes", nBytes)
	}
	// Allocate 30 bytes, keep size
	// gocryptfs ||        (0 blocks)
	//      ext4 |  d   |  (1 block)
	//              ^ d = data block
	err = syscallcompat.Fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, 30)
	if err != nil {
		t.Error(err)
	}
	var want int64
	nBytes = test_helpers.Du(t, fd)
	want = 4096
	if nBytes != want {
		t.Errorf("Expected %d allocated bytes, have %d", want, nBytes)
	}
	test_helpers.VerifySize(t, fn, 0)
	// Three ciphertext blocks. The middle one should be a file hole.
	// gocryptfs |  h   |   h  | d|   (1 block)
	//      ext4 |  d  |  h  |  d  |  (2 blocks)
	//                    ^ h = file hole
	// (Note that gocryptfs blocks are slightly bigger than the ext4 blocks,
	// but the last one is partial)
	err = file.Truncate(9000)
	if err != nil {
		t.Fatal(err)
	}
	nBytes = test_helpers.Du(t, fd)
	want = 2 * 4096
	if wellKnown && nBytes != want {
		t.Errorf("Expected %d allocated bytes, have %d", want, nBytes)
	}
	if md5 := test_helpers.Md5fn(fn); md5 != "5420afa22f6423a9f59e669540656bb4" {
		t.Errorf("Wrong md5 %s", md5)
	}
	// Allocate the whole file space
	// gocryptfs |  h   |   h  | d|   (1 block)
	//      ext4 |  d  |  d  |  d  |  (3 blocks
	err = syscallcompat.Fallocate(fd, FALLOC_DEFAULT, 0, 9000)
	if err != nil {
		t.Fatal(err)
	}
	nBytes = test_helpers.Du(t, fd)
	want = 3 * 4096
	if nBytes != want {
		t.Errorf("Expected %d allocated bytes, have %d", want, nBytes)
	}
	// Neither apparent size nor content should have changed
	test_helpers.VerifySize(t, fn, 9000)
	if md5 := test_helpers.Md5fn(fn); md5 != "5420afa22f6423a9f59e669540656bb4" {
		t.Errorf("Wrong md5 %s", md5)
	}

	// Partial block on the end. The first ext4 block is dirtied by the header.
	// gocryptfs |  h   |   h  | d|   (1 block)
	//      ext4 |  d  |  h  |  d  |  (2 blocks)
	file.Truncate(0)
	file.Truncate(9000)
	nBytes = test_helpers.Du(t, fd)
	want = 2 * 4096
	if wellKnown && nBytes != want {
		t.Errorf("Expected %d allocated bytes, have %d", want, nBytes)
	}
	// Allocate 10 bytes in the second block
	// gocryptfs |  h   |   h  | d|   (1 block)
	//      ext4 |  d  |  d  |  d  |  (3 blocks)
	syscallcompat.Fallocate(fd, FALLOC_DEFAULT, 5000, 10)
	nBytes = test_helpers.Du(t, fd)
	want = 3 * 4096
	if wellKnown && nBytes != want {
		t.Errorf("Expected %d allocated bytes, have %d", want, nBytes)
	}
	// Neither apparent size nor content should have changed
	test_helpers.VerifySize(t, fn, 9000)
	if md5 := test_helpers.Md5fn(fn); md5 != "5420afa22f6423a9f59e669540656bb4" {
		t.Errorf("Wrong md5 %s", md5)
	}
	// Grow the file to 4 blocks
	// gocryptfs |  h   |  h   |  d   |d|  (2 blocks)
	//      ext4 |  d  |  d  |  d  |  d  | (4 blocks)
	syscallcompat.Fallocate(fd, FALLOC_DEFAULT, 15000, 10)
	nBytes = test_helpers.Du(t, fd)
	want = 4 * 4096
	if wellKnown && nBytes != want {
		t.Errorf("Expected %d allocated bytes, have %d", want, nBytes)
	}
	test_helpers.VerifySize(t, fn, 15010)
	if md5 := test_helpers.Md5fn(fn); md5 != "c4c44c7a41ab7798a79d093eb44f99fc" {
		t.Errorf("Wrong md5 %s", md5)
	}
	// Shrinking a file using fallocate should have no effect
	for _, off := range []int64{0, 10, 2000, 5000} {
		for _, sz := range []int64{0, 1, 42, 6000} {
			syscallcompat.Fallocate(fd, FALLOC_DEFAULT, off, sz)
			test_helpers.VerifySize(t, fn, 15010)
			if md5 := test_helpers.Md5fn(fn); md5 != "c4c44c7a41ab7798a79d093eb44f99fc" {
				t.Errorf("Wrong md5 %s", md5)
			}
		}
	}
	// We used to allocate 18 bytes too much:
	// https://github.com/rfjakob/gocryptfs/v2/issues/311
	//
	// 8110 bytes of plaintext should get us exactly 8192 bytes of ciphertext.
	err = file.Truncate(0)
	if err != nil {
		t.Fatal(err)
	}
	err = syscallcompat.Fallocate(fd, FALLOC_DEFAULT, 0, 8110)
	if err != nil {
		t.Fatal(err)
	}
	nBytes = test_helpers.Du(t, fd)
	want = 8192
	if nBytes != want {
		t.Errorf("Expected %d allocated bytes, have %d", want, nBytes)
	}
	// Cleanup
	syscall.Unlink(fn)
	if !wellKnown {
		// Even though most tests have been executed still, inform the user
		// that some were disabled
		t.Skipf("backing fs is not ext4 or tmpfs, skipped some disk-usage checks\n")
	}
}