mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-25 16:18:16 +03:00
dead10e033
Updates #6006.
Squashed commit of the following:
commit c72d6375e9c472c73b0bb9d025a8e197f404ba38
Merge: 02d64b10e 0cd441f04
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Tue Jul 18 13:56:26 2023 +0300
Merge branch 'master' into 6006-client-processor
commit 02d64b10e19b2e937e45cab58d2310231a19bfbc
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Jul 17 19:42:07 2023 +0300
client: imp code, tests
commit b1613463089b4dde97484ff6a44b05888f0c2276
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Jul 17 18:42:19 2023 +0300
client: imp code, docs, tests
commit f71a17983b70d79839cf35dbe3279f0fdcac2ed7
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Jul 14 21:53:47 2023 +0300
all: add new client processor; imp code
149 lines
2.8 KiB
Go
149 lines
2.8 KiB
Go
package whois_test
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net"
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
|
"github.com/AdguardTeam/golibs/testutil/fakenet"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestDefault_Process(t *testing.T) {
|
|
const (
|
|
nl = "\n"
|
|
city = "Nonreal"
|
|
country = "Imagiland"
|
|
orgname = "FakeOrgLLC"
|
|
referralserver = "whois.example.net"
|
|
)
|
|
|
|
ip := netip.MustParseAddr("1.2.3.4")
|
|
|
|
testCases := []struct {
|
|
want *whois.Info
|
|
name string
|
|
data string
|
|
}{{
|
|
want: nil,
|
|
name: "empty",
|
|
data: "",
|
|
}, {
|
|
want: nil,
|
|
name: "comments",
|
|
data: "%\n#",
|
|
}, {
|
|
want: nil,
|
|
name: "no_colon",
|
|
data: "city",
|
|
}, {
|
|
want: nil,
|
|
name: "no_value",
|
|
data: "city:",
|
|
}, {
|
|
want: &whois.Info{
|
|
City: city,
|
|
},
|
|
name: "city",
|
|
data: "city: " + city,
|
|
}, {
|
|
want: &whois.Info{
|
|
Country: country,
|
|
},
|
|
name: "country",
|
|
data: "country: " + country,
|
|
}, {
|
|
want: &whois.Info{
|
|
Orgname: orgname,
|
|
},
|
|
name: "orgname",
|
|
data: "orgname: " + orgname,
|
|
}, {
|
|
want: &whois.Info{
|
|
Orgname: orgname,
|
|
},
|
|
name: "orgname_hyphen",
|
|
data: "org-name: " + orgname,
|
|
}, {
|
|
want: &whois.Info{
|
|
Orgname: orgname,
|
|
},
|
|
name: "orgname_descr",
|
|
data: "descr: " + orgname,
|
|
}, {
|
|
want: &whois.Info{
|
|
Orgname: orgname,
|
|
},
|
|
name: "orgname_netname",
|
|
data: "netname: " + orgname,
|
|
}, {
|
|
want: &whois.Info{
|
|
City: city,
|
|
Country: country,
|
|
Orgname: orgname,
|
|
},
|
|
name: "full",
|
|
data: "OrgName: " + orgname + nl + "City: " + city + nl + "Country: " + country,
|
|
}, {
|
|
want: nil,
|
|
name: "whois",
|
|
data: "whois: " + referralserver,
|
|
}, {
|
|
want: nil,
|
|
name: "referralserver",
|
|
data: "referralserver: whois://" + referralserver,
|
|
}, {
|
|
want: nil,
|
|
name: "other",
|
|
data: "other: value",
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
hit := 0
|
|
|
|
fakeConn := &fakenet.Conn{
|
|
OnRead: func(b []byte) (n int, err error) {
|
|
hit++
|
|
|
|
return copy(b, tc.data), io.EOF
|
|
},
|
|
OnWrite: func(b []byte) (n int, err error) { return len(b), nil },
|
|
OnClose: func() (err error) { return nil },
|
|
OnSetDeadline: func(t time.Time) (err error) { return nil },
|
|
}
|
|
|
|
w := whois.New(&whois.Config{
|
|
Timeout: 5 * time.Second,
|
|
DialContext: func(_ context.Context, _, _ string) (_ net.Conn, _ error) {
|
|
hit = 0
|
|
|
|
return fakeConn, nil
|
|
},
|
|
MaxConnReadSize: 1024,
|
|
MaxRedirects: 3,
|
|
MaxInfoLen: 250,
|
|
CacheSize: 100,
|
|
CacheTTL: time.Hour,
|
|
})
|
|
|
|
got, changed := w.Process(context.Background(), ip)
|
|
require.True(t, changed)
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
assert.Equal(t, 1, hit)
|
|
|
|
// From cache.
|
|
got, changed = w.Process(context.Background(), ip)
|
|
require.False(t, changed)
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
assert.Equal(t, 1, hit)
|
|
})
|
|
}
|
|
}
|