github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/utils/unittest/network/fixtures.go (about)

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