Debian Codegraph @

test-data/pkg/writer/decoder.go

raw
1 package writer
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "fmt"
7 "math"
8 "math/big"
9 "net/netip"
10
11 "github.com/maxmind/mmdbwriter"
12 "github.com/maxmind/mmdbwriter/mmdbtype"
13 "go4.org/netipx"
14 )
15
16 // WriteDecoderTestDB writes an mmdb file with all possible record value types.
17 func (w *Writer) WriteDecoderTestDB() error {
18 dbWriter, err := mmdbwriter.New(
19 mmdbwriter.Options{
20 DatabaseType: "MaxMind DB Decoder Test",
21 Description: map[string]string{
22 "en": "MaxMind DB Decoder Test database - contains every MaxMind DB data type",
23 },
24 DisableIPv4Aliasing: false,
25 IncludeReservedNetworks: true,
26 IPVersion: 6,
27 Languages: []string{"en"},
28 RecordSize: 24,
29 },
30 )
31 if err != nil {
32 return fmt.Errorf("creating mmdbwriter: %w", err)
33 }
34
35 addrs, err := parseIPSlice(ipSample)
36 if err != nil {
37 return fmt.Errorf("parsing ip addresses: %w", err)
38 }
39 if err := insertAllTypes(dbWriter, addrs); err != nil {
40 return fmt.Errorf("inserting all types records: %w", err)
41 }
42
43 zeroAddr, err := netip.ParsePrefix("::0.0.0.0/128")
44 if err != nil {
45 return fmt.Errorf("parsing ip: %w", err)
46 }
47 if err := insertAllTypesZero(dbWriter, []netip.Prefix{zeroAddr}); err != nil {
48 return fmt.Errorf("inserting all types records: %w", err)
49 }
50
51 maxAddr, err := netip.ParsePrefix("::255.255.255.255/128")
52 if err != nil {
53 return fmt.Errorf("parsing ip: %w", err)
54 }
55 if err := insertNumericMax(dbWriter, []netip.Prefix{maxAddr}); err != nil {
56 return fmt.Errorf("inserting all types records: %w", err)
57 }
58
59 if err := w.write(dbWriter, "MaxMind-DB-test-decoder.mmdb"); err != nil {
60 return fmt.Errorf("writing database: %w", err)
61 }
62 return nil
63 }
64
65 // insertAllTypes inserts records with all possible value types.
66 func insertAllTypes(w *mmdbwriter.Tree, ipAddresses []netip.Prefix) error {
67 buf := new(bytes.Buffer)
68 if err := binary.Write(buf, binary.BigEndian, uint32(42)); err != nil {
69 return fmt.Errorf("creating buffer for all types record: %w", err)
70 }
71
72 ui64 := big.Int{}
73 ui64.Lsh(big.NewInt(1), 60)
74
75 ui128 := big.Int{}
76 ui128.Lsh(big.NewInt(1), 120)
77 mmdbUint128 := mmdbtype.Uint128(ui128)
78
79 allTypes := mmdbtype.Map{
80 "array": mmdbtype.Slice{
81 mmdbtype.Uint32(1),
82 mmdbtype.Uint32(2),
83 mmdbtype.Uint32(3),
84 },
85 "bytes": mmdbtype.Bytes(buf.Bytes()),
86 "boolean": mmdbtype.Bool(true),
87 "double": mmdbtype.Float64(42.123456),
88 "float": mmdbtype.Float32(1.1),
89 "int32": mmdbtype.Int32(-1 * math.Pow(2, 28)),
90 "map": mmdbtype.Map{
91 "mapX": mmdbtype.Map{
92 "utf8_stringX": mmdbtype.String("hello"),
93 "arrayX": mmdbtype.Slice{
94 mmdbtype.Uint32(7),
95 mmdbtype.Uint32(8),
96 mmdbtype.Uint32(9),
97 },
98 },
99 },
100 "uint16": mmdbtype.Uint16(100),
101 "uint32": mmdbtype.Uint32(math.Pow(2, 28)),
102 "uint64": mmdbtype.Uint64(ui64.Uint64()),
103 "uint128": mmdbUint128.Copy(),
104 "utf8_string": mmdbtype.String("unicode! ☯ - ♫"),
105 }
106
107 for _, addr := range ipAddresses {
108 err := w.Insert(
109 netipx.PrefixIPNet(addr),
110 allTypes,
111 )
112 if err != nil {
113 return fmt.Errorf("inserting ip: %w", err)
114 }
115 }
116 return nil
117 }
118
119 // insertAllTypesZero inserts records with all possible value types with zero values.
120 func insertAllTypesZero(w *mmdbwriter.Tree, ipAddresses []netip.Prefix) error {
121 var uint128 big.Int
122 mmdbUint128 := mmdbtype.Uint128(uint128)
123
124 zeroValues := mmdbtype.Map{
125 "array": mmdbtype.Slice{},
126 "bytes": mmdbtype.Bytes([]byte{}),
127 "boolean": mmdbtype.Bool(false),
128 "double": mmdbtype.Float64(0),
129 "float": mmdbtype.Float32(0),
130 "int32": mmdbtype.Int32(0),
131 "map": mmdbtype.Map{},
132 "uint16": mmdbtype.Uint16(0),
133 "uint32": mmdbtype.Uint32(0),
134 "uint64": mmdbtype.Uint64(0),
135 "uint128": mmdbUint128.Copy(),
136 "utf8_string": mmdbtype.String(""),
137 }
138
139 for _, addr := range ipAddresses {
140 err := w.Insert(
141 netipx.PrefixIPNet(addr),
142 zeroValues,
143 )
144 if err != nil {
145 return fmt.Errorf("inserting ip: %w", err)
146 }
147 }
148 return nil
149 }
150
151 // insertNumericMax inserts records with numeric types maxed out.
152 func insertNumericMax(w *mmdbwriter.Tree, ipAddresses []netip.Prefix) error {
153 var uint128Max big.Int
154 uint128Max.Exp(big.NewInt(2), big.NewInt(128), nil)
155 uint128Max.Sub(&uint128Max, big.NewInt(1))
156 mmdbUint128 := mmdbtype.Uint128(uint128Max)
157
158 numMax := mmdbtype.Map{
159 "double": mmdbtype.Float64(math.Inf(1)),
160 "float": mmdbtype.Float32(float32(math.Inf(1))),
161 "int32": mmdbtype.Int32(1<<31 - 1),
162 "uint16": mmdbtype.Uint16(0xffff),
163 "uint32": mmdbtype.Uint32(0xffffffff),
164 "uint64": mmdbtype.Uint64(0xffffffffffffffff),
165 "uint128": mmdbUint128.Copy(),
166 }
167
168 for _, addr := range ipAddresses {
169 err := w.Insert(
170 netipx.PrefixIPNet(addr),
171 numMax,
172 )
173 if err != nil {
174 return fmt.Errorf("inserting ip: %w", err)
175 }
176 }
177 return nil
178 }