whoissdk/parse_common_unit_test.go

138 lines
4.9 KiB
Go
Raw Normal View History

2026-03-19 11:53:07 +08:00
package whois
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestCommonParserDateFlagsOnlyOnParseSuccess(t *testing.T) {
raw := strings.Join([]string{
"Domain Name: EXAMPLE.COM",
"Creation Date: invalid-date",
"Updated Date: 2026-03-17T12:34:56+08:00",
"Registry Expiry Date: 2027-03-17T12:34:56+08:00",
"Registrar: TEST-REG",
}, "\n")
got, err := commonParser("example.com", raw)
if err != nil {
t.Fatalf("commonParser returned error: %v", err)
}
if !got.Exists() {
t.Fatal("expected domain to exist")
}
if got.HasRegisterDate() {
t.Fatal("expected register date flag false when creation date parsing fails")
}
if !got.HasUpdateDate() {
t.Fatal("expected update date flag true")
}
if !got.HasExpireDate() {
t.Fatal("expected expire date flag true")
}
}
func TestParseCachedBinSamples(t *testing.T) {
tests := []struct {
name string
domain string
file string
wantExists bool
wantDomain string
minNS int
wantRegistrarContains string
}{
{name: "ee", domain: "nic.ee", file: "nic.ee.txt", wantExists: true, wantDomain: "nic.ee", minNS: 1},
{name: "fi", domain: "nic.fi", file: "nic.fi.txt", wantExists: true, wantDomain: "nic.fi", minNS: 2},
{name: "gg", domain: "nic.gg", file: "nic.gg.txt", wantExists: true, wantDomain: "nic.gg", minNS: 2},
{name: "je", domain: "nic.je", file: "nic.je.txt", wantExists: true, wantDomain: "nic.je", minNS: 2},
{name: "kg", domain: "nic.kg", file: "nic.kg.txt", wantExists: true, wantDomain: "nic.kg", minNS: 1},
{name: "kz", domain: "nic.kz", file: "nic.kz.txt", wantExists: true, wantDomain: "nic.kz", minNS: 2},
{name: "lu", domain: "nic.lu", file: "nic.lu.txt", wantExists: true, wantDomain: "nic.lu", minNS: 2},
{name: "md", domain: "nic.md", file: "nic.md.txt", wantExists: true, wantDomain: "nic.md", minNS: 2},
{name: "im", domain: "nic.im", file: "nic.im.txt", wantExists: true, wantDomain: "nic.im", wantRegistrarContains: "im registry"},
{name: "no", domain: "nic.no", file: "nic.no.txt", wantExists: true, wantDomain: "nic.no", wantRegistrarContains: "reg1-norid"},
{name: "sn", domain: "nic.sn", file: "nic.sn.txt", wantExists: true, wantDomain: "nic.sn"},
{name: "tn", domain: "nic.tn", file: "nic.tn.txt", wantExists: true, wantDomain: "nic.tn"},
{name: "uk", domain: "nic.uk", file: "nic.uk.txt", wantExists: true, wantDomain: "nic.uk", minNS: 4},
{name: "bn_empty", domain: "nic.bn", file: "nic.bn.txt", wantExists: false},
{name: "ch_not_found", domain: "nic.ch", file: "nic.ch.txt", wantExists: false},
{name: "es_access_denied", domain: "nic.es", file: "nic.es.txt", wantExists: false},
{name: "int_not_found", domain: "nic.int", file: "nic.int.txt", wantExists: false},
{name: "il_policy_only", domain: "nic.il", file: "nic.il.txt", wantExists: false},
{name: "kr_restricted", domain: "nic.kr", file: "nic.kr.txt", wantExists: false},
{name: "pf_not_found", domain: "nic.pf", file: "nic.pf.txt", wantExists: false},
{name: "tw_reserved", domain: "nic.tw", file: "nic.tw.txt", wantExists: false},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
raw := readBinRaw(t, tc.file)
got, err := parse(tc.domain, raw)
if err != nil {
t.Fatalf("parse returned error: %v", err)
}
if got.Exists() != tc.wantExists {
t.Fatalf("exists mismatch: got=%v want=%v", got.Exists(), tc.wantExists)
}
if tc.wantExists {
if strings.ToLower(got.Domain()) != tc.wantDomain {
t.Fatalf("domain mismatch: got=%q want=%q", got.Domain(), tc.wantDomain)
}
if tc.minNS > 0 && len(got.NsServers()) < tc.minNS {
t.Fatalf("ns count too small: got=%d want>=%d ns=%v", len(got.NsServers()), tc.minNS, got.NsServers())
}
if tc.wantRegistrarContains != "" && !strings.Contains(strings.ToLower(got.Registrar()), strings.ToLower(tc.wantRegistrarContains)) {
t.Fatalf("registrar mismatch: got=%q want contains %q", got.Registrar(), tc.wantRegistrarContains)
}
}
})
}
}
func readBinRaw(t *testing.T, filename string) string {
t.Helper()
path := filepath.Join("bin", filename)
b, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
return string(b)
}
func TestCommonParserPipelineWithCRLFSections(t *testing.T) {
raw := strings.Join([]string{
"Domain:",
" EXAMPLE.COM",
"Name servers:",
"",
" ns1.example.com",
" ns2.example.com",
"Status:",
" active",
"",
}, "\r\n")
got, err := commonParser("example.com", raw)
if err != nil {
t.Fatalf("commonParser returned error: %v", err)
}
if !got.Exists() {
t.Fatal("expected exists=true")
}
if strings.ToLower(got.Domain()) != "example.com" {
t.Fatalf("unexpected domain: %q", got.Domain())
}
if len(got.NsServers()) != 2 {
t.Fatalf("unexpected ns count: %d (%v)", len(got.NsServers()), got.NsServers())
}
if len(got.Status()) != 1 || got.Status()[0] != "active" {
t.Fatalf("unexpected status: %v", got.Status())
}
}