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
|
package defaults
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"syscall"
"testing"
xattr "github.com/rfjakob/pkg-xattr"
"github.com/rfjakob/gocryptfs/tests/test_helpers"
)
// On modern Linux distributions, /tmp may be on tmpfs,
// which does not support user xattrs. Try /var/tmp instead.
var alternateTestParentDir = "/var/tmp/gocryptfs-xattr-test-parent"
func TestMain(m *testing.M) {
if !xattrSupported(test_helpers.TmpDir) {
test_helpers.SwitchTestParentDir(alternateTestParentDir)
}
if !xattrSupported(test_helpers.TmpDir) {
fmt.Printf("xattrs not supported on %q", test_helpers.TmpDir)
os.Exit(1)
}
test_helpers.ResetTmpDir(true)
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, "-zerokey")
r := m.Run()
test_helpers.UnmountPanic(test_helpers.DefaultPlainDir)
os.RemoveAll(test_helpers.TmpDir)
os.Exit(r)
}
func TestXattrSetGetRm(t *testing.T) {
attr := "user.foo"
fn := test_helpers.DefaultPlainDir + "/TestXattrSetGetRm"
err := ioutil.WriteFile(fn, nil, 0700)
if err != nil {
t.Fatalf("creating empty file failed: %v", err)
}
// Set
val1 := []byte("123456789")
err = xattr.Set(fn, attr, val1)
if err != nil {
t.Fatal(err)
}
// Read back
val2, err := xattr.Get(fn, attr)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val1, val2) {
t.Fatalf("wrong readback value: %v != %v", val1, val2)
}
// Remove
err = xattr.Remove(fn, attr)
if err != nil {
t.Fatal(err)
}
// Read back
val3, err := xattr.Get(fn, attr)
if err == nil {
t.Fatalf("attr is still there after deletion!? val3=%v", val3)
}
}
func TestXattrSetEmpty(t *testing.T) {
attr := "user.foo"
fn := test_helpers.DefaultPlainDir + "/TestXattrSetEmpty1"
err := ioutil.WriteFile(fn, nil, 0700)
if err != nil {
t.Fatalf("creating empty file failed: %v", err)
}
// Make sure it does not exist already
_, err = xattr.Get(fn, attr)
if err == nil {
t.Fatal("we should have got an error here")
}
// Set empty value
err = xattr.Set(fn, attr, nil)
if err != nil {
t.Fatal(err)
}
// Read back
val, err := xattr.Get(fn, attr)
if err != nil {
t.Fatal(err)
}
if len(val) != 0 {
t.Errorf("wrong length: want=0 have=%d", len(val))
}
// Overwrite empty value with something
val1 := []byte("xyz123")
err = xattr.Set(fn, attr, val1)
if err != nil {
t.Fatal(err)
}
// Read back
val2, err := xattr.Get(fn, attr)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val1, val2) {
t.Fatalf("wrong readback value: %v != %v", val1, val2)
}
// Overwrite something with empty value
err = xattr.Set(fn, attr, nil)
if err != nil {
t.Fatal(err)
}
// Read back
val, err = xattr.Get(fn, attr)
if err != nil {
t.Fatal(err)
}
if len(val) != 0 {
t.Errorf("wrong length: want=0 have=%d", len(val2))
}
}
func TestXattrList(t *testing.T) {
fn := test_helpers.DefaultPlainDir + "/TestXattrList"
err := ioutil.WriteFile(fn, nil, 0700)
if err != nil {
t.Fatalf("creating empty file failed: %v", err)
}
val := []byte("xxxxxxxxyyyyyyyyyyyyyyyzzzzzzzzzzzzz")
num := 20
for i := 1; i <= num; i++ {
attr := fmt.Sprintf("user.TestXattrList.%02d", i)
err = xattr.Set(fn, attr, val)
if err != nil {
t.Fatal(err)
}
}
names, err := xattr.List(fn)
if err != nil {
t.Fatal(err)
}
if len(names) != num {
t.Errorf("wrong number of names, want=%d have=%d", num, len(names))
}
for _, n := range names {
if !strings.HasPrefix(n, "user.TestXattrList.") {
t.Errorf("unexpected attr name: %q", n)
}
}
}
func xattrSupported(path string) bool {
_, err := xattr.Get(path, "user.xattrSupported-dummy-value")
if err == nil {
return true
}
err2 := err.(*xattr.Error)
if err2.Err == syscall.EOPNOTSUPP {
return false
}
return true
}
|