"Fossies" - the Fresh Open Source Software Archive 
Member "libmaxminddb-1.5.2/t/maxminddb_test_helper.c" (18 Feb 2021, 6745 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 "maxminddb_test_helper.c":
1.5.0_vs_1.5.2.
1 #if HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #define _POSIX_C_SOURCE 200112L
6 #include <assert.h>
7 #include <stdarg.h>
8 #include <sys/types.h>
9
10 #include "maxminddb.h"
11 #include "maxminddb_test_helper.h"
12
13 #ifdef _WIN32
14 #include <io.h>
15 #else
16 #include <libgen.h>
17 #include <unistd.h>
18 #endif
19
20 void for_all_record_sizes(const char *filename_fmt,
21 void (*tests)(int record_size,
22 const char *filename,
23 const char *description)) {
24 int sizes[] = {24, 28, 32};
25 for (int i = 0; i < 3; i++) {
26 int size = sizes[i];
27
28 char filename[500];
29 snprintf(filename, 500, filename_fmt, size);
30
31 char description[14];
32 snprintf(description, 14, "%i bit record", size);
33
34 tests(size, filename, description);
35 }
36 }
37
38 void for_all_modes(void (*tests)(int mode, const char *description)) {
39 tests(MMDB_MODE_MMAP, "mmap mode");
40 }
41
42 const char *test_database_path(const char *filename) {
43 char *test_db_dir;
44 #ifdef _WIN32
45 test_db_dir = "../t/maxmind-db/test-data";
46 #else
47 char cwd[500];
48 char *UNUSED(tmp) = getcwd(cwd, 500);
49
50 if (strcmp(basename(cwd), "t") == 0) {
51 test_db_dir = "./maxmind-db/test-data";
52 } else {
53 test_db_dir = "./t/maxmind-db/test-data";
54 }
55 #endif
56
57 char *path = malloc(500);
58 assert(NULL != path);
59
60 snprintf(path, 500, "%s/%s", test_db_dir, filename);
61
62 return (const char *)path;
63 }
64
65 const char *dup_entry_string_or_bail(MMDB_entry_data_s entry_data) {
66 const char *string =
67 mmdb_strndup(entry_data.utf8_string, entry_data.data_size);
68 if (NULL == string) {
69 BAIL_OUT("mmdb_strndup failed");
70 }
71
72 return string;
73 }
74
75 MMDB_s *open_ok(const char *db_file, int mode, const char *mode_desc) {
76 if (0 != access(db_file, R_OK)) {
77 BAIL_OUT("could not read the specified file - %s\nIf you are in a git "
78 "checkout try running 'git submodule update --init'",
79 db_file);
80 }
81
82 MMDB_s *mmdb = (MMDB_s *)calloc(1, sizeof(MMDB_s));
83
84 if (NULL == mmdb) {
85 BAIL_OUT("could not allocate memory for our MMDB_s struct");
86 }
87
88 int status = MMDB_open(db_file, mode, mmdb);
89
90 int is_ok = ok(MMDB_SUCCESS == status,
91 "open %s status is success - %s",
92 db_file,
93 mode_desc);
94
95 if (!is_ok) {
96 diag("open status code = %d (%s)", status, MMDB_strerror(status));
97 free(mmdb);
98 return NULL;
99 }
100
101 is_ok = ok(mmdb->file_size > 0,
102 "mmdb struct has been set for %s - %s",
103 db_file,
104 mode_desc);
105
106 if (!is_ok) {
107 free(mmdb);
108 return NULL;
109 }
110
111 return mmdb;
112 }
113
114 MMDB_lookup_result_s lookup_string_ok(MMDB_s *mmdb,
115 const char *ip,
116 const char *file,
117 const char *mode_desc) {
118 int gai_error, mmdb_error;
119 MMDB_lookup_result_s result =
120 MMDB_lookup_string(mmdb, ip, &gai_error, &mmdb_error);
121
122 test_lookup_errors(
123 gai_error, mmdb_error, "MMDB_lookup_string", ip, file, mode_desc);
124
125 return result;
126 }
127
128 MMDB_lookup_result_s lookup_sockaddr_ok(MMDB_s *mmdb,
129 const char *ip,
130 const char *file,
131 const char *mode_desc) {
132 int ai_flags = AI_NUMERICHOST;
133 struct addrinfo hints = {.ai_socktype = SOCK_STREAM};
134 struct addrinfo *addresses = NULL;
135
136 if (ip[0] == ':') {
137 hints.ai_flags = ai_flags;
138 #if defined AI_V4MAPPED && !defined __FreeBSD__
139 hints.ai_flags |= AI_V4MAPPED;
140 #endif
141 hints.ai_family = AF_INET6;
142 } else {
143 hints.ai_flags = ai_flags;
144 hints.ai_family = AF_INET;
145 }
146
147 int gai_error = getaddrinfo(ip, NULL, &hints, &addresses);
148
149 int mmdb_error = 0;
150 MMDB_lookup_result_s result = {.found_entry = false};
151 if (gai_error == 0) {
152 result = MMDB_lookup_sockaddr(mmdb, addresses->ai_addr, &mmdb_error);
153 }
154 if (NULL != addresses) {
155 freeaddrinfo(addresses);
156 }
157
158 test_lookup_errors(
159 gai_error, mmdb_error, "MMDB_lookup_sockaddr", ip, file, mode_desc);
160
161 return result;
162 }
163
164 void test_lookup_errors(int gai_error,
165 int mmdb_error,
166 const char *function,
167 const char *ip,
168 const char *file,
169 const char *mode_desc) {
170
171 int is_ok = ok(0 == gai_error,
172 "no getaddrinfo error in call to %s for %s - %s - %s",
173 function,
174 ip,
175 file,
176 mode_desc);
177
178 if (!is_ok) {
179 diag("error from call to getaddrinfo for %s - %s",
180 ip,
181 gai_strerror(gai_error));
182 }
183
184 is_ok = ok(0 == mmdb_error,
185 "no MMDB error in call to %s for %s - %s - %s",
186 function,
187 ip,
188 file,
189 mode_desc);
190
191 if (!is_ok) {
192 diag("MMDB error - %s", MMDB_strerror(mmdb_error));
193 }
194 }
195
196 MMDB_entry_data_s data_ok(MMDB_lookup_result_s *result,
197 uint32_t expect_type,
198 const char *description,
199 ...) {
200 va_list keys;
201 va_start(keys, description);
202
203 MMDB_entry_data_s data;
204 int status = MMDB_vget_value(&result->entry, &data, keys);
205
206 va_end(keys);
207
208 if (cmp_ok(status,
209 "==",
210 MMDB_SUCCESS,
211 "no error from call to MMDB_vget_value - %s",
212 description)) {
213
214 if (!cmp_ok(data.type,
215 "==",
216 expect_type,
217 "got the expected data type - %s",
218 description)) {
219
220 diag(" data type value is %i but expected %i",
221 data.type,
222 expect_type);
223 }
224 } else {
225 diag(" error from MMDB_vget_value - %s", MMDB_strerror(status));
226 }
227
228 return data;
229 }
230
231 void compare_double(double got, double expect) {
232 double diff = fabs(got - expect);
233 int is_ok = ok(diff < 0.01, "double value was approximately %2.6f", expect);
234 if (!is_ok) {
235 diag(
236 " got %2.6f but expected %2.6f (diff = %2.6f)", got, expect, diff);
237 }
238 }
239
240 void compare_float(float got, float expect) {
241 float diff = fabsf(got - expect);
242 int is_ok = ok(diff < 0.01, "float value was approximately %2.1f", expect);
243 if (!is_ok) {
244 diag(
245 " got %2.4f but expected %2.1f (diff = %2.1f)", got, expect, diff);
246 }
247 }