cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.10.4

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

edgediscovery/allregions/mocks_for_test.go

94lines · modecode

1package allregions
2
3import (
4 "fmt"
5 "math"
6 "math/rand"
7 "net"
8 "reflect"
9 "testing/quick"
10)
11
12type mockAddrs struct {
13 // a set of synthetic SRV records
14 addrMap map[net.SRV][]*EdgeAddr
15 // the total number of addresses, aggregated across addrMap.
16 // For the convenience of test code that would otherwise have to compute
17 // this by hand every time.
18 numAddrs int
19}
20
21func newMockAddrs(port uint16, numRegions uint8, numAddrsPerRegion uint8) mockAddrs {
22 addrMap := make(map[net.SRV][]*EdgeAddr)
23 numAddrs := 0
24
25 for r := uint8(0); r < numRegions; r++ {
26 var (
27 srv = net.SRV{Target: fmt.Sprintf("test-region-%v.example.com", r), Port: port}
28 addrs []*EdgeAddr
29 )
30 for a := uint8(0); a < numAddrsPerRegion; a++ {
31 tcpAddr := &net.TCPAddr{
32 IP: net.ParseIP(fmt.Sprintf("10.0.%v.%v", r, a)),
33 Port: int(port),
34 }
35 udpAddr := &net.UDPAddr{
36 IP: net.ParseIP(fmt.Sprintf("10.0.%v.%v", r, a)),
37 Port: int(port),
38 }
39 addrs = append(addrs, &EdgeAddr{tcpAddr, udpAddr})
40 }
41 addrMap[srv] = addrs
42 numAddrs += len(addrs)
43 }
44 return mockAddrs{addrMap: addrMap, numAddrs: numAddrs}
45}
46
47var _ quick.Generator = mockAddrs{}
48
49func (mockAddrs) Generate(rand *rand.Rand, size int) reflect.Value {
50 port := uint16(rand.Intn(math.MaxUint16))
51 numRegions := uint8(1 + rand.Intn(10))
52 numAddrsPerRegion := uint8(1 + rand.Intn(32))
53 result := newMockAddrs(port, numRegions, numAddrsPerRegion)
54 return reflect.ValueOf(result)
55}
56
57// Returns a function compatible with net.LookupSRV that will return the SRV
58// records from mockAddrs.
59func mockNetLookupSRV(
60 m mockAddrs,
61) func(service, proto, name string) (cname string, addrs []*net.SRV, err error) {
62 var addrs []*net.SRV
63 for k := range m.addrMap {
64 addr := k
65 addrs = append(addrs, &addr)
66 // We can't just do
67 // addrs = append(addrs, &k)
68 // `k` will be reused by subsequent loop iterations,
69 // so all the copies of `&k` would point to the same location.
70 }
71 return func(_, _, _ string) (string, []*net.SRV, error) {
72 return "", addrs, nil
73 }
74}
75
76// Returns a function compatible with net.LookupIP that translates the SRV records
77// from mockAddrs into IP addresses, based on the TCP addresses in mockAddrs.
78func mockNetLookupIP(
79 m mockAddrs,
80) func(host string) ([]net.IP, error) {
81 return func(host string) ([]net.IP, error) {
82 for srv, addrs := range m.addrMap {
83 if srv.Target != host {
84 continue
85 }
86 result := make([]net.IP, len(addrs))
87 for i, addr := range addrs {
88 result[i] = addr.TCP.IP
89 }
90 return result, nil
91 }
92 return nil, fmt.Errorf("No IPs for %v", host)
93 }
94}
95