geoip2_c 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/ext/geoip2/libmaxminddb/.gitignore +35 -0
  3. data/ext/geoip2/libmaxminddb/.gitmodules +9 -0
  4. data/ext/geoip2/libmaxminddb/.perltidyrc +11 -0
  5. data/ext/geoip2/libmaxminddb/.travis.yml +48 -0
  6. data/ext/geoip2/libmaxminddb/.uncrustify.cfg +78 -0
  7. data/ext/geoip2/libmaxminddb/AUTHORS +0 -0
  8. data/ext/geoip2/libmaxminddb/Changes.md +238 -0
  9. data/ext/geoip2/libmaxminddb/LICENSE +202 -0
  10. data/ext/geoip2/libmaxminddb/Makefile.am +41 -0
  11. data/ext/geoip2/libmaxminddb/NOTICE +13 -0
  12. data/ext/geoip2/libmaxminddb/README.dev.md +58 -0
  13. data/ext/geoip2/libmaxminddb/README.md +122 -0
  14. data/ext/geoip2/libmaxminddb/appveyor.yml +33 -0
  15. data/ext/geoip2/libmaxminddb/bin/Makefile.am +5 -0
  16. data/ext/geoip2/libmaxminddb/bin/mmdblookup.c +397 -0
  17. data/ext/geoip2/libmaxminddb/bootstrap +21 -0
  18. data/ext/geoip2/libmaxminddb/common.mk +7 -0
  19. data/ext/geoip2/libmaxminddb/configure.ac +132 -0
  20. data/ext/geoip2/libmaxminddb/dev-bin/make-man-pages.pl +76 -0
  21. data/ext/geoip2/libmaxminddb/dev-bin/ppa-release.sh +50 -0
  22. data/ext/geoip2/libmaxminddb/dev-bin/regen-prototypes.pl +136 -0
  23. data/ext/geoip2/libmaxminddb/dev-bin/regen-win32-test-projs.pl +54 -0
  24. data/ext/geoip2/libmaxminddb/dev-bin/release.sh +106 -0
  25. data/ext/geoip2/libmaxminddb/dev-bin/uncrustify-all.sh +21 -0
  26. data/ext/geoip2/libmaxminddb/dev-bin/valgrind-all.pl +46 -0
  27. data/ext/geoip2/libmaxminddb/doc/libmaxminddb.md +889 -0
  28. data/ext/geoip2/libmaxminddb/doc/mmdblookup.md +103 -0
  29. data/ext/geoip2/libmaxminddb/include/maxminddb.h +232 -0
  30. data/ext/geoip2/libmaxminddb/include/maxminddb_config.h.in +14 -0
  31. data/ext/geoip2/libmaxminddb/projects/VS12/README.md +59 -0
  32. data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb-release.props +32 -0
  33. data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb-x64.props +14 -0
  34. data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb.props +32 -0
  35. data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb.sln +150 -0
  36. data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb.vcxproj +141 -0
  37. data/ext/geoip2/libmaxminddb/projects/VS12/libmaxminddb.vcxproj.filters +26 -0
  38. data/ext/geoip2/libmaxminddb/projects/VS12/maxminddb_config.h +14 -0
  39. data/ext/geoip2/libmaxminddb/projects/VS12-tests/bad_pointers.vcxproj +105 -0
  40. data/ext/geoip2/libmaxminddb/projects/VS12-tests/basic_lookup.vcxproj +105 -0
  41. data/ext/geoip2/libmaxminddb/projects/VS12-tests/data_entry_list.vcxproj +105 -0
  42. data/ext/geoip2/libmaxminddb/projects/VS12-tests/data_types.vcxproj +105 -0
  43. data/ext/geoip2/libmaxminddb/projects/VS12-tests/dump.vcxproj +105 -0
  44. data/ext/geoip2/libmaxminddb/projects/VS12-tests/get_value.vcxproj +105 -0
  45. data/ext/geoip2/libmaxminddb/projects/VS12-tests/get_value_pointer_bug.vcxproj +105 -0
  46. data/ext/geoip2/libmaxminddb/projects/VS12-tests/ipv4_start_cache.vcxproj +105 -0
  47. data/ext/geoip2/libmaxminddb/projects/VS12-tests/ipv6_lookup_in_ipv4.vcxproj +105 -0
  48. data/ext/geoip2/libmaxminddb/projects/VS12-tests/libtap.vcxproj +85 -0
  49. data/ext/geoip2/libmaxminddb/projects/VS12-tests/maxminddb_test_helper.vcxproj +107 -0
  50. data/ext/geoip2/libmaxminddb/projects/VS12-tests/metadata.vcxproj +105 -0
  51. data/ext/geoip2/libmaxminddb/projects/VS12-tests/metadata_pointers.vcxproj +105 -0
  52. data/ext/geoip2/libmaxminddb/projects/VS12-tests/no_map_get_value.vcxproj +105 -0
  53. data/ext/geoip2/libmaxminddb/projects/VS12-tests/read_node.vcxproj +105 -0
  54. data/ext/geoip2/libmaxminddb/projects/VS12-tests/shared.vcxproj +104 -0
  55. data/ext/geoip2/libmaxminddb/projects/VS12-tests/threads.vcxproj +103 -0
  56. data/ext/geoip2/libmaxminddb/projects/VS12-tests/version.vcxproj +105 -0
  57. data/ext/geoip2/libmaxminddb/projects/test.vcxproj.template +105 -0
  58. data/ext/geoip2/libmaxminddb/src/Makefile.am +9 -0
  59. data/ext/geoip2/libmaxminddb/src/libmaxminddb.pc.in +11 -0
  60. data/ext/geoip2/libmaxminddb/src/maxminddb-compat-util.h +167 -0
  61. data/ext/geoip2/libmaxminddb/src/maxminddb.c +2171 -0
  62. data/ext/geoip2/libmaxminddb/t/Makefile.am +23 -0
  63. data/ext/geoip2/libmaxminddb/t/bad_databases_t.c +66 -0
  64. data/ext/geoip2/libmaxminddb/t/bad_pointers_t.c +53 -0
  65. data/ext/geoip2/libmaxminddb/t/basic_lookup_t.c +172 -0
  66. data/ext/geoip2/libmaxminddb/t/compile_c++_t.pl +107 -0
  67. data/ext/geoip2/libmaxminddb/t/data_entry_list_t.c +353 -0
  68. data/ext/geoip2/libmaxminddb/t/data_types_t.c +439 -0
  69. data/ext/geoip2/libmaxminddb/t/dump_t.c +103 -0
  70. data/ext/geoip2/libmaxminddb/t/get_value_pointer_bug_t.c +66 -0
  71. data/ext/geoip2/libmaxminddb/t/get_value_t.c +249 -0
  72. data/ext/geoip2/libmaxminddb/t/ipv4_start_cache_t.c +36 -0
  73. data/ext/geoip2/libmaxminddb/t/ipv6_lookup_in_ipv4_t.c +48 -0
  74. data/ext/geoip2/libmaxminddb/t/maxminddb_test_helper.c +255 -0
  75. data/ext/geoip2/libmaxminddb/t/maxminddb_test_helper.h +69 -0
  76. data/ext/geoip2/libmaxminddb/t/metadata_pointers_t.c +32 -0
  77. data/ext/geoip2/libmaxminddb/t/metadata_t.c +226 -0
  78. data/ext/geoip2/libmaxminddb/t/mmdblookup_t.pl +158 -0
  79. data/ext/geoip2/libmaxminddb/t/no_map_get_value_t.c +32 -0
  80. data/ext/geoip2/libmaxminddb/t/read_node_t.c +157 -0
  81. data/ext/geoip2/libmaxminddb/t/threads_t.c +196 -0
  82. data/ext/geoip2/libmaxminddb/t/version_t.c +10 -0
  83. data/geoip2_c.gemspec +1 -1
  84. data/lib/geoip2/version.rb +1 -1
  85. metadata +82 -1
