2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-10 23:31:09 +00:00
restic/vendor/github.com/pkg/xattr/xattr_test.go

96 lines
1.6 KiB
Go
Raw Normal View History

2017-07-23 12:24:45 +00:00
// +build linux darwin freebsd
package xattr
import (
"io/ioutil"
"os"
"testing"
)
const UserPrefix = "user."
func Test(t *testing.T) {
tmp, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())
// Check if filesystem supports extended attributes
if !Supported(tmp.Name()) {
t.Skip("Skipping test - filesystem does not support extended attributes")
}
2017-07-23 12:24:45 +00:00
err = Set(tmp.Name(), UserPrefix+"test", []byte("test-attr-value"))
if err != nil {
t.Fatal(err)
}
list, err := List(tmp.Name())
if err != nil {
t.Fatal(err)
}
found := false
for _, name := range list {
if name == UserPrefix+"test" {
found = true
}
}
if !found {
t.Fatal("Listxattr did not return test attribute")
}
var data []byte
data, err = Get(tmp.Name(), UserPrefix+"test")
if err != nil {
t.Fatal(err)
}
value := string(data)
t.Log(value)
if "test-attr-value" != value {
t.Fail()
}
err = Remove(tmp.Name(), UserPrefix+"test")
if err != nil {
t.Fatal(err)
}
}
func TestNoData(t *testing.T) {
tmp, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmp.Name())
// Check if filesystem supports extended attributes
if !Supported(tmp.Name()) {
t.Skip("Skipping test - filesystem does not support extended attributes")
}
2017-07-23 12:24:45 +00:00
err = Set(tmp.Name(), UserPrefix+"test", []byte{})
if err != nil {
t.Fatal(err)
}
list, err := List(tmp.Name())
if err != nil {
t.Fatal(err)
}
found := false
for _, name := range list {
if name == UserPrefix+"test" {
found = true
}
}
if !found {
t.Fatal("Listxattr did not return test attribute")
}
}