2014-09-30 05:13:36 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package xattr_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/xattr"
|
2014-09-30 05:13:36 +08:00
|
|
|
)
|
|
|
|
|
2015-11-29 11:54:42 +08:00
|
|
|
func TestXattr(t *testing.T) {
|
2014-09-30 05:13:36 +08:00
|
|
|
tmp := "xattr_test"
|
2015-11-29 11:54:42 +08:00
|
|
|
out, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE, 0)
|
2014-09-30 05:13:36 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed")
|
|
|
|
}
|
2015-11-29 11:54:42 +08:00
|
|
|
defer os.Remove(tmp)
|
2014-09-30 05:13:36 +08:00
|
|
|
attr := "user.test"
|
|
|
|
out.Close()
|
|
|
|
|
|
|
|
if !xattr.XattrEnabled(tmp) {
|
|
|
|
t.Log("Disabled")
|
|
|
|
t.Fatal("failed")
|
|
|
|
}
|
|
|
|
t.Log("Success")
|
|
|
|
|
|
|
|
err = xattr.Setxattr(tmp, attr, "test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var value string
|
|
|
|
value, err = xattr.Getxattr(tmp, attr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed")
|
|
|
|
}
|
|
|
|
if value != "test" {
|
|
|
|
t.Fatal("failed")
|
|
|
|
}
|
|
|
|
t.Log("Success")
|
|
|
|
|
|
|
|
var names []string
|
|
|
|
names, err = xattr.Listxattr(tmp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
var found int
|
|
|
|
for _, name := range names {
|
|
|
|
if name == attr {
|
|
|
|
found = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Listxattr doesn't return trusted.* and system.* namespace
|
|
|
|
// attrs when run in unprevileged mode.
|
|
|
|
if found != 1 {
|
|
|
|
t.Fatal("failed")
|
|
|
|
}
|
|
|
|
t.Log("Success")
|
|
|
|
|
|
|
|
big := "0000000000000000000000000000000000000000000000000000000000000000000008c6419ad822dfe29283fb3ac98dcc5908810cb31f4cfe690040c42c144b7492eicompslf20dxmlpgz"
|
|
|
|
// Test for long xattrs larger than 128 bytes
|
|
|
|
err = xattr.Setxattr(tmp, attr, big)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed to add long value")
|
|
|
|
}
|
|
|
|
value, err = xattr.Getxattr(tmp, attr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("failed to get long value")
|
|
|
|
}
|
|
|
|
t.Log("Success")
|
|
|
|
|
|
|
|
if value != big {
|
|
|
|
t.Fatal("failed, value doesn't match")
|
|
|
|
}
|
|
|
|
t.Log("Success")
|
|
|
|
}
|