adguard_querylog_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // adguard_querylog_test.go — parseo y lectura del query log (#2078).
  2. package adapters
  3. import (
  4. "context"
  5. "encoding/json"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "time"
  10. )
  11. const sampleQueryLog = `{
  12. "oldest": "2026-11-16T10:00:00Z",
  13. "data": [
  14. {
  15. "time": "2026-11-16T10:59:58.123Z",
  16. "client": "192.168.1.42",
  17. "client_proto": "",
  18. "client_info": {"name": "laptop"},
  19. "question": {"name": "YouTube.COM.", "type": "A"},
  20. "status": "NOERROR",
  21. "reason": "NotFilteredNotFound",
  22. "rules": [],
  23. "answer": [{"type": "A", "value": "142.250.1.1"}]
  24. },
  25. {
  26. "time": "2026-11-16T10:59:59Z",
  27. "client": "192.168.1.43",
  28. "client_proto": "doh",
  29. "client_info": {"name": "phone"},
  30. "question": {"name": "ads.example.com", "type": "AAAA"},
  31. "status": "NOERROR",
  32. "reason": "FilteredBlackList",
  33. "rules": [{"text": "||ads.example.com^"}]
  34. },
  35. {
  36. "time": "2026-11-16T10:59:00Z",
  37. "client": "192.168.1.44",
  38. "question": {"name": "", "type": "A"},
  39. "status": "NOERROR",
  40. "reason": "NotFilteredNotFound"
  41. }
  42. ]
  43. }`
  44. func TestParseQueryLogNormalizesAndFilters(t *testing.T) {
  45. page, err := parseQueryLogJSON([]byte(sampleQueryLog))
  46. if err != nil {
  47. t.Fatalf("parse: %v", err)
  48. }
  49. if len(page.Queries) != 2 {
  50. t.Fatalf("queries = %d, want 2 (la entrada sin dominio se descarta)", len(page.Queries))
  51. }
  52. q := page.Queries[0]
  53. if q.Domain != "youtube.com" {
  54. t.Errorf("domain = %q, want youtube.com", q.Domain)
  55. }
  56. if q.BlockedByFilter() {
  57. t.Error("NotFilteredNotFound no es bloqueo")
  58. }
  59. if q.ClientName != "laptop" || q.QType != "A" {
  60. t.Errorf("cliente/tipo = %q/%q", q.ClientName, q.QType)
  61. }
  62. if !page.Queries[1].BlockedByFilter() {
  63. t.Error("FilteredBlackList debe contar como bloqueo")
  64. }
  65. if page.Queries[1].Rule != "||ads.example.com^" {
  66. t.Errorf("rule = %q", page.Queries[1].Rule)
  67. }
  68. if page.Queries[1].Proto != "doh" {
  69. t.Errorf("proto = %q, want doh", page.Queries[1].Proto)
  70. }
  71. if !page.Oldest.Equal(time.Date(2026, 11, 16, 10, 0, 0, 0, time.UTC)) {
  72. t.Errorf("oldest = %v", page.Oldest)
  73. }
  74. }
  75. func TestBlockedByFilterReasons(t *testing.T) {
  76. cases := map[string]bool{
  77. "FilteredBlackList": true,
  78. "FilteredSafeBrowsing": true,
  79. "FilteredParental": true,
  80. "FilteredBlockedService": true,
  81. "FilteredSafeSearch": false, // reescribe, no bloquea
  82. "Rewrite": false,
  83. "NotFilteredNotFound": false,
  84. "": false,
  85. }
  86. for reason, want := range cases {
  87. if got := (DNSQuery{Reason: reason}).BlockedByFilter(); got != want {
  88. t.Errorf("reason %q → %v, want %v", reason, got, want)
  89. }
  90. }
  91. }
  92. func TestQueryLogHTTPUsesOlderThanAndLimit(t *testing.T) {
  93. var gotPath, gotOlder, gotLimit string
  94. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  95. gotPath = r.URL.Path
  96. gotOlder = r.URL.Query().Get("older_than")
  97. gotLimit = r.URL.Query().Get("limit")
  98. w.Header().Set("Content-Type", "application/json")
  99. _, _ = w.Write([]byte(sampleQueryLog))
  100. }))
  101. defer srv.Close()
  102. c := NewAdGuardClient(srv.URL, "u", "p")
  103. page, err := c.QueryLog(context.Background(), "2026-11-16T09:00:00Z", 500)
  104. if err != nil {
  105. t.Fatalf("QueryLog: %v", err)
  106. }
  107. if gotPath != "/control/querylog" {
  108. t.Errorf("path = %q", gotPath)
  109. }
  110. if gotOlder != "2026-11-16T09:00:00Z" {
  111. t.Errorf("older_than = %q", gotOlder)
  112. }
  113. if gotLimit != "500" {
  114. t.Errorf("limit = %q", gotLimit)
  115. }
  116. if len(page.Queries) != 2 {
  117. t.Errorf("queries = %d", len(page.Queries))
  118. }
  119. }
  120. func TestQueryLogConfigParses(t *testing.T) {
  121. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  122. if r.URL.Path != "/control/querylog/config" {
  123. t.Errorf("path = %q", r.URL.Path)
  124. }
  125. _ = json.NewEncoder(w).Encode(map[string]any{
  126. "enabled": true, "anonymize_client_ip": true, "interval": 86400000,
  127. })
  128. }))
  129. defer srv.Close()
  130. c := NewAdGuardClient(srv.URL, "u", "p")
  131. cfg, err := c.QueryLogConfig(context.Background())
  132. if err != nil {
  133. t.Fatalf("QueryLogConfig: %v", err)
  134. }
  135. if !cfg.Enabled || !cfg.AnonymizeClientIP {
  136. t.Errorf("cfg = %+v", cfg)
  137. }
  138. if cfg.IntervalMS != 86400000 {
  139. t.Errorf("interval = %v", cfg.IntervalMS)
  140. }
  141. }