File: capability_test.go

package info (click to toggle)
golang-gocapability-dev 0.0%2Bgit20200815.42c35b4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, bookworm-proposed-updates, forky, sid, trixie
  • size: 108 kB
  • sloc: makefile: 3
file content (83 lines) | stat: -rw-r--r-- 2,363 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2013, Suryandaru Triandana <syndtr@gmail.com>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package capability

import "testing"

func TestState(t *testing.T) {
	testEmpty := func(name string, c Capabilities, whats CapType) {
		for i := CapType(1); i <= BOUNDING; i <<= 1 {
			if (i&whats) != 0 && !c.Empty(i) {
				t.Errorf(name+": capabilities set %q wasn't empty", i)
			}
		}
	}
	testFull := func(name string, c Capabilities, whats CapType) {
		for i := CapType(1); i <= BOUNDING; i <<= 1 {
			if (i&whats) != 0 && !c.Full(i) {
				t.Errorf(name+": capabilities set %q wasn't full", i)
			}
		}
	}
	testPartial := func(name string, c Capabilities, whats CapType) {
		for i := CapType(1); i <= BOUNDING; i <<= 1 {
			if (i&whats) != 0 && (c.Empty(i) || c.Full(i)) {
				t.Errorf(name+": capabilities set %q wasn't partial", i)
			}
		}
	}
	testGet := func(name string, c Capabilities, whats CapType, max Cap) {
		for i := CapType(1); i <= BOUNDING; i <<= 1 {
			if (i & whats) == 0 {
				continue
			}
			for j := Cap(0); j <= max; j++ {
				if !c.Get(i, j) {
					t.Errorf(name+": capability %q wasn't found on %q", j, i)
				}
			}
		}
	}

	capf := new(capsFile)
	capf.data.version = 2
	for _, tc := range []struct {
		name string
		c    Capabilities
		sets CapType
		max  Cap
	}{
		{"v1", new(capsV1), EFFECTIVE | PERMITTED, CAP_AUDIT_CONTROL},
		{"v3", new(capsV3), EFFECTIVE | PERMITTED | BOUNDING, CAP_LAST_CAP},
		{"file_v1", new(capsFile), EFFECTIVE | PERMITTED, CAP_AUDIT_CONTROL},
		{"file_v2", capf, EFFECTIVE | PERMITTED, CAP_LAST_CAP},
	} {
		testEmpty(tc.name, tc.c, tc.sets)
		tc.c.Fill(CAPS | BOUNDS)
		testFull(tc.name, tc.c, tc.sets)
		testGet(tc.name, tc.c, tc.sets, tc.max)
		tc.c.Clear(CAPS | BOUNDS)
		testEmpty(tc.name, tc.c, tc.sets)
		for i := CapType(1); i <= BOUNDING; i <<= 1 {
			for j := Cap(0); j <= CAP_LAST_CAP; j++ {
				tc.c.Set(i, j)
			}
		}
		testFull(tc.name, tc.c, tc.sets)
		testGet(tc.name, tc.c, tc.sets, tc.max)
		for i := CapType(1); i <= BOUNDING; i <<= 1 {
			for j := Cap(0); j <= CAP_LAST_CAP; j++ {
				tc.c.Unset(i, j)
			}
		}
		testEmpty(tc.name, tc.c, tc.sets)
		tc.c.Set(PERMITTED, CAP_CHOWN)
		testPartial(tc.name, tc.c, PERMITTED)
		tc.c.Clear(CAPS | BOUNDS)
		testEmpty(tc.name, tc.c, tc.sets)
	}
}