bare_host_1968_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package routerstore
  2. import (
  3. "testing"
  4. "github.com/gnacho/netpulse/server-go/internal/db"
  5. )
  6. // #1968: the integration form asks for the full URL, but the unit only accepts
  7. // an IP or a hostname. The address is taken out of the URL and anything that
  8. // does not end up being a valid host is returned untouched.
  9. func TestBareHostFromIntegrationForm(t *testing.T) {
  10. cases := []struct{ in, want string }{
  11. {"https://192.168.1.1", "192.168.1.1"},
  12. {"https://fw.mandor.lan", "fw.mandor.lan"},
  13. {"https://192.168.1.1:8443", "192.168.1.1"},
  14. {"http://opnsense.local/", "opnsense.local"},
  15. {"https://user:pass@fw.local:8443/ui", "fw.local"},
  16. {"192.168.1.1", "192.168.1.1"},
  17. {" fw.local ", "fw.local"},
  18. {"fw.local:8443", "fw.local"},
  19. {"", ""},
  20. // IPv6 does not fit the form contract (it rejects ':'), so it keeps the
  21. // original value instead of becoming something the form still rejects.
  22. {"https://[2001:db8::1]:443", "https://[2001:db8::1]:443"},
  23. {"https:///path", "https:///path"},
  24. }
  25. for _, c := range cases {
  26. if got := BareHost(c.in); got != c.want {
  27. t.Errorf("BareHost(%q) = %q, want %q", c.in, got, c.want)
  28. }
  29. }
  30. }
  31. func TestAddRouterStoresTheBareHost(t *testing.T) {
  32. d, err := db.Open(t.TempDir())
  33. if err != nil {
  34. t.Fatalf("db: %v", err)
  35. }
  36. defer d.Close()
  37. cfg, err := AddRouter(d.DB, AddInput{Name: "opnsense", Host: "https://192.168.1.1:8443", Type: "external", AgentOnly: true})
  38. if err != nil {
  39. t.Fatalf("add: %v", err)
  40. }
  41. if cfg.Host != "192.168.1.1" {
  42. t.Fatalf("host de la unidad = %q, esperaba 192.168.1.1", cfg.Host)
  43. }
  44. listed := ListRouters(d.DB)
  45. if len(listed) != 1 || listed[0].Host != "192.168.1.1" {
  46. t.Fatalf("ListRouters devolvio %+v", listed)
  47. }
  48. }
  49. func TestNormalizeExternalHostsFixesExistingRows(t *testing.T) {
  50. d, err := db.Open(t.TempDir())
  51. if err != nil {
  52. t.Fatalf("db: %v", err)
  53. }
  54. defer d.Close()
  55. cfg, err := AddRouter(d.DB, AddInput{Name: "fw", Host: "192.168.1.1", Type: "external", AgentOnly: true})
  56. if err != nil {
  57. t.Fatalf("add: %v", err)
  58. }
  59. // Fila tal como la dejaba el alta anterior al arreglo.
  60. if _, err := d.DB.Exec(`UPDATE routers SET host = ? WHERE id = ?`, "https://192.168.1.1:8443", cfg.ID); err != nil {
  61. t.Fatalf("seed legacy row: %v", err)
  62. }
  63. if n := NormalizeExternalHosts(d.DB); n != 1 {
  64. t.Fatalf("normalizadas = %d, esperaba 1", n)
  65. }
  66. if got := ListRouters(d.DB)[0].Host; got != "192.168.1.1" {
  67. t.Fatalf("host tras normalizar = %q", got)
  68. }
  69. if n := NormalizeExternalHosts(d.DB); n != 0 {
  70. t.Fatalf("segunda pasada cambio %d filas, no es idempotente", n)
  71. }
  72. // Una unidad que no es external no se toca: su host lo valida el formulario.
  73. local, err := AddRouter(d.DB, AddInput{Name: "rt1", Host: "192.168.1.2", Type: "openwrt"})
  74. if err != nil {
  75. t.Fatalf("add local: %v", err)
  76. }
  77. if _, err := d.DB.Exec(`UPDATE routers SET host = ? WHERE id = ?`, "https://192.168.1.2", local.ID); err != nil {
  78. t.Fatalf("seed local row: %v", err)
  79. }
  80. if n := NormalizeExternalHosts(d.DB); n != 0 {
  81. t.Fatalf("normalizo %d unidades que no son external", n)
  82. }
  83. }