selinux_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. selinux.FreeLxcContexts(plabel)
  31. plabel, flabel = selinux.GetLxcContexts()
  32. t.Log(plabel)
  33. t.Log(flabel)
  34. selinux.FreeLxcContexts(plabel)
  35. t.Log("getenforce ", selinux.SelinuxGetEnforce())
  36. t.Log("getenforcemode ", selinux.SelinuxGetEnforceMode())
  37. pid := os.Getpid()
  38. t.Log("PID:%d MCS:%s\n", pid, selinux.IntToMcs(pid, 1023))
  39. err = selinux.Setfscreatecon("unconfined_u:unconfined_r:unconfined_t:s0")
  40. if err == nil {
  41. t.Log(selinux.Getfscreatecon())
  42. } else {
  43. t.Log("setfscreatecon failed", err)
  44. t.Fatal(err)
  45. }
  46. err = selinux.Setfscreatecon("")
  47. if err == nil {
  48. t.Log(selinux.Getfscreatecon())
  49. } else {
  50. t.Log("setfscreatecon failed", err)
  51. t.Fatal(err)
  52. }
  53. t.Log(selinux.Getpidcon(1))
  54. } else {
  55. t.Log("Disabled")
  56. }
  57. }