@@ -0,0 +1,226 @@
1
+ #include "maxminddb_test_helper.h"
2
+
3
+ void test_metadata(MMDB_s *mmdb, const char *mode_desc)
4
+ {
5
+ cmp_ok(mmdb->metadata.node_count, "==", 37, "node_count is 37 - %s",
6
+ mode_desc);
7
+ cmp_ok(mmdb->metadata.record_size, "==", 24, "record_size is 24 - %s",
8
+ mode_desc);
9
+ cmp_ok(mmdb->metadata.ip_version, "==", 4, "ip_version is 4 - %s",
10
+ mode_desc);
11
+ is(mmdb->metadata.database_type, "Test", "database_type is Test - %s",
12
+ mode_desc);
13
+ // 2013-07-01T00:00:00Z
14
+ uint64_t expect_epoch = 1372636800;
15
+ int is_ok =
16
+ cmp_ok(mmdb->metadata.build_epoch, ">=", expect_epoch,
17
+ "build_epoch > %lli", expect_epoch);
18
+ if (!is_ok) {
19
+ diag(" epoch is %lli", mmdb->metadata.build_epoch);
20
+ }
21
+
22
+ cmp_ok(mmdb->metadata.binary_format_major_version, "==", 2,
23
+ "binary_format_major_version is 2 - %s", mode_desc);
24
+ cmp_ok(mmdb->metadata.binary_format_minor_version, "==", 0,
25
+ "binary_format_minor_version is 0 - %s", mode_desc);
26
+
27
+ cmp_ok(mmdb->metadata.languages.count, "==", 2, "found 2 languages - %s",
28
+ mode_desc);
29
+ is(mmdb->metadata.languages.names[0], "en", "first language is en - %s",
30
+ mode_desc);
31
+ is(mmdb->metadata.languages.names[1], "zh", "second language is zh - %s",
32
+ mode_desc);
33
+
34
+ cmp_ok(mmdb->metadata.description.count, "==", 2,
35
+ "found 2 descriptions - %s", mode_desc);
36
+ for (uint16_t i = 0; i < mmdb->metadata.description.count; i++) {
37
+ const char *language =
38
+ mmdb->metadata.description.descriptions[i]->language;
39
+ const char *description =
40
+ mmdb->metadata.description.descriptions[i]->description;
41
+ if (strncmp(language, "en", 2) == 0) {
42
+ ok(1, "found en description");
43
+ is(description, "Test Database", "en description");
44
+ } else if (strncmp(language, "zh", 2) == 0) {
45
+ ok(1, "found zh description");
46
+ is(description, "Test Database Chinese", "zh description");
47
+ } else {
48
+ ok(0, "found unknown description in unexpected language - %s",
49
+ language);
50
+ }
51
+ }
52
+
53
+ cmp_ok(mmdb->full_record_byte_size, "==", 6,
54
+ "full_record_byte_size is 6 - %s", mode_desc);
55
+ }
56
+
57
+ MMDB_entry_data_list_s *test_languages_value(MMDB_entry_data_list_s
58
+ *entry_data_list)
59
+ {
60
+ MMDB_entry_data_list_s *languages = entry_data_list = entry_data_list->next;
61
+
62
+ cmp_ok(languages->entry_data.type, "==", MMDB_DATA_TYPE_ARRAY,
63
+ "'languages' key's value is an array");
64
+ cmp_ok(languages->entry_data.data_size, "==", 2,
65
+ "'languages' key's value has 2 elements");
66
+
67
+ MMDB_entry_data_list_s *idx0 = entry_data_list = entry_data_list->next;
68
+ cmp_ok(idx0->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
69
+ "first array entry is a UTF8_STRING");
70
+ const char *lang0 = dup_entry_string_or_bail(idx0->entry_data);
71
+ is(lang0, "en", "first language is en");
72
+ free((void *)lang0);
73
+
74
+ MMDB_entry_data_list_s *idx1 = entry_data_list = entry_data_list->next;
75
+ cmp_ok(idx1->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
76
+ "second array entry is a UTF8_STRING");
77
+ const char *lang1 = dup_entry_string_or_bail(idx1->entry_data);
78
+ is(lang1, "zh", "second language is zh");
79
+ free((void *)lang1);
80
+
81
+ return entry_data_list;
82
+ }
83
+
84
+ MMDB_entry_data_list_s *test_description_value(
85
+ MMDB_entry_data_list_s *entry_data_list)
86
+ {
87
+ MMDB_entry_data_list_s *description = entry_data_list =
88
+ entry_data_list->next;
89
+ cmp_ok(description->entry_data.type, "==", MMDB_DATA_TYPE_MAP,
90
+ "'description' key's value is a map");
91
+ cmp_ok(description->entry_data.data_size, "==", 2,
92
+ "'description' key's value has 2 key/value pairs");
93
+
94
+ for (int i = 0; i < 2; i++) {
95
+ MMDB_entry_data_list_s *key = entry_data_list =
96
+ entry_data_list->next;
97
+ cmp_ok(key->entry_data.type, "==",
98
+ MMDB_DATA_TYPE_UTF8_STRING,
99
+ "found a map key in 'map'");
100
+ const char *key_name = dup_entry_string_or_bail(key->entry_data);
101
+
102
+ MMDB_entry_data_list_s *value = entry_data_list =
103
+ entry_data_list->next;
104
+ cmp_ok(value->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
105
+ "map value is a UTF8_STRING");
106
+ const char *description =
107
+ dup_entry_string_or_bail(value->entry_data);
108
+
109
+ if (strcmp(key_name, "en") == 0) {
110
+ is(description, "Test Database",
111
+ "en description == 'Test Database'");
112
+ } else if (strcmp(key_name, "zh") == 0) {
113
+ is(description, "Test Database Chinese",
114
+ "zh description == 'Test Database Chinese'");
115
+ } else {
116
+ ok(0, "unknown key found in description map - %s", key_name);
117
+ }
118
+
119
+ free((void *)key_name);
120
+ free((void *)description);
121
+ }
122
+
123
+ return entry_data_list;
124
+ }
125
+
126
+ void test_metadata_as_data_entry_list(MMDB_s * mmdb,
127
+ const char *mode_desc)
128
+ {
129
+ MMDB_entry_data_list_s *entry_data_list, *first;
130
+ int status =
131
+ MMDB_get_metadata_as_entry_data_list(mmdb, &entry_data_list);
132
+
133
+ first = entry_data_list;
134
+
135
+ cmp_ok(status, "==", MMDB_SUCCESS, "get metadata as data_entry_list - %s",
136
+ mode_desc);
137
+
138
+ cmp_ok(first->entry_data.data_size, "==", 9,
139
+ "metadata map has 9 key/value pairs");
140
+
141
+ while (1) {
142
+ MMDB_entry_data_list_s *key = entry_data_list =
143
+ entry_data_list->next;
144
+
145
+ if (!key) {
146
+ break;
147
+ }
148
+
149
+ cmp_ok(key->entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
150
+ "found a map key");
151
+
152
+ const char *key_name = dup_entry_string_or_bail(key->entry_data);
153
+ if (strcmp(key_name, "node_count") == 0) {
154
+ MMDB_entry_data_list_s *value
155
+ = entry_data_list = entry_data_list->next;
156
+ cmp_ok(value->entry_data.uint32, "==", 37, "node_count == 37");
157
+ } else if (strcmp(key_name, "record_size") == 0) {
158
+ MMDB_entry_data_list_s *value
159
+ = entry_data_list = entry_data_list->next;
160
+ cmp_ok(value->entry_data.uint16, "==", 24, "record_size == 24");
161
+ } else if (strcmp(key_name, "ip_version") == 0) {
162
+ MMDB_entry_data_list_s *value
163
+ = entry_data_list = entry_data_list->next;
164
+ cmp_ok(value->entry_data.uint16, "==", 4, "ip_version == 4");
165
+ } else if (strcmp(key_name, "binary_format_major_version") == 0) {
166
+ MMDB_entry_data_list_s *value
167
+ = entry_data_list = entry_data_list->next;
168
+ cmp_ok(value->entry_data.uint16, "==", 2,
169
+ "binary_format_major_version == 2");
170
+ } else if (strcmp(key_name, "binary_format_minor_version") == 0) {
171
+ MMDB_entry_data_list_s *value
172
+ = entry_data_list = entry_data_list->next;
173
+ cmp_ok(value->entry_data.uint16, "==", 0,
174
+ "binary_format_minor_version == 0");
175
+ } else if (strcmp(key_name, "build_epoch") == 0) {
176
+ MMDB_entry_data_list_s *value
177
+ = entry_data_list = entry_data_list->next;
178
+ ok(value->entry_data.uint64 > 1373571901,
179
+ "build_epoch > 1373571901");
180
+ } else if (strcmp(key_name, "database_type") == 0) {
181
+ MMDB_entry_data_list_s *value
182
+ = entry_data_list = entry_data_list->next;
183
+ const char *type = dup_entry_string_or_bail(value->entry_data);
184
+ is(type, "Test", "type == Test");
185
+ free((void *)type);
186
+ } else if (strcmp(key_name, "languages") == 0) {
187
+ entry_data_list = test_languages_value(entry_data_list);
188
+ } else if (strcmp(key_name, "description") == 0) {
189
+ entry_data_list = test_description_value(entry_data_list);
190
+ } else {
191
+ ok(0, "unknown key found in metadata map - %s",
192
+ key_name);
193
+ }
194
+
195
+ free((void *)key_name);
196
+ }
197
+
198
+ MMDB_free_entry_data_list(first);
199
+ }
200
+
201
+ void run_tests(int mode, const char *mode_desc)
202
+ {
203
+ const char *file = "MaxMind-DB-test-ipv4-24.mmdb";
204
+ const char *path = test_database_path(file);
205
+ MMDB_s *mmdb = open_ok(path, mode, mode_desc);
206
+
207
+ // All of the remaining tests require an open mmdb
208
+ if (NULL == mmdb) {
209
+ diag("could not open %s - skipping remaining tests", path);
210
+ return;
211
+ }
212
+ free((void *)path);
213
+
214
+ test_metadata(mmdb, mode_desc);
215
+ test_metadata_as_data_entry_list(mmdb, mode_desc);
216
+
217
+ MMDB_close(mmdb);
218
+ free(mmdb);
219
+ }
220
+
221
+ int main(void)
222
+ {
223
+ plan(NO_PLAN);
224
+ for_all_modes(&run_tests);
225
+ done_testing();
226
+ }
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env perl
2
+
3
+ use strict;
4
+ use warnings;
5
+
6
+ use FindBin qw( $Bin );
7
+
8
+ eval <<'EOF';
9
+ use Test::More 0.88;
10
+ use IPC::Run3 qw( run3 );
11
+ EOF
12
+
13
+ if ($@) {
14
+ print
15
+ "1..0 # skip all tests skipped - these tests need the Test::More 0.88, IPC::Run3 and Test::Output modules:\n";
16
+ print "$@";
17
+ exit 0;
18
+ }
19
+
20
+ my $mmdblookup = "$Bin/../bin/mmdblookup";
21
+ my $test_data_dir = "$Bin/maxmind-db/test-data";
22
+
23
+ {
24
+ ok( -x $mmdblookup, 'mmdblookup script is executable' );
25
+ }
26
+
27
+ for my $arg (qw( -h -? --help )) {
28
+ _test_stdout(
29
+ [$arg],
30
+ qr{mmdblookup --file.+This application accepts the following options:}s,
31
+ 0,
32
+ "help output from $arg"
33
+ );
34
+ }
35
+
36
+ _test_both(
37
+ [],
38
+ qr{mmdblookup --file.+This application accepts the following options:}s,
39
+ qr{ERROR: You must provide a filename with --file},
40
+ 1,
41
+ "help output with no CLI options"
42
+ );
43
+
44
+ _test_stderr(
45
+ [qw( --file foo )],
46
+ qr{ERROR: You must provide an IP address with --ip},
47
+ 1,
48
+ 'error when no --ip is given'
49
+ );
50
+
51
+ _test_stdout(
52
+ [qw( --version )],
53
+ qr/mmdblookup version \d+\.\d+\.\d+/,
54
+ 0,
55
+ 'output for --version'
56
+ );
57
+
58
+ _test_stdout(
59
+ ['--file', "$test_data_dir/GeoIP2-City-Test.mmdb", '--ip', '2.125.160.216'],
60
+ qr/"en"\s*:\s*"Boxford"/,
61
+ 0,
62
+ 'output for 2.125.160.216'
63
+ );
64
+
65
+ _test_stdout(
66
+ ['--file', "$test_data_dir/GeoIP2-City-Test.mmdb", '--ip', '2.125.160.216', '--verbose'],
67
+ qr/Database metadata.+"en"\s*:\s*"Boxford"/s,
68
+ 0,
69
+ 'verbose output for 2.125.160.216'
70
+ );
71
+
72
+ _test_stdout(
73
+ ['--file', "$test_data_dir/GeoIP2-City-Test.mmdb", '--ip', '2.125.160.216', qw( location latitude )],
74
+ qr/^\s*51\.750000 <double>\s*$/s,
75
+ 0,
76
+ 'output for 2.125.160.216 with lookup path'
77
+ );
78
+
79
+ _test_stderr(
80
+ [ qw( --file this/path/better/not/exist.mmdb --ip 1.2.3.4 ) ],
81
+ qr{Can't open this/path/better/not/exist.mmdb}s,
82
+ 2,
83
+ 'error for file that does not exist'
84
+ );
85
+
86
+ _test_stderr(
87
+ ['--file', "$test_data_dir/GeoIP2-City-Test.mmdb", '--ip', 'not-an-ip-address' ],
88
+ qr{Error from call to getaddrinfo for not-an-ip-address}s,
89
+ 3,
90
+ 'error for bad IP address'
91
+ );
92
+
93
+ _test_stderr(
94
+ ['--file', "$test_data_dir/GeoIP2-City-Test.mmdb", '--ip', '10.2.3.4' ],
95
+ qr{\QCould not find an entry for this IP address (10.2.3.4)}s,
96
+ 6,
97
+ 'error for bad PI address'
98
+ );
99
+
100
+ done_testing();
101
+
102
+ sub _test_stdout {
103
+ my $args = shift;
104
+ my $expect_stdout = shift;
105
+ my $expect_status = shift;
106
+ my $desc = shift;
107
+
108
+ _test_both( $args, $expect_stdout, q{}, $expect_status, $desc );
109
+ }
110
+
111
+ sub _test_stderr {
112
+ my $args = shift;
113
+ my $expect_stderr = shift;
114
+ my $expect_status = shift;
115
+ my $desc = shift;
116
+
117
+ _test_both( $args, undef, $expect_stderr, $expect_status, $desc );
118
+ }
119
+
120
+ sub _test_both {
121
+ my $args = shift;
122
+ my $expect_stdout = shift;
123
+ my $expect_stderr = shift;
124
+ my $expect_status = shift;
125
+ my $desc = shift;
126
+
127
+ my $stdout;
128
+ my $stderr;
129
+ run3(
130
+ [ $mmdblookup, @{$args} ],
131
+ \undef,
132
+ \$stdout,
133
+ \$stderr,
134
+ );
135
+
136
+ my $exit_status = $? >> 8;
137
+
138
+ # We don't need to retest that the help output shows up for all errors
139
+ if ( defined $expect_stdout ) {
140
+ like(
141
+ $stdout,
142
+ $expect_stdout,
143
+ "stdout for mmdblookup @{$args}"
144
+ );
145
+ }
146
+
147
+ if ( ref $expect_stderr ) {
148
+ like( $stderr, $expect_stderr, "stderr for mmdblookup @{$args}" );
149
+ }
150
+ else {
151
+ is( $stderr, $expect_stderr, "stderr for mmdblookup @{$args}" );
152
+ }
153
+
154
+ is(
155
+ $exit_status, $expect_status,
156
+ "exit status was $expect_status for mmdblookup @{$args}"
157
+ );
158
+ }
@@ -0,0 +1,32 @@
1
+ #include "maxminddb_test_helper.h"
2
+
3
+ void run_tests(int mode, const char *mode_desc)
4
+ {
5
+ const char *filename = "MaxMind-DB-string-value-entries.mmdb";
6
+ const char *path = test_database_path(filename);
7
+ MMDB_s *mmdb = open_ok(path, mode, mode_desc);
8
+ free((void *)path);
9
+
10
+ const char *ip = "1.1.1.1";
11
+ MMDB_lookup_result_s result =
12
+ lookup_string_ok(mmdb, ip, filename, mode_desc);
13
+
14
+ MMDB_entry_data_s entry_data;
15
+ int status = MMDB_get_value(&result.entry, &entry_data, NULL);
16
+
17
+ cmp_ok(status, "==", MMDB_SUCCESS,
18
+ "status for MMDB_get_value() is MMDB_SUCCESS");
19
+ ok(entry_data.has_data, "found a value when varargs list is just NULL");
20
+ cmp_ok(entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
21
+ "returned entry type is utf8_string");
22
+
23
+ MMDB_close(mmdb);
24
+ free(mmdb);
25
+ }
26
+
27
+ int main(void)
28
+ {
29
+ plan(NO_PLAN);
30
+ for_all_modes(&run_tests);
31
+ done_testing();
32
+ }
@@ -0,0 +1,157 @@
1
+ #include "maxminddb_test_helper.h"
2
+
3
+ void test_entry_data(MMDB_s *mmdb, MMDB_entry_s *entry, uint32_t node_number,
4
+ char * node_record)
5
+ {
6
+ MMDB_entry_data_s entry_data;
7
+ int status =
8
+ MMDB_get_value(entry, &entry_data, "ip",
9
+ NULL);
10
+ cmp_ok(status, "==", MMDB_SUCCESS,
11
+ "successful data lookup for node");
12
+ cmp_ok(
13
+ entry_data.type, "==", MMDB_DATA_TYPE_UTF8_STRING,
14
+ "returned entry type is UTF8_STRING for %s record of node %i",
15
+ node_record, node_number);
16
+ }
17
+
18
+ void run_read_node_tests(MMDB_s *mmdb, const uint32_t tests[][5],
19
+ int test_count,
20
+ uint8_t record_size)
21
+ {
22
+ for (int i = 0; i < test_count; i++) {
23
+ uint32_t node_number = tests[i][0];
24
+ MMDB_search_node_s node;
25
+ int status = MMDB_read_node(mmdb, node_number, &node);
26
+ if (MMDB_SUCCESS == status) {
27
+ cmp_ok(node.left_record, "==", tests[i][1],
28
+ "left record for node %i is %i - %i bit DB",
29
+ node_number, tests[i][1], record_size);
30
+ cmp_ok(node.left_record_type, "==", tests[i][2],
31
+ "left record type for node %i is %i", node_number,
32
+ tests[i][2]);
33
+ if (node.left_record_type == MMDB_RECORD_TYPE_DATA) {
34
+ test_entry_data(mmdb, &node.left_record_entry, node_number,
35
+ "left");
36
+ }
37
+
38
+ cmp_ok(node.right_record, "==", tests[i][3],
39
+ "right record for node %i is %i - %i bit DB",
40
+ node_number, tests[i][3], record_size);
41
+ cmp_ok(node.right_record_type, "==", tests[i][4],
42
+ "right record type for node %i is %i", node_number,
43
+ tests[i][4]);
44
+
45
+ if (node.right_record_type == MMDB_RECORD_TYPE_DATA) {
46
+ test_entry_data(mmdb, &node.right_record_entry, node_number,
47
+ "right");
48
+ }
49
+ } else {
50
+ diag("call to MMDB_read_node for node %i failed - %i bit DB",
51
+ node_number,
52
+ record_size);
53
+ }
54
+ }
55
+ }
56
+
57
+ void run_24_bit_record_tests(int mode, const char *mode_desc)
58
+ {
59
+ const char *filename = "MaxMind-DB-test-mixed-24.mmdb";
60
+ const char *path = test_database_path(filename);
61
+ MMDB_s *mmdb = open_ok(path, mode, mode_desc);
62
+ free((void *)path);
63
+
64
+ const uint32_t tests[7][5] = {
65
+ { 0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
66
+ MMDB_RECORD_TYPE_EMPTY },
67
+ { 80, 81, MMDB_RECORD_TYPE_SEARCH_NODE, 197,
68
+ MMDB_RECORD_TYPE_SEARCH_NODE, },
69
+ { 96, 97, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
70
+ MMDB_RECORD_TYPE_EMPTY, },
71
+ { 103, 242, MMDB_RECORD_TYPE_EMPTY, 104,
72
+ MMDB_RECORD_TYPE_SEARCH_NODE, },
73
+ { 127, 242, MMDB_RECORD_TYPE_EMPTY, 315,
74
+ MMDB_RECORD_TYPE_DATA, },
75
+ { 132, 329, MMDB_RECORD_TYPE_DATA, 242,
76
+ MMDB_RECORD_TYPE_EMPTY, },
77
+ { 241, 96, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
78
+ MMDB_RECORD_TYPE_EMPTY, }
79
+ };
80
+ run_read_node_tests(mmdb, tests, 7, 24);
81
+
82
+ MMDB_close(mmdb);
83
+ free(mmdb);
84
+ }
85
+
86
+ void run_28_bit_record_tests(int mode, const char *mode_desc)
87
+ {
88
+ const char *filename = "MaxMind-DB-test-mixed-28.mmdb";
89
+ const char *path = test_database_path(filename);
90
+ MMDB_s *mmdb = open_ok(path, mode, mode_desc);
91
+ free((void *)path);
92
+
93
+ const uint32_t tests[7][5] = {
94
+ { 0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
95
+ MMDB_RECORD_TYPE_EMPTY },
96
+ { 80, 81, MMDB_RECORD_TYPE_SEARCH_NODE, 197,
97
+ MMDB_RECORD_TYPE_SEARCH_NODE, },
98
+ { 96, 97, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
99
+ MMDB_RECORD_TYPE_EMPTY, },
100
+ { 103, 242, MMDB_RECORD_TYPE_EMPTY, 104,
101
+ MMDB_RECORD_TYPE_SEARCH_NODE, },
102
+ { 127, 242, MMDB_RECORD_TYPE_EMPTY, 315,
103
+ MMDB_RECORD_TYPE_DATA, },
104
+ { 132, 329, MMDB_RECORD_TYPE_DATA, 242,
105
+ MMDB_RECORD_TYPE_EMPTY, },
106
+ { 241, 96, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
107
+ MMDB_RECORD_TYPE_EMPTY, }
108
+ };
109
+ run_read_node_tests(mmdb, tests, 7, 28);
110
+
111
+ MMDB_close(mmdb);
112
+ free(mmdb);
113
+ }
114
+
115
+ void run_32_bit_record_tests(int mode, const char *mode_desc)
116
+ {
117
+ const char *filename = "MaxMind-DB-test-mixed-32.mmdb";
118
+ const char *path = test_database_path(filename);
119
+ MMDB_s *mmdb = open_ok(path, mode, mode_desc);
120
+ free((void *)path);
121
+
122
+ const uint32_t tests[7][5] = {
123
+ { 0, 1, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
124
+ MMDB_RECORD_TYPE_EMPTY },
125
+ { 80, 81, MMDB_RECORD_TYPE_SEARCH_NODE, 197,
126
+ MMDB_RECORD_TYPE_SEARCH_NODE, },
127
+ { 96, 97, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
128
+ MMDB_RECORD_TYPE_EMPTY, },
129
+ { 103, 242, MMDB_RECORD_TYPE_EMPTY, 104,
130
+ MMDB_RECORD_TYPE_SEARCH_NODE, },
131
+ { 127, 242, MMDB_RECORD_TYPE_EMPTY, 315,
132
+ MMDB_RECORD_TYPE_DATA, },
133
+ { 132, 329, MMDB_RECORD_TYPE_DATA, 242,
134
+ MMDB_RECORD_TYPE_EMPTY, },
135
+ { 241, 96, MMDB_RECORD_TYPE_SEARCH_NODE, 242,
136
+ MMDB_RECORD_TYPE_EMPTY, }
137
+ };
138
+
139
+ run_read_node_tests(mmdb, tests, 7, 32);
140
+
141
+ MMDB_close(mmdb);
142
+ free(mmdb);
143
+ }
144
+
145
+ void run_tests(int mode, const char *mode_desc)
146
+ {
147
+ run_24_bit_record_tests(mode, mode_desc);
148
+ run_28_bit_record_tests(mode, mode_desc);
149
+ run_32_bit_record_tests(mode, mode_desc);
150
+ }
151
+
152
+ int main(void)
153
+ {
154
+ plan(NO_PLAN);
155
+ for_all_modes(&run_tests);
156
+ done_testing();
157
+ }