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

import (
	"encoding/json"
	"io/ioutil"
	"math/rand"
	"net"
	"testing"

	"github.com/stretchr/testify/assert"
11
	rnet "github.com/yl2chen/cidranger/net"
Yulin Chen's avatar
Yulin Chen committed
12 13 14 15 16 17 18 19 20 21 22 23
)

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

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

24 25 26 27 28 29 30
/*
 ******************************************************************
 Test Contains/ContainingNetworks against basic brute force ranger.
 ******************************************************************
*/

func TestContainsAgainstBase(t *testing.T) {
Yulin Chen's avatar
Yulin Chen committed
31
	rangers := []Ranger{NewLPCTrieRanger()}
32
	baseRanger := NewBruteRanger()
Yulin Chen's avatar
Yulin Chen committed
33 34 35
	for _, ranger := range rangers {
		configureRangerWithAWSRanges(t, ranger)
	}
36
	configureRangerWithAWSRanges(t, baseRanger)
Yulin Chen's avatar
Yulin Chen committed
37 38

	for i := 0; i < 100000; i++ {
39 40
		nn := rnet.NetworkNumber{rand.Uint32()}
		expected, err := baseRanger.Contains(nn.ToIP())
Yulin Chen's avatar
Yulin Chen committed
41 42
		for _, ranger := range rangers {
			assert.NoError(t, err)
43
			actual, err := ranger.Contains(nn.ToIP())
Yulin Chen's avatar
Yulin Chen committed
44 45 46 47 48 49
			assert.NoError(t, err)
			assert.Equal(t, expected, actual)
		}
	}
}

50
func TestContainingNetworksAgainstBase(t *testing.T) {
Yulin Chen's avatar
Yulin Chen committed
51
	rangers := []Ranger{NewLPCTrieRanger()}
52
	baseRanger := NewBruteRanger()
Yulin Chen's avatar
Yulin Chen committed
53 54 55
	for _, ranger := range rangers {
		configureRangerWithAWSRanges(t, ranger)
	}
56
	configureRangerWithAWSRanges(t, baseRanger)
Yulin Chen's avatar
Yulin Chen committed
57 58

	for i := 0; i < 100000; i++ {
59 60
		nn := rnet.NetworkNumber{rand.Uint32()}
		expected, err := baseRanger.ContainingNetworks(nn.ToIP())
Yulin Chen's avatar
Yulin Chen committed
61 62
		for _, ranger := range rangers {
			assert.NoError(t, err)
63
			actual, err := ranger.ContainingNetworks(nn.ToIP())
Yulin Chen's avatar
Yulin Chen committed
64 65 66 67 68 69 70 71 72
			assert.NoError(t, err)
			assert.Equal(t, len(expected), len(actual))
			for _, network := range actual {
				assert.Contains(t, expected, network)
			}
		}
	}
}

73 74 75 76 77 78 79 80 81 82
func BenchmarkLPCTrieHitUsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("52.95.110.1"), NewLPCTrieRanger())
}

func BenchmarkBruteRangerHitUsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("52.95.110.1"), NewBruteRanger())
}

func BenchmarkLPCTrieMissUsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("123.123.123.123"), NewLPCTrieRanger())
Yulin Chen's avatar
Yulin Chen committed
83 84
}

85 86
func BenchmarkBruteRangerMissUsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("123.123.123.123"), NewBruteRanger())
Yulin Chen's avatar
Yulin Chen committed
87 88 89 90 91 92 93 94 95 96 97
}

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

98
func benchmarkContainsUsingAWSRanges(tb testing.TB, nn net.IP, ranger Ranger) {
Yulin Chen's avatar
Yulin Chen committed
99 100
	configureRangerWithAWSRanges(tb, ranger)
	for n := 0; n < tb.(*testing.B).N; n++ {
101
		ranger.Contains(nn)
Yulin Chen's avatar
Yulin Chen committed
102 103 104 105 106 107 108 109 110 111 112 113
	}
}

func loadAWSRanges(tb testing.TB) *AWSRanges {
	file, err := ioutil.ReadFile("./testdata/aws_ip_ranges.json")
	assert.NoError(tb, err)

	var ranges AWSRanges
	err = json.Unmarshal(file, &ranges)
	assert.NoError(tb, err)
	return &ranges
}