test: Add tests for isds_find_box_by_fulltext()
[libisds.git] / test / simline / isds_find_box_by_fulltext.c
blobee6608105f7b68780929062a54a82da00812862c
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;
123 static int test_isds_find_box_by_fulltext(const isds_error expected_error,
124 struct isds_ctx *context,
125 const char *query,
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) {
136 isds_error error;
137 unsigned long int total_matching_boxes = 99;
138 unsigned long int current_page_beginning = 99;
139 unsigned long int current_page_size = 99;
140 _Bool last_page = 1;
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 : &current_page_beginning,
149 (NULL == expected_current_page_size) ?
150 NULL : &current_page_size,
151 (NULL == expected_last_page) ?
152 NULL : &last_page,
153 &results);
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)
163 PASS_TEST;
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,
173 current_page_size);
174 if (NULL != expected_last_page)
175 TEST_BOOLEAN_DUPLICITY(*expected_last_page,
176 last_page);
178 if (compare_result_lists(expected_results, results))
179 return 1;
182 PASS_TEST;
185 int main(int argc, char **argv) {
186 int error;
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");
195 if (isds_init()) {
196 isds_cleanup();
197 ABORT_UNIT("isds_init() failed\n");
199 context = isds_ctx_create();
200 if (!context) {
201 isds_cleanup();
202 ABORT_UNIT("isds_ctx_create() failed\n");
206 char *url = NULL;
208 struct isds_list name_match_start = {
209 .next = NULL,
210 .data = NULL,
211 .destructor = NULL
213 struct isds_list name_match_end = {
214 .next = NULL,
215 .data = NULL,
216 .destructor = NULL
218 struct isds_list address_match_start = {
219 .next = NULL,
220 .data = NULL,
221 .destructor = NULL
223 struct isds_list address_match_end = {
224 .next = NULL,
225 .data = NULL,
226 .destructor = NULL
228 struct isds_fulltext_result result = {
229 .dbID = "foo1234",
230 .dbType = DBTYPE_OVM,
231 .name = "Foo name",
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,
237 .ic = "Foo IC",
238 .biDate = NULL,
239 .dbEffectiveOVM = 1,
240 .active = 1,
241 .public_sending = 1,
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 = {
249 .id = "foo1234",
250 .type = "OVM",
251 .name = "|$*HL_START*$|Foo|$*HL_END*$| name",
252 .address = "Address |$*HL_START*$|foo|$*HL_END*$|",
253 .birth_date = NULL,
254 .ic = "Foo IC",
255 .ovm = 1,
256 .send_options = "ALL"
258 const struct isds_list results = {
259 .next = NULL,
260 .data = &result,
261 .destructor = NULL
263 const struct server_list server_results = {
264 .next = NULL,
265 .data = &server_result,
266 .destructor = NULL
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;
278 _Bool last_page = 1;
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 = &current_count,
290 .position = &position,
291 .last_page = &last_page,
292 .results_exists = 0,
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,
304 .services = services
306 error = start_server(&server_process, &url,
307 server_basic_authentication, &server_arguments, NULL);
308 if (error == -1) {
309 isds_ctx_free(&context);
310 isds_cleanup();
311 ABORT_UNIT(server_error);
313 TEST("login", test_login, IE_SUCCESS,
314 context, url, username, password, NULL, NULL);
315 free(url);
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,
326 &results);
328 isds_logout(context);
329 if (stop_server(server_process)) {
330 isds_ctx_free(&context);
331 isds_cleanup();
332 ABORT_UNIT(server_error);
337 isds_ctx_free(&context);
338 isds_cleanup();
339 SUM_TEST();