usteer_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package adapters
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. // TestBuildUsteerAP parsea una salida sintética de `ubus call usteer
  7. // local_info` (shapes verificados en rt3) a UsteerAP, comprobando band/freq/
  8. // load/n_assoc/local/bssid uppercase.
  9. func TestBuildUsteerAP(t *testing.T) {
  10. const localInfo = `{
  11. "hostapd.phy0-ap0": {
  12. "bssid": "9c:9d:7e:1b:ea:b3",
  13. "ssid": "temiscira",
  14. "freq": 5260,
  15. "n_assoc": 0,
  16. "noise": -108,
  17. "load": 3,
  18. "max_assoc": 0,
  19. "roam_events": { "source": 0, "target": 0 },
  20. "rrm_nr": ["9c:9d:7e:1b:ea:b3"]
  21. },
  22. "hostapd.phy1-ap0": {
  23. "bssid": "9c:9d:7e:1b:ea:b2",
  24. "ssid": "temiscira",
  25. "freq": 2442,
  26. "n_assoc": 2,
  27. "load": 20
  28. }
  29. }`
  30. var local map[string]usteerAPRaw
  31. if err := json.Unmarshal([]byte(localInfo), &local); err != nil {
  32. t.Fatalf("unmarshal local_info: %v", err)
  33. }
  34. if len(local) != 2 {
  35. t.Fatalf("esperaba 2 APs, got %d", len(local))
  36. }
  37. ap := buildUsteerAP("hostapd.phy0-ap0", local["hostapd.phy0-ap0"], "rt3", true)
  38. if ap.SSID != "temiscira" || ap.BSSID != "9C:9D:7E:1B:EA:B3" {
  39. t.Errorf("ap = %+v", ap)
  40. }
  41. if ap.Band != "5 GHz" || ap.Freq != 5260 {
  42. t.Errorf("band/freq = %q/%d, want 5 GHz/5260", ap.Band, ap.Freq)
  43. }
  44. if ap.UtilizationPct != 3 || ap.ClientCount != 0 {
  45. t.Errorf("load/n_assoc = %d/%d, want 3/0", ap.UtilizationPct, ap.ClientCount)
  46. }
  47. if !ap.Local || ap.Hostname != "rt3" {
  48. t.Errorf("local/hostname = %v/%q", ap.Local, ap.Hostname)
  49. }
  50. if ap.Clients == nil {
  51. t.Error("Clients debería ser slice vacío, no nil")
  52. }
  53. ap2 := buildUsteerAP("hostapd.phy1-ap0", local["hostapd.phy1-ap0"], "rt3", true)
  54. if ap2.Band != "2.4 GHz" || ap2.Freq != 2442 || ap2.ClientCount != 2 {
  55. t.Errorf("ap2 = %+v", ap2)
  56. }
  57. }
  58. // TestBuildUsteerAPWithClients verifica que connected_clients se asocia al AP
  59. // por iface y se ordena por MAC.
  60. func TestBuildUsteerAPWithClients(t *testing.T) {
  61. raw := usteerAPRaw{
  62. BSSID: "9c:9d:7e:1b:ea:b2",
  63. SSID: "temiscira",
  64. Freq: 2462,
  65. NAssoc: 2,
  66. Load: 39,
  67. }
  68. clients := map[string]usteerClientRaw{
  69. "40:44:f7:38:32:28": {Signal: -83},
  70. "d8:d6:68:32:7c:46": {Signal: -53},
  71. "00:00:00:00:00:00": {Signal: 0}, // se ignora
  72. }
  73. ap := buildUsteerAPWithClients("hostapd.phy1-ap0", raw, clients, "rt3", true)
  74. if len(ap.Clients) != 2 {
  75. t.Fatalf("esperaba 2 clientes, got %d", len(ap.Clients))
  76. }
  77. if ap.Clients[0].MAC != "40:44:F7:38:32:28" || ap.Clients[0].Signal != -83 {
  78. t.Errorf("client[0] = %+v", ap.Clients[0])
  79. }
  80. if ap.Clients[1].MAC != "D8:D6:68:32:7C:46" || ap.Clients[1].Signal != -53 {
  81. t.Errorf("client[1] = %+v", ap.Clients[1])
  82. }
  83. }
  84. // TestBuildUsteerAPVacio: entrada sin ssid/bssid se descarta en GetUsteer.
  85. func TestBuildUsteerAPVacio(t *testing.T) {
  86. ap := buildUsteerAP("hostapd.phy0-ap0", usteerAPRaw{}, "rt3", true)
  87. if ap.SSID != "" || ap.BSSID != "" {
  88. t.Errorf("AP vacío debería tener ssid/bssid vacío: %+v", ap)
  89. }
  90. if ap.Band != "2.4 GHz" {
  91. t.Errorf("freq 0 debería caer en 2.4 GHz: %q", ap.Band)
  92. }
  93. }