2018-11-11 15:48:39 +01:00
|
|
|
//+build linux
|
|
|
|
|
|
|
|
// Darwin does not support Fgetxattr and friends!
|
|
|
|
|
|
|
|
package xattr_tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/tests/test_helpers"
|
2018-11-11 15:48:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFdXattr(t *testing.T) {
|
|
|
|
attr := "user.foo"
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/TestFdXattr"
|
|
|
|
err := ioutil.WriteFile(fn, nil, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating empty file failed: %v", err)
|
|
|
|
}
|
|
|
|
fd, err := syscall.Open(fn, syscall.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer syscall.Close(fd)
|
|
|
|
buf := make([]byte, 1000)
|
|
|
|
sz, err := unix.Flistxattr(fd, buf)
|
2021-08-18 15:47:17 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if sz != 0 {
|
2018-11-11 15:48:39 +01:00
|
|
|
t.Errorf("expected zero size, got %d", sz)
|
|
|
|
}
|
|
|
|
val1 := []byte("123456789")
|
|
|
|
unix.Fsetxattr(fd, attr, val1, 0)
|
|
|
|
sz, err = unix.Flistxattr(fd, buf)
|
2018-11-11 17:43:48 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-11 15:48:39 +01:00
|
|
|
// Length of "user.attr" + terminating null byte
|
|
|
|
expectedSz := len(attr) + 1
|
|
|
|
if sz != expectedSz {
|
|
|
|
t.Errorf("expected size %d, got %d", expectedSz, sz)
|
|
|
|
}
|
2020-07-12 13:40:21 +02:00
|
|
|
str := ""
|
|
|
|
if sz > 0 {
|
|
|
|
str = string(buf[:sz-1])
|
|
|
|
}
|
2018-11-11 15:48:39 +01:00
|
|
|
if str != attr {
|
|
|
|
t.Errorf("expected name %q, got %q", attr, str)
|
|
|
|
}
|
|
|
|
// Check content
|
|
|
|
sz, err = unix.Fgetxattr(fd, attr, buf)
|
2018-11-11 17:43:48 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-11-11 15:48:39 +01:00
|
|
|
str = string(buf[:sz])
|
|
|
|
if str != string(val1) {
|
|
|
|
t.Errorf("expected val %q, got %q", val1, str)
|
|
|
|
}
|
|
|
|
// Delete value
|
|
|
|
err = unix.Fremovexattr(fd, attr)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
sz, err = unix.Flistxattr(fd, buf)
|
2021-08-18 15:47:17 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if sz != 0 {
|
2018-11-11 15:48:39 +01:00
|
|
|
t.Errorf("expected zero size, got %d", sz)
|
|
|
|
}
|
|
|
|
}
|