2014-07-18 02:57:57 +08:00
|
|
|
// +build selinux,linux
|
|
|
|
|
|
|
|
package label
|
|
|
|
|
|
|
|
import (
|
2015-11-25 01:54:30 +08:00
|
|
|
"os"
|
2014-10-29 08:49:57 +08:00
|
|
|
"strings"
|
2014-07-18 02:57:57 +08:00
|
|
|
"testing"
|
|
|
|
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/selinux"
|
2014-07-18 02:57:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInit(t *testing.T) {
|
|
|
|
if selinux.SelinuxEnabled() {
|
|
|
|
var testNull []string
|
|
|
|
plabel, mlabel, err := InitLabels(testNull)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("InitLabels Failed")
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-12 20:39:12 +08:00
|
|
|
testDisabled := []string{"label=disable"}
|
2016-10-18 04:56:42 +08:00
|
|
|
roMountLabel := GetROMountLabel()
|
|
|
|
if roMountLabel == "" {
|
|
|
|
t.Errorf("GetROMountLabel Failed")
|
|
|
|
}
|
2014-07-18 02:57:57 +08:00
|
|
|
plabel, mlabel, err = InitLabels(testDisabled)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("InitLabels Disabled Failed")
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if plabel != "" {
|
|
|
|
t.Log("InitLabels Disabled Failed")
|
2016-04-12 16:12:23 +08:00
|
|
|
t.FailNow()
|
2014-07-18 02:57:57 +08:00
|
|
|
}
|
2016-10-12 20:39:12 +08:00
|
|
|
testUser := []string{"label=user:user_u", "label=role:user_r", "label=type:user_t", "label=level:s0:c1,c15"}
|
2014-07-18 02:57:57 +08:00
|
|
|
plabel, mlabel, err = InitLabels(testUser)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("InitLabels User Failed")
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if plabel != "user_u:user_r:user_t:s0:c1,c15" || mlabel != "user_u:object_r:svirt_sandbox_file_t:s0:c1,c15" {
|
2014-10-29 08:49:57 +08:00
|
|
|
t.Log("InitLabels User Match Failed")
|
2014-07-18 02:57:57 +08:00
|
|
|
t.Log(plabel, mlabel)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-10-12 20:39:12 +08:00
|
|
|
testBadData := []string{"label=user", "label=role:user_r", "label=type:user_t", "label=level:s0:c1,c15"}
|
2016-03-18 04:28:04 +08:00
|
|
|
if _, _, err = InitLabels(testBadData); err == nil {
|
2014-07-18 02:57:57 +08:00
|
|
|
t.Log("InitLabels Bad Failed")
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 08:49:57 +08:00
|
|
|
func TestDuplicateLabel(t *testing.T) {
|
|
|
|
secopt := DupSecOpt("system_u:system_r:svirt_lxc_net_t:s0:c1,c2")
|
|
|
|
t.Log(secopt)
|
|
|
|
for _, opt := range secopt {
|
2016-04-29 19:59:58 +08:00
|
|
|
parts := strings.SplitN(opt, "=", 2)
|
|
|
|
if len(parts) != 2 || parts[0] != "label" {
|
2014-10-29 08:49:57 +08:00
|
|
|
t.Errorf("Invalid DupSecOpt return value")
|
|
|
|
continue
|
|
|
|
}
|
2016-04-29 19:59:58 +08:00
|
|
|
con := strings.SplitN(parts[1], ":", 2)
|
|
|
|
if con[0] == "user" {
|
|
|
|
if con[1] != "system_u" {
|
2014-10-29 08:49:57 +08:00
|
|
|
t.Errorf("DupSecOpt Failed user incorrect")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2016-04-29 19:59:58 +08:00
|
|
|
if con[0] == "role" {
|
|
|
|
if con[1] != "system_r" {
|
2014-10-29 08:49:57 +08:00
|
|
|
t.Errorf("DupSecOpt Failed role incorrect")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2016-04-29 19:59:58 +08:00
|
|
|
if con[0] == "type" {
|
|
|
|
if con[1] != "svirt_lxc_net_t" {
|
2014-10-29 08:49:57 +08:00
|
|
|
t.Errorf("DupSecOpt Failed type incorrect")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2016-04-29 19:59:58 +08:00
|
|
|
if con[0] == "level" {
|
|
|
|
if con[1] != "s0:c1,c2" {
|
2014-10-29 08:49:57 +08:00
|
|
|
t.Errorf("DupSecOpt Failed level incorrect")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2016-04-29 19:59:58 +08:00
|
|
|
t.Errorf("DupSecOpt Failed invalid field %q", con[0])
|
2014-10-29 08:49:57 +08:00
|
|
|
}
|
|
|
|
secopt = DisableSecOpt()
|
2016-04-29 19:59:58 +08:00
|
|
|
if secopt[0] != "label=disable" {
|
2014-10-29 08:49:57 +08:00
|
|
|
t.Errorf("DisableSecOpt Failed level incorrect")
|
|
|
|
}
|
|
|
|
}
|
2015-04-06 22:07:08 +08:00
|
|
|
func TestRelabel(t *testing.T) {
|
|
|
|
testdir := "/tmp/test"
|
2015-11-25 01:54:30 +08:00
|
|
|
if err := os.Mkdir(testdir, 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(testdir)
|
2016-10-12 20:39:12 +08:00
|
|
|
label := "system_u:object_r:svirt_sandbox_file_t:s0:c1,c2"
|
2015-07-31 04:36:35 +08:00
|
|
|
if err := Relabel(testdir, "", true); err != nil {
|
2016-04-12 16:12:23 +08:00
|
|
|
t.Fatalf("Relabel with no label failed: %v", err)
|
2015-04-06 22:07:08 +08:00
|
|
|
}
|
2015-07-31 04:36:35 +08:00
|
|
|
if err := Relabel(testdir, label, true); err != nil {
|
2016-04-12 16:12:23 +08:00
|
|
|
t.Fatalf("Relabel shared failed: %v", err)
|
2015-04-06 22:07:08 +08:00
|
|
|
}
|
2015-07-31 04:36:35 +08:00
|
|
|
if err := Relabel(testdir, label, false); err != nil {
|
2016-04-12 16:12:23 +08:00
|
|
|
t.Fatalf("Relabel unshared failed: %v", err)
|
2015-04-06 22:07:08 +08:00
|
|
|
}
|
2015-07-31 04:36:35 +08:00
|
|
|
if err := Relabel("/etc", label, false); err == nil {
|
2016-04-12 16:12:23 +08:00
|
|
|
t.Fatalf("Relabel /etc succeeded")
|
2015-04-15 03:23:53 +08:00
|
|
|
}
|
2015-07-31 04:36:35 +08:00
|
|
|
if err := Relabel("/", label, false); err == nil {
|
2016-04-12 16:12:23 +08:00
|
|
|
t.Fatalf("Relabel / succeeded")
|
2015-04-15 03:23:53 +08:00
|
|
|
}
|
2015-07-31 04:36:35 +08:00
|
|
|
if err := Relabel("/usr", label, false); err == nil {
|
2016-04-12 16:12:23 +08:00
|
|
|
t.Fatalf("Relabel /usr succeeded")
|
2015-04-06 22:07:08 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 04:36:35 +08:00
|
|
|
|
|
|
|
func TestValidate(t *testing.T) {
|
|
|
|
if err := Validate("zZ"); err != ErrIncompatibleLabel {
|
|
|
|
t.Fatalf("Expected incompatible error, got %v", err)
|
|
|
|
}
|
|
|
|
if err := Validate("Z"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := Validate("z"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := Validate(""); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsShared(t *testing.T) {
|
|
|
|
if shared := IsShared("Z"); shared {
|
2016-04-12 16:12:23 +08:00
|
|
|
t.Fatalf("Expected label `Z` to not be shared, got %v", shared)
|
2015-07-31 04:36:35 +08:00
|
|
|
}
|
|
|
|
if shared := IsShared("z"); !shared {
|
2016-04-12 16:12:23 +08:00
|
|
|
t.Fatalf("Expected label `z` to be shared, got %v", shared)
|
2015-07-31 04:36:35 +08:00
|
|
|
}
|
|
|
|
if shared := IsShared("Zz"); !shared {
|
2016-04-12 16:12:23 +08:00
|
|
|
t.Fatalf("Expected label `Zz` to be shared, got %v", shared)
|
2015-07-31 04:36:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|