From c654d4c29893b1919d161f303e2256240d1f14bd Mon Sep 17 00:00:00 2001 From: Adam Luzsi Date: Mon, 15 Apr 2024 20:21:38 +0200 Subject: [PATCH] add random Domain name generation support --- random/Random.go | 5 + random/Random_test.go | 21 +++- ...udflare-radar-domains-top-100-20240416.csv | 101 ++++++++++++++++++ random/strings.go | 59 ++++++++-- 4 files changed, 176 insertions(+), 10 deletions(-) create mode 100644 random/internal/fixtures/cloudflare-radar-domains-top-100-20240416.csv diff --git a/random/Random.go b/random/Random.go index 5165f6b..8c47629 100644 --- a/random/Random.go +++ b/random/Random.go @@ -232,3 +232,8 @@ func (r *Random) Repeat(min, max int, do func()) int { } return n } + +// Domain will return a valid domain name. +func (r *Random) Domain() string { + return r.SliceElement(fixtureStrings.domains).(string) +} diff --git a/random/Random_test.go b/random/Random_test.go index 24f6cf4..3c90623 100644 --- a/random/Random_test.go +++ b/random/Random_test.go @@ -2,14 +2,15 @@ package random_test import ( "fmt" - "go.llib.dev/testcase/internal" - "go.llib.dev/testcase/let" "math/rand" "regexp" "strings" "testing" "time" + "go.llib.dev/testcase/internal" + "go.llib.dev/testcase/let" + "go.llib.dev/testcase/random/sextype" "go.llib.dev/testcase/assert" @@ -577,6 +578,22 @@ func SpecRandomMethods(s *testcase.Spec, rnd testcase.Var[*random.Random]) { }) }) + s.Describe(".Domain", func(s *testcase.Spec) { + act := func(t *testcase.T) string { + return rnd.Get(t).Domain() + } + + s.Then("a non empty domain is returned", func(t *testcase.T) { + t.Must.NotEmpty(act(t)) + }) + + s.Then("it returns a valid common domain", func(t *testcase.T) { + t.Eventually(func(it assert.It) { it.Must.Equal(act(t), "google.com") }) + t.Eventually(func(it assert.It) { it.Must.Equal(act(t), "amazon.com") }) + t.Eventually(func(it assert.It) { it.Must.Equal(act(t), "youtube.com") }) + }) + }) + s.Context("DEPRECATED", func(s *testcase.Spec) { s.Describe(".Name().First()", func(s *testcase.Spec) { act := func(t *testcase.T) string { diff --git a/random/internal/fixtures/cloudflare-radar-domains-top-100-20240416.csv b/random/internal/fixtures/cloudflare-radar-domains-top-100-20240416.csv new file mode 100644 index 0000000..823375a --- /dev/null +++ b/random/internal/fixtures/cloudflare-radar-domains-top-100-20240416.csv @@ -0,0 +1,101 @@ +rank,domain,categories +1,google.com,Search Engines +2,googleapis.com,Content Servers;Webmail +3,facebook.com,Social Networks +4,apple.com,Information Technology;Technology +5,gstatic.com,Content Servers +6,microsoft.com,Information Technology;Business +7,tiktokcdn.com,Content Servers;Social Networks +8,amazonaws.com,Technology +9,root-servers.net,Technology +10,googlevideo.com,Search Engines;Video Streaming +11,doubleclick.net,Advertisements +12,apple-dns.net,Content Servers +13,fbcdn.net,Social Networks +14,icloud.com,File Sharing +15,ui.com,Technology +16,akadns.net,Content Servers +17,amazon.com,Ecommerce +18,youtube.com,Video Streaming +19,akamai.net,Content Servers +20,tiktokv.com,Social Networks;Video Streaming +21,ring.com,Business;Photography +22,googleusercontent.com,Content Servers +23,cloudflare-dns.com,Content Servers +24,googlesyndication.com,Search Engines;Advertisements +25,akamaiedge.net,Content Servers +26,instagram.com,Social Networks +27,cloudfront.net,Content Servers;Science;Technology +28,gvt2.com,Content Servers +29,whatsapp.net,Instant Messengers;Internet Phone & VOIP +30,live.com,Technology;Webmail +31,ntp.org,Technology +32,bing.com,Search Engines +33,netflix.com,Movies;Video Streaming +34,office.com,Technology +35,cdn77.org,Content Servers +36,cloudflare.com,Technology +37,spotify.com,Audio Streaming +38,aaplimg.com,Information Technology;Content Servers;Technology +39,one.one,Information Technology +40,snapchat.com,Social Networks +41,ytimg.com,Content Servers +42,yahoo.com,News & Media +43,facebook-hardware.com,Content Servers +44,app-measurement.com,APIs +45,bytefcdn-oversea.com, +46,google-analytics.com,Business;Technology +47,ttlivecdn.com,Content Servers +48,trafficmanager.net,Content Servers +49,azure.com,Technology +50,msftncsi.com,Content Servers +51,amazon-adsystem.com,Advertisements +52,tiktokcdn-us.com,Content Servers +53,googleadservices.com,Advertisements +54,msn.com,News & Media +55,fastly.net,Information Security;Information Technology;Technology +56,twitter.com,Social Networks +57,cdninstagram.com,Content Servers +58,digicert.com,Information Security +59,office365.com,Technology +60,criteo.com,Business +61,pki.goog,Information Security +62,dns.google,Information Technology +63,microsoftonline.com,Technology +64,office.net,Content Servers +65,googletagmanager.com,Content Servers +66,unity3d.com,Gaming;Technology +67,gvt1.com,Content Servers +68,skype.com,Instant Messengers;Internet Phone & VOIP +69,ggpht.com,Content Servers +70,applovin.com,Gaming +71,roblox.com,Gaming;Safe for Kids +72,windows.net,Technology +73,linkedin.com,Professional Networking +74,gmail.com,Webmail +75,windowsupdate.com,Information Technology;Business +76,akamaized.net,Content Servers +77,rubiconproject.com,Advertisements +78,sharepoint.com,File Sharing;Technology +79,windows.com,Information Technology +80,sentry.io,Business +81,a2z.com,Content Servers +82,example.com,Technology +83,azureedge.net,Content Servers +84,bytefcdn-ttpeu.com,Content Servers +85,taboola.com,Business;Advertisements +86,samsung.com,Home & Garden;Technology +87,casalemedia.com,Advertisements +88,mzstatic.com,Information Technology;Technology +89,adnxs.com,Content Servers +90,2mdn.net,Content Servers +91,baidu.com,Search Engines +92,smartadserver.com,Advertisements +93,doubleverify.com,Business;Technology +94,amazon.dev,Content Servers +95,outlook.com,Webmail +96,pubmatic.com,Business;Technology +97,cdn-apple.com,Content Servers +98,a-msedge.net,Content Servers +99,lencr.org,Information Technology +100,nr-data.net,Content Servers diff --git a/random/strings.go b/random/strings.go index 475b512..d84f19c 100644 --- a/random/strings.go +++ b/random/strings.go @@ -4,7 +4,10 @@ import ( "bufio" "bytes" _ "embed" + "encoding/csv" "fmt" + "io" + "os" "path" "regexp" "sort" @@ -13,8 +16,10 @@ import ( ) var fixtureStrings struct { - naughty []string - errors []string + naughty []string + errors []string + domains []string // https://radar.cloudflare.com/domains/ + emailDomains []string names struct { @@ -31,6 +36,41 @@ func init() { fixtureStrings.names.last = getLines("contacts", "lastnames.txt") fixtureStrings.names.male = getLines("contacts", "malenames.txt") fixtureStrings.names.female = getLines("contacts", "femalenames.txt") + fixtureStrings.domains = getDomains() + +} + +func getDomains() []string { + filePath := path.Join("fixtures", "cloudflare-radar-domains-top-100-20240416.csv") + + file, err := internal.FixturesFS.Open(filePath) + if err != nil { + stderrLog(err) + return nil + } + + reader := csv.NewReader(file) + reader.Comma = ',' + + _, _ = reader.Read() + + var domains []string + for { + record, err := reader.Read() + if err == io.EOF { + break + } + if err != nil { + stderrLog(err) + return nil + } + if len(record) != 3 { + stderrLog(fmt.Errorf("invalid cloudflare domain export format")) + return nil + } + domains = append(domains, record[1]) + } + return domains } func getNaughtyStrings() []string { @@ -43,22 +83,25 @@ func getNaughtyStrings() []string { return ns } +func stderrLog(err error) { + if err != nil { + return + } + fmt.Fprintln(os.Stderr, "Error", "testcase/random", "err:", err.Error()) +} + func getLines(paths ...string) []string { filePath := path.Join("fixtures", path.Join(paths...)) - errOut := func(err error) { - fmt.Println("Error", "testcase/random", "fixtures:", filePath, "err:", err.Error()) - } - data, err := internal.FixturesFS.ReadFile(filePath) if err != nil { - errOut(err) + stderrLog(err) return nil } lines, err := extractLines(data) if err != nil { - errOut(err) + stderrLog(err) return nil }