@@ -28,6 +28,16 @@ import (
2828 "golang.org/x/sys/unix"
2929)
3030
31+ // specialXattrs come from the OCI xattr remapping logic in oci/layer/xattr.go.
32+ // These are xattrs that will be auto-set by the system and we should ignore
33+ // their existence when doing xattr-related operations (for our tests, this
34+ // especially means to take care of them when checking sets of xattrs that
35+ // exist on the filesystem).
36+ var specialXattrs = map [string ]struct {}{
37+ "security.selinux" : {},
38+ "system.nfs4_acl" : {},
39+ }
40+
3141func TestClearxattrFilter (t * testing.T ) {
3242 dir := t .TempDir ()
3343
@@ -38,6 +48,12 @@ func TestClearxattrFilter(t *testing.T) {
3848 path := file .Name ()
3949 defer os .RemoveAll (path ) //nolint:errcheck
4050
51+ autosetXattrs , err := Llistxattr (path )
52+ require .NoErrorf (t , err , "llistxattr %q" , path )
53+ for _ , xattr := range autosetXattrs {
54+ require .Contains (t , specialXattrs , xattr , "auto-set xattrs must be part of special list" )
55+ }
56+
4157 xattrs := []struct {
4258 name , value string
4359 forbidden bool
@@ -65,11 +81,16 @@ func TestClearxattrFilter(t *testing.T) {
6581 }
6682 require .NoErrorf (t , err , "lsetxattr %q=%q on %q" , xattr .name , xattr .value , path )
6783 }
84+ // If we are running on an SELinux-enabled system, all new files get a
85+ // security.selinux xattr that gets auto-set and cannot be removed so we
86+ // need to include it in the expected set.
87+ expectAllXattrNames := append (setXattrNames , autosetXattrs ... )
88+ expectRemainingXattrNames := append (forbiddenXattrNames , autosetXattrs ... )
6889
6990 // Check they're all present.
7091 allXattrList , err := Llistxattr (path )
7192 require .NoErrorf (t , err , "llistxattr %q" , path )
72- assert .ElementsMatch (t , setXattrNames , allXattrList , "all xattrs should be present after setting" )
93+ assert .ElementsMatch (t , expectAllXattrNames , allXattrList , "all xattrs should be present after setting" )
7394
7495 // Now clear them.
7596 err = Lclearxattrs (path , func (xattrName string ) bool {
@@ -79,9 +100,9 @@ func TestClearxattrFilter(t *testing.T) {
79100 require .NoErrorf (t , err , "lclearxattrs %q (forbidden=%v)" , path , forbiddenXattrs )
80101
81102 // Check that only the forbidden ones remain.
82- forbiddenXattrList , err := Llistxattr (path )
103+ remainingXattrList , err := Llistxattr (path )
83104 require .NoErrorf (t , err , "llistxattr %q" , path )
84- assert .NotElementsMatch (t , setXattrNames , forbiddenXattrList , "there should be a different set of xattrs after clearing" )
85- assert .ElementsMatch (t , forbiddenXattrNames , forbiddenXattrList , "only explicitly forbidden xattrs should be allowed to remain after clearing" )
86- assert .NotEmpty (t , forbiddenXattrList , "there should be some remaining xattrs after clearing" )
105+ assert .NotElementsMatch (t , expectAllXattrNames , remainingXattrList , "there should be a different set of xattrs after clearing" )
106+ assert .ElementsMatch (t , expectRemainingXattrNames , remainingXattrList , "only explicitly forbidden xattrs should be allowed to remain after clearing" )
107+ assert .NotEmpty (t , remainingXattrList , "there should be some remaining xattrs after clearing" )
87108}
0 commit comments