mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 04:55:33 +03:00
93ab0fde23
Squashed commit of the following:
commit 402ac873839fcf4dfb762872497b740bd55b2dcc
Merge: 209e1b171 af38476ef
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Sep 21 16:46:47 2023 +0300
Merge branch 'master' into AG-23168-imp-updater-tests
commit 209e1b171cb4c55d177ef5a4198cf24c2bc41195
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Sep 21 14:09:43 2023 +0300
updater: fix windows build
commit d112ca53540e0c999fe126442e8c526ff7e33bf5
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Sep 20 18:57:54 2023 +0300
all: imp tests, docs
commit f940724f81a90af6c07de523c8fcce66240dbec9
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Wed Sep 20 16:07:48 2023 +0300
all: imp tests
commit 8915818e81360a7f6ff5b652a983ca034ad6d0c4
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Tue Sep 19 19:33:20 2023 +0300
all: add test
commit c1cb8df2b056cefc5d7f1b32c089cd0ede0af9d2
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Sep 14 19:12:47 2023 +0300
updater: imp tests
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
// Package aghtest contains utilities for testing.
|
|
package aghtest
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/netip"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
// ReqHost is the common request host for filtering tests.
|
|
ReqHost = "www.host.example"
|
|
|
|
// ReqFQDN is the common request FQDN for filtering tests.
|
|
ReqFQDN = ReqHost + "."
|
|
)
|
|
|
|
// ReplaceLogWriter moves logger output to w and uses Cleanup method of t to
|
|
// revert changes.
|
|
func ReplaceLogWriter(t testing.TB, w io.Writer) {
|
|
t.Helper()
|
|
|
|
prev := log.Writer()
|
|
t.Cleanup(func() { log.SetOutput(prev) })
|
|
log.SetOutput(w)
|
|
}
|
|
|
|
// ReplaceLogLevel sets logging level to l and uses Cleanup method of t to
|
|
// revert changes.
|
|
func ReplaceLogLevel(t testing.TB, l log.Level) {
|
|
t.Helper()
|
|
|
|
switch l {
|
|
case log.INFO, log.DEBUG, log.ERROR:
|
|
// Go on.
|
|
default:
|
|
t.Fatalf("wrong l value (must be one of %v, %v, %v)", log.INFO, log.DEBUG, log.ERROR)
|
|
}
|
|
|
|
prev := log.GetLevel()
|
|
t.Cleanup(func() { log.SetLevel(prev) })
|
|
log.SetLevel(l)
|
|
}
|
|
|
|
// HostToIPs is a helper that generates one IPv4 and one IPv6 address from host.
|
|
func HostToIPs(host string) (ipv4, ipv6 netip.Addr) {
|
|
hash := sha256.Sum256([]byte(host))
|
|
|
|
return netip.AddrFrom4([4]byte(hash[:4])), netip.AddrFrom16([16]byte(hash[4:20]))
|
|
}
|
|
|
|
// StartHTTPServer is a helper that starts the HTTP server, which is configured
|
|
// to return data on every request, and returns the client and server URL.
|
|
func StartHTTPServer(t testing.TB, data []byte) (c *http.Client, u *url.URL) {
|
|
t.Helper()
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = w.Write(data)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
u, err := url.Parse(srv.URL)
|
|
require.NoError(t, err)
|
|
|
|
return srv.Client(), u
|
|
}
|