ppp_server_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package vpn
  2. import (
  3. "os"
  4. "strings"
  5. "testing"
  6. )
  7. func TestParseIPRangeAndFirstAvailable(t *testing.T) {
  8. start, end, err := parseIPRange("192.168.133.2-192.168.133.254")
  9. if err != nil {
  10. t.Fatalf("unexpected error parsing range: %v", err)
  11. }
  12. if start.String() != "192.168.133.2" || end.String() != "192.168.133.254" {
  13. t.Fatalf("unexpected parsed range: %s - %s", start, end)
  14. }
  15. used := map[string]bool{
  16. "192.168.133.2": true,
  17. "192.168.133.3": true,
  18. }
  19. first := firstAvailableIPFromRange("192.168.133.2-192.168.133.254", used)
  20. if first != "192.168.133.4" {
  21. t.Fatalf("expected 192.168.133.4, got %s", first)
  22. }
  23. }
  24. func TestSyncSecretsInheritanceAndOverrides(t *testing.T) {
  25. tmpDir, err := os.MkdirTemp("", "ppp-test-*")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. defer os.RemoveAll(tmpDir)
  30. mgr := &PPPServerManager{
  31. baseDir: tmpDir,
  32. profiles: make(map[string]*PPPProfile),
  33. secrets: make(map[string]*PPPSecret),
  34. }
  35. mgr.initDefaults()
  36. // Add custom profile with range
  37. mgr.profiles["p2p"] = &PPPProfile{
  38. ID: "p2p",
  39. Name: "p2p",
  40. LocalAddress: "192.168.133.1",
  41. RemoteAddress: "192.168.133.2-192.168.133.254",
  42. }
  43. // Add profile with single static IP
  44. mgr.profiles["static-prof"] = &PPPProfile{
  45. ID: "static-prof",
  46. Name: "static-prof",
  47. LocalAddress: "10.200.1.1",
  48. RemoteAddress: "10.200.1.50",
  49. }
  50. // Secret 1: explicit override on secret
  51. mgr.secrets["s1"] = &PPPSecret{
  52. ID: "s1",
  53. Name: "user_explicit",
  54. Password: "pass1",
  55. Profile: "p2p",
  56. RemoteAddress: "192.168.133.99",
  57. }
  58. // Secret 2: inherits from range profile
  59. mgr.secrets["s2"] = &PPPSecret{
  60. ID: "s2",
  61. Name: "user_range_inherit",
  62. Password: "pass2",
  63. Profile: "p2p",
  64. }
  65. // Secret 3: inherits from single static IP profile
  66. mgr.secrets["s3"] = &PPPSecret{
  67. ID: "s3",
  68. Name: "user_static_inherit",
  69. Password: "pass3",
  70. Profile: "static-prof",
  71. }
  72. // Secret 4: default profile range inherit
  73. mgr.secrets["s4"] = &PPPSecret{
  74. ID: "s4",
  75. Name: "user_default_inherit",
  76. Password: "pass4",
  77. Profile: "default",
  78. }
  79. if err := mgr.syncSecretsToSystemLocked(); err != nil {
  80. t.Fatalf("sync error: %v", err)
  81. }
  82. data, err := os.ReadFile("/etc/ppp/chap-secrets")
  83. if err != nil {
  84. t.Skip("skipping chap-secrets read if no root permission")
  85. }
  86. content := string(data)
  87. // Verify user_explicit got 192.168.133.99
  88. if !strings.Contains(content, "\"user_explicit\"\t*\t\"pass1\"\t192.168.133.99") {
  89. t.Errorf("user_explicit expected 192.168.133.99 in chap-secrets, got:\n%s", content)
  90. }
  91. // Verify user_static_inherit got 10.200.1.50
  92. if !strings.Contains(content, "\"user_static_inherit\"\t*\t\"pass3\"\t10.200.1.50") {
  93. t.Errorf("user_static_inherit expected 10.200.1.50 in chap-secrets, got:\n%s", content)
  94. }
  95. // Verify user_range_inherit got first available from p2p (192.168.133.2)
  96. if !strings.Contains(content, "\"user_range_inherit\"\t*\t\"pass2\"\t192.168.133.2") {
  97. t.Errorf("user_range_inherit expected 192.168.133.2 in chap-secrets, got:\n%s", content)
  98. }
  99. }