net_ifaces_test.go 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package probe
  2. import (
  3. "context"
  4. "reflect"
  5. "testing"
  6. )
  7. const travelNetworkDump = `{"interface":[
  8. {"interface":"lan","up":true,"proto":"static","l3_device":"br-lan","device":"br-lan","dynamic":false},
  9. {"interface":"loopback","up":true,"proto":"static","l3_device":"lo","device":"lo","dynamic":false},
  10. {"interface":"wan","up":false,"proto":"dhcp","l3_device":null,"device":"eth0.2","dynamic":false},
  11. {"interface":"wan6","up":false,"proto":"dhcpv6","l3_device":null,"device":"eth0.2","dynamic":false},
  12. {"interface":"wan_4","up":true,"proto":"dhcp","l3_device":"eth0.2","device":"eth0.2","dynamic":true},
  13. {"interface":"wg0","up":true,"proto":"wireguard","l3_device":"wg0","device":null,"dynamic":false},
  14. {"interface":"wwan","up":true,"proto":"dhcp","l3_device":"phy1-sta0","device":"phy1-sta0","dynamic":false,"ipv4-address":[{"address":"10.20.0.177","mask":24}],"route":[{"target":"0.0.0.0","mask":0,"nexthop":"10.20.0.1"}]}
  15. ]}`
  16. func TestParseInterfaceDumpTravelRouter(t *testing.T) {
  17. got := ParseInterfaceDump([]byte(travelNetworkDump), "").Ifaces
  18. want := []NetIface{
  19. {Name: "lan", Proto: "static", Up: true, Devices: []string{"br-lan"}},
  20. {Name: "wan", Proto: "dhcp", Up: false, Devices: []string{"eth0.2"}},
  21. {Name: "wan6", Proto: "dhcpv6", Up: false, Devices: []string{"eth0.2"}},
  22. {Name: "wg0", Proto: "wireguard", Up: true, Devices: []string{"wg0"}},
  23. {Name: "wwan", Proto: "dhcp", Up: true, Devices: []string{"phy1-sta0"}},
  24. }
  25. if !reflect.DeepEqual(got, want) {
  26. t.Fatalf("ParseInterfaceDump:\ngot %+v\nwant %+v", got, want)
  27. }
  28. }
  29. func TestParseInterfaceDumpEdgeCases(t *testing.T) {
  30. t.Run("l3_device y device distintos conserva ambos en orden", func(t *testing.T) {
  31. raw := `{"interface":[{"interface":"wan","up":true,"proto":"pppoe","l3_device":"pppoe-wan","device":"eth0.2","dynamic":false}]}`
  32. got := ParseInterfaceDump([]byte(raw), "").Ifaces
  33. want := []NetIface{
  34. {Name: "wan", Proto: "pppoe", Up: true, Devices: []string{"pppoe-wan", "eth0.2"}},
  35. }
  36. if !reflect.DeepEqual(got, want) {
  37. t.Fatalf("pppoe:\ngot %+v\nwant %+v", got, want)
  38. }
  39. })
  40. t.Run("json invalido o vacio devuelve nil", func(t *testing.T) {
  41. if got := ParseInterfaceDump([]byte("{roto"), "").Ifaces; got != nil {
  42. t.Fatalf("JSON inválido debía devolver nil: %+v", got)
  43. }
  44. if got := ParseInterfaceDump([]byte(`{"interface":[{"interface":"loopback","up":true,"l3_device":"lo","device":"lo"}]}`), "").Ifaces; got != nil {
  45. t.Fatalf("solo loopback debía devolver nil: %+v", got)
  46. }
  47. if got := ParseInterfaceDump(nil, "").Ifaces; got != nil {
  48. t.Fatalf("entrada nil debía devolver nil: %+v", got)
  49. }
  50. })
  51. }
  52. func TestProberBuildReportsNetIfaces(t *testing.T) {
  53. t.Run("router con uplink", func(t *testing.T) {
  54. p := NewProber(fakeRunner{outs: map[string]string{
  55. CmdNetworkDump: travelNetworkDump,
  56. }}, Options{ScanInterval: ScanDisabled})
  57. pl := p.Build(context.Background(), "travel", "test")
  58. if pl.Data.Wan == nil || pl.Data.Wan.Device != "phy1-sta0" {
  59. t.Fatalf("esperaba Wan en phy1-sta0, obtuve %+v", pl.Data.Wan)
  60. }
  61. if len(pl.Data.NetIfaces) != 5 || pl.Data.NetIfaces[0].Name != "lan" || pl.Data.NetIfaces[4].Name != "wwan" {
  62. t.Fatalf("NetIfaces inesperadas en Build: %+v", pl.Data.NetIfaces)
  63. }
  64. })
  65. t.Run("ap sin uplink sigue reportando sus redes", func(t *testing.T) {
  66. apDump := `{"interface":[
  67. {"interface":"loopback","up":true,"proto":"static","l3_device":"lo","device":"lo","dynamic":false},
  68. {"interface":"lan","up":true,"proto":"dhcp","l3_device":"br-lan","device":"br-lan","dynamic":false}
  69. ]}`
  70. p := NewProber(fakeRunner{outs: map[string]string{
  71. CmdNetworkDump: apDump,
  72. }}, Options{ScanInterval: ScanDisabled})
  73. pl := p.Build(context.Background(), "ap", "test")
  74. if pl.Data.Wan != nil {
  75. t.Fatalf("un AP sin uplink no debía reportar Wan: %+v", pl.Data.Wan)
  76. }
  77. want := []NetIface{{Name: "lan", Proto: "dhcp", Up: true, Devices: []string{"br-lan"}}}
  78. if !reflect.DeepEqual(pl.Data.NetIfaces, want) {
  79. t.Fatalf("NetIfaces del AP:\ngot %+v\nwant %+v", pl.Data.NetIfaces, want)
  80. }
  81. })
  82. }