cidranger_test.go 5.83 KB
Newer Older
Yulin Chen's avatar
Yulin Chen committed
1 2 3 4 5 6 7 8
package cidranger

import (
	"encoding/json"
	"io/ioutil"
	"math/rand"
	"net"
	"testing"
9
	"time"
Yulin Chen's avatar
Yulin Chen committed
10 11

	"github.com/stretchr/testify/assert"
12
	rnet "github.com/yl2chen/cidranger/net"
Yulin Chen's avatar
Yulin Chen committed
13 14
)

15 16 17 18 19 20
/*
 ******************************************************************
 Test Contains/ContainingNetworks against basic brute force ranger.
 ******************************************************************
*/

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
func TestContainsAgainstBaseIPv4(t *testing.T) {
	testContainsAgainstBase(t, 100000, randIPv4Gen)
}

func TestContainingNetworksAgaistBaseIPv4(t *testing.T) {
	testContainingNetworksAgainstBase(t, 100000, randIPv4Gen)
}

// IPv6 spans an extremely large address space (2^128), randomly generated IPs
// will often fall outside of the test ranges (AWS public CIDR blocks), so it
// it more meaningful for testing to run from a curated list of IPv6 IPs.
func TestContainsAgaistBaseIPv6(t *testing.T) {
	testContainsAgainstBase(t, 100000, curatedAWSIPv6Gen)
}

func TestContainingNetworksAgaistBaseIPv6(t *testing.T) {
	testContainingNetworksAgainstBase(t, 100000, curatedAWSIPv6Gen)
}

func testContainsAgainstBase(t *testing.T, iterations int, ipGen ipGenerator) {
41
	rangers := []Ranger{NewPCTrieRanger()}
42
	baseRanger := newBruteRanger()
Yulin Chen's avatar
Yulin Chen committed
43 44 45
	for _, ranger := range rangers {
		configureRangerWithAWSRanges(t, ranger)
	}
46
	configureRangerWithAWSRanges(t, baseRanger)
Yulin Chen's avatar
Yulin Chen committed
47

48 49
	for i := 0; i < iterations; i++ {
		nn := ipGen()
50
		expected, err := baseRanger.Contains(nn.ToIP())
51
		assert.NoError(t, err)
Yulin Chen's avatar
Yulin Chen committed
52
		for _, ranger := range rangers {
53
			actual, err := ranger.Contains(nn.ToIP())
Yulin Chen's avatar
Yulin Chen committed
54 55 56 57 58 59
			assert.NoError(t, err)
			assert.Equal(t, expected, actual)
		}
	}
}

60
func testContainingNetworksAgainstBase(t *testing.T, iterations int, ipGen ipGenerator) {
61
	rangers := []Ranger{NewPCTrieRanger()}
62
	baseRanger := newBruteRanger()
Yulin Chen's avatar
Yulin Chen committed
63 64 65
	for _, ranger := range rangers {
		configureRangerWithAWSRanges(t, ranger)
	}
66
	configureRangerWithAWSRanges(t, baseRanger)
Yulin Chen's avatar
Yulin Chen committed
67

68 69
	for i := 0; i < iterations; i++ {
		nn := ipGen()
70
		expected, err := baseRanger.ContainingNetworks(nn.ToIP())
71
		assert.NoError(t, err)
Yulin Chen's avatar
Yulin Chen committed
72
		for _, ranger := range rangers {
73
			actual, err := ranger.ContainingNetworks(nn.ToIP())
Yulin Chen's avatar
Yulin Chen committed
74 75 76 77 78 79 80 81 82
			assert.NoError(t, err)
			assert.Equal(t, len(expected), len(actual))
			for _, network := range actual {
				assert.Contains(t, expected, network)
			}
		}
	}
}

83 84 85 86 87 88
/*
 ******************************************************************
 Benchmarks.
 ******************************************************************
*/

89 90
func BenchmarkPCTrieHitIPv4UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("52.95.110.1"), NewPCTrieRanger())
91 92 93 94
}
func BenchmarkBruteRangerHitIPv4UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("52.95.110.1"), newBruteRanger())
}
95 96 97 98

