2021-02-02 12:05:47 +01:00
|
|
|
package resolver_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCacheFailure(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
2022-05-16 10:46:53 +02:00
|
|
|
r := resolver.NewFakeResolverWithExplicitError(expected)
|
2021-02-02 12:05:47 +01:00
|
|
|
cache := &resolver.CacheResolver{Resolver: r}
|
|
|
|
addrs, err := cache.LookupHost(context.Background(), "www.google.com")
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected")
|
|
|
|
}
|
|
|
|
if addrs != nil {
|
|
|
|
t.Fatal("expected nil addrs here")
|
|
|
|
}
|
|
|
|
if cache.Get("www.google.com") != nil {
|
|
|
|
t.Fatal("expected empty cache here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheHitSuccess(t *testing.T) {
|
2022-05-16 10:46:53 +02:00
|
|
|
expected := errors.New("mocked error")
|
|
|
|
r := resolver.NewFakeResolverWithExplicitError(expected)
|
2021-02-02 12:05:47 +01:00
|
|
|
cache := &resolver.CacheResolver{Resolver: r}
|
|
|
|
cache.Set("dns.google.com", []string{"8.8.8.8"})
|
|
|
|
addrs, err := cache.LookupHost(context.Background(), "dns.google.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(addrs) != 1 || addrs[0] != "8.8.8.8" {
|
|
|
|
t.Fatal("not the result we expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheMissSuccess(t *testing.T) {
|
2022-05-16 10:46:53 +02:00
|
|
|
r := resolver.NewFakeResolverWithResult([]string{"8.8.8.8"})
|
2021-02-02 12:05:47 +01:00
|
|
|
cache := &resolver.CacheResolver{Resolver: r}
|
|
|
|
addrs, err := cache.LookupHost(context.Background(), "dns.google.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(addrs) != 1 || addrs[0] != "8.8.8.8" {
|
|
|
|
t.Fatal("not the result we expected")
|
|
|
|
}
|
|
|
|
if cache.Get("dns.google.com")[0] != "8.8.8.8" {
|
|
|
|
t.Fatal("expected full cache here")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCacheReadonlySuccess(t *testing.T) {
|
2022-05-16 10:46:53 +02:00
|
|
|
r := resolver.NewFakeResolverWithResult([]string{"8.8.8.8"})
|
2021-02-02 12:05:47 +01:00
|
|
|
cache := &resolver.CacheResolver{Resolver: r, ReadOnly: true}
|
|
|
|
addrs, err := cache.LookupHost(context.Background(), "dns.google.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(addrs) != 1 || addrs[0] != "8.8.8.8" {
|
|
|
|
t.Fatal("not the result we expected")
|
|
|
|
}
|
|
|
|
if cache.Get("dns.google.com") != nil {
|
|
|
|
t.Fatal("expected empty cache here")
|
|
|
|
}
|
|
|
|
}
|