whoissdk/parse_test.go

249 lines
8.7 KiB
Go
Raw Permalink Normal View History

2024-08-15 08:38:28 +08:00
package whois
import (
"fmt"
2024-08-16 21:30:27 +08:00
"os"
2024-08-15 08:38:28 +08:00
"testing"
)
2026-03-19 11:53:07 +08:00
func requireIntegration(t *testing.T) {
t.Helper()
if os.Getenv("WHOIS_INTEGRATION") != "1" {
t.Skip("set WHOIS_INTEGRATION=1 to run network whois integration tests")
}
}
2024-08-16 21:30:27 +08:00
func TestWhoisInfo(t *testing.T) {
2026-03-19 11:53:07 +08:00
requireIntegration(t)
2024-08-16 21:30:27 +08:00
c := NewClient()
2026-03-19 11:53:07 +08:00
domain := "ra.com"
2024-08-16 21:30:27 +08:00
h, err := c.Whois(domain)
if err != nil {
t.Fatal(err)
}
os.WriteFile("./bin/"+domain+".txt", []byte(h.RawData()), 0644)
fmt.Println("Domain:", h.Domain())
fmt.Println("Exists:", h.Exists())
fmt.Println("RegistrarDate:", h.RegisterDate())
fmt.Println("ExpireDate:", h.ExpireDate())
fmt.Println("UpdateDate:", h.UpdateDate())
fmt.Println("Status:", h.Status())
fmt.Println("NsServers:", h.NsServers())
fmt.Println("NsIp:", h.NsIps())
fmt.Println("Dnssec:", h.Dnssec())
fmt.Println("WhoisSer:", h.WhoisSer())
fmt.Println("IanaID:", h.IanaID())
fmt.Println("======RegisterInfo=======")
fmt.Println("Name:", h.RegisterInfo().Name)
fmt.Println("Organization:", h.RegisterInfo().Org)
fmt.Println("Addr:", h.RegisterInfo().Addr)
fmt.Println("State:", h.RegisterInfo().State)
fmt.Println("Country:", h.RegisterInfo().Country)
fmt.Println("Email:", h.RegisterInfo().Email)
fmt.Println("Phone:", h.RegisterInfo().Phone)
fmt.Println("Fax:", h.RegisterInfo().Fax)
fmt.Println("======AdminInfo=======")
fmt.Println("Name:", h.AdminInfo().Name)
fmt.Println("Organization:", h.AdminInfo().Org)
fmt.Println("Addr:", h.AdminInfo().Addr)
fmt.Println("State:", h.AdminInfo().State)
fmt.Println("Country:", h.AdminInfo().Country)
fmt.Println("Email:", h.AdminInfo().Email)
fmt.Println("Phone:", h.AdminInfo().Phone)
fmt.Println("Fax:", h.AdminInfo().Fax)
fmt.Println("======TechInfo=======")
fmt.Println("Name:", h.TechInfo().Name)
fmt.Println("Organization:", h.TechInfo().Org)
fmt.Println("Addr:", h.TechInfo().Addr)
fmt.Println("State:", h.TechInfo().State)
fmt.Println("Country:", h.TechInfo().Country)
fmt.Println("Email:", h.TechInfo().Email)
fmt.Println("Phone:", h.TechInfo().Phone)
fmt.Println("Fax:", h.TechInfo().Fax)
fmt.Println("====================")
fmt.Println("\nRaw:", h.RawData())
}
2024-08-15 08:38:28 +08:00
func TestWhois(t *testing.T) {
2026-03-19 11:53:07 +08:00
requireIntegration(t)
2024-08-16 21:30:27 +08:00
os.MkdirAll("./bin", 0755)
domainSuffix := []string{"com", "net", "org", "cn", "io", "me", "cc", "top", "xyz", "vip", "club", "site", "win", "bid",
"loan", "ek", "kim", "ren", "ltd", "link", "red", "pro", "info", "mobi", "name", "tv", "ws", "asia",
"biz", "gov", "edu", "mil", "int", "aero", "coop", "museum", "jobs", "travel", "xxx",
"ac", "ad", "ae", "af", "ag", "ai", "al", "am", "an", "ao", "aq", "ar", "as", "at", "au", "aw", "az",
"ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz",
"ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cx", "cy", "cz",
"de", "dj", "dk", "dm", "do", "dz",
"ec", "ee", "eg", "eh", "er", "es", "et", "eu",
"fi", "fj", "fk", "fm", "fo", "fr",
"ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy",
"hk", "hm", "hn", "hr", "ht", "hu",
"id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it",
"je", "jm", "jo", "jp",
"ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz",
"la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly",
"ma", "mc", "md", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz",
"na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz",
"om",
"pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py",
"qa",
"re", "ro", "ru", "rw",
"sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "sv", "sy", "sz",
"tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tp", "tr", "tt", "tv", "tw", "tz",
"ua", "ug", "uk", "us", "uy", "uz",
"va", "vc", "ve", "vg", "vi", "vn", "vu",
"wf", "ws",
"ye", "yt", "yu",
"za", "zm", "zw"}
prefix := "nic."
2024-08-15 08:38:28 +08:00
c := NewClient()
2024-08-16 21:30:27 +08:00
for idx, suffix := range domainSuffix {
if idx < 50 {
continue
}
domain := prefix + suffix
if suffix == "cn" {
domain = "cnnic.cn"
}
if suffix == "int" {
domain = "who.int"
}
h, err := c.Whois(domain)
if err != nil {
fmt.Println(idx, domain, "net fail", err)
continue
}
os.WriteFile("./bin/"+domain+".txt", []byte(h.RawData()), 0644)
if !h.exists {
fmt.Println(idx, h.Domain(), "fail")
2026-03-19 11:53:07 +08:00
continue
//t.Fatal(domain, "not exists")
2024-08-16 21:30:27 +08:00
}
fmt.Println(idx, h.Domain(), "ok")
fmt.Println(h.ExpireDate(), h.RegisterDate())
if h.HasExpireDate() && h.ExpireDate().IsZero() {
t.Fatal("ExpireDate is zero")
}
if h.Domain() == "" {
t.Fatal("Domain is empty")
}
if h.HasRegisterDate() && h.RegisterDate().IsZero() {
t.Fatal("RegisterDate is zero")
}
if len(h.NsServers()) == 0 {
fmt.Println("NsServers is empty")
}
fmt.Println(h.Status())
}
2024-08-15 08:38:28 +08:00
}
2026-03-19 11:53:07 +08:00
func TestWhoisFull(t *testing.T) {
requireIntegration(t)
os.MkdirAll("./bin", 0755)
domainSuffix := []string{"com", "net", "org", "cn", "io", "me", "cc", "top", "xyz", "vip", "club", "site", "win", "bid",
"loan", "ek", "kim", "ren", "ltd", "link", "red", "pro", "info", "mobi", "name", "tv", "ws", "asia",
"biz", "gov", "edu", "mil", "int", "aero", "coop", "museum", "jobs", "travel", "xxx",
"ac", "ad", "ae", "af", "ag", "ai", "al", "am", "an", "ao", "aq", "ar", "as", "at", "au", "aw", "az",
"ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz",
"ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cx", "cy", "cz",
"de", "dj", "dk", "dm", "do", "dz",
"ec", "ee", "eg", "eh", "er", "es", "et", "eu",
"fi", "fj", "fk", "fm", "fo", "fr",
"ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy",
"hk", "hm", "hn", "hr", "ht", "hu",
"id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it",
"je", "jm", "jo", "jp",
"ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz",
"la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly",
"ma", "mc", "md", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz",
"na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz",
"om",
"pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py",
"qa",
"re", "ro", "ru", "rw",
"sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "sv", "sy", "sz",
"tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tp", "tr", "tt", "tv", "tw", "tz",
"ua", "ug", "uk", "us", "uy", "uz",
"va", "vc", "ve", "vg", "vi", "vn", "vu",
"wf", "ws",
"ye", "yt", "yu",
"za", "zm", "zw",
}
prefix := "nic."
c := NewClient()
var failedTLDs []string
for idx, suffix := range domainSuffix {
domain := prefix + suffix
if suffix == "cn" {
domain = "cnnic.cn"
}
if suffix == "int" {
domain = "who.int"
}
h, _, err := c.Lookup(domain, WithLookupMode(LookupModeWHOISOnly), WithLookupProxy("socks5://127.0.0.1:29992"))
if err != nil {
// 网络/查询失败:记录失败,不退出
fmt.Printf("[FAIL][NET] idx=%d tld=%s domain=%s err=%v\n", idx, suffix, domain, err)
failedTLDs = append(failedTLDs, suffix+"(net)")
continue
}
// 解析失败判定(你可以按需要再加规则)
parseFailed := false
var failReason string
if !h.Exists() {
parseFailed = true
failReason = "exists=false"
} else if h.Domain() == "" {
parseFailed = true
failReason = "domain empty"
} else {
// 可选严格校验
if h.HasRegisterDate() && h.RegisterDate().IsZero() {
parseFailed = true
failReason = "register date zero"
}
if h.HasExpireDate() && h.ExpireDate().IsZero() {
parseFailed = true
failReason = "expire date zero"
}
}
if parseFailed {
// 仅失败时写 bin
_ = os.WriteFile("./bin/"+domain+".txt", []byte(h.RawData()), 0644)
fmt.Printf("[FAIL][PARSE] idx=%d tld=%s domain=%s reason=%s -> raw saved\n", idx, suffix, domain, failReason)
failedTLDs = append(failedTLDs, suffix+"(parse)")
continue
}
// 成功提示
fmt.Printf("[OK] idx=%d tld=%s domain=%s register=%v expire=%v ns=%d\n",
idx, suffix, h.Domain(), h.RegisterDate(), h.ExpireDate(), len(h.NsServers()))
}
// 最后汇总打印失败 tld
fmt.Println("====================================")
if len(failedTLDs) == 0 {
fmt.Println("[SUMMARY] all passed")
} else {
fmt.Printf("[SUMMARY] failed tlds (%d): %v\n", len(failedTLDs), failedTLDs)
}
}
func TestParseDomain(t *testing.T) {
data, err := os.ReadFile("./bin/nic.me.txt")
if err != nil {
t.Fatal(err)
}
fmt.Println(parse("nic.me", string(data)))
}