rtlconsole_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // rtlconsole_test.go — #639: sondeo HTTP de la consola RTLPlayground.
  2. // El test levanta un httptest que emula el firmware (login por POST /login
  3. // con Set-Cookie session, /information.json con sw_ver/hw_ver, /cmd time con
  4. // el contador en hex) y verifica fetch/snapshot/attach contra él.
  5. package httpapi
  6. import (
  7. "fmt"
  8. "net/http"
  9. "net/http/httptest"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/gnacho/netpulse/agent/probe"
  14. )
  15. // newRTLConsoleServer: emula la consola RTLPlayground. uptimeSec es el valor
  16. // que devuelve /cmd time (en segundos); el servidor lo convierte a "0x…".
  17. func newRTLConsoleServer(t *testing.T, uptimeSec uint64) *httptest.Server {
  18. t.Helper()
  19. mux := http.NewServeMux()
  20. mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
  21. if err := r.ParseForm(); err != nil || r.Form.Get("pwd") != "1234" {
  22. http.Error(w, "bad", http.StatusUnauthorized)
  23. return
  24. }
  25. w.Header().Set("Set-Cookie", "session=test-session-abc; SameSite=Strict")
  26. w.Header().Set("Location", "index.html")
  27. w.WriteHeader(http.StatusFound)
  28. })
  29. mux.HandleFunc("/information.json", func(w http.ResponseWriter, r *http.Request) {
  30. if !strings.Contains(r.Header.Get("Cookie"), "session=") {
  31. http.Error(w, "no auth", http.StatusUnauthorized)
  32. return
  33. }
  34. w.Header().Set("Content-Type", "application/json")
  35. fmt.Fprintf(w, `{"sw_ver":"v0.1.0-e1fa080-dirty","hw_ver":"keepLink KP-9000-9XHML-X V3.1","mac_address":"78:d8:00:32:31:49"}`)
  36. })
  37. mux.HandleFunc("/cmd", func(w http.ResponseWriter, r *http.Request) {
  38. if !strings.Contains(r.Header.Get("Cookie"), "session=") {
  39. http.Error(w, "no auth", http.StatusUnauthorized)
  40. return
  41. }
  42. // El firmware exige Content-Type en todo POST (sin él responde 404).
  43. if r.Header.Get("Content-Type") == "" {
  44. http.Error(w, "no content type", http.StatusNotFound)
  45. return
  46. }
  47. body := make([]byte, 32)
  48. n, _ := r.Body.Read(body)
  49. if strings.TrimSpace(string(body[:n])) != "time" {
  50. http.Error(w, "unknown command", http.StatusBadRequest)
  51. return
  52. }
  53. fmt.Fprintf(w, "0x%08x\n", uptimeSec)
  54. })
  55. return httptest.NewServer(mux)
  56. }
  57. func TestRtlConsoleFetch(t *testing.T) {
  58. srv := newRTLConsoleServer(t, 167581) // ~46.5 h
  59. defer srv.Close()
  60. host := strings.TrimPrefix(srv.URL, "http://")
  61. c := newRtlConsoleCache("1234", 300)
  62. e, err := c.fetch(host)
  63. if err != nil {
  64. t.Fatalf("fetch: %v", err)
  65. }
  66. if e.Firmware != "v0.1.0-e1fa080-dirty" {
  67. t.Fatalf("Firmware = %q, esperaba v0.1.0-e1fa080-dirty", e.Firmware)
  68. }
  69. if e.Model != "keepLink KP-9000-9XHML-X V3.1" {
  70. t.Fatalf("Model = %q", e.Model)
  71. }
  72. if e.MAC != "78:d8:00:32:31:49" {
  73. t.Fatalf("MAC = %q, esperaba 78:d8:00:32:31:49", e.MAC)
  74. }
  75. // bootUnix ≈ now - 167581s
  76. age := time.Since(e.BootUnix)
  77. if age < 167580*time.Second || age > 167582*time.Second {
  78. t.Fatalf("BootUnix no cuadra con uptime 167581s: age=%v", age)
  79. }
  80. }
  81. func TestRtlConsoleSnapshotAttachesSystem(t *testing.T) {
  82. srv := newRTLConsoleServer(t, 3600)
  83. defer srv.Close()
  84. host := strings.TrimPrefix(srv.URL, "http://")
  85. c := newRtlConsoleCache("1234", 300)
  86. // snapshot dispara el poll en background; esperamos a que haya datos.
  87. var e *rtlConsoleEntry
  88. deadline := time.Now().Add(3 * time.Second)
  89. for time.Now().Before(deadline) {
  90. e = c.snapshot("switch16", host)
  91. if e != nil {
  92. break
  93. }
  94. time.Sleep(50 * time.Millisecond)
  95. }
  96. if e == nil {
  97. t.Fatal("snapshot no produjo datos en 3 s")
  98. }
  99. // attach sobre un payload de beacon: System debe llevar board+uptime.
  100. s := &server{rtlConsole: c}
  101. pl := &probe.Payload{Data: probe.PayloadData{}}
  102. s.attachRTLConsole("switch16", host, pl)
  103. if pl.Data.System == nil {
  104. t.Fatal("attach no adjuntó System")
  105. }
  106. if pl.Data.System.Board == nil || pl.Data.System.Board.Release.Description != "RTLPlayground v0.1.0-e1fa080-dirty" {
  107. t.Fatalf("Board inesperado: %+v", pl.Data.System.Board)
  108. }
  109. // uptime derivado ≈ 1h (3600 s) ± margen del poll.
  110. if pl.Data.System.SysInfo == nil || pl.Data.System.SysInfo.Uptime < 3590 || pl.Data.System.SysInfo.Uptime > 3700 {
  111. t.Fatalf("Uptime inesperado: %+v", pl.Data.System.SysInfo)
  112. }
  113. // BridgeMAC normalizada a mayúsculas (formato canónico del resto de la app).
  114. if pl.Data.System.BridgeMAC != "78:D8:00:32:31:49" {
  115. t.Fatalf("BridgeMAC = %q, esperaba 78:D8:00:32:31:49", pl.Data.System.BridgeMAC)
  116. }
  117. }
  118. func TestRtlConsoleInvalidaTrasReboot(t *testing.T) {
  119. srv := newRTLConsoleServer(t, 3600)
  120. defer srv.Close()
  121. host := strings.TrimPrefix(srv.URL, "http://")
  122. c := newRtlConsoleCache("1234", 300)
  123. var e *rtlConsoleEntry
  124. deadline := time.Now().Add(3 * time.Second)
  125. for time.Now().Before(deadline) {
  126. e = c.snapshot("switch16", host)
  127. if e != nil {
  128. break
  129. }
  130. time.Sleep(50 * time.Millisecond)
  131. }
  132. if e == nil {
  133. t.Fatal("snapshot inicial sin datos")
  134. }
  135. c.invalidate("switch16")
  136. c.mu.Lock()
  137. polledAt := c.byID["switch16"].PolledAt
  138. c.mu.Unlock()
  139. if !polledAt.IsZero() {
  140. t.Fatalf("invalidate no reseteó PolledAt: %v", polledAt)
  141. }
  142. }
  143. func TestRtlConsoleFetchLoginFalloNoAdjunta(t *testing.T) {
  144. // El servidor rechaza el login (pass distinta): fetch devuelve error.
  145. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  146. w.WriteHeader(http.StatusUnauthorized)
  147. }))
  148. defer srv.Close()
  149. host := strings.TrimPrefix(srv.URL, "http://")
  150. c := newRtlConsoleCache("1234", 300)
  151. if _, err := c.fetch(host); err == nil {
  152. t.Fatal("fetch debería fallar con login 401")
  153. }
  154. }