aboutsummaryrefslogtreecommitdiff
path: root/tests/xattr/xattr_fd_test.go
blob: f3586cf5f57cfe4bdbd4889fc3072e3e923f3526 (plain)
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
//go:build linux
// +build linux

// Darwin does not support Fgetxattr and friends!

package xattr_tests

import (
	"io/ioutil"
	"syscall"
	"testing"

	"golang.org/x/sys/unix"

	"github.com/rfjakob/gocryptfs/v2/tests/test_helpers"
)

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)
	if err != nil {
		t.Error(err)
	} else if sz != 0 {
		t.Errorf("expected zero size, got %d", sz)
	}
	val1 := []byte("123456789")
	unix.Fsetxattr(fd, attr, val1, 0)
	sz, err = unix.Flistxattr(fd, buf)
	if err != nil {
		t.Fatal(err)
	}
	// Length of "user.attr" + terminating null byte
	expectedSz := len(attr) + 1
	if sz != expectedSz {
		t.Errorf("expected size %d, got %d", expectedSz, sz)
	}
	str := ""
	if sz > 0 {
		str = string(buf[:sz-1])
	}
	if str != attr {
		t.Errorf("expected name %q, got %q", attr, str)
	}
	// Check content
	sz, err = unix.Fgetxattr(fd, attr, buf)
	if err != nil {
		t.Fatal(err)
	}
	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)
	if err != nil {
		t.Error(err)
	} else if sz != 0 {
		t.Errorf("expected zero size, got %d", sz)
	}
}