Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions capability/capability_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,15 @@ func (c *capsV3) Load() (err error) {
}
break
}
if strings.HasPrefix(line, "CapB") {
_, err = fmt.Sscanf(line[4:], "nd: %08x%08x", &c.bounds[1], &c.bounds[0])
if val, ok := strings.CutPrefix(line, "CapBnd:\t"); ok {
_, err = fmt.Sscanf(val, "%08x%08x", &c.bounds[1], &c.bounds[0])
if err != nil {
break
}
continue
}
if strings.HasPrefix(line, "CapA") {
_, err = fmt.Sscanf(line[4:], "mb: %08x%08x", &c.ambient[1], &c.ambient[0])
if val, ok := strings.CutPrefix(line, "CapAmb:\t"); ok {
_, err = fmt.Sscanf(val, "%08x%08x", &c.ambient[1], &c.ambient[0])
if err != nil {
break
}
Expand Down
59 changes: 41 additions & 18 deletions capability/capability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package capability
package capability_test

import (
"runtime"
"testing"

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

// Based on the fact Go 1.18+ supports Linux >= 2.6.32, and
Expand All @@ -24,47 +26,68 @@ const (
func TestLastCap(t *testing.T) {
last, err := LastCap()
switch runtime.GOOS {
case "linux":
if err != nil {
t.Fatal(err)
}
default:
if err == nil {
t.Fatal(runtime.GOOS, ": want error, got nil")
}
return
case "linux":
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it that you can have default as the first case in switch; it improves readability in here (otherwise I would have to add a comment saying "Below is linux-only stuff").

}

// Sanity checks (Linux only).
if err != nil {
t.Fatalf("LastCap: want nil, got error: %v", err)
}
// Sanity checks.
if last < minLastCap {
t.Fatalf("LastCap returned %d (%s), expected >= %d (%s)",
t.Errorf("LastCap: want >= %d (%s), got %d (%s)",
last, last, minLastCap, minLastCap)
}
if last > maxLastCap {
t.Fatalf("LastCap returned %d, expected <= %d (%s). Package needs to be updated.",
last, maxLastCap, maxLastCap)
t.Errorf("LastCap: want <= %d (%s), got %d (%s). Package needs to be updated.",
last, last, maxLastCap, maxLastCap)
}
}

func TestListSupported(t *testing.T) {
list, err := ListSupported()
switch runtime.GOOS {
case "linux":
if err != nil {
t.Fatal(err)
}
default:
if err == nil {
t.Fatal(runtime.GOOS, ": want error, got nil")
}
}
if runtime.GOOS != "linux" {
return
case "linux":
}
if err != nil {
t.Fatalf("ListSupported: want nil, got error: %v", err)
}
// Sanity check (Linux only).
t.Logf("got +%v (len %d)", list, len(list))
minLen := int(minLastCap) + 1
if len(list) < minLen {
t.Fatalf("result is too short (got %d, want %d): +%v", len(list), minLen, list)
t.Errorf("ListSupported: too short (want %d, got %d): +%v", minLen, len(list), list)
}
}

func TestNewPid2Load(t *testing.T) {
c, err := NewPid2(0)
switch runtime.GOOS {
default:
if err == nil {
t.Fatal(runtime.GOOS, ": want error, got nil")
}
return
case "linux":
}
if err != nil {
t.Fatalf("NewPid2: want nil, got error: %v", err)
}
err = c.Load()
if err != nil {
t.Fatalf("Load: %v", err)
}
// Assuming that at least bounding set is not empty.
bset := c.StringCap(BOUNDING)
t.Logf("Bounding set: %s", bset)
if len(bset) == 0 {
t.Fatal("loaded bounding set: want non-empty, got empty")
}
}