ppp_server_test.go 3.4 KB

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