Debian Codegraph @

capability/state_linux_test.go

raw symbols SCIP
1 // Copyright 2023 The Capability Authors.
2 // Copyright 2013 Suryandaru Triandana <[email protected]>
3 // All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 package capability
9
10 import "testing"
11
12 func TestState(t *testing.T) {
13 testEmpty := func(name string, c Capabilities, what CapType) {
14 for i := CapType(1); i <= BOUNDING; i <<= 1 {
15 if (i&what) != 0 && !c.Empty(i) {
16 t.Errorf(name+": capabilities set %q wasn't empty", i)
17 }
18 }
19 }
20 testFull := func(name string, c Capabilities, what CapType) {
21 for i := CapType(1); i <= BOUNDING; i <<= 1 {
22 if (i&what) != 0 && !c.Full(i) {
23 t.Errorf(name+": capabilities set %q wasn't full", i)
24 }
25 }
26 }
27 testPartial := func(name string, c Capabilities, what CapType) {
28 for i := CapType(1); i <= BOUNDING; i <<= 1 {
29 if (i&what) != 0 && (c.Empty(i) || c.Full(i)) {
30 t.Errorf(name+": capabilities set %q wasn't partial", i)
31 }
32 }
33 }
34 testGet := func(name string, c Capabilities, what CapType, max Cap) {
35 for i := CapType(1); i <= BOUNDING; i <<= 1 {
36 if (i & what) == 0 {
37 continue
38 }
39 for j := Cap(0); j <= max; j++ {
40 if !c.Get(i, j) {
41 t.Errorf(name+": capability %q wasn't found on %q", j, i)
42 }
43 }
44 }
45 }
46
47 last, err := LastCap()
48 if err != nil {
49 t.Fatal(err)
50 }
51 capf := new(capsFile)
52 capf.data.version = 2
53 for _, tc := range []struct {
54 name string
55 c Capabilities
56 sets CapType
57 max Cap
58 }{
59 {"v3", new(capsV3), EFFECTIVE | PERMITTED | BOUNDING, last},
60 {"file_v1", new(capsFile), EFFECTIVE | PERMITTED, CAP_AUDIT_CONTROL},
61 {"file_v2", capf, EFFECTIVE | PERMITTED, last},
62 } {
63 testEmpty(tc.name, tc.c, tc.sets)
64 tc.c.Fill(CAPS | BOUNDS)
65 testFull(tc.name, tc.c, tc.sets)
66 testGet(tc.name, tc.c, tc.sets, tc.max)
67 tc.c.Clear(CAPS | BOUNDS)
68 testEmpty(tc.name, tc.c, tc.sets)
69 for i := CapType(1); i <= BOUNDING; i <<= 1 {
70 for j := Cap(0); j <= last; j++ {
71 tc.c.Set(i, j)
72 }
73 }
74 testFull(tc.name, tc.c, tc.sets)
75 testGet(tc.name, tc.c, tc.sets, tc.max)
76 for i := CapType(1); i <= BOUNDING; i <<= 1 {
77 for j := Cap(0); j <= last; j++ {
78 tc.c.Unset(i, j)
79 }
80 }
81 testEmpty(tc.name, tc.c, tc.sets)
82 tc.c.Set(PERMITTED, CAP_CHOWN)
83 testPartial(tc.name, tc.c, PERMITTED)
84 tc.c.Clear(CAPS | BOUNDS)
85 testEmpty(tc.name, tc.c, tc.sets)
86 }
87 }