store_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // store_test.go — consultas del agregado DNS (#2078).
  2. package dnsactivity
  3. import (
  4. "context"
  5. "testing"
  6. "time"
  7. )
  8. func TestTopForMACAggregatesAndOrders(t *testing.T) {
  9. d := testDB(t)
  10. st := NewStore(d)
  11. ctx := context.Background()
  12. hour := time.Date(2026, 11, 16, 10, 0, 0, 0, time.UTC)
  13. if err := st.Apply(ctx,
  14. []Bucket{
  15. // La ingesta guarda la MAC de device_seen (mayúsculas); la API
  16. // consulta con la de la ruta normalizada (minúsculas).
  17. {MAC: "AA:BB:CC:DD:EE:01", Domain: "a.example", Hour: hour, Queries: 5, Blocked: 1, FirstSeen: hour, LastSeen: hour.Add(time.Minute)},
  18. {MAC: "AA:BB:CC:DD:EE:01", Domain: "b.example", Hour: hour, Queries: 9, Blocked: 0, FirstSeen: hour, LastSeen: hour},
  19. {MAC: "AA:BB:CC:DD:EE:02", Domain: "c.example", Hour: hour, Queries: 50, Blocked: 0, FirstSeen: hour, LastSeen: hour},
  20. },
  21. nil); err != nil {
  22. t.Fatalf("Apply: %v", err)
  23. }
  24. from := hour.Add(-time.Hour)
  25. to := hour.Add(2 * time.Hour)
  26. top, err := st.TopForMAC(ctx, "aa:bb:cc:dd:ee:01", from, to, 10)
  27. if err != nil {
  28. t.Fatalf("TopForMAC: %v", err)
  29. }
  30. if len(top) != 2 {
  31. t.Fatalf("len = %d, want 2", len(top))
  32. }
  33. if top[0].Domain != "b.example" || top[0].Queries != 9 {
  34. t.Errorf("primero = %+v, want b.example con 9", top[0])
  35. }
  36. if top[1].Domain != "a.example" || top[1].Blocked != 1 {
  37. t.Errorf("segundo = %+v", top[1])
  38. }
  39. q, b, err := st.TotalsForMAC(ctx, "aa:bb:cc:dd:ee:01", from, to)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if q != 14 || b != 1 {
  44. t.Errorf("totales = %d/%d, want 14/1", q, b)
  45. }
  46. }
  47. func TestTopUsesWindow(t *testing.T) {
  48. d := testDB(t)
  49. st := NewStore(d)
  50. ctx := context.Background()
  51. now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC)
  52. if err := st.Apply(ctx, []Bucket{
  53. {MAC: "m1", Domain: "old.example", Hour: now.Add(-30 * time.Hour), Queries: 99, FirstSeen: now, LastSeen: now},
  54. {MAC: "m1", Domain: "new.example", Hour: now.Add(-time.Hour), Queries: 3, FirstSeen: now, LastSeen: now},
  55. }, nil); err != nil {
  56. t.Fatal(err)
  57. }
  58. top, err := st.TopAll(ctx, now.Add(-24*time.Hour), now, 10)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if len(top) != 1 || top[0].Domain != "new.example" {
  63. t.Errorf("top = %+v, la ventana de 24h no debe incluir 30h atrás", top)
  64. }
  65. }
  66. func TestNewDomains(t *testing.T) {
  67. d := testDB(t)
  68. st := NewStore(d)
  69. ctx := context.Background()
  70. now := time.Date(2026, 11, 16, 12, 0, 0, 0, time.UTC)
  71. if err := st.Apply(ctx, nil, []FirstSeen{
  72. {Domain: "new.example", MAC: "m1", FirstSeenMS: now.Add(-time.Minute).UnixMilli()},
  73. {Domain: "old.example", MAC: "m2", FirstSeenMS: now.Add(-72 * time.Hour).UnixMilli()},
  74. }); err != nil {
  75. t.Fatal(err)
  76. }
  77. // Repetir no debe duplicar ni mover la fecha.
  78. if err := st.Apply(ctx, nil, []FirstSeen{
  79. {Domain: "new.example", MAC: "m3", FirstSeenMS: now.UnixMilli()},
  80. }); err != nil {
  81. t.Fatal(err)
  82. }
  83. got, err := st.NewDomains(ctx, now.Add(-24*time.Hour), now, 10)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. if len(got) != 1 || got[0].Domain != "new.example" || got[0].MAC != "m1" {
  88. t.Errorf("new domains = %+v", got)
  89. }
  90. }
  91. func TestApplyIsAdditive(t *testing.T) {
  92. d := testDB(t)
  93. st := NewStore(d)
  94. ctx := context.Background()
  95. hour := time.Date(2026, 11, 16, 10, 0, 0, 0, time.UTC)
  96. b := Bucket{MAC: "m1", Domain: "a.example", Hour: hour, Queries: 2, Blocked: 1,
  97. FirstSeen: hour.Add(time.Minute), LastSeen: hour.Add(2 * time.Minute)}
  98. if err := st.Apply(ctx, []Bucket{b}, nil); err != nil {
  99. t.Fatal(err)
  100. }
  101. b.Queries = 3
  102. b.Blocked = 0
  103. b.FirstSeen = hour
  104. b.LastSeen = hour.Add(5 * time.Minute)
  105. if err := st.Apply(ctx, []Bucket{b}, nil); err != nil {
  106. t.Fatal(err)
  107. }
  108. var queries, blocked, first, last int64
  109. if err := d.QueryRow(`SELECT queries, blocked, first_seen, last_seen FROM dns_activity
  110. WHERE mac='m1' AND domain='a.example'`).Scan(&queries, &blocked, &first, &last); err != nil {
  111. t.Fatal(err)
  112. }
  113. if queries != 5 || blocked != 1 {
  114. t.Errorf("queries/blocked = %d/%d, want 5/1", queries, blocked)
  115. }
  116. if first != hour.UnixMilli() || last != hour.Add(5*time.Minute).UnixMilli() {
  117. t.Errorf("first/last = %d/%d", first, last)
  118. }
  119. }
  120. func TestResolveMACsCanonicalizes(t *testing.T) {
  121. d := testDB(t)
  122. if _, err := d.Exec(`INSERT INTO device_seen (mac, first_seen, last_seen, ip) VALUES
  123. ('m1', 0, 0, ' 192.168.1.42 ')`); err != nil {
  124. t.Fatal(err)
  125. }
  126. macs, err := resolveMACs(context.Background(), d)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. if macs["192.168.1.42"] != "m1" {
  131. t.Errorf("mapa = %+v", macs)
  132. }
  133. }