selinux_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package selinux_test
  2. import (
  3. "github.com/dotcloud/docker/pkg/selinux"
  4. "os"
  5. "testing"
  6. )
  7. func testSetfilecon(t *testing.T) {
  8. if selinux.SelinuxEnabled() {
  9. tmp := "selinux_test"
  10. out, _ := os.OpenFile(tmp, os.O_WRONLY, 0)
  11. out.Close()
  12. err := selinux.Setfilecon(tmp, "system_u:object_r:bin_t:s0")
  13. if err != nil {
  14. t.Log("Setfilecon failed")
  15. t.Fatal(err)
  16. }
  17. os.Remove(tmp)
  18. }
  19. }
  20. func TestSELinux(t *testing.T) {
  21. var (
  22. err error
  23. plabel, flabel string
  24. )
  25. if selinux.SelinuxEnabled() {
  26. t.Log("Enabled")
  27. plabel, flabel = selinux.GetLxcContexts()
  28. t.Log(plabel)
  29. t.Log(flabel)
  30. plabel, flabel = selinux.GetLxcContexts()
  31. t.Log(plabel)
  32. t.Log(flabel)
  33. t.Log("getenforce ", selinux.SelinuxGetEnforce())
  34. t.Log("getenforcemode ", selinux.SelinuxGetEnforceMode())
  35. pid := os.Getpid()
  36. t.Log("PID:%d MCS:%s\n", pid, selinux.IntToMcs(pid, 1023))
  37. err = selinux.Setfscreatecon("unconfined_u:unconfined_r:unconfined_t:s0")
  38. if err == nil {
  39. t.Log(selinux.Getfscreatecon())
  40. } else {
  41. t.Log("setfscreatecon failed", err)
  42. t.Fatal(err)
  43. }
  44. err = selinux.Setfscreatecon("")
  45. if err == nil {
  46. t.Log(selinux.Getfscreatecon())
  47. } else {
  48. t.Log("setfscreatecon failed", err)
  49. t.Fatal(err)
  50. }
  51. t.Log(selinux.Getpidcon(1))
  52. } else {
  53. t.Log("Disabled")
  54. }
  55. }