github.com/koko1123/flow-go-1@v0.29.6/utils/unittest/network/fixtures.go (about) 1 package network 2 3 import ( 4 "fmt" 5 "math/rand" 6 "net" 7 ) 8 9 type IpLookupTestCase struct { 10 Domain string 11 Result []net.IPAddr 12 TimeStamp int64 13 } 14 15 type TxtLookupTestCase struct { 16 Txt string 17 Records []string 18 TimeStamp int64 19 } 20 21 func NetIPAddrFixture() net.IPAddr { 22 token := make([]byte, 4) 23 rand.Read(token) 24 25 ip := net.IPAddr{ 26 IP: net.IPv4(token[0], token[1], token[2], token[3]), 27 Zone: "flow0", 28 } 29 30 return ip 31 } 32 33 func TxtIPFixture() string { 34 token := make([]byte, 4) 35 rand.Read(token) 36 return "dnsaddr=" + net.IPv4(token[0], token[1], token[2], token[3]).String() 37 } 38 39 func IpLookupFixture(count int) map[string]*IpLookupTestCase { 40 tt := make(map[string]*IpLookupTestCase) 41 for i := 0; i < count; i++ { 42 ipTestCase := &IpLookupTestCase{ 43 Domain: fmt.Sprintf("example%d.com", i), 44 Result: []net.IPAddr{ // resolves each domain to 4 addresses. 45 NetIPAddrFixture(), 46 NetIPAddrFixture(), 47 NetIPAddrFixture(), 48 NetIPAddrFixture(), 49 }, 50 TimeStamp: rand.Int63(), 51 } 52 53 tt[ipTestCase.Domain] = ipTestCase 54 } 55 56 return tt 57 } 58 59 func TxtLookupFixture(count int) map[string]*TxtLookupTestCase { 60 tt := make(map[string]*TxtLookupTestCase) 61 62 for i := 0; i < count; i++ { 63 ttTestCase := &TxtLookupTestCase{ 64 Txt: fmt.Sprintf("_dnsaddr.example%d.com", i), 65 Records: []string{ // resolves each txt to 4 addresses. 66 TxtIPFixture(), 67 TxtIPFixture(), 68 TxtIPFixture(), 69 TxtIPFixture(), 70 }, 71 TimeStamp: rand.Int63(), 72 } 73 74 tt[ttTestCase.Txt] = ttTestCase 75 } 76 77 return tt 78 } 79 80 func IpLookupListFixture(count int) []*IpLookupTestCase { 81 tt := make([]*IpLookupTestCase, 0) 82 for i := 0; i < count; i++ { 83 ipTestCase := &IpLookupTestCase{ 84 Domain: fmt.Sprintf("example%d.com", i), 85 Result: []net.IPAddr{ // resolves each domain to 4 addresses. 86 NetIPAddrFixture(), 87 NetIPAddrFixture(), 88 NetIPAddrFixture(), 89 NetIPAddrFixture(), 90 }, 91 TimeStamp: rand.Int63(), 92 } 93 94 tt = append(tt, ipTestCase) 95 } 96 97 return tt 98 } 99 100 func TxtLookupListFixture(count int) []*TxtLookupTestCase { 101 tt := make([]*TxtLookupTestCase, 0) 102 103 for i := 0; i < count; i++ { 104 ttTestCase := &TxtLookupTestCase{ 105 Txt: fmt.Sprintf("_dnsaddr.example%d.com", i), 106 Records: []string{ // resolves each txt to 4 addresses. 107 TxtIPFixture(), 108 TxtIPFixture(), 109 TxtIPFixture(), 110 TxtIPFixture(), 111 }, 112 TimeStamp: rand.Int63(), 113 } 114 115 tt = append(tt, ttTestCase) 116 } 117 118 return tt 119 }