package netinfo
import (
"strings"
"testing"
)
const sample = "1.0.0.0\t1.0.0.255\t13335\tUS\tCLOUDFLARENET\n" +
"1.0.1.0\t1.0.3.255\t0\tNone\tNot routed\n" +
"5.9.0.0\t5.9.255.255\t24940\tDE\tHETZNER-AS\n" +
"66.249.64.0\t66.249.95.255\t15169\tUS\tGOOGLE - Google LLC\n" +
"2a01:4f8::\t2a01:4f8:ffff:ffff:ffff:ffff:ffff:ffff\t24940\tDE\tHETZNER-AS\n"
func TestLookup(t *testing.T) {
tab, err := Parse(strings.NewReader(sample))
if err != nil {
t.Fatal(err)
}
for addr, want := range map[string]uint32{
"66.249.66.1": 15169,
"5.9.10.20": 24940,
"::ffff:5.9.10.20": 24940,
"2a01:4f8:c17:1234::1": 24940,
"1.0.2.1": 0, // unrouted
"9.9.9.9": 0, // not in the table
"not an address": 0,
} {
n, ok := tab.Lookup(addr)
if ok != (want != 0) || n.ASN != want {
t.Errorf("FAIL: %s → %+v %v, want AS%d", addr, n, ok, want)
}
}
if n, _ := tab.Lookup("66.249.66.1"); n.Name != "Google LLC" || n.Country != "US" {
t.Errorf("FAIL: Google reads %+v", n)
}
}
// A table with nothing in it is refused, so a truncated download never
// replaces a working one.
func TestAnEmptyTableIsRefused(t *testing.T) {
if _, err := Parse(strings.NewReader("")); err == nil {
t.Error("FAIL: an empty table parsed")
}
}