live_kick_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package adapters
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "testing"
  7. "time"
  8. )
  9. type fakeKickRunner struct {
  10. calls []struct{ host, cmd string }
  11. responses map[string]string
  12. errors map[string]error
  13. }
  14. func (f *fakeKickRunner) Run(host, cmd string, timeout time.Duration) (string, error) {
  15. f.calls = append(f.calls, struct{ host, cmd string }{host, cmd})
  16. key := host + "|" + cmd
  17. if e, ok := f.errors[key]; ok {
  18. return "", e
  19. }
  20. return f.responses[key], nil
  21. }
  22. func TestKickUsteerClient_Success(t *testing.T) {
  23. runner := &fakeKickRunner{
  24. responses: map[string]string{
  25. "192.168.1.2|ubus call usteer connected_clients": `{"hostapd.phy1-ap0":{"38:c6:ce:2f:06:5a":{"signal":-50}}}`,
  26. fmt.Sprintf("192.168.1.2|ubus call hostapd.phy1-ap0 del_client '%s'", `{"addr":"38:C6:CE:2F:06:5A","reason":5,"deauth":true,"ban_time":120000}`): "",
  27. },
  28. }
  29. routers := []RouterConfig{{ID: "rt2", Name: "rt2", Host: "192.168.1.2"}}
  30. if err := kickUsteerClient("38:C6:CE:2F:06:5A", routers, runner); err != nil {
  31. t.Fatalf("unexpected error: %v", err)
  32. }
  33. if len(runner.calls) != 2 {
  34. t.Fatalf("expected 2 calls, got %d", len(runner.calls))
  35. }
  36. }
  37. func TestKickUsteerClient_ClientNotFound(t *testing.T) {
  38. runner := &fakeKickRunner{
  39. responses: map[string]string{
  40. "192.168.1.2|ubus call usteer connected_clients": `{"hostapd.phy1-ap0":{}}`,
  41. },
  42. }
  43. routers := []RouterConfig{{ID: "rt2", Name: "rt2", Host: "192.168.1.2"}}
  44. err := kickUsteerClient("38:C6:CE:2F:06:5A", routers, runner)
  45. if err == nil || err.Error() != "client not found" {
  46. t.Fatalf("expected 'client not found', got: %v", err)
  47. }
  48. }
  49. func TestKickUsteerClient_ContinuesAfterFailure(t *testing.T) {
  50. runner := &fakeKickRunner{
  51. responses: map[string]string{
  52. "192.168.1.2|ubus call usteer connected_clients": `{"hostapd.phy1-ap0":{"38:c6:ce:2f:06:5a":{"signal":-50}}}`,
  53. "192.168.1.3|ubus call usteer connected_clients": `{"hostapd.phy1-ap0":{"38:c6:ce:2f:06:5a":{"signal":-60}}}`,
  54. fmt.Sprintf("192.168.1.3|ubus call hostapd.phy1-ap0 del_client '%s'", `{"addr":"38:C6:CE:2F:06:5A","reason":5,"deauth":true,"ban_time":120000}`): "",
  55. },
  56. errors: map[string]error{
  57. fmt.Sprintf("192.168.1.2|ubus call hostapd.phy1-ap0 del_client '%s'", `{"addr":"38:C6:CE:2F:06:5A","reason":5,"deauth":true,"ban_time":120000}`): errors.New("boom"),
  58. },
  59. }
  60. routers := []RouterConfig{
  61. {ID: "rt2", Name: "rt2", Host: "192.168.1.2"},
  62. {ID: "rt3", Name: "rt3", Host: "192.168.1.3"},
  63. }
  64. if err := kickUsteerClient("38:C6:CE:2F:06:5A", routers, runner); err != nil {
  65. t.Fatalf("unexpected error: %v", err)
  66. }
  67. if len(runner.calls) != 4 {
  68. t.Fatalf("expected 4 calls, got %d", len(runner.calls))
  69. }
  70. }
  71. func TestKickUsteerClient_AllRoutersFail(t *testing.T) {
  72. runner := &fakeKickRunner{
  73. responses: map[string]string{
  74. "192.168.1.2|ubus call usteer connected_clients": `{"hostapd.phy1-ap0":{"38:c6:ce:2f:06:5a":{"signal":-50}}}`,
  75. },
  76. errors: map[string]error{
  77. fmt.Sprintf("192.168.1.2|ubus call hostapd.phy1-ap0 del_client '%s'", `{"addr":"38:C6:CE:2F:06:5A","reason":5,"deauth":true,"ban_time":120000}`): errors.New("boom"),
  78. },
  79. }
  80. routers := []RouterConfig{{ID: "rt2", Name: "rt2", Host: "192.168.1.2"}}
  81. err := kickUsteerClient("38:C6:CE:2F:06:5A", routers, runner)
  82. if err == nil {
  83. t.Fatalf("expected error")
  84. }
  85. want := "could not disconnect from rt2"
  86. if err.Error() != want {
  87. t.Fatalf("expected %q, got %q", want, err.Error())
  88. }
  89. }
  90. func TestKickUsteerClient_ConnectedClientsFails(t *testing.T) {
  91. runner := &fakeKickRunner{
  92. responses: map[string]string{
  93. "192.168.1.3|ubus call usteer connected_clients": `{"hostapd.phy1-ap0":{"38:c6:ce:2f:06:5a":{"signal":-50}}}`,
  94. fmt.Sprintf("192.168.1.3|ubus call hostapd.phy1-ap0 del_client '%s'", `{"addr":"38:C6:CE:2F:06:5A","reason":5,"deauth":true,"ban_time":120000}`): "",
  95. },
  96. errors: map[string]error{
  97. "192.168.1.2|ubus call usteer connected_clients": errors.New("ssh timeout"),
  98. },
  99. }
  100. routers := []RouterConfig{
  101. {ID: "rt2", Name: "rt2", Host: "192.168.1.2"},
  102. {ID: "rt3", Name: "rt3", Host: "192.168.1.3"},
  103. }
  104. if err := kickUsteerClient("38:C6:CE:2F:06:5A", routers, runner); err != nil {
  105. t.Fatalf("unexpected error: %v", err)
  106. }
  107. if len(runner.calls) != 3 {
  108. t.Fatalf("expected 3 calls, got %d", len(runner.calls))
  109. }
  110. }
  111. func TestKickUsteerClient_HyphenatedAndBareMAC(t *testing.T) {
  112. t.Run("MAC con guiones encuentra al cliente conectado", func(t *testing.T) {
  113. runner := &fakeKickRunner{
  114. responses: map[string]string{
  115. "192.0.2.2|ubus call usteer connected_clients": `{"hostapd.phy1-ap0":{"02:00:00:00:00:aa":{"signal":-52}}}`,
  116. fmt.Sprintf("192.0.2.2|ubus call hostapd.phy1-ap0 del_client '%s'", `{"addr":"02:00:00:00:00:AA","reason":5,"deauth":true,"ban_time":120000}`): "",
  117. },
  118. }
  119. routers := []RouterConfig{{ID: "ap1", Name: "ap1", Host: "192.0.2.2"}}
  120. if err := kickUsteerClient("02-00-00-00-00-aa", routers, runner); err != nil {
  121. t.Fatalf("expulsar MAC con guiones falló: %v", err)
  122. }
  123. })
  124. t.Run("MAC de 12 hex sin separador encuentra al cliente conectado", func(t *testing.T) {
  125. runner := &fakeKickRunner{
  126. responses: map[string]string{
  127. "192.0.2.2|ubus call usteer connected_clients": `{"hostapd.phy1-ap0":{"02:00:00:00:00:aa":{"signal":-52}}}`,
  128. fmt.Sprintf("192.0.2.2|ubus call hostapd.phy1-ap0 del_client '%s'", `{"addr":"02:00:00:00:00:AA","reason":5,"deauth":true,"ban_time":120000}`): "",
  129. },
  130. }
  131. routers := []RouterConfig{{ID: "ap1", Name: "ap1", Host: "192.0.2.2"}}
  132. if err := kickUsteerClient("0200000000aa", routers, runner); err != nil {
  133. t.Fatalf("expulsar MAC de 12 hex sin separador falló: %v", err)
  134. }
  135. })
  136. t.Run("KickUsteerClient acepta MAC de 12 hex sin separador", func(t *testing.T) {
  137. l := &Live{}
  138. err := l.KickUsteerClient(context.Background(), "0200000000aa")
  139. if err != nil && err.Error() == "invalid MAC" {
  140. t.Fatalf("KickUsteerClient rechazó MAC de 12 hex sin separador con %q", err)
  141. }
  142. })
  143. }