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 /* Empty response due to an error */
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",
366 .search_scope
= NULL
,
367 .search_page_number
= &search_page_number
,
368 .search_page_size
= &search_page_size
,
369 .search_highlighting_value
= NULL
,
371 .current_count
= 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,
388 error
= start_server(&server_process
, &url
,
389 server_basic_authentication
, &server_arguments
, NULL
);
391 isds_ctx_free(&context
);
393 ABORT_UNIT(server_error
);
395 TEST("login", test_login
, IE_SUCCESS
,
396 context
, url
, username
, password
, NULL
, NULL
);
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
,
410 isds_logout(context
);
411 if (stop_server(server_process
)) {
412 isds_ctx_free(&context
);
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
425 const struct arguments_DS_df_ISDSSearch2 service_arguments
= {
426 .status_code
= "0000",
427 .status_message
= "totalCount is missing",
428 .search_text
= "foo",
430 .search_scope
= NULL
,
431 .search_page_number
= NULL
,
432 .search_page_size
= NULL
,
433 .search_highlighting_value
= NULL
,
435 .current_count
= 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,
452 error
= start_server(&server_process
, &url
,
453 server_basic_authentication
, &server_arguments
, NULL
);
455 isds_ctx_free(&context
);
457 ABORT_UNIT(server_error
);
459 TEST("login", test_login
, IE_SUCCESS
,
460 context
, url
, username
, password
, NULL
, NULL
);
463 TEST("Missing totalCount in a response", test_isds_find_box_by_fulltext
,
464 IE_SUCCESS
, context
, service_arguments
.search_text
, NULL
, NULL
,
474 isds_logout(context
);
475 if (stop_server(server_process
)) {
476 isds_ctx_free(&context
);
478 ABORT_UNIT(server_error
);
483 isds_ctx_free(&context
);