Skip to content

Commit

Permalink
Replace submodule for generated test db
Browse files Browse the repository at this point in the history
Finally addresses #35.
  • Loading branch information
fgsch committed Jan 3, 2024
1 parent a3ce817 commit 4a2af53
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 46 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "maxmind-db"]
path = maxmind-db
url = https://github.com/maxmind/MaxMind-DB.git
3 changes: 0 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,3 @@ SUBDIRS = src
DISTCHECK_CONFIGURE_FLAGS = VMOD_DIR='$${libdir}/varnish/vmods'

dist_doc_DATA = LICENSE

EXTRA_DIST = $(top_srcdir)/maxmind-db/LICENSE \
$(top_srcdir)/maxmind-db/test-data/*.mmdb
1 change: 0 additions & 1 deletion maxmind-db
Submodule maxmind-db deleted from c1c744
5 changes: 2 additions & 3 deletions src/tests/b00000.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ server s1 {
txresp
} -start

shell "cp ${vmod_topsrc}/maxmind-db/test-data/*.mmdb ${tmpdir}"
shell "cp ${vmod_topsrc}/src/tests/libvmod-geoip2.mmdb ${tmpdir}"

varnish v1 -vcl+backend {
import geoip2 from "${vmod_topbuild}/src/.libs/libvmod_geoip2.so";
import std;

sub vcl_init {
new db1 = geoip2.geoip2(
"${tmpdir}/MaxMind-DB-test-decoder.mmdb");
new db1 = geoip2.geoip2("${tmpdir}/libvmod-geoip2.mmdb");
}

sub vcl_deliver {
Expand Down
11 changes: 4 additions & 7 deletions src/tests/b00001.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ server s1 {
txresp
} -start

shell "cp ${vmod_topsrc}/maxmind-db/test-data/*.mmdb ${tmpdir}"
shell "cp ${vmod_topsrc}/src/tests/libvmod-geoip2.mmdb ${tmpdir}"

varnish v1 -vcl+backend {
import geoip2 from "${vmod_topbuild}/src/.libs/libvmod_geoip2.so";
import std;

sub vcl_init {
new db1 = geoip2.geoip2(
"${tmpdir}/GeoIP2-City-Test.mmdb");
new db2 = geoip2.geoip2(
"${tmpdir}/GeoIP2-Connection-Type-Test.mmdb");
new db3 = geoip2.geoip2(
"${tmpdir}/GeoIP2-ISP-Test.mmdb");
new db1 = geoip2.geoip2("${tmpdir}/libvmod-geoip2.mmdb");
new db2 = geoip2.geoip2("${tmpdir}/libvmod-geoip2.mmdb");
new db3 = geoip2.geoip2("${tmpdir}/libvmod-geoip2.mmdb");
}

sub vcl_deliver {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/b00002.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ server s1 -repeat 2 {
txresp
} -start

shell "cp ${vmod_topsrc}/maxmind-db/test-data/*.mmdb ${tmpdir}"
shell "cp ${vmod_topsrc}/src/tests/libvmod-geoip2.mmdb ${tmpdir}"

varnish v1 -arg "-p thread_pools=1" -vcl+backend { } -start

Expand All @@ -25,7 +25,7 @@ varnish v1 -vcl+backend {
import std;

sub vcl_init {
new db = geoip2.geoip2("${tmpdir}/GeoIP2-City-Test.mmdb");
new db = geoip2.geoip2("${tmpdir}/libvmod-geoip2.mmdb");
}

sub vcl_deliver {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/b00004.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ server s1 {
txresp
} -start

shell "cp ${vmod_topsrc}/maxmind-db/test-data/*.mmdb ${tmpdir}"
shell "cp ${vmod_topsrc}/src/tests/libvmod-geoip2.mmdb ${tmpdir}"

varnish v1 -vcl+backend {
import geoip2 from "${vmod_topbuild}/src/.libs/libvmod_geoip2.so";
import std;

sub vcl_init {
new db = geoip2.geoip2("${tmpdir}/GeoIP2-City-Test.mmdb");
new db = geoip2.geoip2("${tmpdir}/libvmod-geoip2.mmdb");
std.log(db.lookup(
"city/names/en",
std.ip("0.0.0.0", "0.0.0.0")));
Expand Down
4 changes: 2 additions & 2 deletions src/tests/b00005.vtc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ server s1 {
txresp
} -start

shell "cp ${vmod_topsrc}/maxmind-db/test-data/*.mmdb ${tmpdir}"
shell "cp ${vmod_topsrc}/src/tests/libvmod-geoip2.mmdb ${tmpdir}"

varnish v1 -vcl+backend {
import geoip2 from "${vmod_topbuild}/src/.libs/libvmod_geoip2.so";
import std;
import vtc;

sub vcl_init {
new db = geoip2.geoip2("${tmpdir}/GeoIP2-City-Test.mmdb");
new db = geoip2.geoip2("${tmpdir}/libvmod-geoip2.mmdb");
std.log(db.lookup("city/names/en", vtc.no_ip()));
}
} -start
Expand Down
Binary file modified src/tests/libvmod-geoip2.mmdb
Binary file not shown.
112 changes: 112 additions & 0 deletions src/tests/mmdb-writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"encoding/csv"
"encoding/hex"
"io"
"log"
"net"
"os"
"strconv"
"strings"

"github.com/maxmind/mmdbwriter"
"github.com/maxmind/mmdbwriter/inserter"
"github.com/maxmind/mmdbwriter/mmdbtype"
)

func makeRecord(key, mmdbType, value string) mmdbtype.DataType {
var mmdbValue mmdbtype.DataType
switch mmdbType {
case "bool":
v, _ := strconv.ParseBool(value)
mmdbValue = mmdbtype.Bool(v)
case "bytes":
v, _ := hex.DecodeString(value)
mmdbValue = mmdbtype.Bytes(v)
case "double":
v, _ := strconv.ParseFloat(value, 64)
mmdbValue = mmdbtype.Float64(v)
case "float":
v, _ := strconv.ParseFloat(value, 32)
mmdbValue = mmdbtype.Float32(v)
case "int32":
v, _ := strconv.ParseInt(value, 10, 32)
mmdbValue = mmdbtype.Int32(v)
case "uint16":
v, _ := strconv.ParseInt(value, 10, 16)
mmdbValue = mmdbtype.Uint16(v)
case "uint32":
v, _ := strconv.ParseInt(value, 10, 32)
mmdbValue = mmdbtype.Uint32(v)
case "uint64":
v, _ := strconv.ParseInt(value, 10, 64)
mmdbValue = mmdbtype.Uint64(v)
case "string":
mmdbValue = mmdbtype.String(value)
case "map":
keys := strings.Split(key, "/")
key = keys[0]
mmdbValue = mmdbtype.Map{
mmdbtype.String(keys[1]): mmdbtype.Map{
mmdbtype.String(keys[2]): mmdbtype.String(value),
},
}
}
record := mmdbtype.Map{}
record[mmdbtype.String(key)] = mmdbValue

return record
}

func main() {
writer, err := mmdbwriter.New(
mmdbwriter.Options{
DatabaseType: "libvmod-geoip2",
Description: map[string]string{"en": "libvmod-geoip2 test database"},
IncludeReservedNetworks: true,
Inserter: inserter.TopLevelMergeWith,
},
)
if err != nil {
log.Fatal(err)
}

file, err := os.Open("test-data.csv")
if err != nil {
log.Fatal(err)
}

reader := csv.NewReader(file)

for {
row, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}

_, network, err := net.ParseCIDR(row[0])
if err != nil {
log.Fatal(err)
}

record := makeRecord(row[1], row[2], row[3])
err = writer.Insert(network, record)
if err != nil {
log.Fatal(err)
}
}

file, err = os.Create("libvmod-geoip2.mmdb")
if err != nil {
log.Fatal(err)
}

_, err = writer.WriteTo(file)
if err != nil {
log.Fatal(err)
}
}
15 changes: 15 additions & 0 deletions src/tests/test-data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"1.0.128.1/32","connection_type","string","Cable/DSL"
"1.0.128.1/32","isp","string","TOT Public Company Limited"
"1.1.1.1/32","boolean","bool","true"
"1.1.1.1/32","bytes","bytes","0000002a"
"1.1.1.1/32","double","double","42.123456"
"1.1.1.1/32","float","float","1.100000"
"1.1.1.1/32","int32","int32","-268435456"
"1.1.1.1/32","map/mapX/utf8_stringX","map","hello"
"1.1.1.1/32","uint16","uint16","100"
"1.1.1.1/32","uint32","uint32","268435456"
"1.1.1.1/32","uint64","uint64","1152921504606846976"
"1.1.1.1/32","utf8_string","string","unicode! ☯ - ♫"
"127.0.0.1/32","long_string","string","thequickbrownfoxjumpsoverthelazydog1234567890"
"2001:218::1/32","country/names/en","map","Japan"
"81.2.69.192/32","city/names/en","map","London"
23 changes: 0 additions & 23 deletions src/tests/test-db-writer.pl

This file was deleted.

0 comments on commit 4a2af53

Please sign in to comment.