github.com/simcap/rir@v0.0.0-20220118092508-5563ee4470e2/reader_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "log" 7 "os" 8 "path/filepath" 9 "testing" 10 ) 11 12 // Fri Feb 27 22:11:38 CET 2015 File of 4.2M 13 // BenchmarkReader 1 1181852831 ns/op (~1.18s) 14 15 // Mon Mar 16 14:33:52 CET 2015 16 // BenchmarkReader 1 1235675316 ns/op 17 func BenchmarkReader(b *testing.B) { 18 path := filepath.Join(os.Getenv("HOME"), ".rir", "ripencc", "latest") 19 content, err := ioutil.ReadFile(path) 20 if err != nil { 21 log.Fatalf("Cannot read file for bench. %v", err) 22 } 23 if len(content) == 0 { 24 log.Fatal(" File for bench is empty!") 25 } 26 b.ResetTimer() 27 data := bytes.NewBuffer(content) 28 NewReader(data).Read() 29 } 30 31 func findIpWith(records *Records, address string) *IpRecord { 32 for _, ip := range records.Ips { 33 if address == ip.Start.String() { 34 return ip 35 } 36 } 37 log.Fatalf("Cannot find ip with address %s", address) 38 return &IpRecord{} 39 } 40 41 func findAsnWith(records *Records, number int) *AsnRecord { 42 for _, asn := range records.Asns { 43 if number == asn.Start { 44 return asn 45 } 46 } 47 log.Fatalf("Cannot find asn with number %d", number) 48 return &AsnRecord{} 49 50 } 51 52 func TestParsingRegularFile(t *testing.T) { 53 data := bytes.NewBufferString(regularData) 54 55 records, _ := NewReader(data).Read() 56 57 recordsCount, asnCount, ipv4Count, ipv6Count := 23486, 3986, 17947, 1553 58 59 if records.Version != 2.3 { 60 t.Errorf("records version: expected 2.3 got %f", records.Version) 61 } 62 if records.Count != recordsCount { 63 t.Errorf("total records count: expected %d got %d", recordsCount, records.Count) 64 } 65 if records.AsnCount != asnCount { 66 t.Errorf("asn count: expected %d got %d", asnCount, records.AsnCount) 67 } 68 if records.Ipv4Count != ipv4Count { 69 t.Errorf("ipv4 count: expected %d got %d", ipv4Count, records.Ipv4Count) 70 } 71 if records.Ipv6Count != ipv6Count { 72 t.Errorf("ipv6 count: expected %d got %d", ipv6Count, records.Ipv6Count) 73 } 74 75 if len(records.Asns) != 2 { 76 t.Errorf("asn real count: expected %d got %d", 2, len(records.Asns)) 77 } 78 79 if len(records.Ips) != 10 { 80 t.Errorf("ips real count: expected %d got %d. Content: %v", 10, len(records.Ips), records.Ips) 81 } 82 83 asnRecord := findAsnWith(records, 173) 84 if asnRecord.Status != "allocated" { 85 t.Errorf("asn record status: expected 'allocated' got %q", asnRecord.Status) 86 } 87 if asnRecord.OpaqueId != "A91BD5FB" { 88 t.Errorf("asn record opaque id: expected 'A91BD5FB' got %q", asnRecord.OpaqueId) 89 } 90 91 ipRecord := findIpWith(records, "203.81.160.0") 92 if ipRecord.Status != "assigned" { 93 t.Errorf("ip record status: expected 'assigned' got %q", ipRecord.Status) 94 } 95 if ipRecord.OpaqueId != "" { 96 t.Errorf("ip record opaque id: expected empty got %q", ipRecord.OpaqueId) 97 } 98 99 otherIpRecord := findIpWith(records, "193.9.26.0") 100 if otherIpRecord.Status != "assigned" { 101 t.Errorf("ip record status: expected 'assigned' got %q", otherIpRecord.Status) 102 } 103 if otherIpRecord.OpaqueId != "A91872ED" { 104 t.Errorf("ip record opaque id: expected 'A91872ED' got %q", otherIpRecord.OpaqueId) 105 } 106 107 splitRecord := findIpWith(records, "193.18.0.0") 108 splitNets := splitRecord.Net() 109 if !(len(splitNets) == 2 && splitNets[0].String() == "193.18.0.0/16" && splitNets[1].String() == "193.19.0.0/19") { 110 t.Errorf("invalid parsing of split subnet, got: %v", splitNets) 111 } 112 } 113 114 var regularData = `2.3|apnic|20110113|23486|19850701|20110112|+1000 115 # line to be ignored 116 apnic|*|asn|*|3986|summary 117 apnic|*|ipv4|*|17947|summary 118 119 apnic|*|ipv6|*|1553|summary 120 apnic|JP|asn|173|1|20020801|allocated|A91BD5FB 121 apnic|NZ|asn|681|1|20020801|allocated 122 apnic|MM|ipv4|203.81.64.0|8192|20100504|assigned 123 apnic|MM|ipv4|203.81.160.0|4096|20100122|assigned 124 apnic|KP|ipv4|175.45.176.0|1024|20100122|assigned 125 apnic|JP|ipv6|2001:200::|35|19990813|allocated 126 apnic|JP|ipv6|2001:200:2000::|35|20030423|allocated 127 apnic|JP|ipv6|2001:200:4000::|34|20030423|allocated 128 apnic|JP|ipv6|2001:200:8000::|33|20030423|allocated 129 ripencc|PL|ipv4|193.9.25.0|256|20090225|assigned 130 ripencc|DE|ipv4|193.18.0.0|73728|19920922|assigned 131 ripencc|HU|ipv4|193.9.26.0|512|20081222|assigned|A91872ED`