-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreader_test.go
131 lines (113 loc) · 3.9 KB
/
reader_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
package main
import (
"bytes"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
)
// Fri Feb 27 22:11:38 CET 2015 File of 4.2M
// BenchmarkReader 1 1181852831 ns/op (~1.18s)
// Mon Mar 16 14:33:52 CET 2015
// BenchmarkReader 1 1235675316 ns/op
func BenchmarkReader(b *testing.B) {
path := filepath.Join(os.Getenv("HOME"), ".rir", "ripencc", "latest")
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Cannot read file for bench. %v", err)
}
if len(content) == 0 {
log.Fatal(" File for bench is empty!")
}
b.ResetTimer()
data := bytes.NewBuffer(content)
NewReader(data).Read()
}
func findIpWith(records *Records, address string) *IpRecord {
for _, ip := range records.Ips {
if address == ip.Start.String() {
return ip
}
}
log.Fatalf("Cannot find ip with address %s", address)
return &IpRecord{}
}
func findAsnWith(records *Records, number int) *AsnRecord {
for _, asn := range records.Asns {
if number == asn.Start {
return asn
}
}
log.Fatalf("Cannot find asn with number %d", number)
return &AsnRecord{}
}
func TestParsingRegularFile(t *testing.T) {
data := bytes.NewBufferString(regularData)
records, _ := NewReader(data).Read()
recordsCount, asnCount, ipv4Count, ipv6Count := 23486, 3986, 17947, 1553
if records.Version != 2.3 {
t.Errorf("records version: expected 2.3 got %f", records.Version)
}
if records.Count != recordsCount {
t.Errorf("total records count: expected %d got %d", recordsCount, records.Count)
}
if records.AsnCount != asnCount {
t.Errorf("asn count: expected %d got %d", asnCount, records.AsnCount)
}
if records.Ipv4Count != ipv4Count {
t.Errorf("ipv4 count: expected %d got %d", ipv4Count, records.Ipv4Count)
}
if records.Ipv6Count != ipv6Count {
t.Errorf("ipv6 count: expected %d got %d", ipv6Count, records.Ipv6Count)
}
if len(records.Asns) != 2 {
t.Errorf("asn real count: expected %d got %d", 2, len(records.Asns))
}
if len(records.Ips) != 10 {
t.Errorf("ips real count: expected %d got %d. Content: %v", 10, len(records.Ips), records.Ips)
}
asnRecord := findAsnWith(records, 173)
if asnRecord.Status != "allocated" {
t.Errorf("asn record status: expected 'allocated' got %q", asnRecord.Status)
}
if asnRecord.OpaqueId != "A91BD5FB" {
t.Errorf("asn record opaque id: expected 'A91BD5FB' got %q", asnRecord.OpaqueId)
}
ipRecord := findIpWith(records, "203.81.160.0")
if ipRecord.Status != "assigned" {
t.Errorf("ip record status: expected 'assigned' got %q", ipRecord.Status)
}
if ipRecord.OpaqueId != "" {
t.Errorf("ip record opaque id: expected empty got %q", ipRecord.OpaqueId)
}
otherIpRecord := findIpWith(records, "193.9.26.0")
if otherIpRecord.Status != "assigned" {
t.Errorf("ip record status: expected 'assigned' got %q", otherIpRecord.Status)
}
if otherIpRecord.OpaqueId != "A91872ED" {
t.Errorf("ip record opaque id: expected 'A91872ED' got %q", otherIpRecord.OpaqueId)
}
splitRecord := findIpWith(records, "193.18.0.0")
splitNets := splitRecord.Net()
if !(len(splitNets) == 2 && splitNets[0].String() == "193.18.0.0/16" && splitNets[1].String() == "193.19.0.0/19") {
t.Errorf("invalid parsing of split subnet, got: %v", splitNets)
}
}
var regularData = `2.3|apnic|20110113|23486|19850701|20110112|+1000
# line to be ignored
apnic|*|asn|*|3986|summary
apnic|*|ipv4|*|17947|summary
apnic|*|ipv6|*|1553|summary
apnic|JP|asn|173|1|20020801|allocated|A91BD5FB
apnic|NZ|asn|681|1|20020801|allocated
apnic|MM|ipv4|203.81.64.0|8192|20100504|assigned
apnic|MM|ipv4|203.81.160.0|4096|20100122|assigned
apnic|KP|ipv4|175.45.176.0|1024|20100122|assigned
apnic|JP|ipv6|2001:200::|35|19990813|allocated
apnic|JP|ipv6|2001:200:2000::|35|20030423|allocated
apnic|JP|ipv6|2001:200:4000::|34|20030423|allocated
apnic|JP|ipv6|2001:200:8000::|33|20030423|allocated
ripencc|PL|ipv4|193.9.25.0|256|20090225|assigned
ripencc|DE|ipv4|193.18.0.0|73728|19920922|assigned
ripencc|HU|ipv4|193.9.26.0|512|20081222|assigned|A91872ED`