Skip to content

Commit

Permalink
add test for apply bounding or ambient caps for other process
Browse files Browse the repository at this point in the history
Signed-off-by: lifubang <lifubang@acmcoder.com>
  • Loading branch information
lifubang committed Oct 15, 2024
1 parent 9638323 commit f298c15
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions capability/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package capability_test
package capability

import (
"errors"
"os/exec"
"runtime"
"testing"

. "github.com/moby/sys/capability"
)

// Based on the fact Go 1.18+ supports Linux >= 2.6.32, and
Expand Down Expand Up @@ -151,3 +151,40 @@ func TestAmbientCapSet(t *testing.T) {
}
}
}

func TestApplyCapsForOtherProcess(t *testing.T) {
if runtime.GOOS != "linux" {
return
}
requirePCapSet(t)

cmd := exec.Command("sleep", "sleep", "infinity")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
defer func() {
_ = cmd.Process.Kill()
_, _ = cmd.Process.Wait()
}()

pid, err := NewPid(cmd.Process.Pid)
if err != nil {
t.Fatal(err)
}

if err = pid.Load(); err != nil {
t.Fatal(err)
}
err = pid.Apply(BOUNDING)
if !errors.Is(err, errBoundingNotMine) {
t.Fatalf("expected not support error when drop bounding caps for other process, but got: %v", err)
}
err = pid.Apply(CAPS)
if err != nil {
t.Fatal(err)
}
err = pid.Apply(AMBIENT)
if !errors.Is(err, errAmbientNotMine) {
t.Fatalf("expected not support error when rasing ambient caps for other process, but got: %v", err)
}
}

0 comments on commit f298c15

Please sign in to comment.