func BenchmarkPCTrieHitIPv6UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620:107:300f::36b7:ff81"), NewPCTrieRanger())
}
99 100
func BenchmarkBruteRangerHitIPv6UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620:107:300f::36b7:ff81"), newBruteRanger())
101 102
}

103 104
func BenchmarkPCTrieMissIPv4UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("123.123.123.123"), NewPCTrieRanger())
Yulin Chen's avatar
Yulin Chen committed
105
}
106 107 108
func BenchmarkBruteRangerMissIPv4UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("123.123.123.123"), newBruteRanger())
}
109 110 111 112

func BenchmarkPCTrieHMissIPv6UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620::ffff"), NewPCTrieRanger())
}
113 114
func BenchmarkBruteRangerMissIPv6UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620::ffff"), newBruteRanger())
Yulin Chen's avatar
Yulin Chen committed
115 116
}

117
func benchmarkContainsUsingAWSRanges(tb testing.TB, nn net.IP, ranger Ranger) {
Yulin Chen's avatar
Yulin Chen committed
118 119
	configureRangerWithAWSRanges(tb, ranger)
	for n := 0; n < tb.(*testing.B).N; n++ {
120
		ranger.Contains(nn)
Yulin Chen's avatar
Yulin Chen committed
121 122 123
	}
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/*
 ******************************************************************
 Helper methods and initialization.
 ******************************************************************
*/

type ipGenerator func() rnet.NetworkNumber

func randIPv4Gen() rnet.NetworkNumber {
	return rnet.NetworkNumber{rand.Uint32()}
}
func randIPv6Gen() rnet.NetworkNumber {
	return rnet.NetworkNumber{rand.Uint32(), rand.Uint32(), rand.Uint32(), rand.Uint32()}
}
func curatedAWSIPv6Gen() rnet.NetworkNumber {
	randIdx := rand.Intn(len(ipV6AWSRangesIPNets))

	// Randomly generate an IP somewhat near the range.
	network := ipV6AWSRangesIPNets[randIdx]
	nn := rnet.NewNetworkNumber(network.IP)
	ones, bits := network.Mask.Size()
	zeros := bits - ones
	nnPartIdx := zeros / rnet.BitsPerUint32
	nn[nnPartIdx] = rand.Uint32()
	return nn
}

type AWSRanges struct {
	Prefixes     []Prefix     `json:"prefixes"`
	IPv6Prefixes []IPv6Prefix `json:"ipv6_prefixes"`
}

type Prefix struct {
	IPPrefix string `json:"ip_prefix"`
	Region   string `json:"region"`
	Service  string `json:"service"`
}

type IPv6Prefix struct {
	IPPrefix string `json:"ipv6_prefix"`
	Region   string `json:"region"`
	Service  string `json:"service"`
}
Yulin Chen's avatar
Yulin Chen committed
167

168 169 170 171 172 173 174 175
var awsRanges *AWSRanges
var ipV6AWSRangesIPNets []*net.IPNet

func loadAWSRanges() *AWSRanges {
	file, err := ioutil.ReadFile("./testdata/aws_ip_ranges.json")
	if err != nil {
		panic(err)
	}
Yulin Chen's avatar
Yulin Chen committed
176 177
	var ranges AWSRanges
	err = json.Unmarshal(file, &ranges)
178 179 180
	if err != nil {
		panic(err)
	}
Yulin Chen's avatar
Yulin Chen committed
181 182
	return &ranges
}
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

func configureRangerWithAWSRanges(tb testing.TB, ranger Ranger) {
	for _, prefix := range awsRanges.Prefixes {
		_, network, err := net.ParseCIDR(prefix.IPPrefix)
		assert.NoError(tb, err)
		ranger.Insert(*network)
	}
	for _, prefix := range awsRanges.IPv6Prefixes {
		_, network, err := net.ParseCIDR(prefix.IPPrefix)
		assert.NoError(tb, err)
		ranger.Insert(*network)
	}
}

func init() {
	awsRanges = loadAWSRanges()
	for _, prefix := range awsRanges.IPv6Prefixes {
		_, network, _ := net.ParseCIDR(prefix.IPPrefix)
		ipV6AWSRangesIPNets = append(ipV6AWSRangesIPNets, network)
	}
	rand.Seed(time.Now().Unix())
}