-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmemfd_test.go
252 lines (240 loc) · 5.07 KB
/
memfd_test.go
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package memfd
import (
"os"
"testing"
)
func TestCreate(t *testing.T) {
mfd, err := Create()
if err != nil {
t.Errorf("Create failed: %v", err)
}
mfd.Close()
}
func TestCreateFlags(t *testing.T) {
mfd, err := CreateNameFlags("test", Cloexec)
if err != nil {
t.Errorf("CreateFlags failed: %v", err)
}
mfd.Close()
}
func TestSealing(t *testing.T) {
mfd, err := Create()
if err != nil {
t.Errorf("Create failed: %v", err)
}
defer mfd.Close()
seals := mfd.Seals()
if seals != 0 {
t.Errorf("Expected no seals initially, got %d", seals)
}
if mfd.IsImmutable() {
t.Errorf("Expected not fully sealed initially")
}
err = mfd.SetSeals(SealWrite)
if err != nil {
t.Errorf("SetSeals failed: %v", err)
}
seals = mfd.Seals()
if seals != SealWrite {
t.Errorf("Expected write seal, got %d", seals)
}
if mfd.IsImmutable() {
t.Errorf("Expected not fully immutable after setting write seal")
}
err = mfd.SetImmutable()
if err != nil {
t.Errorf("SetImmutable failed: %v", err)
}
if !mfd.IsImmutable() {
t.Errorf("Expected fully immutable after setting immutable")
}
seals = mfd.Seals()
if seals != SealAll {
t.Errorf("Expected all seals set after setting immutable: got %d", seals)
}
}
func TestResize(t *testing.T) {
mfd, err := Create()
if err != nil {
t.Errorf("Create failed: %v", err)
}
defer mfd.Close()
err = mfd.SetSize(1024)
if err != nil {
t.Errorf("Grow failed: %v", err)
}
err = mfd.SetSeals(SealShrink)
if err != nil {
t.Errorf("SetSeals failed: %v", err)
}
err = mfd.SetSize(2048)
if err != nil {
t.Errorf("Grow failed: %v", err)
}
err = mfd.SetSize(0)
if err == nil {
t.Errorf("Shrink succeeded after seal")
}
err = mfd.SetSeals(SealGrow)
if err != nil {
t.Errorf("SetSeals failed: %v", err)
}
err = mfd.SetSize(4096)
if err == nil {
t.Errorf("Grow succeeded after seal")
}
}
func TestImmutable(t *testing.T) {
mfd, err := Create()
if err != nil {
t.Errorf("Create failed: %v", err)
}
defer mfd.Close()
err = mfd.SetImmutable()
if err != nil {
t.Errorf("SetImmutable failed: %v", err)
}
err = mfd.SetSize(1024)
if err == nil {
t.Errorf("Resize succeeded after seal")
}
}
func TestMap(t *testing.T) {
mfd, err := Create()
if err != nil {
t.Errorf("Create failed: %v", err)
}
defer mfd.Close()
text := "Putting something in the memfd"
n, err := mfd.WriteString(text)
if err != nil {
t.Errorf("Write error: %v", err)
}
if n != len(text) {
t.Errorf("Short write")
}
b, err := mfd.Map()
if err != nil {
t.Errorf("Map read write error: %v", err)
}
if string(b) != text {
t.Errorf("Did not read previous write: %s", string(b))
}
err = mfd.SetImmutable()
if err == nil {
t.Errorf("Should not be able to set immutable while there is a writeable mapping")
}
err = mfd.Unmap()
if err != nil {
t.Errorf("Unmap error: %v", err)
}
err = mfd.SetImmutable()
if err != nil {
t.Errorf("SetImmutable failed: %v", err)
}
b, err = mfd.Map()
if err != nil {
t.Errorf("Map read only error: %v", err)
}
err = mfd.Unmap()
if err != nil {
t.Errorf("Unmap error: %v", err)
}
}
func TestMapZero(t *testing.T) {
mfd, err := Create()
if err != nil {
t.Errorf("Create failed: %v", err)
}
defer mfd.Close()
b, err := mfd.Map()
if err != nil {
t.Errorf("Map error: %v", err)
}
if cap(b) != 0 {
t.Errorf("Expected zero capacity map, got %d", cap(b))
}
if len(b) != 0 {
t.Errorf("Expected zero length map, got %d", len(b))
}
err = mfd.Unmap()
if err != nil {
t.Errorf("Unmap error: %v", err)
}
}
func TestRemap(t *testing.T) {
mfd, err := Create()
if err != nil {
t.Errorf("Create failed: %v", err)
}
defer mfd.Close()
err = mfd.SetSize(100)
if err != nil {
t.Errorf("Resize failed: %v", err)
}
b, err := mfd.Map()
if err != nil {
t.Errorf("Map error: %v", err)
}
if cap(b) != 100 {
t.Errorf("Expected 100 capacity map, got %d", cap(b))
}
if len(b) != 100 {
t.Errorf("Expected 100 length map, got %d", len(b))
}
mfd.SetSize(200)
if cap(b) != 100 {
t.Errorf("Expected 100 capacity map, got %d", cap(b))
}
if len(b) != 100 {
t.Errorf("Expected 100 length map, got %d", len(b))
}
b, err = mfd.Remap()
if err != nil {
t.Errorf("Remap error: %v", err)
}
if cap(b) != 200 {
t.Errorf("Expected 200 capacity map, got %d", cap(b))
}
if len(b) != 200 {
t.Errorf("Expected 200 length map, got %d", len(b))
}
err = mfd.Unmap()
if err != nil {
t.Errorf("Unmap error: %v", err)
}
}
func TestNew(t *testing.T) {
mfd0, err := Create()
if err != nil {
t.Errorf("Create failed: %v", err)
}
defer mfd0.Close()
fd := mfd0.Fd()
mfd, err := New(uintptr(fd))
if err != nil {
t.Errorf("New failed: %v", err)
}
mfd.Close()
}
func TestNotMemfdNewMemfd(t *testing.T) {
file, err := os.Open("/dev/null")
if err != nil {
t.Errorf("Cannot open /dev/null")
}
fd := file.Fd()
_, err = New(fd)
if err == nil {
t.Errorf("Expected an error calling New with /dev/null")
}
}
func TestCloexec(t *testing.T) {
mfd, err := Create()
if err != nil {
t.Errorf("Create failed: %v", err)
}
defer mfd.Close()
// does not really test it is working at present
mfd.SetCloexec()
mfd.ClearCloexec()
}