-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
keywords_linux_test.go
90 lines (81 loc) · 1.88 KB
/
keywords_linux_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
//go:build linux
// +build linux
package mtree
import (
"os"
"path/filepath"
"testing"
"github.com/vbatts/go-mtree/xattr"
)
//gocyclo:ignore
func TestXattr(t *testing.T) {
testDir, present := os.LookupEnv("MTREE_TESTDIR")
if present == false {
// a bit dirty to create/destroy a directory in cwd,
// but often /tmp is mounted tmpfs and doesn't support
// xattrs
testDir = "."
}
dir, err := os.MkdirTemp(testDir, "test.xattrs.")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
fh, err := os.Create(filepath.Join(dir, "file"))
if err != nil {
t.Fatal(err)
}
_, err = fh.WriteString("howdy")
if err != nil {
t.Errorf("failed to write string: %s", err)
}
err = fh.Sync()
if err != nil {
t.Errorf("failed to sync file: %s", err)
}
if _, err := fh.Seek(0, 0); err != nil {
t.Fatal(err)
}
if err := os.Symlink("./no/such/path", filepath.Join(dir, "symlink")); err != nil {
t.Fatal(err)
}
if err := xattr.Set(dir, "user.test", []byte("directory")); err != nil {
t.Skipf("skipping: %q does not support xattrs", dir)
}
if err := xattr.Set(filepath.Join(dir, "file"), "user.test", []byte("regular file")); err != nil {
t.Fatal(err)
}
dirstat, err := os.Lstat(dir)
if err != nil {
t.Fatal(err)
}
// Check the directory
kvs, err := xattrKeywordFunc(dir, dirstat, nil)
if err != nil {
t.Error(err)
}
if len(kvs) == 0 {
t.Errorf("expected a keyval; got none")
}
filestat, err := fh.Stat()
if err != nil {
t.Fatal(err)
}
// Check the regular file
kvs, err = xattrKeywordFunc(filepath.Join(dir, "file"), filestat, fh)
if err != nil {
t.Error(err)
}
if len(kvs) == 0 {
t.Errorf("expected a keyval; got none")
}
linkstat, err := os.Lstat(filepath.Join(dir, "symlink"))
if err != nil {
t.Fatal(err)
}
// Check a broken symlink
_, err = xattrKeywordFunc(filepath.Join(dir, "symlink"), linkstat, nil)
if err != nil {
t.Error(err)
}
}