Skip to content

Commit

Permalink
add random Domain name generation support
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Apr 15, 2024
1 parent 851b69b commit c654d4c
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 10 deletions.
5 changes: 5 additions & 0 deletions random/Random.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
21 changes: 19 additions & 2 deletions random/Random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
101 changes: 101 additions & 0 deletions random/internal/fixtures/cloudflare-radar-domains-top-100-20240416.csv
Original file line number Diff line number Diff line change
@@ -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
59 changes: 51 additions & 8 deletions random/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"bufio"
"bytes"
_ "embed"
"encoding/csv"
"fmt"
"io"
"os"
"path"
"regexp"
"sort"
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down

0 comments on commit c654d4c

Please sign in to comment.