Skip to content

Commit 931542d

Browse files
committed
label: stop using deprecated stuff in tests
Move 4 Linux tests that only test stuff from the selinux pkg (TestDuplicateLabel, TestSELinuxNoLevel, TestSocketLabel, TestKeyLabel) from label pkg to selinux, following commit 15c906f ("label: deprecate selinux wrappers"). Remove some stub tests in label pkg (TestSocketLabel, TestKeyLabel, TestProcessLabel, and parts of TestCheckLabelCompile) that already have equivalents in selinux (TestSELinuxStubs). Fix a few remaining issues in label to not use deprecated stuff (ROMountLabel -> selinux.ROFileLabel, GenLabels -> InitLabels). Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 21fd359 commit 931542d

File tree

3 files changed

+109
-149
lines changed

3 files changed

+109
-149
lines changed

go-selinux/label/label_linux_test.go

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package label
33
import (
44
"errors"
55
"os"
6-
"strings"
76
"testing"
87

98
"github.com/opencontainers/selinux/go-selinux"
@@ -25,9 +24,8 @@ func TestInit(t *testing.T) {
2524
t.Fatalf("InitLabels failed: %v:", err)
2625
}
2726
testDisabled := []string{"disable"}
28-
roMountLabel := ROMountLabel()
29-
if roMountLabel == "" {
30-
t.Fatal("ROMountLabel: empty")
27+
if selinux.ROFileLabel() == "" {
28+
t.Fatal("selinux.ROFileLabel: empty")
3129
}
3230
plabel, mlabel, err := InitLabels(testDisabled)
3331
if err != nil {
@@ -55,45 +53,6 @@ func TestInit(t *testing.T) {
5553
}
5654
}
5755

58-
func TestDuplicateLabel(t *testing.T) {
59-
secopt, err := DupSecOpt("system_u:system_r:container_t:s0:c1,c2")
60-
if err != nil {
61-
t.Fatalf("DupSecOpt: %v", err)
62-
}
63-
for _, opt := range secopt {
64-
con := strings.SplitN(opt, ":", 2)
65-
if con[0] == "user" {
66-
if con[1] != "system_u" {
67-
t.Errorf("DupSecOpt Failed user incorrect")
68-
}
69-
continue
70-
}
71-
if con[0] == "role" {
72-
if con[1] != "system_r" {
73-
t.Errorf("DupSecOpt Failed role incorrect")
74-
}
75-
continue
76-
}
77-
if con[0] == "type" {
78-
if con[1] != "container_t" {
79-
t.Errorf("DupSecOpt Failed type incorrect")
80-
}
81-
continue
82-
}
83-
if con[0] == "level" {
84-
if con[1] != "s0:c1,c2" {
85-
t.Errorf("DupSecOpt Failed level incorrect")
86-
}
87-
continue
88-
}
89-
t.Errorf("DupSecOpt failed: invalid field %q", con[0])
90-
}
91-
secopt = DisableSecOpt()
92-
if secopt[0] != "disable" {
93-
t.Errorf("DisableSecOpt failed: expected \"disable\", got %q", secopt[0])
94-
}
95-
}
96-
9756
func TestRelabel(t *testing.T) {
9857
needSELinux(t)
9958

@@ -157,59 +116,6 @@ func TestIsShared(t *testing.T) {
157116
}
158117
}
159118

160-
func TestSELinuxNoLevel(t *testing.T) {
161-
needSELinux(t)
162-
163-
tlabel := "system_u:system_r:container_t"
164-
dup, err := DupSecOpt(tlabel)
165-
if err != nil {
166-
t.Fatal(err)
167-
}
168-
169-
if len(dup) != 3 {
170-
t.Errorf("DupSecOpt failed on non mls label: expected 3, got %d", len(dup))
171-
}
172-
con, err := selinux.NewContext(tlabel)
173-
if err != nil {
174-
t.Fatal(err)
175-
}
176-
if con.Get() != tlabel {
177-
t.Errorf("NewContaxt and con.Get() failed on non mls label: expected %q, got %q", tlabel, con.Get())
178-
}
179-
}
180-
181-
func TestSocketLabel(t *testing.T) {
182-
needSELinux(t)
183-
184-
label := "system_u:object_r:container_t:s0:c1,c2"
185-
if err := selinux.SetSocketLabel(label); err != nil {
186-
t.Fatal(err)
187-
}
188-
nlabel, err := selinux.SocketLabel()
189-
if err != nil {
190-
t.Fatal(err)
191-
}
192-
if label != nlabel {
193-
t.Errorf("SocketLabel %s != %s", nlabel, label)
194-
}
195-
}
196-
197-
func TestKeyLabel(t *testing.T) {
198-
needSELinux(t)
199-
200-
label := "system_u:object_r:container_t:s0:c1,c2"
201-
if err := selinux.SetKeyLabel(label); err != nil {
202-
t.Fatal(err)
203-
}
204-
nlabel, err := selinux.KeyLabel()
205-
if err != nil {
206-
t.Fatal(err)
207-
}
208-
if label != nlabel {
209-
t.Errorf("KeyLabel %s != %s", nlabel, label)
210-
}
211-
}
212-
213119
func TestFileLabel(t *testing.T) {
214120
needSELinux(t)
215121

go-selinux/label/label_stub_test.go

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
package label
55

6-
import "testing"
6+
import (
7+
"testing"
8+
9+
"github.com/opencontainers/selinux/go-selinux"
10+
)
711

812
const testLabel = "system_u:object_r:container_file_t:s0:c1,c2"
913

@@ -15,9 +19,8 @@ func TestInit(t *testing.T) {
1519
t.Fatal(err)
1620
}
1721
testDisabled := []string{"disable"}
18-
roMountLabel := ROMountLabel()
19-
if roMountLabel != "" {
20-
t.Errorf("ROMountLabel Failed")
22+
if selinux.ROFileLabel() != "" {
23+
t.Error("selinux.ROFileLabel Failed")
2124
}
2225
plabel, mlabel, err := InitLabels(testDisabled)
2326
if err != nil {
@@ -44,45 +47,12 @@ func TestRelabel(t *testing.T) {
4447
}
4548
}
4649

47-
func TestSocketLabel(t *testing.T) {
48-
label := testLabel
49-
if err := SetSocketLabel(label); err != nil {
50-
t.Fatal(err)
51-
}
52-
if _, err := SocketLabel(); err != nil {
53-
t.Fatal(err)
54-
}
55-
}
56-
57-
func TestKeyLabel(t *testing.T) {
58-
label := testLabel
59-
if err := SetKeyLabel(label); err != nil {
60-
t.Fatal(err)
61-
}
62-
if _, err := KeyLabel(); err != nil {
63-
t.Fatal(err)
64-
}
65-
}
66-
67-
func TestProcessLabel(t *testing.T) {
68-
label := testLabel
69-
if err := SetProcessLabel(label); err != nil {
70-
t.Fatal(err)
71-
}
72-
if _, err := ProcessLabel(); err != nil {
73-
t.Fatal(err)
74-
}
75-
}
76-
7750
func TestCheckLabelCompile(t *testing.T) {
78-
if _, _, err := GenLabels(""); err != nil {
51+
if _, _, err := InitLabels(nil); err != nil {
7952
t.Fatal(err)
8053
}
8154

8255
tmpDir := t.TempDir()
83-
if _, err := FileLabel(tmpDir); err != nil {
84-
t.Fatal(err)
85-
}
8656

8757
if err := SetFileLabel(tmpDir, "foobar"); err != nil {
8858
t.Fatal(err)
@@ -92,21 +62,6 @@ func TestCheckLabelCompile(t *testing.T) {
9262
t.Fatal(err)
9363
}
9464

95-
if _, err := PidLabel(0); err != nil {
96-
t.Fatal(err)
97-
}
98-
99-
ClearLabels()
100-
101-
if err := ReserveLabel("foobar"); err != nil {
102-
t.Fatal(err)
103-
}
104-
105-
if err := ReleaseLabel("foobar"); err != nil {
106-
t.Fatal(err)
107-
}
108-
109-
_, _ = DupSecOpt("foobar")
11065
DisableSecOpt()
11166

11267
if err := Validate("foobar"); err != nil {

go-selinux/selinux_linux_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010
"strconv"
11+
"strings"
1112
"testing"
1213
)
1314

@@ -119,6 +120,104 @@ func TestInitLabels(t *testing.T) {
119120
ReleaseLabel(plabel)
120121
}
121122

123+
func TestDuplicateLabel(t *testing.T) {
124+
secopt, err := DupSecOpt("system_u:system_r:container_t:s0:c1,c2")
125+
if err != nil {
126+
t.Fatalf("DupSecOpt: %v", err)
127+
}
128+
for _, opt := range secopt {
129+
con := strings.SplitN(opt, ":", 2)
130+
if con[0] == "user" {
131+
if con[1] != "system_u" {
132+
t.Errorf("DupSecOpt Failed user incorrect")
133+
}
134+
continue
135+
}
136+
if con[0] == "role" {
137+
if con[1] != "system_r" {
138+
t.Errorf("DupSecOpt Failed role incorrect")
139+
}
140+
continue
141+
}
142+
if con[0] == "type" {
143+
if con[1] != "container_t" {
144+
t.Errorf("DupSecOpt Failed type incorrect")
145+
}
146+
continue
147+
}
148+
if con[0] == "level" {
149+
if con[1] != "s0:c1,c2" {
150+
t.Errorf("DupSecOpt Failed level incorrect")
151+
}
152+
continue
153+
}
154+
t.Errorf("DupSecOpt failed: invalid field %q", con[0])
155+
}
156+
secopt = DisableSecOpt()
157+
if secopt[0] != "disable" {
158+
t.Errorf(`DisableSecOpt failed: want "disable", got %q`, secopt[0])
159+
}
160+
}
161+
162+
func TestSELinuxNoLevel(t *testing.T) {
163+
if !GetEnabled() {
164+
t.Skip("SELinux not enabled, skipping.")
165+
}
166+
167+
tlabel := "system_u:system_r:container_t"
168+
dup, err := DupSecOpt(tlabel)
169+
if err != nil {
170+
t.Fatal(err)
171+
}
172+
173+
if len(dup) != 3 {
174+
t.Errorf("DupSecOpt failed on non mls label: want 3, got %d", len(dup))
175+
}
176+
con, err := NewContext(tlabel)
177+
if err != nil {
178+
t.Fatal(err)
179+
}
180+
if con.Get() != tlabel {
181+
t.Errorf("NewContext and con.Get() failed on non mls label: want %q, got %q", tlabel, con.Get())
182+
}
183+
}
184+
185+
func TestSocketLabel(t *testing.T) {
186+
if !GetEnabled() {
187+
t.Skip("SELinux not enabled, skipping.")
188+
}
189+
190+
label := "system_u:object_r:container_t:s0:c1,c2"
191+
if err := SetSocketLabel(label); err != nil {
192+
t.Fatal(err)
193+
}
194+
nlabel, err := SocketLabel()
195+
if err != nil {
196+
t.Fatal(err)
197+
}
198+
if label != nlabel {
199+
t.Errorf("SocketLabel %s != %s", nlabel, label)
200+
}
201+
}
202+
203+
func TestKeyLabel(t *testing.T) {
204+
if !GetEnabled() {
205+
t.Skip("SELinux not enabled, skipping.")
206+
}
207+
208+
label := "system_u:object_r:container_t:s0:c1,c2"
209+
if err := SetKeyLabel(label); err != nil {
210+
t.Fatal(err)
211+
}
212+
nlabel, err := KeyLabel()
213+
if err != nil {
214+
t.Fatal(err)
215+
}
216+
if label != nlabel {
217+
t.Errorf("KeyLabel: want %q, got %q", label, nlabel)
218+
}
219+
}
220+
122221
func BenchmarkContextGet(b *testing.B) {
123222
ctx, err := NewContext("system_u:object_r:container_file_t:s0:c1022,c1023")
124223
if err != nil {

0 commit comments

Comments
 (0)