2 #define _POSIX_SOURCE /* For getaddrinfo(3) */
6 #define _BSD_SOURCE /* For NI_MAXHOST up to glibc-2.19 */
8 #ifndef _DEFAULT_SOURCE
9 #define _DEFAULT_SOURCE /* For NI_MAXHOST since glibc-2.20 */
13 #define _XOPEN_SOURCE 600 /* For unsetenv(3) */
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
) {
30 err
= isds_login(context
, url
, username
, password
, pki_credentials
, otp
);
32 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
33 isds_strerror(error
), isds_strerror(err
),
34 isds_long_message(context
));
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
;
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
);
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
))
76 if (compare_match_lists("end name",
77 expected_result
->name
, expected_result
->name_match_end
,
78 result
->name
, result
->name_match_end
))
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
))
85 if (compare_match_lists("end address",
86 expected_result
->address
, expected_result
->address_match_end
,
87 result
->address
, result
->address_match_end
))
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
);
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
;
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
))
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
);
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
;
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
);
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
,
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
) {
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
,
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
);
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
))
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");
214 ABORT_UNIT("isds_init() failed\n");
216 context
= isds_ctx_create();
219 ABORT_UNIT("isds_ctx_create() failed\n");
226 struct isds_list name_match_start
= {
231 struct isds_list name_match_end
= {
236 struct isds_list address_match_start
= {
241 struct isds_list address_match_end
= {
246 struct isds_fulltext_result result
= {
248 .dbType
= DBTYPE_OVM
,
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
,
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
= {
269 .name
= "|$*HL_START*$|Foo|$*HL_END*$| name",
270 .address
= "Address |$*HL_START*$|foo|$*HL_END*$|",
274 .send_options
= "ALL"
276 const struct isds_list results
= {
281 const struct server_list server_results
= {
283 .data
= &server_result
,
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;
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
= ¤t_count
,
308 .position
= &position
,
309 .last_page
= &last_page
,
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,
324 error
= start_server(&server_process
, &url
,
325 server_basic_authentication
, &server_arguments
, NULL
);
327 isds_ctx_free(&context
);
329 ABORT_UNIT(server_error
);
331 TEST("login", test_login
, IE_SUCCESS
,
332 context
, url
, username
, password
, NULL
, NULL
);
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
,
346 isds_logout(context
);
347 if (stop_server(server_process
)) {
348 isds_ctx_free(&context
);
350 ABORT_UNIT(server_error
);
356 /* Client must support DBTYPE_OVM_MAIN, send an empty reponse */
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;
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
= ¤t_count
,
378 .position
= &position
,
379 .last_page
= &last_page
,
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,
394 error
= start_server(&server_process
, &url
,
395 server_basic_authentication
, &server_arguments
, NULL
);
397 isds_ctx_free(&context
);
399 ABORT_UNIT(server_error
);
401 TEST("login", test_login
, IE_SUCCESS
,
402 context
, url
, username
, password
, NULL
, NULL
);
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
,
416 isds_logout(context
);
417 if (stop_server(server_process
)) {
418 isds_ctx_free(&context
);
420 ABORT_UNIT(server_error
);
426 /* Empty response due to an error */
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",
436 .search_scope
= NULL
,
437 .search_page_number
= &search_page_number
,
438 .search_page_size
= &search_page_size
,
439 .search_highlighting_value
= NULL
,
441 .current_count
= 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,
458 error
= start_server(&server_process
, &url
,
459 server_basic_authentication
, &server_arguments
, NULL
);
461 isds_ctx_free(&context
);
463 ABORT_UNIT(server_error
);
465 TEST("login", test_login
, IE_SUCCESS
,
466 context
, url
, username
, password
, NULL
, NULL
);
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
,
480 isds_logout(context
);
481 if (stop_server(server_process
)) {
482 isds_ctx_free(&context
);
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
495 const struct arguments_DS_df_ISDSSearch2 service_arguments
= {
496 .status_code
= "0000",
497 .status_message
= "totalCount is missing",
498 .search_text
= "foo",
500 .search_scope
= NULL
,
501 .search_page_number
= NULL
,
502 .search_page_size
= NULL
,
503 .search_highlighting_value
= NULL
,
505 .current_count
= 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,
522 error
= start_server(&server_process
, &url
,
523 server_basic_authentication
, &server_arguments
, NULL
);
525 isds_ctx_free(&context
);
527 ABORT_UNIT(server_error
);
529 TEST("login", test_login
, IE_SUCCESS
,
530 context
, url
, username
, password
, NULL
, NULL
);
533 TEST("Missing totalCount in a response", test_isds_find_box_by_fulltext
,
534 IE_SUCCESS
, context
, service_arguments
.search_text
, NULL
, NULL
,
544 isds_logout(context
);
545 if (stop_server(server_process
)) {
546 isds_ctx_free(&context
);
548 ABORT_UNIT(server_error
);
553 isds_ctx_free(&context
);