manager_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package aerotun
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestManagerPersistenceValidationAndStartupErrors(t *testing.T) {
  8. m := &Manager{instances: map[string]TunnelInstance{}, runners: map[string]*TunnelRunner{}, errors: map[string]string{}, filePath: filepath.Join(t.TempDir(), "instances.json")}
  9. cfg := TunnelInstance{Name: "atest", Mode: "client", LocalAddress: "10.99.0.1/24", PeerAddress: "10.99.0.2/24", RemoteEndpoint: "192.0.2.1:51830", PSK: testPSK, Disabled: true}
  10. saved, err := m.Save(cfg)
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. info, err := os.Stat(m.filePath)
  15. if err != nil || info.Mode().Perm() != 0600 {
  16. t.Fatalf("config permissions: %v %v", info, err)
  17. }
  18. m2 := &Manager{instances: map[string]TunnelInstance{}, filePath: m.filePath}
  19. if err = m2.load(); err != nil {
  20. t.Fatal(err)
  21. }
  22. if got := m2.instances[saved.ID]; got.PSK != testPSK || !got.Disabled {
  23. t.Fatal("configuration did not roundtrip")
  24. }
  25. duplicate := cfg
  26. duplicate.ID = "duplicate"
  27. if _, err = m.Save(duplicate); err == nil {
  28. t.Fatal("duplicate interface name accepted")
  29. }
  30. // A bad endpoint fails before any interface creation, and surfaces to the UI.
  31. m.instances[saved.ID] = saved
  32. saved.RemoteEndpoint = "bad endpoint"
  33. m.instances[saved.ID] = saved
  34. if err = m.StartTunnel(saved.ID); err == nil {
  35. t.Fatal("startup failure hidden")
  36. }
  37. if got, _ := m.Get(saved.ID); got.Running || got.LastError == "" {
  38. t.Fatal("startup failure not exposed in telemetry")
  39. }
  40. }
  41. func TestManagerFailedPersistenceDoesNotChangeConfig(t *testing.T) {
  42. dir := t.TempDir()
  43. m := &Manager{instances: map[string]TunnelInstance{}, runners: map[string]*TunnelRunner{}, errors: map[string]string{}, filePath: filepath.Join(dir, "missing", "instances.json")}
  44. cfg := TunnelInstance{ID: "test", Name: "atest", Mode: "server", LocalAddress: "10.99.0.1/24", PSK: testPSK, Disabled: true}
  45. m.instances[cfg.ID] = cfg
  46. changed := cfg
  47. changed.Comment = "new"
  48. if _, err := m.Save(changed); err == nil {
  49. t.Fatal("expected persistence error")
  50. }
  51. if m.instances[cfg.ID].Comment != "" {
  52. t.Fatal("failed save changed in-memory configuration")
  53. }
  54. if err := m.ToggleState(cfg.ID, false); err == nil {
  55. t.Fatal("expected persistence error")
  56. }
  57. if !m.instances[cfg.ID].Disabled {
  58. t.Fatal("failed persistence enabled the tunnel")
  59. }
  60. }