-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bogon_test.go
140 lines (127 loc) · 3.56 KB
/
bogon_test.go
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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) Liam Stanley <me@liamstanley.io>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package bogon
import (
"net"
"reflect"
"testing"
)
func TestDefaultRanges(t *testing.T) {
got := DefaultRanges()
if got == nil || len(got) < 1 {
t.Fatal("DefaultRanges() returned invalid results")
}
}
func TestBogon_Ranges(t *testing.T) {
tests := []struct {
name string
b *Bogon
want []*net.IPNet
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.Ranges(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Bogon.Ranges() = %v, want %v", got, tt.want)
}
})
}
}
func dummyBogon() *Bogon {
b, err := New([]string{"127.0.0.1/32", "10.0.0.0/8", "11.0.0.0/24", "fe80::/10"})
if err != nil {
panic(err)
}
return b
}
const dummyString = "127.0.0.1/32 10.0.0.0/8 11.0.0.0/24 fe80::/10"
func TestBogon_String(t *testing.T) {
b := dummyBogon()
if b.String() != dummyString {
t.Errorf("Bogon.String() = %v, want %v", b.String(), dummyString)
}
}
func TestBogon_Is(t *testing.T) {
type args struct {
ip string
}
tests := []struct {
name string
b *Bogon
args args
wantIsIn bool
wantRepresentation string
}{
{"is single", dummyBogon(), args{ip: "127.0.0.1"}, true, "127.0.0.1/32"},
{"is past single", dummyBogon(), args{ip: "127.0.0.2"}, false, ""},
{"not in", dummyBogon(), args{ip: "1.2.3.4"}, false, ""},
{"in range", dummyBogon(), args{ip: "10.0.0.100"}, true, "10.0.0.0/8"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotIsIn, gotRepresentation := tt.b.Is(tt.args.ip)
if gotIsIn != tt.wantIsIn {
t.Errorf("Bogon.Is() gotIsIn = %v, want %v", gotIsIn, tt.wantIsIn)
}
if gotRepresentation != tt.wantRepresentation {
t.Errorf("Bogon.Is() gotRepresentation = %v, want %v", gotRepresentation, tt.wantRepresentation)
}
})
}
}
func TestNew(t *testing.T) {
type args struct {
cidrList []string
}
tests := []struct {
name string
args args
want *Bogon
wantErr bool
}{
{"valid", args{cidrList: []string{"10.0.0.0/8"}}, &Bogon{ipRange: []*net.IPNet{MustCIDR("10.0.0.0/8")}}, false},
{"valid", args{cidrList: []string{"fe80::/10"}}, &Bogon{ipRange: []*net.IPNet{MustCIDR("fe80::/10")}}, false},
{"invalid", args{cidrList: []string{"10.0.0.0/1000", "fe80::/10000"}}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.cidrList)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("New() = %v, want %v", got, tt.want)
}
})
}
}
func TestIs(t *testing.T) {
type args struct {
ip string
}
tests := []struct {
name string
args args
wantIsIn bool
wantRepresentation string
}{
{"is bogon", args{ip: "10.1.2.3"}, true, "10.0.0.0/8"},
{"is bogon", args{ip: "11.1.2.3"}, false, ""},
{"is bogon", args{ip: "fe80::5079:5bd6:8e43:5b80"}, true, "fe80::/10"},
{"is bogon", args{ip: "2607:f8b0:4009:815::200e"}, false, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotIsIn, gotRepresentation := Is(tt.args.ip)
if gotIsIn != tt.wantIsIn {
t.Errorf("Is() gotIsIn = %v, want %v", gotIsIn, tt.wantIsIn)
}
if gotRepresentation != tt.wantRepresentation {
t.Errorf("Is() gotRepresentation = %v, want %v", gotRepresentation, tt.wantRepresentation)
}
})
}
}