cidranger_test.go 5.84 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) {
Yulin Chen's avatar
Yulin Chen committed
41
	rangers := []Ranger{NewLPCTrieRanger()}
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())
Yulin Chen's avatar
Yulin Chen committed
51 52
		for _, ranger := range rangers {
			assert.NoError(t, err)
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) {
Yulin Chen's avatar
Yulin Chen committed
61
	rangers := []Ranger{NewLPCTrieRanger()}
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())
Yulin Chen's avatar
Yulin Chen committed
71 72
		for _, ranger := range rangers {
			assert.NoError(t, err)
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 89
/*
 ******************************************************************
 Benchmarks.
 ******************************************************************
*/

func BenchmarkLPCTrieHitIPv4UsingAWSRanges(b *testing.B) {
90 91
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("52.95.110.1"), NewLPCTrieRanger())
}
92 93 94 95 96 97 98 99
func BenchmarkLPCTrieHitIPv6UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620:107:300f::36b7:ff81"), NewLPCTrieRanger())
}
func BenchmarkBruteRangerHitIPv4UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("52.95.110.1"), newBruteRanger())
}
func BenchmarkBruteRangerHitIPv6UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620:107:300f::36b7:ff81"), newBruteRanger())
100 101
}

102
func BenchmarkLPCTrieMissIPv4UsingAWSRanges(b *testing.B) {
103
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("123.123.123.123"), NewLPCTrieRanger())
Yulin Chen's avatar
Yulin Chen committed
104
}
105 106
func BenchmarkLPCTrieHMissIPv6UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620::ffff"), NewLPCTrieRanger())
Yulin Chen's avatar
Yulin Chen committed
107
}
108 109 110 111 112
func BenchmarkBruteRangerMissIPv4UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("123.123.123.123"), newBruteRanger())
}
func BenchmarkBruteRangerMissIPv6UsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620::ffff"), newBruteRanger())
Yulin Chen's avatar
Yulin Chen committed
113 114
}

115
func benchmarkContainsUsingAWSRanges(tb testing.TB, nn net.IP, ranger Ranger) {
Yulin Chen's avatar
Yulin Chen committed
116 117
	configureRangerWithAWSRanges(tb, ranger)
	for n := 0; n < tb.(*testing.B).N; n++ {
118
		ranger.Contains(nn)
Yulin Chen's avatar
Yulin Chen committed
119 120 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
/*
 ******************************************************************
 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
165

166 167 168 169 170 171 172 173
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
174 175
	var ranges AWSRanges
	err = json.Unmarshal(file, &ranges)
176 177 178
	if err != nil {
		panic(err)
	}
Yulin Chen's avatar
Yulin Chen committed
179 180
	return &ranges
}
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

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())
}