btest/files: Add mmdb testing databases and generator code

This commit is contained in:
Arne Welzel 2023-10-23 23:19:09 +02:00
parent 688d68cbf6
commit 05922132b3
7 changed files with 112 additions and 0 deletions

1
testing/btest/Files/mmdb/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
testmmdb

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

View file

@ -0,0 +1,13 @@
These .mmdb databases were created with the mmdbwriter from MaxMind [1] for
testing purposes. See the main.go file. They only contain information about
LBL's network ranges:
128.3.0.0/16
131.243.0.0/16
Rebuild with:
go build
./testmmdb
[1] https://github.com/maxmind/mmdbwriter

View file

@ -0,0 +1,11 @@
module testmmdb
go 1.21.0
require github.com/maxmind/mmdbwriter v1.0.0
require (
github.com/oschwald/maxminddb-golang v1.12.0 // indirect
go4.org/netipx v0.0.0-20220812043211-3cc044ffd68d // indirect
golang.org/x/sys v0.10.0 // indirect
)

View file

@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/maxmind/mmdbwriter v1.0.0 h1:bieL4P6yaYaHvbtLSwnKtEvScUKKD6jcKaLiTM3WSMw=
github.com/maxmind/mmdbwriter v1.0.0/go.mod h1:noBMCUtyN5PUQ4H8ikkOvGSHhzhLok51fON2hcrpKj8=
github.com/oschwald/maxminddb-golang v1.12.0 h1:9FnTOD0YOhP7DGxGsq4glzpGy5+w7pq50AS6wALUMYs=
github.com/oschwald/maxminddb-golang v1.12.0/go.mod h1:q0Nob5lTCqyQ8WT6FYgS1L7PXKVVbgiymefNwIjPzgY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go4.org/netipx v0.0.0-20220812043211-3cc044ffd68d h1:ggxwEf5eu0l8v+87VhX1czFh8zJul3hK16Gmruxn7hw=
go4.org/netipx v0.0.0-20220812043211-3cc044ffd68d/go.mod h1:tgPU4N2u9RByaTN3NC2p9xOzyFpte4jYwsIIRF7XlSc=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View file

@ -0,0 +1,71 @@
// Create test MaxMind DB database files containing information about
// just LBL's IPv4 ranges for testing.
package main
import (
"log"
"net"
"os"
"github.com/maxmind/mmdbwriter"
"github.com/maxmind/mmdbwriter/mmdbtype"
)
func writeDb(fname, name string, record mmdbtype.Map, nets ...*net.IPNet) {
writer, err := mmdbwriter.New(
mmdbwriter.Options{
DatabaseType: name,
},
)
if err != nil {
log.Fatal(err)
}
for _, n := range nets {
if err = writer.Insert(n, record); err != nil {
log.Fatal(err)
}
}
fh, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer fh.Close()
_, err = writer.WriteTo(fh)
if err != nil {
log.Fatal(err)
}
}
func main() {
_, net1, _ := net.ParseCIDR("128.3.0.0/16")
_, net2, _ := net.ParseCIDR("131.243.0.0/16")
// The ASN record.
asn_record := mmdbtype.Map{}
asn_record["autonomous_system_number"] = mmdbtype.Uint32(16)
asn_record["autonomous_system_organization"] = mmdbtype.String("Lawrence Berkeley National Laboratory")
writeDb("GeoLite2-ASN.mmdb", "My-ASN-DB", asn_record, net1, net2)
// The Location record.
loc_record := mmdbtype.Map{
"country": mmdbtype.Map{
"iso_code": mmdbtype.String("US"),
"names": mmdbtype.Map{
"en": mmdbtype.String("United States"),
},
},
"location": mmdbtype.Map{
"latitude": mmdbtype.Float64(37.75100),
"longitude": mmdbtype.Float64(-97.822000),
},
"city": mmdbtype.Map{
"names": mmdbtype.Map{
"en": mmdbtype.String("Berkeley"),
},
},
}
writeDb("GeoLite2-City.mmdb", "My-City-DB", loc_record, net1, net2)
}