Skip to content

Commit 7e225a6

Browse files
committed
Add tests for xattr error handling in exec_binfmt
Signed-off-by: Tiger Kaovilai <[email protected]>
1 parent 7426230 commit 7e225a6

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package ops
2+
3+
import (
4+
"syscall"
5+
"testing"
6+
7+
"github.com/pkg/errors"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestBinfmtXAttrErrorHandler(t *testing.T) {
12+
handler := createXAttrErrorHandler()
13+
14+
tests := []struct {
15+
name string
16+
dst string
17+
src string
18+
xattrKey string
19+
inputErr error
20+
expectErr bool
21+
description string
22+
}{
23+
{
24+
name: "ENOTSUP_security_selinux_ignored",
25+
dst: "/tmp/dest",
26+
src: "/tmp/src",
27+
xattrKey: "security.selinux",
28+
inputErr: syscall.ENOTSUP,
29+
expectErr: false,
30+
description: "ENOTSUP error for security.selinux should be ignored",
31+
},
32+
{
33+
name: "ENOTSUP_other_xattr_propagated",
34+
dst: "/tmp/dest",
35+
src: "/tmp/src",
36+
xattrKey: "user.some_attr",
37+
inputErr: syscall.ENOTSUP,
38+
expectErr: true,
39+
description: "ENOTSUP error for non-selinux xattr should propagate",
40+
},
41+
{
42+
name: "ENOTSUP_security_capability_propagated",
43+
dst: "/tmp/dest",
44+
src: "/tmp/src",
45+
xattrKey: "security.capability",
46+
inputErr: syscall.ENOTSUP,
47+
expectErr: true,
48+
description: "ENOTSUP error for other security xattr should propagate",
49+
},
50+
{
51+
name: "other_error_security_selinux_propagated",
52+
dst: "/tmp/dest",
53+
src: "/tmp/src",
54+
xattrKey: "security.selinux",
55+
inputErr: syscall.EPERM,
56+
expectErr: true,
57+
description: "Non-ENOTSUP errors should always propagate",
58+
},
59+
{
60+
name: "wrapped_ENOTSUP_error_handled",
61+
dst: "/tmp/dest",
62+
src: "/tmp/src",
63+
xattrKey: "security.selinux",
64+
inputErr: errors.Wrap(syscall.ENOTSUP, "wrapped error"),
65+
expectErr: false,
66+
description: "Wrapped ENOTSUP errors should be handled with errors.Is",
67+
},
68+
{
69+
name: "nil_error_returns_nil",
70+
dst: "/tmp/dest",
71+
src: "/tmp/src",
72+
xattrKey: "security.selinux",
73+
inputErr: nil,
74+
expectErr: false,
75+
description: "Nil error should return nil",
76+
},
77+
}
78+
79+
for _, tt := range tests {
80+
t.Run(tt.name, func(t *testing.T) {
81+
t.Parallel()
82+
83+
result := handler(tt.dst, tt.src, tt.xattrKey, tt.inputErr)
84+
85+
if tt.expectErr {
86+
require.Error(t, result, tt.description)
87+
require.Equal(t, tt.inputErr, result, "Error should be propagated unchanged")
88+
} else {
89+
require.NoError(t, result, tt.description)
90+
}
91+
})
92+
}
93+
}
94+
95+
func createXAttrErrorHandler() func(dst, src, xattrKey string, err error) error {
96+
return func(dst, src, xattrKey string, err error) error {
97+
if errors.Is(err, syscall.ENOTSUP) && xattrKey == "security.selinux" {
98+
return nil
99+
}
100+
return err
101+
}
102+
}

0 commit comments

Comments
 (0)