Add missing user and sender types to example client's dump code
[libisds.git] / test / simline / isds_find_box_by_fulltext.c
bloba8e60e085b5257be1e65b18c3df643b9bf35c71f
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 /* Client must support DBTYPE_OVM_MAIN, send an empty reponse */
357 char *url = NULL;
359 isds_fulltext_target target = FULLTEXT_ALL;
360 isds_DbType box_type = DBTYPE_OVM_MAIN;
361 long int search_page_number = 42;
362 long int search_page_size = 43;
363 unsigned long int total_count = 0;
364 unsigned long int current_count = 0;
365 unsigned long int position = 43 * 42;
366 _Bool last_page = 1;
367 const struct arguments_DS_df_ISDSSearch2 service_arguments = {
368 .status_code = "0000",
369 .status_message = "No boxes match",
370 .search_text = "foo",
371 .search_type = "GENERAL",
372 .search_scope = "OVM_MAIN",
373 .search_page_number = &search_page_number,
374 .search_page_size = &search_page_size,
375 .search_highlighting_value = NULL,
376 .total_count = &total_count,
377 .current_count = &current_count,
378 .position = &position,
379 .last_page = &last_page,
380 .results_exists = 1,
381 .results = NULL
383 const struct service_configuration services[] = {
384 { SERVICE_DS_Dz_DummyOperation, NULL },
385 { SERVICE_DS_df_ISDSSearch2, &service_arguments },
386 { SERVICE_END, NULL }
388 const struct arguments_basic_authentication server_arguments = {
389 .username = username,
390 .password = password,
391 .isds_deviations = 1,
392 .services = services
394 error = start_server(&server_process, &url,
395 server_basic_authentication, &server_arguments, NULL);
396 if (error == -1) {
397 isds_ctx_free(&context);
398 isds_cleanup();
399 ABORT_UNIT(server_error);
401 TEST("login", test_login, IE_SUCCESS,
402 context, url, username, password, NULL, NULL);
403 free(url);
405 TEST("DBTYPE_OVM_MAIN", test_isds_find_box_by_fulltext, IE_SUCCESS,
406 context, service_arguments.search_text, &target, &box_type,
407 (unsigned long int *)service_arguments.search_page_size,
408 (unsigned long int *)service_arguments.search_page_number,
409 service_arguments.search_highlighting_value,
410 service_arguments.total_count,
411 service_arguments.position,
412 service_arguments.current_count,
413 service_arguments.last_page,
414 NULL);
416 isds_logout(context);
417 if (stop_server(server_process)) {
418 isds_ctx_free(&context);
419 isds_cleanup();
420 ABORT_UNIT(server_error);
426 /* Empty response due to an error */
427 char *url = NULL;
429 long int search_page_number = 42;
430 long int search_page_size = 43;
431 const struct arguments_DS_df_ISDSSearch2 service_arguments = {
432 .status_code = "1156",
433 .status_message = "pageSize is too large",
434 .search_text = "foo",
435 .search_type = NULL,
436 .search_scope = NULL,
437 .search_page_number = &search_page_number,
438 .search_page_size = &search_page_size,
439 .search_highlighting_value = NULL,
440 .total_count = NULL,
441 .current_count = NULL,
442 .position = NULL,
443 .last_page = NULL,
444 .results_exists = 0,
445 .results = NULL
447 const struct service_configuration services[] = {
448 { SERVICE_DS_Dz_DummyOperation, NULL },
449 { SERVICE_DS_df_ISDSSearch2, &service_arguments },
450 { SERVICE_END, NULL }
452 const struct arguments_basic_authentication server_arguments = {
453 .username = username,
454 .password = password,
455 .isds_deviations = 1,
456 .services = services
458 error = start_server(&server_process, &url,
459 server_basic_authentication, &server_arguments, NULL);
460 if (error == -1) {
461 isds_ctx_free(&context);
462 isds_cleanup();
463 ABORT_UNIT(server_error);
465 TEST("login", test_login, IE_SUCCESS,
466 context, url, username, password, NULL, NULL);
467 free(url);
469 TEST("No data", test_isds_find_box_by_fulltext, IE_2BIG,
470 context, service_arguments.search_text, NULL, NULL,
471 (unsigned long int *)service_arguments.search_page_size,
472 (unsigned long int *)service_arguments.search_page_number,
473 NULL,
474 NULL,
475 NULL,
476 NULL,
477 NULL,
478 NULL);
480 isds_logout(context);
481 if (stop_server(server_process)) {
482 isds_ctx_free(&context);
483 isds_cleanup();
484 ABORT_UNIT(server_error);
490 /* Successful response without a mandatory element must be discarded
491 * by the client, otherwise a garbage would appear in the output
492 * arguments. */
493 char *url = NULL;
495 const struct arguments_DS_df_ISDSSearch2 service_arguments = {
496 .status_code = "0000",
497 .status_message = "totalCount is missing",
498 .search_text = "foo",
499 .search_type = NULL,
500 .search_scope = NULL,
501 .search_page_number = NULL,
502 .search_page_size = NULL,
503 .search_highlighting_value = NULL,
504 .total_count = NULL,
505 .current_count = NULL,
506 .position = NULL,
507 .last_page = NULL,
508 .results_exists = 0,
509 .results = NULL
511 const struct service_configuration services[] = {
512 { SERVICE_DS_Dz_DummyOperation, NULL },
513 { SERVICE_DS_df_ISDSSearch2, &service_arguments },
514 { SERVICE_END, NULL }
516 const struct arguments_basic_authentication server_arguments = {
517 .username = username,
518 .password = password,
519 .isds_deviations = 1,
520 .services = services
522 error = start_server(&server_process, &url,
523 server_basic_authentication, &server_arguments, NULL);
524 if (error == -1) {
525 isds_ctx_free(&context);
526 isds_cleanup();
527 ABORT_UNIT(server_error);
529 TEST("login", test_login, IE_SUCCESS,
530 context, url, username, password, NULL, NULL);
531 free(url);
533 TEST("Missing totalCount in a response", test_isds_find_box_by_fulltext,
534 IE_SUCCESS, context, service_arguments.search_text, NULL, NULL,
535 NULL,
536 NULL,
537 NULL,
538 NULL,
539 NULL,
540 NULL,
541 NULL,
542 NULL);
544 isds_logout(context);
545 if (stop_server(server_process)) {
546 isds_ctx_free(&context);
547 isds_cleanup();
548 ABORT_UNIT(server_error);
553 isds_ctx_free(&context);
554 isds_cleanup();
555 SUM_TEST();