security_linux_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //go:build linux
  2. package ims
  3. import (
  4. "context"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. func TestLinuxIPSecInstallerLifecycle(t *testing.T) {
  12. if os.Getenv("VOCAT_NETNS_TEST") != "1" {
  13. t.Skip("set VOCAT_NETNS_TEST=1 inside an isolated Linux network namespace")
  14. }
  15. handle, err := (linuxIPSecInstaller{ipCommand: "ip"}).Install(
  16. context.Background(),
  17. testIPSecSAConfig(),
  18. )
  19. if err != nil {
  20. t.Fatalf("install ipsec-3gpp XFRM set: %v", err)
  21. }
  22. states, err := exec.Command("ip", "xfrm", "state").CombinedOutput()
  23. if err != nil {
  24. t.Fatalf("list XFRM states: %v: %s", err, states)
  25. }
  26. if count := strings.Count(string(states), "src 10.0.0.2 dst 10.0.0.3"); count != 2 {
  27. t.Fatalf("outbound XFRM state count = %d: %s", count, states)
  28. }
  29. if count := strings.Count(string(states), "src 10.0.0.3 dst 10.0.0.2"); count != 2 {
  30. t.Fatalf("inbound XFRM state count = %d: %s", count, states)
  31. }
  32. policies, err := exec.Command("ip", "xfrm", "policy").CombinedOutput()
  33. if err != nil {
  34. t.Fatalf("list XFRM policies: %v: %s", err, policies)
  35. }
  36. if count := strings.Count(string(policies), "sport 40666 dport 50600"); count != 2 {
  37. t.Fatalf("UE-client policy count = %d: %s", count, policies)
  38. }
  39. closeContext, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  40. defer cancel()
  41. if err := handle.Close(closeContext); err != nil {
  42. t.Fatalf("close ipsec-3gpp XFRM set: %v", err)
  43. }
  44. states, err = exec.Command("ip", "xfrm", "state").CombinedOutput()
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if strings.TrimSpace(string(states)) != "" {
  49. t.Fatalf("XFRM states survived Close: %s", states)
  50. }
  51. policies, err = exec.Command("ip", "xfrm", "policy").CombinedOutput()
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. if strings.TrimSpace(string(policies)) != "" {
  56. t.Fatalf("XFRM policies survived Close: %s", policies)
  57. }
  58. }