multiwan_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package probe
  2. import "testing"
  3. // A two-uplink router whose policy sends everything through the mobile link
  4. // while the wired one still holds the cheapest default route — the state
  5. // that made the panel show an address nobody was using. The mobile parent
  6. // carries its address here the way a policy manager reports it.
  7. const dumpPolicyDisagreesWithRoutes = `{"interface":[
  8. {"interface":"lan","up":true,"proto":"static","l3_device":"br-home.10","device":"br-home.10",
  9. "ipv4-address":[{"address":"192.0.2.1","mask":24}],"route":[]},
  10. {"interface":"cell","up":true,"proto":"qmi","l3_device":"wwan0",
  11. "ipv4-address":[{"address":"192.0.2.77","mask":32}],
  12. "route":[{"target":"0.0.0.0","mask":0,"nexthop":"192.0.2.78"}],
  13. "dns-server":["192.0.2.53"]},
  14. {"interface":"fiber","up":true,"proto":"pppoe","l3_device":"pppoe-fiber","device":"lan4",
  15. "ipv4-address":[{"address":"203.0.113.45","mask":32,"ptpaddress":"198.51.100.1"}],
  16. "route":[{"target":"0.0.0.0","mask":0,"nexthop":"198.51.100.1"}],
  17. "dns-server":["198.51.100.53"]}]}`
  18. func TestParseWanStatusFollowsThePolicyOverTheRoutes(t *testing.T) {
  19. // Without being told, the first routed interface wins — the old answer.
  20. if info := ParseWanStatus([]byte(dumpPolicyDisagreesWithRoutes)); info.IP != "192.0.2.77" {
  21. t.Fatalf("unprompted pick = %+v", info)
  22. }
  23. // Told which uplink is carrying traffic, everything follows it: the
  24. // address, the gateway, the resolvers and the socket.
  25. info := ParseWanStatusPreferring([]byte(dumpPolicyDisagreesWithRoutes), "fiber")
  26. if info.IP != "203.0.113.45" || info.Gateway != "198.51.100.1" {
  27. t.Fatalf("ip/gateway = %+v, want the policy's uplink", info)
  28. }
  29. if info.Proto != "pppoe" || info.Port != "lan4" || info.Device != "pppoe-fiber" {
  30. t.Fatalf("identity = %+v", info)
  31. }
  32. if len(info.DNS) != 1 || info.DNS[0] != "198.51.100.53" {
  33. t.Fatalf("dns = %v, want that uplink's own", info.DNS)
  34. }
  35. }
  36. // The shape that matters on real hardware: the policy names the modem
  37. // interface, which holds neither address nor route — the runtime child
  38. // netifd spawned underneath it holds both, and cannot be named in a policy.
  39. const dumpModemParentAndChild = `{"interface":[
  40. {"interface":"fiber","up":true,"proto":"pppoe","l3_device":"pppoe-fiber","device":"lan4",
  41. "ipv4-address":[{"address":"203.0.113.45","mask":32}],
  42. "route":[{"target":"0.0.0.0","mask":0,"nexthop":"198.51.100.1"}],
  43. "dns-server":["198.51.100.53"]},
  44. {"interface":"cell","up":true,"proto":"qmi","l3_device":"wwan0",
  45. "ipv4-address":[],"route":[],"dns-server":[]},
  46. {"interface":"cell_4","up":true,"proto":"dhcp","l3_device":"wwan0","device":"wwan0","dynamic":true,
  47. "ipv4-address":[{"address":"192.0.2.77","mask":32}],
  48. "route":[{"target":"0.0.0.0","mask":0,"nexthop":"192.0.2.78"}],
  49. "dns-server":["192.0.2.53"]}]}`
  50. func TestPreferredModemUplinkTakesItsChildsAddress(t *testing.T) {
  51. info := ParseWanStatusPreferring([]byte(dumpModemParentAndChild), "cell")
  52. if info.IP != "192.0.2.77" || info.Gateway != "192.0.2.78" {
  53. t.Fatalf("ip/gateway = %+v, want the child's", info)
  54. }
  55. // The identity stays the parent's: that is the name the policy uses and
  56. // the only one with a configuration behind it.
  57. if info.Proto != "qmi" || info.Device != "wwan0" {
  58. t.Fatalf("identity = %+v, want the parent's", info)
  59. }
  60. if len(info.DNS) != 1 || info.DNS[0] != "192.0.2.53" {
  61. t.Fatalf("dns = %v", info.DNS)
  62. }
  63. }
  64. func TestParseWanStatusIgnoresAnUnusablePreference(t *testing.T) {
  65. // A name nothing matches falls back to the route table rather than
  66. // reporting a connection with nothing on it.
  67. if info := ParseWanStatusPreferring([]byte(dumpUplinkNotNamedWan), "gone"); info.IP != "203.0.113.45" {
  68. t.Fatalf("unknown preference gave %+v, want the routed uplink", info)
  69. }
  70. // And an empty preference is exactly the old behaviour.
  71. empty, old := ParseWanStatusPreferring([]byte(dumpUplinkNotNamedWan), ""), ParseWanStatus([]byte(dumpUplinkNotNamedWan))
  72. if empty.IP != old.IP || empty.Proto != old.Proto || empty.Port != old.Port || empty.Gateway != old.Gateway {
  73. t.Fatalf("an empty preference changed the answer: %+v vs %+v", empty, old)
  74. }
  75. }
  76. func TestActiveUplinkName(t *testing.T) {
  77. if got := ActiveUplinkName(nil); got != "" {
  78. t.Fatalf("no policy should name nothing, got %q", got)
  79. }
  80. if got := ActiveUplinkName(&MultiWanInfo{Mode: "failover", Active: "cell"}); got != "cell" {
  81. t.Fatalf("got %q", got)
  82. }
  83. }