-
Notifications
You must be signed in to change notification settings - Fork 5
/
peeringdb_test.go
195 lines (152 loc) · 4.44 KB
/
peeringdb_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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"github.com/ipcjk/ixgen/libapiserver"
"github.com/ipcjk/ixgen/peeringdb"
"net/http"
"testing"
)
var apiserverDbTest *libapiserver.Apiserver
var apikey = "29Rln1SH.AfvG0jhVczbYVdPsVGnKbwtr2g9Y7qit"
func init() {
apiserverDbTest = libapiserver.NewAPIServer("localhost:0", "./cache", "./templates", "./configuration")
apiserverDbTest.RunAPIServer()
}
func TestGetIXByName(t *testing.T) {
peerDB := peeringdb.Peeringdb("https://www.peeringdb.com/api", apikey)
ix, err := peerDB.SearchIXByIxName("DE-CIX Frankfurt")
if err != nil {
t.Errorf("Cant search by IxName: %s", err)
}
if ix.Data[0].Name != "DE-CIX Frankfurt" {
t.Error("Cant find the DE-CIX Frankfurt, something wrong the data-set!")
}
}
func TestGetIXByID(t *testing.T) {
peerDB := peeringdb.Peeringdb("https://www.peeringdb.com/api", apikey)
ix, err := peerDB.SearchIXByIxId("73")
if err != nil {
t.Errorf("Cant search by IxID: %s", err)
}
if ix.Data[0].Name != "ECIX-MUC" {
t.Error("Cant find the INXS, something wrong the data-set!")
}
}
func TestRechabilityPeeringDB(t *testing.T) {
_, err := http.Get("https://www.peeringdb.com/api")
if err != nil {
t.Error("Cant connect to peeringdb.com")
}
}
func TestGetASN(t *testing.T) {
peerDB := peeringdb.Peeringdb("https://www.peeringdb.com/api", apikey)
peer, err := peerDB.GetNetworkByAsN(196922)
if err != nil {
t.Errorf("Cant search by the target ASN: %s", err)
}
if peer.Data[0].Asn != 196922 {
t.Error("Cant find my home asn, something is wrong!")
}
if peer.Data[0].Name != "Hofmeir Media" {
t.Error("Cant find my home network name, something is wrong! Found ", peer.Data[0].Name)
}
peer, err = peerDB.GetNetworkByAsN(2914)
if err != nil {
t.Errorf("Cant search by the target ASN: %s", err)
}
if peer.Data[0].Asn != 2914 {
t.Error("Cant find NTT in the peering db?")
}
}
func TestRunAPIserver(t *testing.T) {
_, err := http.Get("http://" + apiserverDbTest.AddrPort + "/api")
if err != nil {
t.Error("Cant connect to api service on localhost")
}
}
func TestQueryAPIserver(t *testing.T) {
peerDB := peeringdb.Peeringdb("http://"+apiserverDbTest.AddrPort+"/api", apikey)
ix, err := peerDB.SearchIXByIxName("DE-CIX Frankfurt")
if err != nil {
t.Errorf("Cant search by the IxName: %s", err)
}
if ix.Data[0].Name != "DE-CIX Frankfurt" {
t.Error("Cant find the INXS, something wrong the data-set!")
}
}
func TestGetASNLocalApi(t *testing.T) {
peerDB := peeringdb.Peeringdb("http://"+apiserverDbTest.AddrPort+"/api", apikey)
peer, err := peerDB.GetNetworkByAsN(196922)
if err != nil {
t.Errorf("Cant search by target ASN: %s", err)
}
if peer.Data[0].Asn != 196922 {
t.Error("Cant find my home asn, something is wrong!")
}
if peer.Data[0].Name != "Hofmeir Media" {
t.Error("Cant find my home network name, something is wrong!")
}
peer, err = peerDB.GetNetworkByAsN(2914)
if err != nil {
t.Errorf("Cant search by target ASN: %s", err)
}
if peer.Data[0].Asn != 2914 {
t.Error("Cant find NTT in the peering db?")
}
}
func BenchmarkAPIserver(b *testing.B) {
peerDB := peeringdb.Peeringdb("http://"+apiserverDbTest.AddrPort+"/api", apikey)
for i := 0; i < b.N; i++ {
peerDB.GetNetworkByAsN(196922)
}
}
func TestGetPeersOnIX(t *testing.T) {
peerDB := peeringdb.Peeringdb("http://"+apiserverDbTest.AddrPort+"/api", apikey)
myPeers, err := peerDB.GetPeersOnIX("DE-CIX Frankfurt", "0", false)
if err != nil {
t.Errorf("Cant query the API for peers on IX: %s", err)
}
if len(myPeers.Data) < 700 {
t.Error("DE-CIX Frankfurt||Main too small")
}
/* Query by static id */
myPeers, err = peerDB.GetPeersOnIX("", "73", true)
if err != nil {
t.Errorf("Cant query the API for peers on IX: %s", err)
}
if len(myPeers.Data) < 30 {
t.Error("ECIX-MUC too small")
}
}
func mini(a int, b int) int {
if a <= b {
return a
}
return b
}
func TestSplitASN(t *testing.T) {
var list []int
for x := 0; x < 120; x++ {
list = append(list, x)
}
for {
if len(list) == 0 {
break
}
mincut := mini(9, len(list))
list = list[mincut:]
}
}
func TestGetASNsbyList(t *testing.T) {
asnList := []string{"196922", "3356"}
peerDB := peeringdb.Peeringdb("https://www.peeringdb.com/api", apikey)
peers, err := peerDB.GetASNsbyList(asnList)
if err != nil {
t.Errorf("Cant query the API for peers on IX: %s", err)
}
if peers.Data[0].Asn != 3356 {
t.Error("Cant find Lumen in the peering db?")
}
if peers.Data[1].Asn != 196922 {
t.Error("Cant find myself in the peering db?")
}
}