@@ -9,6 +9,7 @@ package cmd
9
9
import (
10
10
"os"
11
11
"path/filepath"
12
+ "syscall"
12
13
"testing"
13
14
14
15
"github.com/stretchr/testify/require"
@@ -26,12 +27,8 @@ func Test_chownPaths(t *testing.T) {
26
27
defer os .RemoveAll (secondParentDir )
27
28
28
29
childDir := filepath .Join (secondParentDir , "child" )
29
- err = os .MkdirAll (childDir , 0o777 )
30
- require .NoError (t , err )
31
30
32
31
childChildDir := filepath .Join (childDir , "child" )
33
- err = os .MkdirAll (childDir , 0o777 )
34
- require .NoError (t , err )
35
32
36
33
pathsToChown := distinctPaths {}
37
34
pathsToChown .addPath (childDir )
@@ -45,24 +42,80 @@ func Test_chownPaths(t *testing.T) {
45
42
require .NoError (t , err )
46
43
}
47
44
45
+ func Test_updateFileCapsFromBoundingSet (t * testing.T ) {
46
+ if os .Geteuid () == 0 {
47
+ t .Skip ("this test requires non-root user" )
48
+ return
49
+ }
50
+
51
+ tmpDir , err := os .MkdirTemp ("" , "test_chown" )
52
+ require .NoError (t , err )
53
+ defer os .RemoveAll (tmpDir )
54
+
55
+ executable := filepath .Join (tmpDir , "test_exec" )
56
+
57
+ err = os .WriteFile (executable , []byte {}, 0o7777 )
58
+ require .NoError (t , err )
59
+
60
+ updated , err := updateFileCapsFromBoundingSet (executable )
61
+ require .ErrorIs (t , err , syscall .EPERM )
62
+ require .False (t , updated )
63
+ }
64
+
48
65
func Test_getMissingBoundingCapsText (t * testing.T ) {
49
66
tc := []struct {
50
- name string
51
- fileCaps []cap.Value
52
- boundCaps []cap.Value
53
- capsText string
67
+ name string
68
+ fileCaps []cap.Value
69
+ fileCapsErr error
70
+ boundCaps []cap.Value
71
+ boundCapsErr error
72
+ capsText string
73
+ expectedErr error
54
74
}{
55
75
{
56
- name : "no missing caps" ,
57
- fileCaps : []cap.Value {cap .CHOWN , cap .SETPCAP },
58
- boundCaps : []cap.Value {cap .CHOWN , cap .SETPCAP },
59
- capsText : "" ,
76
+ name : "no missing caps" ,
77
+ fileCaps : []cap.Value {cap .CHOWN , cap .SETPCAP },
78
+ fileCapsErr : nil ,
79
+ boundCaps : []cap.Value {cap .CHOWN , cap .SETPCAP },
80
+ boundCapsErr : nil ,
81
+ capsText : "" ,
82
+ expectedErr : nil ,
83
+ },
84
+ {
85
+ name : "missing caps" ,
86
+ fileCaps : []cap.Value {cap .CHOWN , cap .SETPCAP },
87
+ fileCapsErr : nil ,
88
+ boundCaps : []cap.Value {cap .CHOWN , cap .SETPCAP , cap .DAC_OVERRIDE },
89
+ boundCapsErr : nil ,
90
+ capsText : "cap_chown,cap_dac_override,cap_setpcap=eip" ,
91
+ expectedErr : nil ,
92
+ },
93
+ {
94
+ name : "no data err" ,
95
+ fileCaps : nil ,
96
+ fileCapsErr : syscall .ENODATA ,
97
+ boundCaps : []cap.Value {cap .CHOWN , cap .SETPCAP , cap .DAC_OVERRIDE },
98
+ boundCapsErr : nil ,
99
+ capsText : "cap_chown,cap_dac_override,cap_setpcap=eip" ,
100
+ expectedErr : nil ,
60
101
},
61
102
{
62
- name : "missing caps" ,
63
- fileCaps : []cap.Value {cap .CHOWN , cap .SETPCAP },
64
- boundCaps : []cap.Value {cap .CHOWN , cap .SETPCAP , cap .DAC_OVERRIDE },
65
- capsText : "cap_chown,cap_dac_override,cap_setpcap=eip" ,
103
+ name : "file caps permission err" ,
104
+ fileCaps : nil ,
105
+ fileCapsErr : syscall .EPERM ,
106
+ boundCaps : []cap.Value {cap .CHOWN , cap .SETPCAP , cap .DAC_OVERRIDE },
107
+ boundCapsErr : nil ,
108
+ capsText : "" ,
109
+ expectedErr : syscall .EPERM ,
110
+ },
111
+ {
112
+ name : "bound caps permission err" ,
113
+ fileCaps : nil ,
114
+ fileCapsErr : nil ,
115
+ boundCaps : []cap.Value {cap .CHOWN , cap .SETPCAP , cap .DAC_OVERRIDE },
116
+ boundCapsErr : syscall .EPERM ,
117
+ capsText : "" ,
118
+ expectedErr : syscall .EPERM ,
66
119
},
67
120
}
68
121
@@ -74,6 +127,10 @@ func Test_getMissingBoundingCapsText(t *testing.T) {
74
127
for _ , tt := range tc {
75
128
t .Run (tt .name , func (t * testing.T ) {
76
129
capBound = func (val cap.Value ) (bool , error ) {
130
+ if tt .boundCapsErr != nil {
131
+ return false , tt .boundCapsErr
132
+ }
133
+
77
134
for _ , boundCap := range tt .boundCaps {
78
135
if boundCap == val {
79
136
return true , nil
@@ -82,17 +139,23 @@ func Test_getMissingBoundingCapsText(t *testing.T) {
82
139
return false , nil
83
140
}
84
141
capGetFile = func (path string ) (* cap.Set , error ) {
85
- set := cap .NewSet ()
142
+ if tt .fileCapsErr != nil {
143
+ return nil , tt .fileCapsErr
144
+ }
86
145
146
+ set := cap .NewSet ()
87
147
if err := set .SetFlag (cap .Effective , true , tt .fileCaps ... ); err != nil {
88
148
return nil , err
89
149
}
90
-
91
150
return set , nil
92
151
}
93
152
94
153
capsText , err := getMissingBoundingCapsText ("non_existent" )
95
- require .NoError (t , err )
154
+ if tt .expectedErr != nil {
155
+ require .ErrorIs (t , err , tt .expectedErr )
156
+ } else {
157
+ require .NoError (t , err )
158
+ }
96
159
require .Equal (t , tt .capsText , capsText )
97
160
})
98
161
}
0 commit comments