• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1package cap
2
3import (
4	"fmt"
5	"testing"
6)
7
8func TestAllMask(t *testing.T) {
9	oldMask := maxValues
10	oldWords := words
11	defer func() {
12		maxValues = oldMask
13		words = oldWords
14	}()
15
16	maxValues = 35
17	words = 3
18
19	vs := []struct {
20		val   Value
21		index uint
22		bit   uint32
23		mask  uint32
24	}{
25		{val: CHOWN, index: 0, bit: 0x1, mask: ^uint32(0)},
26		{val: 38, index: 1, bit: (1 << 6), mask: 0x7},
27		{val: 34, index: 1, bit: (1 << 2), mask: 0x7},
28		{val: 65, index: 2, bit: (1 << 1), mask: 0},
29	}
30	for i, v := range vs {
31		index, bit, err := bitOf(Inheritable, v.val)
32		if err != nil {
33			t.Fatalf("[%d] %v(%d) - not bitOf: %v", i, v.val, v.val, err)
34		} else if index != v.index {
35			t.Errorf("[%d] %v(%d) - index: got=%d want=%d", i, v.val, v.val, index, v.index)
36		}
37		if bit != v.bit {
38			t.Errorf("[%d] %v(%d) - bit: got=%b want=%b", i, v.val, v.val, bit, v.bit)
39		}
40		if mask := allMask(index); mask != v.mask {
41			t.Errorf("[%d] %v(%d) - mask: got=%b want=%b", i, v.val, v.val, mask, v.mask)
42		}
43	}
44}
45
46func TestString(t *testing.T) {
47	a := CHOWN
48	if got, want := a.String(), "cap_chown"; got != want {
49		t.Fatalf("pretty basic failure: got=%q, want=%q", got, want)
50	}
51}
52
53func TestText(t *testing.T) {
54	vs := []struct {
55		from, to string
56		err      error
57	}{
58		{"", "", ErrBadText},
59		{"=", "=", nil},
60		{"= cap_chown+iep cap_chown-i", "cap_chown=ep", nil},
61		{"= cap_setfcap,cap_chown+iep cap_chown-i", "cap_setfcap=eip cap_chown+ep", nil},
62		{"cap_setfcap,cap_chown=iep cap_chown-i", "cap_setfcap=eip cap_chown+ep", nil},
63		{"=i =p", "=p", nil},
64		{"all+pie", "=eip", nil},
65		{"all=p+ie-e", "=ip", nil},
66	}
67	for i, v := range vs {
68		c, err := FromText(v.from)
69		if err != v.err {
70			t.Errorf("[%d] parsing %q failed: got=%v, want=%v", i, v.from, err, v.err)
71			continue
72		}
73		if err != nil {
74			continue
75		}
76		to := c.String()
77		if to != v.to {
78			t.Errorf("[%d] failed to stringify cap: %q -> got=%q, want=%q", i, v.from, to, v.to)
79		}
80		if d, err := FromText(to); err != nil {
81			t.Errorf("[%d] failed to reparse %q: %v", i, to, err)
82		} else if got := d.String(); got != to {
83			t.Errorf("[%d] failed to stringify %q getting %q", i, to, got)
84		}
85	}
86}
87
88func same(a, b *Set) error {
89	if (a == nil) != (b == nil) {
90		return fmt.Errorf("nil-ness miscompare: %q vs %v", a, b)
91	}
92	if a == nil {
93		return nil
94	}
95	if a.nsRoot != b.nsRoot {
96		return fmt.Errorf("capabilities differ in nsRoot: a=%d b=%d", a.nsRoot, b.nsRoot)
97	}
98	for i, f := range a.flat {
99		g := b.flat[i]
100		for s := Effective; s <= Inheritable; s++ {
101			if got, want := f[s], g[s]; got != want {
102				return fmt.Errorf("capabilities differ: a[%d].flat[%v]=0x%08x b[%d].flat[%v]=0x%08x", i, s, got, i, s, want)
103			}
104		}
105	}
106	return nil
107}
108
109func TestImportExport(t *testing.T) {
110	wantQ := "=ep cap_chown-e 63+ip"
111	if q, err := FromText(wantQ); err != nil {
112		t.Fatalf("failed to parse %q: %v", wantQ, err)
113	} else if gotQ := q.String(); gotQ != wantQ {
114		t.Fatalf("static test failed %q -> q -> %q", wantQ, gotQ)
115	}
116
117	// Sanity check empty import/export.
118	c := NewSet()
119	if ex, err := c.Export(); err != nil {
120		t.Fatalf("failed to export empty set: %v", err)
121	} else if len(ex) != 5 {
122		t.Fatalf("wrong length: got=%d want=%d", len(ex), 5)
123	} else if im, err := Import(ex); err != nil {
124		t.Fatalf("failed to import empty set: %v", err)
125	} else if got, want := im.String(), c.String(); got != want {
126		t.Fatalf("import != export: got=%q want=%q", got, want)
127	}
128	// Now keep flipping bits on and off and validate that all
129	// forms of import/export work.
130	for i := uint(0); i < 7000; i += 13 {
131		s := Flag(i % 3)
132		v := Value(i % (maxValues + 3))
133		c.SetFlag(s, i&17 < 8, v)
134		if ex, err := c.Export(); err != nil {
135			t.Fatalf("[%d] failed to export (%q): %v", i, c, err)
136		} else if im, err := Import(ex); err != nil {
137			t.Fatalf("[%d] failed to import (%q) set: %v", i, c, err)
138		} else if got, want := im.String(), c.String(); got != want {
139			t.Fatalf("[%d] import != export: got=%q want=%q [%02x]", i, got, want, ex)
140		} else if parsed, err := FromText(got); err != nil {
141			t.Fatalf("[%d] failed to parse %q: %v", i, got, err)
142		} else if err := same(c, parsed); err != nil {
143			t.Fatalf("[%d] miscompare (%q vs. %q): %v", i, got, parsed, err)
144		}
145	}
146}
147
148func TestIAB(t *testing.T) {
149	vs := []struct {
150		text string
151		bad  bool
152	}{
153		{text: "cup_full", bad: true},
154		{text: ""},
155		{text: "!%cap_chown"},
156		{text: "!cap_chown,^cap_setuid"},
157		{text: "cap_chown,cap_setuid"},
158		{text: "^cap_chown,cap_setuid"},
159		{text: "^cap_chown,!cap_setuid"},
160	}
161	for i, v := range vs {
162		want := v.text
163		iab, err := IABFromText(want)
164		if err != nil {
165			if v.bad {
166				continue
167			}
168			t.Errorf("[%d] want=%q, got=%q", i, want, iab)
169			continue
170		}
171		if got := iab.String(); got != want {
172			t.Errorf("[%d] got=%q want=%q", i, got, want)
173		}
174	}
175
176	one, err := GetPID(1)
177	if err != nil {
178		t.Fatalf("failed to get init's capabilities: %v", err)
179	}
180	iab := IABInit()
181	iab.Fill(Amb, one, Permitted)
182	for i := 0; i < words; i++ {
183		if iab.i[i] != iab.a[i] {
184			t.Errorf("[%d] i=0x%08x != a=0x%08x", i, iab.i[i], iab.a[i])
185		}
186	}
187	one.ClearFlag(Inheritable)
188	iab.Fill(Inh, one, Inheritable)
189	for i := 0; i < words; i++ {
190		if iab.i[i] != iab.a[i] {
191			t.Errorf("[%d] i=0x%08x != a=0x%08x", i, iab.i[i], iab.a[i])
192		}
193	}
194
195	for n := uint(0); n < 1000; n += 13 {
196		enabled := ((n % 5) & 2) != 0
197		vec := Vector(n % 3)
198		c := Value(n % maxValues)
199		if err := iab.SetVector(vec, enabled, c); err != nil {
200			t.Errorf("[%d] failed to set vec=%v enabled=%v %q in %q", n, vec, enabled, c, iab)
201			continue
202		}
203		replay, err := IABFromText(iab.String())
204		if err != nil {
205			t.Errorf("failed to replay: %v", err)
206			continue
207		}
208		for i := 0; i < words; i++ {
209			if replay.i[i] != iab.i[i] || replay.a[i] != iab.a[i] || replay.nb[i] != iab.nb[i] {
210				t.Errorf("[%d,%d] got=%q want=%q", n, i, replay, iab)
211			}
212		}
213	}
214}
215