reanchor_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package adapters
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. )
  8. // reanchorRunner es un fake sshRunner genérico para los tests de re-anchor.
  9. type reanchorRunner struct {
  10. responses map[string]string
  11. errors map[string]error
  12. }
  13. func (r *reanchorRunner) Run(host, cmd string, timeout time.Duration) (string, error) {
  14. key := host + "|" + cmd
  15. if e, ok := r.errors[key]; ok {
  16. return "", e
  17. }
  18. return r.responses[key], nil
  19. }
  20. func TestReanchorUsteer(t *testing.T) {
  21. const (
  22. local1 = `{
  23. "hostapd.phy0-ap0": { "bssid": "9c:9d:7e:1b:ea:b3", "ssid": "temiscira", "freq": 5260, "n_assoc": 1, "load": 3 },
  24. "hostapd.phy1-ap0": { "bssid": "9c:9d:7e:1b:ea:b2", "ssid": "temiscira", "freq": 2442, "n_assoc": 0, "load": 10 }
  25. }`
  26. local2 = `{
  27. "hostapd.phy0-ap0": { "bssid": "cc:d8:43:b1:e5:97", "ssid": "temiscira", "freq": 5180, "n_assoc": 0, "load": 5 }
  28. }`
  29. remote2 = `{
  30. "192.168.1.2#hostapd.phy0-ap0": { "bssid": "9c:9d:7e:1b:ea:b3", "ssid": "temiscira", "freq": 5260, "n_assoc": 1, "load": 3 },
  31. "192.168.1.2#hostapd.phy1-ap0": { "bssid": "9c:9d:7e:1b:ea:b2", "ssid": "temiscira", "freq": 2442, "n_assoc": 0, "load": 10 }
  32. }`
  33. connected1 = `{
  34. "hostapd.phy1-ap0": { "aa:bb:cc:dd:ee:ff": { "signal": -80 } }
  35. }`
  36. hearing1 = `{
  37. "aa:bb:cc:dd:ee:ff": {
  38. "hostapd.phy1-ap0": { "connected": true, "signal": -80 },
  39. "192.168.1.2#hostapd.phy0-ap0": { "connected": false, "signal": -55 }
  40. }
  41. }`
  42. )
  43. runner := &reanchorRunner{
  44. responses: map[string]string{
  45. "192.168.1.1|ubus call usteer local_info": local1,
  46. "192.168.1.1|ubus call usteer remote_info": "{}",
  47. "192.168.1.1|ubus call usteer connected_clients": connected1,
  48. "192.168.1.1|ubus call usteer get_clients": hearing1,
  49. "192.168.1.2|ubus call usteer local_info": local2,
  50. "192.168.1.2|ubus call usteer remote_info": remote2,
  51. "192.168.1.2|ubus call usteer connected_clients": "{}",
  52. "192.168.1.2|ubus call usteer get_clients": "{}",
  53. },
  54. }
  55. routers := []RouterConfig{
  56. {ID: "rt1", Name: "rt1", Host: "192.168.1.1"},
  57. {ID: "rt2", Name: "rt2", Host: "192.168.1.2"},
  58. }
  59. recs, ok := usteerReanchor(context.Background(), routers, ReanchorConfig{MinRecommendedSignal: -65, MinDeltaDbm: 10}, runner)
  60. if !ok {
  61. t.Fatal("expected usteer available")
  62. }
  63. if len(recs) != 1 {
  64. t.Fatalf("expected 1 recommendation, got %d", len(recs))
  65. }
  66. r := recs[0]
  67. if r.MAC != "AA:BB:CC:DD:EE:FF" {
  68. t.Errorf("mac = %q", r.MAC)
  69. }
  70. if r.CurrentBSSID != "9C:9D:7E:1B:EA:B2" || r.RecommendedBSSID != "9C:9D:7E:1B:EA:B3" {
  71. t.Errorf("current/recommended bssid = %q / %q", r.CurrentBSSID, r.RecommendedBSSID)
  72. }
  73. if r.DeltaDbm != 25 {
  74. t.Errorf("delta = %d, want 25", r.DeltaDbm)
  75. }
  76. if r.CurrentHost != "192.168.1.1" {
  77. t.Errorf("current host = %q, want 192.168.1.1", r.CurrentHost)
  78. }
  79. }
  80. func TestReanchorUsteerNoRecommendation(t *testing.T) {
  81. const (
  82. local1 = `{
  83. "hostapd.phy0-ap0": { "bssid": "9c:9d:7e:1b:ea:b3", "ssid": "temiscira", "freq": 5260, "n_assoc": 1, "load": 3 }
  84. }`
  85. connected1 = `{
  86. "hostapd.phy0-ap0": { "aa:bb:cc:dd:ee:ff": { "signal": -55 } }
  87. }`
  88. hearing1 = `{
  89. "aa:bb:cc:dd:ee:ff": {
  90. "hostapd.phy0-ap0": { "connected": true, "signal": -55 },
  91. "192.168.1.2#hostapd.phy0-ap0": { "connected": false, "signal": -60 }
  92. }
  93. }`
  94. )
  95. runner := &reanchorRunner{
  96. responses: map[string]string{
  97. "192.168.1.1|ubus call usteer local_info": local1,
  98. "192.168.1.1|ubus call usteer remote_info": "{}",
  99. "192.168.1.1|ubus call usteer connected_clients": connected1,
  100. "192.168.1.1|ubus call usteer get_clients": hearing1,
  101. },
  102. }
  103. routers := []RouterConfig{{ID: "rt1", Name: "rt1", Host: "192.168.1.1"}}
  104. recs, ok := usteerReanchor(context.Background(), routers, ReanchorConfig{MinRecommendedSignal: -65, MinDeltaDbm: 10}, runner)
  105. if !ok {
  106. t.Fatal("expected usteer available")
  107. }
  108. if len(recs) != 0 {
  109. t.Fatalf("expected 0 recommendations, got %+v", recs)
  110. }
  111. }
  112. func TestReanchorUsteerFallsBackToDawn(t *testing.T) {
  113. const (
  114. network = `{
  115. "temiscira": {
  116. "9c:9d:7e:1b:ea:b2": {
  117. "hostname": "rt1", "freq": 2442, "channel": 6, "utilization": 10, "num_sta": 1,
  118. "clients": { "aa:bb:cc:dd:ee:ff": { "signal": -80 } },
  119. "local": true, "iface": "wlan0"
  120. },
  121. "9c:9d:7e:1b:ea:b3": {
  122. "hostname": "rt2", "freq": 5260, "channel": 48, "utilization": 5, "num_sta": 0,
  123. "clients": {},
  124. "local": false, "iface": "wlan1"
  125. }
  126. }
  127. }`
  128. hearing = `{
  129. "temiscira": {
  130. "aa:bb:cc:dd:ee:ff": {
  131. "9c:9d:7e:1b:ea:b2": { "signal": -80 },
  132. "9c:9d:7e:1b:ea:b3": { "signal": -55 }
  133. }
  134. }
  135. }`
  136. )
  137. runner := &reanchorRunner{
  138. responses: map[string]string{
  139. "192.168.1.1|ubus call usteer local_info": "{}",
  140. "192.168.1.1|ubus call dawn get_network": network,
  141. "192.168.1.1|ubus call dawn get_hearing_map": hearing,
  142. },
  143. }
  144. routers := []RouterConfig{{ID: "rt1", Name: "rt1", Host: "192.168.1.1"}}
  145. recs, ok := dawnReanchor(context.Background(), routers, ReanchorConfig{MinRecommendedSignal: -65, MinDeltaDbm: 10}, runner)
  146. if !ok {
  147. t.Fatal("expected DAWN available")
  148. }
  149. if len(recs) != 1 {
  150. t.Fatalf("expected 1 recommendation, got %d", len(recs))
  151. }
  152. }
  153. func TestReanchorNone(t *testing.T) {
  154. runner := &reanchorRunner{
  155. responses: map[string]string{
  156. "192.168.1.1|ubus call usteer local_info": "{}",
  157. "192.168.1.1|ubus call dawn get_network": "{}",
  158. },
  159. }
  160. routers := []RouterConfig{{ID: "rt1", Name: "rt1", Host: "192.168.1.1"}}
  161. if _, ok := usteerReanchor(context.Background(), routers, ReanchorConfig{}, runner); ok {
  162. t.Fatal("expected no usteer")
  163. }
  164. if _, ok := dawnReanchor(context.Background(), routers, ReanchorConfig{}, runner); ok {
  165. t.Fatal("expected no DAWN")
  166. }
  167. }
  168. func TestReanchorKickScript(t *testing.T) {
  169. got := ReanchorKickScript("aa:bb:cc:dd:ee:ff", "wlan0")
  170. want := fmt.Sprintf("ubus call hostapd.wlan0 del_client '{\"addr\":\"AA:BB:CC:DD:EE:FF\",\"reason\":5,\"deauth\":true,\"ban_time\":120000}'")
  171. if got != want {
  172. t.Fatalf("script = %q, want %q", got, want)
  173. }
  174. }