mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-25 22:45:46 +03:00
c9d2436d77
Merge in DNS/adguard-home from 2574-external-tests-3 to master Updates #2574. Squashed commit of the following: commit 29d429c65dee2621ca503710a7ba9522f14f55f9 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Feb 4 20:06:57 2021 +0300 all: finally fix spacing commit 9e3a3be63b74852a7802e3f1832648444b58e4d0 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Feb 4 19:59:09 2021 +0300 aghtest: polish spacing commit 8a984159fe813b95b989803f5b8b78d01a41bd39 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Feb 4 18:44:47 2021 +0300 all: fix linux tests, imp code quality commit 0c1b42bacba1b23fa847e1fa032579c525b3eaa1 Author: Eugene Burkov <e.burkov@adguard.com> Date: Thu Feb 4 17:33:12 2021 +0300 all: mv testutil to aghtest package, imp tests
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package aghtest
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"net"
|
|
"sync"
|
|
)
|
|
|
|
// TestResolver is a Resolver for tests.
|
|
type TestResolver struct {
|
|
counter int
|
|
counterLock sync.Mutex
|
|
}
|
|
|
|
// HostToIPs generates IPv4 and IPv6 from host.
|
|
//
|
|
// TODO(e.burkov): Replace with LookupIP after upgrading go to v1.15.
|
|
func (r *TestResolver) HostToIPs(host string) (ipv4, ipv6 net.IP) {
|
|
hash := sha256.Sum256([]byte(host))
|
|
|
|
return net.IP(hash[:4]), net.IP(hash[4:20])
|
|
}
|
|
|
|
// LookupIPAddr implements Resolver interface for *testResolver. It returns the
|
|
// slice of net.IPAddr with IPv4 and IPv6 instances.
|
|
func (r *TestResolver) LookupIPAddr(_ context.Context, host string) (ips []net.IPAddr, err error) {
|
|
ipv4, ipv6 := r.HostToIPs(host)
|
|
addrs := []net.IPAddr{{
|
|
IP: ipv4,
|
|
}, {
|
|
IP: ipv6,
|
|
}}
|
|
|
|
r.counterLock.Lock()
|
|
defer r.counterLock.Unlock()
|
|
r.counter++
|
|
|
|
return addrs, nil
|
|
}
|
|
|
|
// LookupHost implements Resolver interface for *testResolver. It returns the
|
|
// slice of IPv4 and IPv6 instances converted to strings.
|
|
func (r *TestResolver) LookupHost(host string) (addrs []string, err error) {
|
|
ipv4, ipv6 := r.HostToIPs(host)
|
|
|
|
r.counterLock.Lock()
|
|
defer r.counterLock.Unlock()
|
|
r.counter++
|
|
|
|
return []string{
|
|
ipv4.String(),
|
|
ipv6.String(),
|
|
}, nil
|
|
}
|
|
|
|
// Counter returns the number of requests handled.
|
|
func (r *TestResolver) Counter() int {
|
|
r.counterLock.Lock()
|
|
defer r.counterLock.Unlock()
|
|
|
|
return r.counter
|
|
}
|