-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathxattr_test.go
367 lines (330 loc) · 8.48 KB
/
xattr_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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//go:build linux || darwin || freebsd || netbsd || solaris
// +build linux darwin freebsd netbsd solaris
package xattr
import (
"bytes"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"syscall"
"testing"
"golang.org/x/sys/unix"
)
const UserPrefix = "user."
type funcFamily struct {
familyName string
get func(path, name string) ([]byte, error)
set func(path, name string, data []byte) error
setWithFlags func(path, name string, data []byte, flags int) error
remove func(path, name string) error
list func(path string) ([]string, error)
}
// Test Get, Set, List, Remove on a regular file
func TestRegularFile(t *testing.T) {
families := []funcFamily{
{
familyName: "Get and friends",
get: Get,
set: Set,
setWithFlags: SetWithFlags,
remove: Remove,
list: List,
},
{
familyName: "LGet and friends",
get: LGet,
set: LSet,
setWithFlags: LSetWithFlags,
remove: LRemove,
list: LList,
},
{
familyName: "FGet and friends",
get: wrapFGet,
set: wrapFSet,
setWithFlags: wrapFSetWithFlags,
remove: wrapFRemove,
list: wrapFList,
},
}
for _, ff := range families {
t.Run(ff.familyName, func(t *testing.T) {
t.Logf("Testing %q on a regular file", ff.familyName)
testRegularFile(t, ff)
})
}
}
// testRegularFile is called with the "Get and friends" and the
// "LGet and friends" function family. Both families should behave
// the same on a regular file.
func testRegularFile(t *testing.T, ff funcFamily) {
tmp, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())
xName := UserPrefix + "test"
xVal := []byte("test-attr-value")
// Test that SetWithFlags succeeds and that the xattr shows up in List()
err = ff.setWithFlags(tmp.Name(), xName, xVal, 0)
checkIfError(t, err)
list, err := ff.list(tmp.Name())
checkIfError(t, err)
found := false
for _, name := range list {
if name == xName {
found = true
}
}
if !found {
t.Fatalf("List/LList did not return test attribute: %q", list)
}
err = ff.remove(tmp.Name(), xName)
checkIfError(t, err)
// Test that Set succeeds and that the the xattr shows up in List()
err = ff.set(tmp.Name(), xName, xVal)
checkIfError(t, err)
list, err = ff.list(tmp.Name())
checkIfError(t, err)
found = false
for _, name := range list {
if name == xName {
found = true
}
}
if !found {
t.Fatalf("List/LList did not return test attribute: %q", list)
}
var data []byte
data, err = ff.get(tmp.Name(), xName)
checkIfError(t, err)
value := string(data)
t.Log(value)
if string(xVal) != value {
t.Fail()
}
err = ff.remove(tmp.Name(), xName)
checkIfError(t, err)
}
// Test that setting an xattr with an empty value works.
func TestNoData(t *testing.T) {
tmp, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())
err = Set(tmp.Name(), UserPrefix+"test", []byte{})
checkIfError(t, err)
list, err := List(tmp.Name())
checkIfError(t, err)
found := false
for _, name := range list {
if name == UserPrefix+"test" {
found = true
}
}
if !found {
t.Fatal("Listxattr did not return test attribute")
}
}
// Test that Get/LGet, Set/LSet etc operate as expected on symlinks. The
// functions should behave differently when operating on a symlink.
func TestSymlink(t *testing.T) {
if runtime.GOOS == "solaris" || runtime.GOOS == "illumos" {
t.Skipf("extended attributes aren't supported for symlinks on %s", runtime.GOOS)
}
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
s := dir + "/symlink1"
err = os.Symlink(dir+"/some/nonexistent/path", s)
if err != nil {
t.Fatal(err)
}
xName := UserPrefix + "TestSymlink"
xVal := []byte("test")
// Test Set/LSet
if err := Set(s, xName, xVal); err == nil {
t.Error("Set on a broken symlink should fail, but did not")
}
err = LSet(s, xName, xVal)
errno := unpackSysErr(err)
setOk := true
if runtime.GOOS == "linux" && errno == syscall.EPERM {
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/xattr.c?h=v4.17-rc5#n122 :
// In the user.* namespace, only regular files and directories can have
// extended attributes.
t.Log("got EPERM, adjusting test scope")
setOk = false
} else {
checkIfError(t, err)
}
// Test List/LList
_, err = List(s)
errno = unpackSysErr(err)
if errno != syscall.ENOENT {
t.Errorf("List() on a broken symlink should fail with ENOENT, got %q", errno)
}
data, err := LList(s)
checkIfError(t, err)
if setOk {
found := false
for _, n := range data {
if n == xName {
found = true
break
}
}
if !found {
t.Errorf("xattr %q did not show up in Llist output: %q", xName, data)
}
}
// Test Get/LGet
_, err = Get(s, xName)
errno = unpackSysErr(err)
if errno != syscall.ENOENT {
t.Errorf("Get() on a broken symlink should fail with ENOENT, got %q", errno)
}
val, err := LGet(s, xName)
if setOk {
checkIfError(t, err)
if !bytes.Equal(xVal, val) {
t.Errorf("wrong xattr value: want=%q have=%q", xVal, val)
}
} else {
errno = unpackSysErr(err)
if errno != ENOATTR {
t.Errorf("expected ENOATTR, got %q", errno)
}
}
// Test Remove/Lremove
err = Remove(s, xName)
errno = unpackSysErr(err)
if errno != syscall.ENOENT {
t.Errorf("Remove() on a broken symlink should fail with ENOENT, got %q", errno)
}
err = LRemove(s, xName)
if setOk {
checkIfError(t, err)
} else {
errno = unpackSysErr(err)
if errno != syscall.EPERM {
t.Errorf("expected EPERM, got %q", errno)
}
}
}
// Verify that Get() handles values larger than the default buffer size (1 KB)
func TestLargeVal(t *testing.T) {
tmp, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())
path := tmp.Name()
key := UserPrefix + "TestERANGE"
// On ext4, key + value length must be <= 4096. Use 4000 so we can test
// reliably on ext4.
val := bytes.Repeat([]byte("z"), 4000)
err = Set(path, key, val)
checkIfError(t, err)
val2, err := Get(path, key)
checkIfError(t, err)
if !bytes.Equal(val, val2) {
t.Errorf("wrong result from Get: want=%s have=%s", string(val), string(val2))
}
}
// Get should work on a FIFO that is not opened by any other process.
// This is mainly relevant on Solaris, where getting the attributes
// requires opening the file and doing that the wrong way blocks the caller.
func TestFIFO(t *testing.T) {
d, err := ioutil.TempDir("", "pkg-xattr-testfifo-*")
if err != nil {
t.Fatal(err)
}
t.Log("tempdir:", d)
defer os.RemoveAll(d)
fifo := filepath.Join(d, "fifo")
err = unix.Mkfifo(fifo, 0777)
if err != nil {
t.Fatal(err)
}
// We only care about this not blocking.
_ = Set(fifo, "foo", []byte("bar"))
_, _ = Get(fifo, "foo")
_, _ = List(fifo)
_ = Remove(fifo, "foo")
}
// checkIfError calls t.Skip() if the underlying syscall.Errno is
// ENOTSUP or EOPNOTSUPP. It calls t.Fatal() on any other non-zero error.
func checkIfError(t *testing.T, err error) {
errno := unpackSysErr(err)
if errno == syscall.Errno(0) {
return
}
// check if filesystem supports extended attributes
if errno == syscall.Errno(syscall.ENOTSUP) || errno == syscall.Errno(syscall.EOPNOTSUPP) {
t.Skip("Skipping test - filesystem does not support extended attributes")
} else {
t.Fatal(err)
}
}
// unpackSysErr unpacks the underlying syscall.Errno from an error value
// returned by Get/Set/...
func unpackSysErr(err error) syscall.Errno {
if err == nil {
return syscall.Errno(0)
}
err2, ok := err.(*Error)
if !ok {
log.Panicf("cannot unpack err=%#v", err)
}
err3, ok := err2.Err.(syscall.Errno)
if !ok {
log.Panicf("cannot unpack err2=%#v", err2)
}
return err3
}
// wrappers to adapt "F" variants to the test
func wrapFGet(path, name string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return FGet(f, name)
}
func wrapFSet(path, name string, data []byte) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return FSet(f, name, data)
}
func wrapFSetWithFlags(path, name string, data []byte, flags int) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return FSetWithFlags(f, name, data, flags)
}
func wrapFRemove(path, name string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return FRemove(f, name)
}
func wrapFList(path string) ([]string, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return FList(f)
}