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
);
123 static int test_isds_find_box_by_fulltext(const isds_error expected_error
,
124 struct isds_ctx
*context
,
126 const isds_fulltext_target
*target
,
127 const isds_DbType
*box_type
,
128 const unsigned long int *page_size
,
129 const unsigned long int *page_number
,
130 const _Bool
*track_matches
,
131 const unsigned long int *expected_total_matching_boxes
,
132 const unsigned long int *expected_current_page_beginning
,
133 const unsigned long int *expected_current_page_size
,
134 const _Bool
*expected_last_page
,
135 const struct isds_list
*expected_results
) {
137 unsigned long int total_matching_boxes
= 99;
138 unsigned long int current_page_beginning
= 99;
139 unsigned long int current_page_size
= 99;
141 struct isds_list
*results
= NULL
;
143 error
= isds_find_box_by_fulltext(context
,
144 query
, target
, box_type
, page_size
, page_number
, track_matches
,
145 (NULL
== expected_total_matching_boxes
) ?
146 NULL
: &total_matching_boxes
,
147 (NULL
== expected_current_page_beginning
) ?
148 NULL
: ¤t_page_beginning
,
149 (NULL
== expected_current_page_size
) ?
150 NULL
: ¤t_page_size
,
151 (NULL
== expected_last_page
) ?
154 TEST_DESTRUCTOR((void(*)(void*))isds_list_free
, &results
);
156 if (expected_error
!= error
) {
157 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
158 isds_strerror(expected_error
), isds_strerror(error
),
159 isds_long_message(context
));
162 if (IE_SUCCESS
!= error
)
165 if (NULL
!= expected_total_matching_boxes
)
166 TEST_INT_DUPLICITY(*expected_total_matching_boxes
,
167 total_matching_boxes
);
168 if (NULL
!= expected_current_page_beginning
)
169 TEST_INT_DUPLICITY(*expected_current_page_beginning
,
170 current_page_beginning
);
171 if (NULL
!= expected_current_page_size
)
172 TEST_INT_DUPLICITY(*expected_current_page_size
,
174 if (NULL
!= expected_last_page
)
175 TEST_BOOLEAN_DUPLICITY(*expected_last_page
,
178 if (compare_result_lists(expected_results
, results
))
185 int main(int argc
, char **argv
) {
187 pid_t server_process
;
188 struct isds_ctx
*context
= NULL
;
190 INIT_TEST("isds_find_box_by_fulltext");
192 if (unsetenv("http_proxy")) {
193 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
197 ABORT_UNIT("isds_init() failed\n");
199 context
= isds_ctx_create();
202 ABORT_UNIT("isds_ctx_create() failed\n");
208 struct isds_list name_match_start
= {
213 struct isds_list name_match_end
= {
218 struct isds_list address_match_start
= {
223 struct isds_list address_match_end
= {
228 struct isds_fulltext_result result
= {
230 .dbType
= DBTYPE_OVM
,
232 .name_match_start
= &name_match_start
,
233 .name_match_end
= &name_match_end
,
234 .address
= "Address foo",
235 .address_match_start
= &address_match_start
,
236 .address_match_end
= &address_match_end
,
242 .commercial_sending
= 1
244 name_match_start
.data
= &result
.name
[0];
245 name_match_end
.data
= &result
.name
[3];
246 address_match_start
.data
= &result
.address
[8];
247 address_match_end
.data
= &result
.address
[11];
248 struct server_db_result server_result
= {
251 .name
= "|$*HL_START*$|Foo|$*HL_END*$| name",
252 .address
= "Address |$*HL_START*$|foo|$*HL_END*$|",
256 .send_options
= "ALL"
258 const struct isds_list results
= {
263 const struct server_list server_results
= {
265 .data
= &server_result
,
270 isds_fulltext_target target
= FULLTEXT_ALL
;
271 isds_DbType box_type
= DBTYPE_SYSTEM
;
272 long int search_page_number
= 42;
273 long int search_page_size
= 43;
274 _Bool search_highlighting_value
= 1;
275 unsigned long int total_count
= 44;
276 unsigned long int current_count
= 1;
277 unsigned long int position
= 43 * 42;
279 const struct arguments_DS_df_ISDSSearch2 service_arguments
= {
280 .status_code
= "0000",
281 .status_message
= "Ok.",
282 .search_text
= "foo",
283 .search_type
= "GENERAL",
284 .search_scope
= "ALL",
285 .search_page_number
= &search_page_number
,
286 .search_page_size
= &search_page_size
,
287 .search_highlighting_value
= &search_highlighting_value
,
288 .total_count
= &total_count
,
289 .current_count
= ¤t_count
,
290 .position
= &position
,
291 .last_page
= &last_page
,
293 .results
= &server_results
295 const struct service_configuration services
[] = {
296 { SERVICE_DS_Dz_DummyOperation
, NULL
},
297 { SERVICE_DS_df_ISDSSearch2
, &service_arguments
},
298 { SERVICE_END
, NULL
}
300 const struct arguments_basic_authentication server_arguments
= {
301 .username
= username
,
302 .password
= password
,
303 .isds_deviations
= 1,
306 error
= start_server(&server_process
, &url
,
307 server_basic_authentication
, &server_arguments
, NULL
);
309 isds_ctx_free(&context
);
311 ABORT_UNIT(server_error
);
313 TEST("login", test_login
, IE_SUCCESS
,
314 context
, url
, username
, password
, NULL
, NULL
);
317 TEST("All data", test_isds_find_box_by_fulltext
, IE_SUCCESS
,
318 context
, service_arguments
.search_text
, &target
, &box_type
,
319 (unsigned long int *)service_arguments
.search_page_size
,
320 (unsigned long int *)service_arguments
.search_page_number
,
321 service_arguments
.search_highlighting_value
,
322 service_arguments
.total_count
,
323 service_arguments
.position
,
324 service_arguments
.current_count
,
325 service_arguments
.last_page
,
328 isds_logout(context
);
329 if (stop_server(server_process
)) {
330 isds_ctx_free(&context
);
332 ABORT_UNIT(server_error
);
337 isds_ctx_free(&context
);