@@ -37,38 +37,37 @@ func init() {
3737 }
3838}
3939
40- // RunWithToken sets up an environment with a BPFFS with the provided delegated permissions.
41- //
42- // The calling test is effectively forked, this function returns true in the parent process, and false in the child
43- // process. Only the child process with have access to the token, so the parent process should do an early return.
40+ // RunWithToken runs the provided `fn` in an environment where a BFPFS is setup to provide tokens with the specified
41+ // delegated permissions. The `name` parameter is used to name the subtest that will run `fn`, and the `delegated`
42+ // parameter specifies the permissions that will be delegated to the BPF tokens.
4443//
4544// Example usage:
4645//
47- // if testutils.RunWithToken(t, testutils.Delegated{
46+ // if testutils.RunWithToken(t, "happy-path-example", testutils.Delegated{
4847// Cmds: []sys.Cmd{sys.BPF_MAP_CREATE},
4948// Maps: []sys.MapType{sys.BPF_MAP_TYPE_HASH},
50- // }) {
51- // return
52- // }
53- // _, err := newMap(t, hashMapSpec, nil)
54- // qt.Assert(t, qt.IsNil(err))
55- func RunWithToken (tb testing.TB , delegated Delegated ) bool {
56- tb .Helper ()
49+ // }, func(t *testing.T) {
50+ // _, err := newMap(t, hashMapSpec, nil)
51+ // qt.Assert(t, qt.IsNil(err))
52+ // })
53+ func RunWithToken (t * testing.T , name string , delegated Delegated , fn func (t * testing.T )) {
54+ t .Helper ()
5755
5856 if ! platform .IsLinux {
59- tb .Skip ("BPF tokens only work on Linux" )
57+ t .Skip ("BPF tokens only work on Linux" )
6058 }
6159
62- SkipOnOldKernel (tb , "6.9" , "BPF_TOKEN_CREATE" )
60+ SkipOnOldKernel (t , "6.9" , "BPF_TOKEN_CREATE" )
6361
64- // Detect when we are running the the child process and bail out .
62+ // Detect when we are running the the child process and call fn .
6563 if _ , ok := os .LookupEnv (TOKEN_SUBTEST ); ok {
66- return false
64+ t .Run (name , fn )
65+ return
6766 }
6867
6968 // Run just the current test in a subprocess
7069 args := []string {
71- "-test.run=^" + tb .Name () + "$" ,
70+ "-test.run=^" + t .Name () + "/" + name + "$" ,
7271 }
7372
7473 // If we are running in verbose mode, pass the flag to the subtest as well.
@@ -80,10 +79,10 @@ func RunWithToken(tb testing.TB, delegated Delegated) bool {
8079 // started with a relative path or just the name of the binary, and we want an absolute path to re-exec ourselves.
8180 exe , err := os .Executable ()
8281 if err != nil {
83- tb .Fatal (err )
82+ t .Fatal (err )
8483 }
8584
86- cmd := exec .CommandContext (tb .Context (), exe , args ... )
85+ cmd := exec .CommandContext (t .Context (), exe , args ... )
8786
8887 // Pass an environment variable to the subtest to indicate that it should run, and not skip.
8988 cmd .Env = append (cmd .Environ (), TOKEN_SUBTEST + "=1" )
@@ -95,7 +94,7 @@ func RunWithToken(tb testing.TB, delegated Delegated) bool {
9594 // Create a socket pair for communication between the parent and child process.
9695 pair , err := unix .Socketpair (unix .AF_UNIX , unix .SOCK_STREAM , 0 )
9796 if err != nil {
98- tb .Fatal (err )
97+ t .Fatal (err )
9998 }
10099 parent , child := pair [0 ], pair [1 ]
101100 defer unix .Close (parent )
@@ -104,7 +103,7 @@ func RunWithToken(tb testing.TB, delegated Delegated) bool {
104103 // So find out all UIDs and GIDs of users on the system and map them into the user namespace of the child process.
105104 uidMappings , gidMappings , err := parseUIDsGIDs ()
106105 if err != nil {
107- tb .Fatal (err )
106+ t .Fatal (err )
108107 }
109108
110109 // Let the child process inherit the child's end of the socket pair.
@@ -125,35 +124,33 @@ func RunWithToken(tb testing.TB, delegated Delegated) bool {
125124 err = cmd .Start ()
126125 if err != nil {
127126 unix .Close (child )
128- tb .Fatal (err )
127+ t .Fatal (err )
129128 }
130129 unix .Close (child )
131130
132131 // Receive the bpffs context fd from the child process.
133132 fds , err := recvFDs (1 , parent )
134133 if err != nil {
135- tb .Fatal (err )
134+ t .Fatal (err )
136135 }
137136 bpffs := fds [0 ]
138137
139138 err = configureDelegated (bpffs , delegated )
140139 if err != nil {
141- tb .Fatal (err )
140+ t .Fatal (err )
142141 }
143142
144143 // Send back a message, we don't care about the content, just a signal to tell the child process that the bpffs is
145144 // configured and it can proceed with the test.
146145 err = unix .Sendmsg (parent , []byte ("done" ), nil , nil , 0 )
147146 if err != nil {
148- tb .Fatal (err )
147+ t .Fatal (err )
149148 }
150149
151150 err = cmd .Wait ()
152151 if err != nil {
153- tb .Fatal (err )
152+ t .Fatal (err )
154153 }
155-
156- return true
157154}
158155
159156func parseUIDsGIDs () (uids , gids []syscall.SysProcIDMap , err error ) {
0 commit comments