cidranger_test.go 3.22 KB
Newer Older
Yulin Chen's avatar
Yulin Chen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
package cidranger

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

	"github.com/stretchr/testify/assert"
	"github.com/yl2chen/cidranger/util/ip"
)

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

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

func TestContains(t *testing.T) {
	rangers := []Ranger{NewLPCTrieRanger()}
	groundRanger := NewBruteRanger()
	for _, ranger := range rangers {
		configureRangerWithAWSRanges(t, ranger)
	}
	configureRangerWithAWSRanges(t, groundRanger)

	for i := 0; i < 100000; i++ {
		nn := ip.Uint32ToIPv4(rand.Uint32())
		expected, err := groundRanger.Contains(nn)
		for _, ranger := range rangers {
			assert.NoError(t, err)
			actual, err := ranger.Contains(nn)
			assert.NoError(t, err)
			assert.Equal(t, expected, actual)
		}
	}
}

func TestContainingNetworks(t *testing.T) {
	rangers := []Ranger{NewLPCTrieRanger()}
	groundRanger := NewBruteRanger()
	for _, ranger := range rangers {
		configureRangerWithAWSRanges(t, ranger)
	}
	configureRangerWithAWSRanges(t, groundRanger)

	for i := 0; i < 100000; i++ {
		nn := ip.Uint32ToIPv4(rand.Uint32())
		expected, err := groundRanger.ContainingNetworks(nn)
		for _, ranger := range rangers {
			assert.NoError(t, err)
			actual, err := ranger.ContainingNetworks(nn)
			assert.NoError(t, err)
			assert.Equal(t, len(expected), len(actual))
			for _, network := range actual {
				assert.Contains(t, expected, network)
			}
		}
	}
}

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
/*
 *********************************
 Benchmarking
 *********************************
*/

func BenchmarkIPv4ToUint32(b *testing.B) {
	nn := net.ParseIP("52.95.110.1")
	for n := 0; n < b.N; n++ {
		ip.IPv4ToUint32(nn)
	}
}

func BenchmarkUint32ToIPv4(b *testing.B) {
	for n := 0; n < b.N; n++ {
		ip.Uint32ToIPv4(878669313)
	}
}

func BenchmarkExtractBits(b *testing.B) {
	nn := net.ParseIP("52.95.110.1")
	ipUint32, _ := ip.IPv4ToUint32(nn)
	for n := 0; n < b.N; n++ {
		ip.IPv4BitsAsUint(ipUint32, 6, 1)
	}
}

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
104 105
}

106 107
func BenchmarkBruteRangerMissUsingAWSRanges(b *testing.B) {
	benchmarkContainsUsingAWSRanges(b, net.ParseIP("123.123.123.123"), NewBruteRanger())
Yulin Chen's avatar
Yulin Chen committed
108 109 110 111 112 113 114 115 116 117 118
}

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

119
func benchmarkContainsUsingAWSRanges(tb testing.TB, nn net.IP, ranger Ranger) {
Yulin Chen's avatar
Yulin Chen committed
120 121
	configureRangerWithAWSRanges(tb, ranger)
	for n := 0; n < tb.(*testing.B).N; n++ {
122
		ranger.Contains(nn)
Yulin Chen's avatar
Yulin Chen committed
123 124 125 126 127 128 129 130 131 132 133 134
	}
}

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
}