cloudflare/cloudflared
Publicmirrored fromhttps://github.com/cloudflare/cloudflaredAvailable
connection/mocks_for_test.go
118lines · modecode
| 1 | package connection |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math" |
| 6 | "math/rand" |
| 7 | "net" |
| 8 | "reflect" |
| 9 | "testing/quick" |
| 10 | ) |
| 11 | |
| 12 | type mockAddrs struct { |
| 13 | // a set of synthetic SRV records |
| 14 | addrMap map[net.SRV][]*net.TCPAddr |
| 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 | |
| 21 | func newMockAddrs(port uint16, numRegions uint8, numAddrsPerRegion uint8) mockAddrs { |
| 22 | addrMap := make(map[net.SRV][]*net.TCPAddr) |
| 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 []*net.TCPAddr |
| 29 | ) |
| 30 | for a := uint8(0); a < numAddrsPerRegion; a++ { |
| 31 | addrs = append(addrs, &net.TCPAddr{ |
| 32 | IP: net.ParseIP(fmt.Sprintf("10.0.%v.%v", r, a)), |
| 33 | Port: int(port), |
| 34 | }) |
| 35 | } |
| 36 | addrMap[srv] = addrs |
| 37 | numAddrs += len(addrs) |
| 38 | } |
| 39 | return mockAddrs{addrMap: addrMap, numAddrs: numAddrs} |
| 40 | } |
| 41 | |
| 42 | var _ quick.Generator = mockAddrs{} |
| 43 | |
| 44 | func (mockAddrs) Generate(rand *rand.Rand, size int) reflect.Value { |
| 45 | port := uint16(rand.Intn(math.MaxUint16)) |
| 46 | numRegions := uint8(1 + rand.Intn(10)) |
| 47 | numAddrsPerRegion := uint8(1 + rand.Intn(32)) |
| 48 | result := newMockAddrs(port, numRegions, numAddrsPerRegion) |
| 49 | return reflect.ValueOf(result) |
| 50 | } |
| 51 | |
| 52 | // Returns a function compatible with net.LookupSRV that will return the SRV |
| 53 | // records from mockAddrs. |
| 54 | func mockNetLookupSRV( |
| 55 | m mockAddrs, |
| 56 | ) func(service, proto, name string) (cname string, addrs []*net.SRV, err error) { |
| 57 | var addrs []*net.SRV |
| 58 | for k := range m.addrMap { |
| 59 | addr := k |
| 60 | addrs = append(addrs, &addr) |
| 61 | // We can't just do |
| 62 | // addrs = append(addrs, &k) |
| 63 | // `k` will be reused by subsequent loop iterations, |
| 64 | // so all the copies of `&k` would point to the same location. |
| 65 | } |
| 66 | return func(_, _, _ string) (string, []*net.SRV, error) { |
| 67 | return "", addrs, nil |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Returns a function compatible with net.LookupIP that translates the SRV records |
| 72 | // from mockAddrs into IP addresses, based on the TCP addresses in mockAddrs. |
| 73 | func mockNetLookupIP( |
| 74 | m mockAddrs, |
| 75 | ) func(host string) ([]net.IP, error) { |
| 76 | return func(host string) ([]net.IP, error) { |
| 77 | for srv, tcpAddrs := range m.addrMap { |
| 78 | if srv.Target != host { |
| 79 | continue |
| 80 | } |
| 81 | result := make([]net.IP, len(tcpAddrs)) |
| 82 | for i, tcpAddr := range tcpAddrs { |
| 83 | result[i] = tcpAddr.IP |
| 84 | } |
| 85 | return result, nil |
| 86 | } |
| 87 | return nil, fmt.Errorf("No IPs for %v", host) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | type mockEdgeServiceDiscoverer struct { |
| 92 | } |
| 93 | |
| 94 | func (mr *mockEdgeServiceDiscoverer) Addr() (*net.TCPAddr, error) { |
| 95 | return &net.TCPAddr{ |
| 96 | IP: net.ParseIP("127.0.0.1"), |
| 97 | Port: 63102, |
| 98 | }, nil |
| 99 | } |
| 100 | |
| 101 | func (mr *mockEdgeServiceDiscoverer) AnyAddr() (*net.TCPAddr, error) { |
| 102 | return &net.TCPAddr{ |
| 103 | IP: net.ParseIP("127.0.0.1"), |
| 104 | Port: 63102, |
| 105 | }, nil |
| 106 | } |
| 107 | |
| 108 | func (mr *mockEdgeServiceDiscoverer) ReplaceAddr(addr *net.TCPAddr) {} |
| 109 | |
| 110 | func (mr *mockEdgeServiceDiscoverer) MarkAddrBad(addr *net.TCPAddr) {} |
| 111 | |
| 112 | func (mr *mockEdgeServiceDiscoverer) AvailableAddrs() int { |
| 113 | return 1 |
| 114 | } |
| 115 | |
| 116 | func (mr *mockEdgeServiceDiscoverer) Refresh() error { |
| 117 | return nil |
| 118 | } |
| 119 | |