2019-09-18 16:44:27 +01:00
|
|
|
package home
|
|
|
|
|
|
|
|
import (
|
2021-01-26 16:44:19 +00:00
|
|
|
"context"
|
2021-04-22 11:38:24 +01:00
|
|
|
"io"
|
|
|
|
"net"
|
2019-09-18 16:44:27 +01:00
|
|
|
"testing"
|
2021-04-22 11:38:24 +01:00
|
|
|
"time"
|
2019-09-18 16:44:27 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-25 13:00:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-09-18 16:44:27 +01:00
|
|
|
)
|
|
|
|
|
2021-04-22 11:38:24 +01:00
|
|
|
// fakeConn is a mock implementation of net.Conn to simplify testing.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Search for other places in code where it may be used. Move
|
|
|
|
// into aghtest then.
|
|
|
|
type fakeConn struct {
|
|
|
|
// Conn is embedded here simply to make *fakeConn a net.Conn without
|
|
|
|
// actually implementing all methods.
|
|
|
|
net.Conn
|
|
|
|
data []byte
|
|
|
|
}
|
2021-03-25 13:00:27 +00:00
|
|
|
|
2021-04-22 11:38:24 +01:00
|
|
|
// Write implements net.Conn interface for *fakeConn. It always returns 0 and a
|
|
|
|
// nil error without mutating the slice.
|
|
|
|
func (c *fakeConn) Write(_ []byte) (n int, err error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2021-03-25 13:00:27 +00:00
|
|
|
|
2021-04-22 11:38:24 +01:00
|
|
|
// Read implements net.Conn interface for *fakeConn. It puts the content of
|
|
|
|
// c.data field into b up to the b's capacity.
|
|
|
|
func (c *fakeConn) Read(b []byte) (n int, err error) {
|
|
|
|
return copy(b, c.data), io.EOF
|
|
|
|
}
|
2021-03-25 13:00:27 +00:00
|
|
|
|
2021-04-22 11:38:24 +01:00
|
|
|
// Close implements net.Conn interface for *fakeConn. It always returns nil.
|
|
|
|
func (c *fakeConn) Close() (err error) {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-04 17:35:13 +00:00
|
|
|
|
2021-04-22 11:38:24 +01:00
|
|
|
// SetReadDeadline implements net.Conn interface for *fakeConn. It always
|
|
|
|
// returns nil.
|
|
|
|
func (c *fakeConn) SetReadDeadline(_ time.Time) (err error) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fakeDial is a mock implementation of customDialContext to simplify testing.
|
|
|
|
func (c *fakeConn) fakeDial(ctx context.Context, network, addr string) (conn net.Conn, err error) {
|
|
|
|
return c, nil
|
2020-05-27 12:54:31 +01:00
|
|
|
}
|
|
|
|
|
2021-06-18 16:13:36 +01:00
|
|
|
func TestWHOIS(t *testing.T) {
|
2021-04-22 11:38:24 +01:00
|
|
|
const (
|
|
|
|
nl = "\n"
|
|
|
|
data = `OrgName: FakeOrg LLC` + nl +
|
|
|
|
`City: Nonreal` + nl +
|
|
|
|
`Country: Imagiland` + nl
|
|
|
|
)
|
|
|
|
|
|
|
|
fc := &fakeConn{
|
|
|
|
data: []byte(data),
|
|
|
|
}
|
2020-05-27 12:54:31 +01:00
|
|
|
|
2021-06-18 16:13:36 +01:00
|
|
|
w := WHOIS{
|
2021-04-22 11:38:24 +01:00
|
|
|
timeoutMsec: 5000,
|
|
|
|
dialContext: fc.fakeDial,
|
|
|
|
}
|
|
|
|
resp, err := w.queryAll(context.Background(), "1.2.3.4")
|
2021-03-25 13:00:27 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-09-18 16:44:27 +01:00
|
|
|
m := whoisParse(resp)
|
2021-03-25 13:00:27 +00:00
|
|
|
require.NotEmpty(t, m)
|
|
|
|
|
2021-04-22 11:38:24 +01:00
|
|
|
assert.Equal(t, "FakeOrg LLC", m["orgname"])
|
|
|
|
assert.Equal(t, "Imagiland", m["country"])
|
|
|
|
assert.Equal(t, "Nonreal", m["city"])
|
2019-09-18 16:44:27 +01:00
|
|
|
}
|
2021-05-20 12:22:06 +01:00
|
|
|
|
2021-06-18 16:13:36 +01:00
|
|
|
func TestWHOISParse(t *testing.T) {
|
2021-05-20 12:22:06 +01:00
|
|
|
const (
|
|
|
|
city = "Nonreal"
|
|
|
|
country = "Imagiland"
|
|
|
|
orgname = "FakeOrgLLC"
|
|
|
|
whois = "whois.example.net"
|
|
|
|
)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
want strmap
|
|
|
|
name string
|
|
|
|
in string
|
|
|
|
}{{
|
|
|
|
want: strmap{},
|
|
|
|
name: "empty",
|
|
|
|
in: ``,
|
|
|
|
}, {
|
|
|
|
want: strmap{},
|
|
|
|
name: "comments",
|
|
|
|
in: "%\n#",
|
|
|
|
}, {
|
|
|
|
want: strmap{},
|
|
|
|
name: "no_colon",
|
|
|
|
in: "city",
|
|
|
|
}, {
|
|
|
|
want: strmap{},
|
|
|
|
name: "no_value",
|
|
|
|
in: "city:",
|
|
|
|
}, {
|
|
|
|
want: strmap{"city": city},
|
|
|
|
name: "city",
|
|
|
|
in: `city: ` + city,
|
|
|
|
}, {
|
|
|
|
want: strmap{"country": country},
|
|
|
|
name: "country",
|
|
|
|
in: `country: ` + country,
|
|
|
|
}, {
|
|
|
|
want: strmap{"orgname": orgname},
|
|
|
|
name: "orgname",
|
|
|
|
in: `orgname: ` + orgname,
|
|
|
|
}, {
|
|
|
|
want: strmap{"orgname": orgname},
|
|
|
|
name: "orgname_hyphen",
|
|
|
|
in: `org-name: ` + orgname,
|
|
|
|
}, {
|
|
|
|
want: strmap{"orgname": orgname},
|
|
|
|
name: "orgname_descr",
|
|
|
|
in: `descr: ` + orgname,
|
|
|
|
}, {
|
|
|
|
want: strmap{"orgname": orgname},
|
|
|
|
name: "orgname_netname",
|
|
|
|
in: `netname: ` + orgname,
|
|
|
|
}, {
|
|
|
|
want: strmap{"whois": whois},
|
|
|
|
name: "whois",
|
|
|
|
in: `whois: ` + whois,
|
|
|
|
}, {
|
|
|
|
want: strmap{"whois": whois},
|
|
|
|
name: "referralserver",
|
|
|
|
in: `referralserver: whois://` + whois,
|
|
|
|
}, {
|
|
|
|
want: strmap{},
|
|
|
|
name: "other",
|
|
|
|
in: `other: value`,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
got := whoisParse(tc.in)
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|