2020-07-15 04:34:07 +08:00
|
|
|
package copier
|
|
|
|
|
|
|
|
import (
|
2022-08-18 02:44:08 +08:00
|
|
|
"errors"
|
2020-07-15 04:34:07 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-04-27 06:02:35 +08:00
|
|
|
func init() {
|
|
|
|
// exercise the ERANGE-handling logic
|
|
|
|
initialXattrListSize = 1
|
|
|
|
initialXattrValueSize = 1
|
|
|
|
}
|
|
|
|
|
2020-07-15 04:34:07 +08:00
|
|
|
func TestXattrs(t *testing.T) {
|
2025-02-04 13:36:03 +08:00
|
|
|
t.Parallel()
|
2020-07-15 04:34:07 +08:00
|
|
|
if !xattrsSupported {
|
|
|
|
t.Skipf("xattrs are not supported on this platform, skipping")
|
|
|
|
}
|
|
|
|
testValues := map[string]string{
|
|
|
|
"user.a": "attribute value a",
|
|
|
|
"user.b": "attribute value b",
|
|
|
|
}
|
2022-08-20 15:02:40 +08:00
|
|
|
tmp := t.TempDir()
|
2020-07-15 04:34:07 +08:00
|
|
|
for attribute, value := range testValues {
|
|
|
|
t.Run(fmt.Sprintf("attribute=%s", attribute), func(t *testing.T) {
|
2022-11-15 00:22:45 +08:00
|
|
|
f, err := os.CreateTemp(tmp, "copier-xattr-test-")
|
2020-07-15 04:34:07 +08:00
|
|
|
if !assert.Nil(t, err, "error creating test file: %v", err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
|
|
|
|
err = Lsetxattrs(f.Name(), map[string]string{attribute: value})
|
2022-08-18 02:44:08 +08:00
|
|
|
if errors.Is(err, syscall.ENOTSUP) {
|
2022-05-10 16:34:53 +08:00
|
|
|
t.Skipf("extended attributes not supported on %q, skipping", tmp)
|
2020-07-15 04:34:07 +08:00
|
|
|
}
|
|
|
|
if !assert.Nil(t, err, "error setting attribute on file: %v", err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
xattrs, err := Lgetxattrs(f.Name())
|
|
|
|
if !assert.Nil(t, err, "error reading attributes of file: %v", err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
xvalue, ok := xattrs[attribute]
|
|
|
|
if !assert.True(t, ok, "did not read back attribute %q for file", attribute) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if !assert.Equal(t, value, xvalue, "read back different value for attribute %q", attribute) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|