l10n: Update translation catalogues
[libisds.git] / test / simline / isds_find_box_by_fulltext.c
blob08e9f4b2a4151c094596576ba1a57ea73d7b7a5c
1 #ifndef _POSIX_SOURCE
2 #define _POSIX_SOURCE /* For getaddrinfo(3) */
3 #endif
5 #ifndef _BSD_SOURCE
6 #define _BSD_SOURCE /* For NI_MAXHOST up to glibc-2.19 */
7 #endif
8 #ifndef _DEFAULT_SOURCE
9 #define _DEFAULT_SOURCE /* For NI_MAXHOST since glibc-2.20 */
10 #endif
12 #ifndef _XOPEN_SOURCE
13 #define _XOPEN_SOURCE 600 /* For unsetenv(3) */
14 #endif
16 #include "../test.h"
17 #include "server.h"
18 #include "isds.h"
20 static const char *username = "Doug1as$";
21 static const char *password = "42aA#bc8";
24 static int test_login(const isds_error error, struct isds_ctx *context,
25 const char *url, const char *username, const char *password,
26 const struct isds_pki_credentials *pki_credentials,
27 struct isds_otp *otp) {
28 isds_error err;
30 err = isds_login(context, url, username, password, pki_credentials, otp);
31 if (error != err)
32 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
33 isds_strerror(error), isds_strerror(err),
34 isds_long_message(context));
36 PASS_TEST;
39 static int compare_match_lists(const char *label,
40 const char *expected_string, const struct isds_list *expected_list,
41 const char* string, const struct isds_list *list) {
42 const struct isds_list *expected_item, *item;
43 ptrdiff_t expected_offset, offset;
44 int i;
46 for (i = 1, expected_item = expected_list, item = list;
47 NULL != expected_item && NULL != item;
48 i++, expected_item = expected_item->next, item = item->next) {
49 expected_offset = (char *)expected_item->data - expected_string;
50 offset = (char *)item->data - string;
51 if (expected_offset != offset)
52 FAIL_TEST("%d. %s match offsets differ: expected=%td, got=%td",
53 i, label, expected_offset, offset);
55 if (NULL != expected_item && NULL == item)
56 FAIL_TEST("%s match offsets list is missing %d. item", label, i);
57 if (NULL == expected_item && NULL != item)
58 FAIL_TEST("%s match offsets list has superfluous %d. item", label, i);
60 return 0;
63 static int compare_fulltext_results(
64 const struct isds_fulltext_result *expected_result,
65 const struct isds_fulltext_result *result) {
67 TEST_POINTER_DUPLICITY(expected_result, result);
69 TEST_STRING_DUPLICITY(expected_result->dbID, result->dbID);
70 TEST_INT_DUPLICITY(expected_result->dbType, result->dbType);
71 TEST_STRING_DUPLICITY(expected_result->name, result->name);
72 if (compare_match_lists("start name",
73 expected_result->name, expected_result->name_match_start,
74 result->name, result->name_match_start))
75 return 1;
76 if (compare_match_lists("end name",
77 expected_result->name, expected_result->name_match_end,
78 result->name, result->name_match_end))
79 return 1;
80 TEST_STRING_DUPLICITY(expected_result->address, result->address);
81 if (compare_match_lists("start address",
82 expected_result->address, expected_result->address_match_start,
83 result->address, result->address_match_start))
84 return 1;
85 if (compare_match_lists("end address",
86 expected_result->address, expected_result->address_match_end,
87 result->address, result->address_match_end))
88 return 1;
89 TEST_STRING_DUPLICITY(expected_result->ic, result->ic);
90 TEST_TMPTR_DUPLICITY(expected_result->biDate, result->biDate);
91 TEST_BOOLEAN_DUPLICITY(expected_result->dbEffectiveOVM,
92 result->dbEffectiveOVM);
93 TEST_BOOLEAN_DUPLICITY(expected_result->active, result->active);
94 TEST_BOOLEAN_DUPLICITY(expected_result->public_sending,
95 result->public_sending);
96 TEST_BOOLEAN_DUPLICITY(expected_result->commercial_sending,
97 result->commercial_sending);
98 return 0;
101 static int compare_result_lists(const struct isds_list *expected_list,
102 const struct isds_list *list) {
103 const struct isds_list *expected_item, *item;
104 int i;
106 for (i = 1, expected_item = expected_list, item = list;
107 NULL != expected_item && NULL != item;
108 i++, expected_item = expected_item->next, item = item->next) {
109 if (compare_fulltext_results(
110 (struct isds_fulltext_result *)expected_item->data,
111 (struct isds_fulltext_result *)item->data))
112 return 1;
114 if (NULL != expected_item && NULL == item)
115 FAIL_TEST("Result list is missing %d. item", i);
116 if (NULL == expected_item && NULL != item)
117 FAIL_TEST("Result list has superfluous %d. item", i);
119 return 0;
122 struct test_destructor_argument {
123 unsigned long int *total_matching_boxes;
124 unsigned long int *current_page_beginning;
125 unsigned long int *current_page_size;
126 _Bool *last_page;
127 struct isds_list *results;
130 static void test_destructor(void *argument) {
131 if (NULL == argument) return;
132 free(((struct test_destructor_argument *)argument)->total_matching_boxes);
133 free(((struct test_destructor_argument *)argument)->current_page_beginning);
134 free(((struct test_destructor_argument *)argument)->current_page_size);
135 free(((struct test_destructor_argument *)argument)->last_page);
136 isds_list_free(
137 &((struct test_destructor_argument *)argument)->results
141 static int test_isds_find_box_by_fulltext(const isds_error expected_error,
142 struct isds_ctx *context,
143 const char *query,
144 const isds_fulltext_target *target,
145 const isds_DbType *box_type,
146 const unsigned long int *page_size,
147 const unsigned long int *page_number,
148 const _Bool *track_matches,
149 const unsigned long int *expected_total_matching_boxes,
150 const unsigned long int *expected_current_page_beginning,
151 const unsigned long int *expected_current_page_size,
152 const _Bool *expected_last_page,
153 const struct isds_list *expected_results) {
154 isds_error error;
155 struct test_destructor_argument allocated;
156 TEST_FILL_INT(allocated.total_matching_boxes);
157 TEST_FILL_INT(allocated.current_page_beginning);
158 TEST_FILL_INT(allocated.current_page_size);
159 TEST_FILL_INT(allocated.last_page);
160 TEST_CALLOC(allocated.results);
162 error = isds_find_box_by_fulltext(context,
163 query, target, box_type, page_size, page_number, track_matches,
164 &allocated.total_matching_boxes,
165 &allocated.current_page_beginning,
166 &allocated.current_page_size,
167 &allocated.last_page,
168 &allocated.results);
169 TEST_DESTRUCTOR(test_destructor, &allocated);
171 if (expected_error != error) {
172 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
173 isds_strerror(expected_error), isds_strerror(error),
174 isds_long_message(context));
177 if (IE_SUCCESS != error) {
178 TEST_POINTER_IS_NULL(allocated.total_matching_boxes);
179 TEST_POINTER_IS_NULL(allocated.current_page_beginning);
180 TEST_POINTER_IS_NULL(allocated.current_page_size);
181 TEST_POINTER_IS_NULL(allocated.last_page);
182 TEST_POINTER_IS_NULL(allocated.results);
183 PASS_TEST;
186 TEST_INTPTR_DUPLICITY(expected_total_matching_boxes,
187 allocated.total_matching_boxes);
188 TEST_INTPTR_DUPLICITY(expected_current_page_beginning,
189 allocated.current_page_beginning);
190 TEST_INTPTR_DUPLICITY(expected_current_page_size,
191 allocated.current_page_size);
192 TEST_BOOLEANPTR_DUPLICITY(expected_last_page,
193 allocated.last_page);
195 if (compare_result_lists(expected_results, allocated.results))
196 return 1;
199 PASS_TEST;
202 int main(void) {
203 int error;
204 pid_t server_process;
205 struct isds_ctx *context = NULL;
207 INIT_TEST("isds_find_box_by_fulltext");
209 if (unsetenv("http_proxy")) {
210 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
212 if (isds_init()) {
213 isds_cleanup();
214 ABORT_UNIT("isds_init() failed\n");
216 context = isds_ctx_create();
217 if (!context) {
218 isds_cleanup();
219 ABORT_UNIT("isds_ctx_create() failed\n");
223 /* Full response */
224 char *url = NULL;
226 struct isds_list name_match_start = {
227 .next = NULL,
228 .data = NULL,
229 .destructor = NULL
231 struct isds_list name_match_end = {
232 .next = NULL,
233 .data = NULL,
234 .destructor = NULL
236 struct isds_list address_match_start = {
237 .next = NULL,
238 .data = NULL,
239 .destructor = NULL
241 struct isds_list address_match_end = {
242 .next = NULL,
243 .data = NULL,
244 .destructor = NULL
246 struct isds_fulltext_result result = {
247 .dbID = "foo1234",
248 .dbType = DBTYPE_OVM,
249 .name = "Foo name",
250 .name_match_start = &name_match_start,
251 .name_match_end = &name_match_end,
252 .address = "Address foo",
253 .address_match_start = &address_match_start,
254 .address_match_end = &address_match_end,
255 .ic = "Foo IC",
256 .biDate = NULL,
257 .dbEffectiveOVM = 1,
258 .active = 1,
259 .public_sending = 1,
260 .commercial_sending = 1
262 name_match_start.data = &result.name[0];
263 name_match_end.data = &result.name[3];
264 address_match_start.data = &result.address[8];
265 address_match_end.data = &result.address[11];
266 struct server_db_result server_result = {
267 .id = "foo1234",
268 .type = "OVM",
269 .name = "|$*HL_START*$|Foo|$*HL_END*$| name",
270 .address = "Address |$*HL_START*$|foo|$*HL_END*$|",
271 .birth_date = NULL,
272 .ic = "Foo IC",
273 .ovm = 1,
274 .send_options = "ALL"
276 const struct isds_list results = {
277 .next = NULL,
278 .data = &result,
279 .destructor = NULL
281 const struct server_list server_results = {
282 .next = NULL,
283 .data = &server_result,
284 .destructor = NULL
288 isds_fulltext_target target = FULLTEXT_ALL;
289 isds_DbType box_type = DBTYPE_SYSTEM;
290 long int search_page_number = 42;
291 long int search_page_size = 43;
292 _Bool search_highlighting_value = 1;
293 unsigned long int total_count = 44;
294 unsigned long int current_count = 1;
295 unsigned long int position = 43 * 42;
296 _Bool last_page = 1;
297 const struct arguments_DS_df_ISDSSearch2 service_arguments = {
298 .status_code = "0000",
299 .status_message = "Ok.",
300 .search_text = "foo",
301 .search_type = "GENERAL",
302 .search_scope = "ALL",
303 .search_page_number = &search_page_number,
304 .search_page_size = &search_page_size,
305 .search_highlighting_value = &search_highlighting_value,
306 .total_count = &total_count,
307 .current_count = &current_count,
308 .position = &position,
309 .last_page = &last_page,
310 .results_exists = 0,
311 .results = &server_results
313 const struct service_configuration services[] = {
314 { SERVICE_DS_Dz_DummyOperation, NULL },
315 { SERVICE_DS_df_ISDSSearch2, &service_arguments },
316 { SERVICE_END, NULL }
318 const struct arguments_basic_authentication server_arguments = {
319 .username = username,
320 .password = password,
321 .isds_deviations = 1,
322 .services = services
324 error = start_server(&server_process, &url,
325 server_basic_authentication, &server_arguments, NULL);
326 if (error == -1) {
327 isds_ctx_free(&context);
328 isds_cleanup();
329 ABORT_UNIT(server_error);
331 TEST("login", test_login, IE_SUCCESS,
332 context, url, username, password, NULL, NULL);
333 free(url);
335 TEST("All data", test_isds_find_box_by_fulltext, IE_SUCCESS,
336 context, service_arguments.search_text, &target, &box_type,
337 (unsigned long int *)service_arguments.search_page_size,
338 (unsigned long int *)service_arguments.search_page_number,
339 service_arguments.search_highlighting_value,
340 service_arguments.total_count,
341 service_arguments.position,
342 service_arguments.current_count,
343 service_arguments.last_page,
344 &results);
346 isds_logout(context);
347 if (stop_server(server_process)) {
348 isds_ctx_free(&context);
349 isds_cleanup();
350 ABORT_UNIT(server_error);
356 /* Empty response due to an error */
357 char *url = NULL;
359 long int search_page_number = 42;
360 long int search_page_size = 43;
361 const struct arguments_DS_df_ISDSSearch2 service_arguments = {
362 .status_code = "1156",
363 .status_message = "pageSize is too large",
364 .search_text = "foo",
365 .search_type = NULL,
366 .search_scope = NULL,
367 .search_page_number = &search_page_number,
368 .search_page_size = &search_page_size,
369 .search_highlighting_value = NULL,
370 .total_count = NULL,
371 .current_count = NULL,
372 .position = NULL,
373 .last_page = NULL,
374 .results_exists = 0,
375 .results = NULL
377 const struct service_configuration services[] = {
378 { SERVICE_DS_Dz_DummyOperation, NULL },
379 { SERVICE_DS_df_ISDSSearch2, &service_arguments },
380 { SERVICE_END, NULL }
382 const struct arguments_basic_authentication server_arguments = {
383 .username = username,
384 .password = password,
385 .isds_deviations = 1,
386 .services = services
388 error = start_server(&server_process, &url,
389 server_basic_authentication, &server_arguments, NULL);
390 if (error == -1) {
391 isds_ctx_free(&context);
392 isds_cleanup();
393 ABORT_UNIT(server_error);
395 TEST("login", test_login, IE_SUCCESS,
396 context, url, username, password, NULL, NULL);
397 free(url);
399 TEST("No data", test_isds_find_box_by_fulltext, IE_2BIG,
400 context, service_arguments.search_text, NULL, NULL,
401 (unsigned long int *)service_arguments.search_page_size,
402 (unsigned long int *)service_arguments.search_page_number,
403 NULL,
404 NULL,
405 NULL,
406 NULL,
407 NULL,
408 NULL);
410 isds_logout(context);
411 if (stop_server(server_process)) {
412 isds_ctx_free(&context);
413 isds_cleanup();
414 ABORT_UNIT(server_error);
420 /* Successful response without a mandatory element must be discarded
421 * by the client, otherwise a garbage would appear in the output
422 * arguments. */
423 char *url = NULL;
425 const struct arguments_DS_df_ISDSSearch2 service_arguments = {
426 .status_code = "0000",
427 .status_message = "totalCount is missing",
428 .search_text = "foo",
429 .search_type = NULL,
430 .search_scope = NULL,
431 .search_page_number = NULL,
432 .search_page_size = NULL,
433 .search_highlighting_value = NULL,
434 .total_count = NULL,
435 .current_count = NULL,
436 .position = NULL,
437 .last_page = NULL,
438 .results_exists = 0,
439 .results = NULL
441 const struct service_configuration services[] = {
442 { SERVICE_DS_Dz_DummyOperation, NULL },
443 { SERVICE_DS_df_ISDSSearch2, &service_arguments },
444 { SERVICE_END, NULL }
446 const struct arguments_basic_authentication server_arguments = {
447 .username = username,
448 .password = password,
449 .isds_deviations = 1,
450 .services = services
452 error = start_server(&server_process, &url,
453 server_basic_authentication, &server_arguments, NULL);
454 if (error == -1) {
455 isds_ctx_free(&context);
456 isds_cleanup();
457 ABORT_UNIT(server_error);
459 TEST("login", test_login, IE_SUCCESS,
460 context, url, username, password, NULL, NULL);
461 free(url);
463 TEST("Missing totalCount in a response", test_isds_find_box_by_fulltext,
464 IE_SUCCESS, context, service_arguments.search_text, NULL, NULL,
465 NULL,
466 NULL,
467 NULL,
468 NULL,
469 NULL,
470 NULL,
471 NULL,
472 NULL);
474 isds_logout(context);
475 if (stop_server(server_process)) {
476 isds_ctx_free(&context);
477 isds_cleanup();
478 ABORT_UNIT(server_error);
483 isds_ctx_free(&context);
484 isds_cleanup();
485 SUM_TEST();