2022-04-04 02:29:36 +00:00
|
|
|
//go:build linux
|
2016-09-01 19:52:24 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package mtree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/vbatts/go-mtree/xattr"
|
|
|
|
)
|
|
|
|
|
2022-04-09 01:59:08 +00:00
|
|
|
//gocyclo:ignore
|
2016-09-01 19:52:24 +00:00
|
|
|
func TestXattr(t *testing.T) {
|
2017-04-13 18:35:01 +00:00
|
|
|
testDir, present := os.LookupEnv("MTREE_TESTDIR")
|
|
|
|
if present == false {
|
2022-04-04 02:29:36 +00:00
|
|
|
// a bit dirty to create/destroy a directory in cwd,
|
2017-04-13 18:35:01 +00:00
|
|
|
// but often /tmp is mounted tmpfs and doesn't support
|
|
|
|
// xattrs
|
|
|
|
testDir = "."
|
|
|
|
}
|
|
|
|
dir, err := ioutil.TempDir(testDir, "test.xattrs.")
|
2016-09-01 19:52:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
fh, err := os.Create(filepath.Join(dir, "file"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fh.WriteString("howdy")
|
|
|
|
fh.Sync()
|
|
|
|
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 {
|
2022-04-09 02:47:26 +00:00
|
|
|
t.Skipf("skipping: %q does not support xattrs", dir)
|
2016-09-01 19:52:24 +00:00
|
|
|
}
|
|
|
|
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
|
2017-06-15 18:54:58 +00:00
|
|
|
kvs, err := xattrKeywordFunc(dir, dirstat, nil)
|
2016-09-01 19:52:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-06-15 18:54:58 +00:00
|
|
|
if len(kvs) == 0 {
|
|
|
|
t.Errorf("expected a keyval; got none")
|
2016-09-01 19:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filestat, err := fh.Stat()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Check the regular file
|
2017-06-15 18:54:58 +00:00
|
|
|
kvs, err = xattrKeywordFunc(filepath.Join(dir, "file"), filestat, fh)
|
2016-09-01 19:52:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-06-15 18:54:58 +00:00
|
|
|
if len(kvs) == 0 {
|
|
|
|
t.Errorf("expected a keyval; got none")
|
2016-09-01 19:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|