"Fossies" - the Fresh Open Source Software Archive 
Member "libmaxminddb-1.5.2/t/data_types_t.c" (18 Feb 2021, 17336 Bytes) of package /linux/misc/libmaxminddb-1.5.2.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the latest
Fossies "Diffs" side-by-side code changes report for "data_types_t.c":
1.5.0_vs_1.5.2.
1 #include "maxminddb_test_helper.h"
2
3 void test_all_data_types(MMDB_lookup_result_s *result,
4 const char *ip,
5 const char *UNUSED(filename),
6 const char *mode_desc) {
7 {
8 char description[500];
9 snprintf(
10 description, 500, "utf8_string field for %s - %s", ip, mode_desc);
11
12 MMDB_entry_data_s data = data_ok(result,
13 MMDB_DATA_TYPE_UTF8_STRING,
14 description,
15 "utf8_string",
16 NULL);
17 const char *string = mmdb_strndup(data.utf8_string, data.data_size);
18 // This is hex for "unicode! ☯ - ♫" as bytes
19 char expect[19] = {0x75,
20 0x6e,
21 0x69,
22 0x63,
23 0x6f,
24 0x64,
25 0x65,
26 0x21,
27 0x20,
28 0xe2,
29 0x98,
30 0xaf,
31 0x20,
32 0x2d,
33 0x20,
34 0xe2,
35 0x99,
36 0xab,
37 0x00};
38 is(string, expect, "got expected utf8_string value");
39
40 free((char *)string);
41 }
42
43 {
44 char description[500];
45 snprintf(description, 500, "double field for %s - %s", ip, mode_desc);
46
47 MMDB_entry_data_s data =
48 data_ok(result, MMDB_DATA_TYPE_DOUBLE, description, "double", NULL);
49
50 compare_double(data.double_value, 42.123456);
51 }
52
53 {
54 char description[500];
55 snprintf(description, 500, "float field for %s - %s", ip, mode_desc);
56
57 MMDB_entry_data_s data =
58 data_ok(result, MMDB_DATA_TYPE_FLOAT, description, "float", NULL);
59
60 compare_float(data.float_value, 1.1F);
61 }
62
63 {
64 char description[500];
65 snprintf(description, 500, "bytes field for %s - %s", ip, mode_desc);
66
67 MMDB_entry_data_s data =
68 data_ok(result, MMDB_DATA_TYPE_BYTES, description, "bytes", NULL);
69 uint8_t expect[] = {0x00, 0x00, 0x00, 0x2a};
70 ok(memcmp((uint8_t *)data.bytes, expect, 4) == 0,
71 "bytes field has expected value");
72 }
73
74 {
75 char description[500];
76 snprintf(description, 500, "uint16 field for %s - %s", ip, mode_desc);
77
78 MMDB_entry_data_s data =
79 data_ok(result, MMDB_DATA_TYPE_UINT16, description, "uint16", NULL);
80 uint16_t expect = 100;
81 ok(data.uint16 == expect, "uint16 field is 100");
82 }
83
84 {
85 char description[500];
86 snprintf(description, 500, "uint32 field for %s - %s", ip, mode_desc);
87
88 MMDB_entry_data_s data =
89 data_ok(result, MMDB_DATA_TYPE_UINT32, description, "uint32", NULL);
90 uint32_t expect = 1 << 28;
91 cmp_ok(data.uint32, "==", expect, "uint32 field is 2**28");
92 }
93
94 {
95 char description[500];
96 snprintf(description, 500, "int32 field for %s - %s", ip, mode_desc);
97
98 MMDB_entry_data_s data =
99 data_ok(result, MMDB_DATA_TYPE_INT32, description, "int32", NULL);
100 int32_t expect = 1 << 28;
101 expect *= -1;
102 cmp_ok(data.int32, "==", expect, "int32 field is -(2**28)");
103 }
104
105 {
106 char description[500];
107 snprintf(description, 500, "uint64 field for %s - %s", ip, mode_desc);
108
109 MMDB_entry_data_s data =
110 data_ok(result, MMDB_DATA_TYPE_UINT64, description, "uint64", NULL);
111 uint64_t expect = 1;
112 expect <<= 60;
113 cmp_ok(data.uint64, "==", expect, "uint64 field is 2**60");
114 }
115
116 {
117 char description[500];
118 snprintf(description, 500, "uint128 field for %s - %s", ip, mode_desc);
119
120 MMDB_entry_data_s data = data_ok(
121 result, MMDB_DATA_TYPE_UINT128, description, "uint128", NULL);
122 #if MMDB_UINT128_IS_BYTE_ARRAY
123 uint8_t expect[16] = {0x01,
124 0x00,
125 0x00,
126 0x00,
127 0x00,
128 0x00,
129 0x00,
130 0x00,
131 0x00,
132 0x00,
133 0x00,
134 0x00,
135 0x00,
136 0x00,
137 0x00,
138 0x00};
139 ok(memcmp(data.uint128, expect, 16) == 0, "uint128 field is 2**120");
140 #else
141 mmdb_uint128_t expect = 1;
142 expect <<= 120;
143 cmp_ok(data.uint128, "==", expect, "uint128 field is 2**120");
144 #endif
145 }
146
147 {
148 char description[500];
149 snprintf(description, 500, "boolean field for %s - %s", ip, mode_desc);
150
151 MMDB_entry_data_s data = data_ok(
152 result, MMDB_DATA_TYPE_BOOLEAN, description, "boolean", NULL);
153 cmp_ok(data.boolean, "==", true, "boolean field is true");
154 }
155
156 {
157 char description[500];
158 snprintf(description, 500, "array field for %s - %s", ip, mode_desc);
159
160 MMDB_entry_data_s data =
161 data_ok(result, MMDB_DATA_TYPE_ARRAY, description, "array", NULL);
162 ok(data.data_size == 3, "array field has 3 elements");
163
164 snprintf(description, 500, "array[0] for %s - %s", ip, mode_desc);
165 data = data_ok(
166 result, MMDB_DATA_TYPE_UINT32, description, "array", "0", NULL);
167 ok(data.uint32 == 1, "array[0] is 1");
168
169 snprintf(description, 500, "array[1] for %s - %s", ip, mode_desc);
170 data = data_ok(
171 result, MMDB_DATA_TYPE_UINT32, description, "array", "1", NULL);
172 ok(data.uint32 == 2, "array[1] is 1");
173
174 snprintf(description, 500, "array[2] for %s - %s", ip, mode_desc);
175 data = data_ok(
176 result, MMDB_DATA_TYPE_UINT32, description, "array", "2", NULL);
177 ok(data.uint32 == 3, "array[2] is 1");
178 }
179
180 {
181 char description[500];
182 snprintf(description, 500, "map field for %s - %s", ip, mode_desc);
183
184 MMDB_entry_data_s data =
185 data_ok(result, MMDB_DATA_TYPE_MAP, description, "map", NULL);
186 ok(data.data_size == 1, "map field has 1 element");
187
188 snprintf(description, 500, "map{mapX} for %s - %s", ip, mode_desc);
189
190 data = data_ok(
191 result, MMDB_DATA_TYPE_MAP, description, "map", "mapX", NULL);
192 ok(data.data_size == 2, "map{mapX} field has 2 elements");
193
194 snprintf(description,
195 500,
196 "map{mapX}{utf8_stringX} for %s - %s",
197 ip,
198 mode_desc);
199
200 data = data_ok(result,
201 MMDB_DATA_TYPE_UTF8_STRING,
202 description,
203 "map",
204 "mapX",
205 "utf8_stringX",
206 NULL);
207 const char *string = mmdb_strndup(data.utf8_string, data.data_size);
208 is(string, "hello", "map{mapX}{utf8_stringX} is 'hello'");
209 free((char *)string);
210
211 snprintf(
212 description, 500, "map{mapX}{arrayX} for %s - %s", ip, mode_desc);
213 data = data_ok(result,
214 MMDB_DATA_TYPE_ARRAY,
215 description,
216 "map",
217 "mapX",
218 "arrayX",
219 NULL);
220 ok(data.data_size == 3, "map{mapX}{arrayX} field has 3 elements");
221
222 snprintf(description,
223 500,
224 "map{mapX}{arrayX}[0] for %s - %s",
225 ip,
226 mode_desc);
227 data = data_ok(result,
228 MMDB_DATA_TYPE_UINT32,
229 description,
230 "map",
231 "mapX",
232 "arrayX",
233 "0",
234 NULL);
235 ok(data.uint32 == 7, "map{mapX}{arrayX}[0] is 7");
236
237 snprintf(description,
238 500,
239 "map{mapX}{arrayX}[1] for %s - %s",
240 ip,
241 mode_desc);
242 data = data_ok(result,
243 MMDB_DATA_TYPE_UINT32,
244 description,
245 "map",
246 "mapX",
247 "arrayX",
248 "1",
249 NULL);
250 ok(data.uint32 == 8, "map{mapX}{arrayX}[1] is 8");
251
252 snprintf(description,
253 500,
254 "map{mapX}{arrayX}[2] for %s - %s",
255 ip,
256 mode_desc);
257 data = data_ok(result,
258 MMDB_DATA_TYPE_UINT32,
259 description,
260 "map",
261 "mapX",
262 "arrayX",
263 "2",
264 NULL);
265 ok(data.uint32 == 9, "map{mapX}{arrayX}[2] is 9");
266 }
267 }
268
269 void test_all_data_types_as_zero(MMDB_lookup_result_s *result,
270 const char *ip,
271 const char *UNUSED(filename),
272 const char *mode_desc) {
273 {
274 char description[500];
275 snprintf(
276 description, 500, "utf8_string field for %s - %s", ip, mode_desc);
277
278 MMDB_entry_data_s data = data_ok(result,
279 MMDB_DATA_TYPE_UTF8_STRING,
280 description,
281 "utf8_string",
282 NULL);
283 is(data.utf8_string, "", "got expected utf8_string value (NULL)");
284 }
285
286 {
287 char description[500];
288 snprintf(description, 500, "double field for %s - %s", ip, mode_desc);
289
290 MMDB_entry_data_s data =
291 data_ok(result, MMDB_DATA_TYPE_DOUBLE, description, "double", NULL);
292
293 compare_double(data.double_value, 0.0);
294 }
295
296 {
297 char description[500];
298 snprintf(description, 500, "float field for %s - %s", ip, mode_desc);
299
300 MMDB_entry_data_s data =
301 data_ok(result, MMDB_DATA_TYPE_FLOAT, description, "float", NULL);
302
303 compare_float(data.float_value, 0.0F);
304 }
305
306 {
307 char description[500];
308 snprintf(description, 500, "bytes field for %s - %s", ip, mode_desc);
309
310 MMDB_entry_data_s data =
311 data_ok(result, MMDB_DATA_TYPE_BYTES, description, "bytes", NULL);
312 ok(data.data_size == 0, "bytes field data_size is 0");
313 /* In C does it makes sense to write something like this?
314 uint8_t expect[0] = {};
315 ok(memcmp(data.bytes, expect, 0) == 0, "got expected bytes value
316 (NULL)"); */
317 }
318
319 {
320 char description[500];
321 snprintf(description, 500, "uint16 field for %s - %s", ip, mode_desc);
322
323 MMDB_entry_data_s data =
324 data_ok(result, MMDB_DATA_TYPE_UINT16, description, "uint16", NULL);
325 uint16_t expect = 0;
326 ok(data.uint16 == expect, "uint16 field is 0");
327 }
328
329 {
330 char description[500];
331 snprintf(description, 500, "uint32 field for %s - %s", ip, mode_desc);
332
333 MMDB_entry_data_s data =
334 data_ok(result, MMDB_DATA_TYPE_UINT32, description, "uint32", NULL);
335 uint32_t expect = 0;
336 cmp_ok(data.uint32, "==", expect, "uint32 field is 0");
337 }
338
339 {
340 char description[500];
341 snprintf(description, 500, "int32 field for %s - %s", ip, mode_desc);
342
343 MMDB_entry_data_s data =
344 data_ok(result, MMDB_DATA_TYPE_INT32, description, "int32", NULL);
345 int32_t expect = 0;
346 expect *= -1;
347 cmp_ok(data.int32, "==", expect, "int32 field is 0");
348 }
349
350 {
351 char description[500];
352 snprintf(description, 500, "uint64 field for %s - %s", ip, mode_desc);
353
354 MMDB_entry_data_s data =
355 data_ok(result, MMDB_DATA_TYPE_UINT64, description, "uint64", NULL);
356 uint64_t expect = 0;
357 cmp_ok(data.uint64, "==", expect, "uint64 field is 0");
358 }
359
360 {
361 char description[500];
362 snprintf(description, 500, "uint128 field for %s - %s", ip, mode_desc);
363
364 MMDB_entry_data_s data = data_ok(
365 result, MMDB_DATA_TYPE_UINT128, description, "uint128", NULL);
366 #if MMDB_UINT128_IS_BYTE_ARRAY
367 uint8_t expect[16] = {0x00,
368 0x00,
369 0x00,
370 0x00,
371 0x00,
372 0x00,
373 0x00,
374 0x00,
375 0x00,
376 0x00,
377 0x00,
378 0x00,
379 0x00,
380 0x00,
381 0x00,
382 0x00};
383 ok(memcmp(data.uint128, expect, 16) == 0, "uint128 field is 0");
384 #else
385 mmdb_uint128_t expect = 0;
386 cmp_ok(data.uint128, "==", expect, "uint128 field is 0");
387 #endif
388 }
389
390 {
391 char description[500];
392 snprintf(description, 500, "boolean field for %s - %s", ip, mode_desc);
393
394 MMDB_entry_data_s data = data_ok(
395 result, MMDB_DATA_TYPE_BOOLEAN, description, "boolean", NULL);
396 cmp_ok(data.boolean, "==", false, "boolean field is false");
397 }
398
399 {
400 char description[500];
401 snprintf(description, 500, "array field for %s - %s", ip, mode_desc);
402
403 MMDB_entry_data_s data =
404 data_ok(result, MMDB_DATA_TYPE_ARRAY, description, "array", NULL);
405 ok(data.data_size == 0, "array field has 0 elements");
406 }
407
408 {
409 char description[500];
410 snprintf(description, 500, "map field for %s - %s", ip, mode_desc);
411
412 MMDB_entry_data_s data =
413 data_ok(result, MMDB_DATA_TYPE_MAP, description, "map", NULL);
414 ok(data.data_size == 0, "map field has 0 elements");
415 }
416 }
417
418 void run_tests(int mode, const char *mode_desc) {
419 const char *filename = "MaxMind-DB-test-decoder.mmdb";
420 const char *path = test_database_path(filename);
421 MMDB_s *mmdb = open_ok(path, mode, mode_desc);
422
423 // All of the remaining tests require an open mmdb
424 if (NULL == mmdb) {
425 diag("could not open %s - skipping remaining tests", path);
426 return;
427 }
428
429 free((void *)path);
430
431 {
432 const char *ip = "not an ip";
433
434 int gai_error, mmdb_error;
435 MMDB_lookup_result_s result =
436 MMDB_lookup_string(mmdb, ip, &gai_error, &mmdb_error);
437
438 cmp_ok(gai_error,
439 "==",
440 EAI_NONAME,
441 "MMDB_lookup populates getaddrinfo error properly - %s",
442 ip);
443
444 ok(!result.found_entry,
445 "no result entry struct returned for invalid IP address '%s'",
446 ip);
447 }
448
449 {
450 const char *ip = "e900::";
451 MMDB_lookup_result_s result =
452 lookup_string_ok(mmdb, ip, filename, mode_desc);
453
454 ok(!result.found_entry,
455 "no result entry struct returned for IP address not in the database "
456 "- %s - %s - %s",
457 ip,
458 filename,
459 mode_desc);
460 }
461
462 {
463 const char *ip = "::1.1.1.1";
464 MMDB_lookup_result_s result =
465 lookup_string_ok(mmdb, ip, filename, mode_desc);
466
467 ok(result.found_entry,
468 "got a result entry struct for IP address in the database - %s - %s "
469 "- %s",
470 ip,
471 filename,
472 mode_desc);
473
474 cmp_ok(result.entry.offset,
475 ">",
476 0,
477 "result.entry.offset > 0 for address in the database - %s - %s "
478 "- %s",
479 ip,
480 filename,
481 mode_desc);
482
483 test_all_data_types(&result, ip, filename, mode_desc);
484 }
485
486 {
487 const char *ip = "::4.5.6.7";
488 MMDB_lookup_result_s result =
489 lookup_string_ok(mmdb, ip, filename, mode_desc);
490
491 ok(result.found_entry,
492 "got a result entry struct for IP address in the database - %s - %s "
493 "- %s",
494 ip,
495 filename,
496 mode_desc);
497
498 cmp_ok(result.entry.offset,
499 ">",
500 0,
501 "result.entry.offset > 0 for address in the database - %s - %s "
502 "- %s",
503 ip,
504 filename,
505 mode_desc);
506
507 test_all_data_types(&result, ip, filename, mode_desc);
508 }
509
510 {
511 const char *ip = "::0.0.0.0";
512 MMDB_lookup_result_s result =
513 lookup_string_ok(mmdb, ip, filename, mode_desc);
514
515 ok(result.found_entry,
516 "got a result entry struct for IP address in the database - %s - %s "
517 "- %s",
518 ip,
519 filename,
520 mode_desc);
521
522 test_all_data_types_as_zero(&result, ip, filename, mode_desc);
523 }
524
525 MMDB_close(mmdb);
526 free(mmdb);
527 }
528
529 int main(void) {
530 plan(NO_PLAN);
531 for_all_modes(&run_tests);
532 done_testing();
533 }