test: Move global variables into test.c
[libisds.git] / test / simline / isds_FindDataBox.c
blob2b7fad7b0ce3a222b040acfb0af0e13edae649ab
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_isds_PersonName(
40 const struct isds_PersonName *expected_result,
41 const struct isds_PersonName *result) {
43 TEST_POINTER_DUPLICITY(expected_result, result);
44 if (NULL == expected_result)
45 return 0;
47 TEST_STRING_DUPLICITY(expected_result->pnFirstName, result->pnFirstName);
48 TEST_STRING_DUPLICITY(expected_result->pnMiddleName, result->pnMiddleName);
49 TEST_STRING_DUPLICITY(expected_result->pnLastName, result->pnLastName);
50 TEST_STRING_DUPLICITY(expected_result->pnLastNameAtBirth,
51 result->pnLastNameAtBirth);
53 return 0;
56 static int compare_isds_BirthInfo(
57 const struct isds_BirthInfo *expected_result,
58 const struct isds_BirthInfo *result) {
60 TEST_POINTER_DUPLICITY(expected_result, result);
61 if (NULL == expected_result)
62 return 0;
64 TEST_TMPTR_DUPLICITY(expected_result->biDate, result->biDate);
65 TEST_STRING_DUPLICITY(expected_result->biCity, result->biCity);
66 TEST_STRING_DUPLICITY(expected_result->biCounty, result->biCounty);
67 TEST_STRING_DUPLICITY(expected_result->biState, result->biState);
69 return 0;
72 static int compare_isds_Address(
73 const struct isds_Address *expected_result,
74 const struct isds_Address *result) {
76 TEST_POINTER_DUPLICITY(expected_result, result);
77 if (NULL == expected_result)
78 return 0;
80 TEST_INTPTR_DUPLICITY(expected_result->adCode, result->adCode);
81 TEST_STRING_DUPLICITY(expected_result->adCity, result->adCity);
82 TEST_STRING_DUPLICITY(expected_result->adDistrict, result->adDistrict);
83 TEST_STRING_DUPLICITY(expected_result->adStreet, result->adStreet);
84 TEST_STRING_DUPLICITY(expected_result->adNumberInStreet,
85 result->adNumberInStreet);
86 TEST_STRING_DUPLICITY(expected_result->adNumberInMunicipality,
87 result->adNumberInMunicipality);
88 TEST_STRING_DUPLICITY(expected_result->adZipCode, result->adZipCode);
89 TEST_STRING_DUPLICITY(expected_result->adState, result->adState);
91 return 0;
94 static int compare_isds_DbOwnerInfo(
95 const struct isds_DbOwnerInfo *expected_result,
96 const struct isds_DbOwnerInfo *result) {
98 TEST_POINTER_DUPLICITY(expected_result, result);
99 if (NULL == expected_result)
100 return 0;
102 TEST_STRING_DUPLICITY(expected_result->dbID, result->dbID);
103 TEST_INTPTR_DUPLICITY(expected_result->dbType, result->dbType);
104 TEST_STRING_DUPLICITY(expected_result->ic, result->ic);
105 if (compare_isds_PersonName(expected_result->personName,
106 result->personName))
107 return 1;
108 TEST_STRING_DUPLICITY(expected_result->firmName, result->firmName);
109 if (compare_isds_BirthInfo(expected_result->birthInfo, result->birthInfo))
110 return 1;
111 if (compare_isds_Address(expected_result->address, result->address))
112 return 1;
113 TEST_STRING_DUPLICITY(expected_result->nationality, result->nationality);
114 TEST_STRING_DUPLICITY(expected_result->email, result->email);
115 TEST_STRING_DUPLICITY(expected_result->telNumber, result->telNumber);
116 TEST_STRING_DUPLICITY(expected_result->identifier, result->identifier);
117 TEST_BOOLEAN_DUPLICITY(expected_result->aifoIsds, result->aifoIsds);
118 TEST_STRING_DUPLICITY(expected_result->registryCode, result->registryCode);
119 TEST_INTPTR_DUPLICITY(expected_result->dbState, result->dbState);
120 TEST_BOOLEANPTR_DUPLICITY(expected_result->dbEffectiveOVM,
121 result->dbEffectiveOVM);
122 TEST_BOOLEANPTR_DUPLICITY(expected_result->dbOpenAddressing,
123 result->dbOpenAddressing);
125 return 0;
128 static int compare_result_lists(const struct isds_list *expected_list,
129 const struct isds_list *list) {
130 const struct isds_list *expected_item, *item;
131 int i;
133 for (i = 1, expected_item = expected_list, item = list;
134 NULL != expected_item && NULL != item;
135 i++, expected_item = expected_item->next, item = item->next) {
136 if (compare_isds_DbOwnerInfo(
137 (struct isds_DbOwnerInfo *)expected_item->data,
138 (struct isds_DbOwnerInfo *)item->data))
139 return 1;
141 if (NULL != expected_item && NULL == item)
142 FAIL_TEST("Result list is missing %d. item", i);
143 if (NULL == expected_item && NULL != item)
144 FAIL_TEST("Result list has superfluous %d. item", i);
146 return 0;
149 static int test_isds_FindDataBox(const isds_error expected_error,
150 struct isds_ctx *context,
151 const struct isds_DbOwnerInfo *criteria,
152 const struct isds_list *expected_results) {
153 isds_error error;
154 struct isds_list *results;
155 TEST_CALLOC(results);
157 error = isds_FindDataBox(context, criteria, &results);
158 TEST_DESTRUCTOR((void(*)(void*))isds_list_free, &results);
160 if (expected_error != error) {
161 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
162 isds_strerror(expected_error), isds_strerror(error),
163 isds_long_message(context));
166 if (IE_SUCCESS != error && IE_2BIG != error) {
167 TEST_POINTER_IS_NULL(results);
168 PASS_TEST;
171 if (compare_result_lists(expected_results, results))
172 return 1;
175 PASS_TEST;
178 int main(void) {
179 int error;
180 pid_t server_process;
181 struct isds_ctx *context = NULL;
183 INIT_TEST("isds_FindDataBox");
185 if (unsetenv("http_proxy")) {
186 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
188 if (isds_init()) {
189 isds_cleanup();
190 ABORT_UNIT("isds_init() failed\n");
192 context = isds_ctx_create();
193 if (!context) {
194 isds_cleanup();
195 ABORT_UNIT("isds_ctx_create() failed\n");
199 /* Full response with two results */
200 char *url = NULL;
202 struct isds_PersonName criteria_person_name = {
203 .pnFirstName = "CN1",
204 .pnMiddleName = "CN2",
205 .pnLastName = "CN3",
206 .pnLastNameAtBirth = "CN4"
208 struct tm criteria_biDate = {
209 .tm_year = 4,
210 .tm_mon = 5,
211 .tm_mday = 6
213 struct isds_BirthInfo criteria_birth_info = {
214 .biDate = &criteria_biDate,
215 .biCity = "CB1",
216 .biCounty = "CB2",
217 .biState = "CB3"
219 struct isds_Address criteria_address = {
220 .adCode = NULL,
221 .adCity = "CA1",
222 .adDistrict = "CA2",
223 .adStreet = "CA3",
224 .adNumberInStreet = "CA4",
225 .adNumberInMunicipality = "CA5",
226 .adZipCode = "CA6",
227 .adState = "CA7"
229 isds_DbType criteria_dbType = DBTYPE_OVM;
230 long int criteria_dbState = 2;
231 _Bool criteria_dbEffectiveOVM = 1;
232 _Bool criteria_dbOpenAddressing = 1;
233 struct isds_DbOwnerInfo criteria = {
234 .dbID = "Cfoo123",
235 .dbType = &criteria_dbType,
236 .ic = "C1",
237 .personName = &criteria_person_name,
238 .firmName = "C2",
239 .birthInfo = &criteria_birth_info,
240 .address = &criteria_address,
241 .nationality = "C3",
242 .email = "C4",
243 .telNumber = "C5",
244 .identifier = "C6",
245 .aifoIsds = NULL,
246 .registryCode = "C7",
247 .dbState = &criteria_dbState,
248 .dbEffectiveOVM = &criteria_dbEffectiveOVM,
249 .dbOpenAddressing = &criteria_dbOpenAddressing
251 struct server_owner_info server_criteria = {
252 .dbID = criteria.dbID,
253 .aifoIsds = NULL,
254 .dbType = "OVM",
255 .ic = criteria.ic,
256 .pnFirstName = criteria.personName->pnFirstName,
257 .pnMiddleName = criteria.personName->pnMiddleName,
258 .pnLastName = criteria.personName->pnLastName,
259 .pnLastNameAtBirth = criteria.personName->pnLastNameAtBirth,
260 .firmName = criteria.firmName,
261 .biDate = criteria.birthInfo->biDate,
262 .biCity = criteria.birthInfo->biCity,
263 .biCounty = criteria.birthInfo->biCounty,
264 .biState = criteria.birthInfo->biState,
265 .adCode = NULL,
266 .adCity = criteria.address->adCity,
267 .adDistrict = NULL,
268 .adStreet = criteria.address->adStreet,
269 .adNumberInStreet = criteria.address->adNumberInStreet,
270 .adNumberInMunicipality = criteria.address->adNumberInMunicipality,
271 .adZipCode = criteria.address->adZipCode,
272 .adState = criteria.address->adState,
273 .nationality = criteria.nationality,
274 .email = criteria.email,
275 .telNumber = criteria.telNumber,
276 .identifier = criteria.identifier,
277 .registryCode = criteria.registryCode,
278 .dbState = criteria.dbState,
279 .dbEffectiveOVM = criteria.dbEffectiveOVM,
280 .dbOpenAddressing = criteria.dbOpenAddressing
283 struct isds_PersonName person_name = {
284 .pnFirstName = "N1",
285 .pnMiddleName = "N2",
286 .pnLastName = "N3",
287 .pnLastNameAtBirth = "N4"
289 struct tm biDate = {
290 .tm_year = 1,
291 .tm_mon = 2,
292 .tm_mday = 3
294 struct isds_BirthInfo birth_info = {
295 .biDate = &biDate,
296 .biCity = "B1",
297 .biCounty = "B2",
298 .biState = "B3"
300 struct isds_Address address = {
301 .adCode = NULL,
302 .adCity = "A1",
303 .adDistrict = NULL,
304 .adStreet = "A3",
305 .adNumberInStreet = "A4",
306 .adNumberInMunicipality = "A5",
307 .adZipCode = "A6",
308 .adState = "A7"
310 isds_DbType dbType = DBTYPE_OVM;
311 long int dbState = 2;
312 _Bool dbEffectiveOVM = 1;
313 _Bool dbOpenAddressing = 1;
314 struct isds_DbOwnerInfo result = {
315 .dbID = "foo1234",
316 .dbType = &dbType,
317 .ic = "1",
318 .personName = &person_name,
319 .firmName = "2",
320 .birthInfo = &birth_info,
321 .address = &address,
322 .nationality = "3",
323 .email = "4",
324 .telNumber = "5",
325 .identifier = "6",
326 .aifoIsds = NULL,
327 .registryCode = "7",
328 .dbState = &dbState,
329 .dbEffectiveOVM = &dbEffectiveOVM,
330 .dbOpenAddressing = &dbOpenAddressing
332 struct isds_DbOwnerInfo result2 = {
335 struct server_owner_info server_result = {
336 .dbID = result.dbID,
337 .aifoIsds = NULL,
338 .dbType = "OVM",
339 .ic = result.ic,
340 .pnFirstName = result.personName->pnFirstName,
341 .pnMiddleName = result.personName->pnMiddleName,
342 .pnLastName = result.personName->pnLastName,
343 .pnLastNameAtBirth = result.personName->pnLastNameAtBirth,
344 .firmName = result.firmName,
345 .biDate = result.birthInfo->biDate,
346 .biCity = result.birthInfo->biCity,
347 .biCounty = result.birthInfo->biCounty,
348 .biState = result.birthInfo->biState,
349 .adCode = NULL,
350 .adCity = result.address->adCity,
351 .adDistrict = NULL,
352 .adStreet = result.address->adStreet,
353 .adNumberInStreet = result.address->adNumberInStreet,
354 .adNumberInMunicipality = result.address->adNumberInMunicipality,
355 .adZipCode = result.address->adZipCode,
356 .adState = result.address->adState,
357 .nationality = result.nationality,
358 .email_exists = 1,
359 .email = result.email,
360 .telNumber_exists = 1,
361 .telNumber = result.telNumber,
362 .identifier = result.identifier,
363 .registryCode = result.registryCode,
364 .dbState = result.dbState,
365 .dbEffectiveOVM = result.dbEffectiveOVM,
366 .dbOpenAddressing = result.dbOpenAddressing
368 struct server_owner_info server_result2 = {
371 struct isds_list results2 = {
372 .next = NULL,
373 .data = &result2,
374 .destructor = NULL
376 struct isds_list results = {
377 .next = &results2,
378 .data = &result,
379 .destructor = NULL
381 struct server_list server_results2 = {
382 .next = NULL,
383 .data = &server_result2,
384 .destructor = NULL
386 struct server_list server_results = {
387 .next = &server_results2,
388 .data = &server_result,
389 .destructor = NULL
392 const struct arguments_DS_df_FindDataBox service_arguments = {
393 .status_code = "0000",
394 .status_message = "Ok.",
395 .criteria = &server_criteria,
396 .results_exists = 0,
397 .results = &server_results
399 const struct service_configuration services[] = {
400 { SERVICE_DS_Dz_DummyOperation, NULL },
401 { SERVICE_DS_df_FindDataBox, &service_arguments },
402 { SERVICE_END, NULL }
404 const struct arguments_basic_authentication server_arguments = {
405 .username = username,
406 .password = password,
407 .isds_deviations = 1,
408 .services = services
410 error = start_server(&server_process, &url,
411 server_basic_authentication, &server_arguments, NULL);
412 if (error == -1) {
413 isds_ctx_free(&context);
414 isds_cleanup();
415 ABORT_UNIT(server_error);
417 TEST("login", test_login, IE_SUCCESS,
418 context, url, username, password, NULL, NULL);
419 free(url);
421 TEST("All data", test_isds_FindDataBox, IE_SUCCESS,
422 context, &criteria, &results);
424 isds_logout(context);
425 if (stop_server(server_process)) {
426 isds_ctx_free(&context);
427 isds_cleanup();
428 ABORT_UNIT(server_error);
433 /* Truncated response with one result */
434 char *url = NULL;
436 struct isds_DbOwnerInfo criteria = {
437 .dbID = "Cfoo123"
439 struct server_owner_info server_criteria = {
440 .dbID = criteria.dbID
443 struct isds_PersonName person_name = {
444 .pnFirstName = "N1",
445 .pnMiddleName = "N2",
446 .pnLastName = "N3",
447 .pnLastNameAtBirth = "N4"
449 struct tm biDate = {
450 .tm_year = 1,
451 .tm_mon = 2,
452 .tm_mday = 3
454 struct isds_BirthInfo birth_info = {
455 .biDate = &biDate,
456 .biCity = "B1",
457 .biCounty = "B2",
458 .biState = "B3"
460 struct isds_Address address = {
461 .adCode = NULL,
462 .adCity = "A1",
463 .adDistrict = NULL,
464 .adStreet = "A3",
465 .adNumberInStreet = "A4",
466 .adNumberInMunicipality = "A5",
467 .adZipCode = "A6",
468 .adState = "A7"
470 isds_DbType dbType = DBTYPE_OVM;
471 long int dbState = 2;
472 _Bool dbEffectiveOVM = 1;
473 _Bool dbOpenAddressing = 1;
474 struct isds_DbOwnerInfo result = {
475 .dbID = "foo1234",
476 .dbType = &dbType,
477 .ic = "1",
478 .personName = &person_name,
479 .firmName = "2",
480 .birthInfo = &birth_info,
481 .address = &address,
482 .nationality = "3",
483 .email = "4",
484 .telNumber = "5",
485 .identifier = "6",
486 .aifoIsds = NULL,
487 .registryCode = "7",
488 .dbState = &dbState,
489 .dbEffectiveOVM = &dbEffectiveOVM,
490 .dbOpenAddressing = &dbOpenAddressing
492 struct server_owner_info server_result = {
493 .dbID = result.dbID,
494 .aifoIsds = NULL,
495 .dbType = "OVM",
496 .ic = result.ic,
497 .pnFirstName = result.personName->pnFirstName,
498 .pnMiddleName = result.personName->pnMiddleName,
499 .pnLastName = result.personName->pnLastName,
500 .pnLastNameAtBirth = result.personName->pnLastNameAtBirth,
501 .firmName = result.firmName,
502 .biDate = result.birthInfo->biDate,
503 .biCity = result.birthInfo->biCity,
504 .biCounty = result.birthInfo->biCounty,
505 .biState = result.birthInfo->biState,
506 .adCode = NULL,
507 .adCity = result.address->adCity,
508 .adDistrict = NULL,
509 .adStreet = result.address->adStreet,
510 .adNumberInStreet = result.address->adNumberInStreet,
511 .adNumberInMunicipality = result.address->adNumberInMunicipality,
512 .adZipCode = result.address->adZipCode,
513 .adState = result.address->adState,
514 .nationality = result.nationality,
515 .email_exists = 1,
516 .email = result.email,
517 .telNumber_exists = 1,
518 .telNumber = result.telNumber,
519 .identifier = result.identifier,
520 .registryCode = result.registryCode,
521 .dbState = result.dbState,
522 .dbEffectiveOVM = result.dbEffectiveOVM,
523 .dbOpenAddressing = result.dbOpenAddressing
525 struct isds_list results = {
526 .next = NULL,
527 .data = &result,
528 .destructor = NULL
530 struct server_list server_results = {
531 .next = NULL,
532 .data = &server_result,
533 .destructor = NULL
536 const struct arguments_DS_df_FindDataBox service_arguments = {
537 .status_code = "0003",
538 .status_message = "Answer was truncated",
539 .criteria = &server_criteria,
540 .results_exists = 0,
541 .results = &server_results
543 const struct service_configuration services[] = {
544 { SERVICE_DS_Dz_DummyOperation, NULL },
545 { SERVICE_DS_df_FindDataBox, &service_arguments },
546 { SERVICE_END, NULL }
548 const struct arguments_basic_authentication server_arguments = {
549 .username = username,
550 .password = password,
551 .isds_deviations = 1,
552 .services = services
554 error = start_server(&server_process, &url,
555 server_basic_authentication, &server_arguments, NULL);
556 if (error == -1) {
557 isds_ctx_free(&context);
558 isds_cleanup();
559 ABORT_UNIT(server_error);
561 TEST("login", test_login, IE_SUCCESS,
562 context, url, username, password, NULL, NULL);
563 free(url);
565 TEST("Truncated asnwer", test_isds_FindDataBox, IE_2BIG,
566 context, &criteria, &results);
568 isds_logout(context);
569 if (stop_server(server_process)) {
570 isds_ctx_free(&context);
571 isds_cleanup();
572 ABORT_UNIT(server_error);
579 /* Client must refuse DBTYPE_OVM_MAIN and DBTYPE_SYSTEM */
580 char *url = NULL;
582 isds_DbType criteria_dbType = DBTYPE_OVM_MAIN;
583 struct isds_DbOwnerInfo criteria = {
584 .dbType = &criteria_dbType
587 const struct service_configuration services[] = {
588 { SERVICE_DS_Dz_DummyOperation, NULL },
589 { SERVICE_END, NULL }
591 const struct arguments_basic_authentication server_arguments = {
592 .username = username,
593 .password = password,
594 .isds_deviations = 1,
595 .services = services
597 error = start_server(&server_process, &url,
598 server_basic_authentication, &server_arguments, NULL);
599 if (error == -1) {
600 isds_ctx_free(&context);
601 isds_cleanup();
602 ABORT_UNIT(server_error);
604 TEST("login", test_login, IE_SUCCESS,
605 context, url, username, password, NULL, NULL);
606 free(url);
608 TEST("Invalid DBTYPE_OVM_MAIN", test_isds_FindDataBox, IE_ENUM,
609 context, &criteria, NULL);
611 criteria_dbType = DBTYPE_SYSTEM;
612 TEST("Invalid DBTYPE_SYSTEM", test_isds_FindDataBox, IE_ENUM,
613 context, &criteria, NULL);
615 isds_logout(context);
616 if (stop_server(server_process)) {
617 isds_ctx_free(&context);
618 isds_cleanup();
619 ABORT_UNIT(server_error);
624 /* Report 0002 server error as IE_NOEXIST */
625 char *url = NULL;
627 struct isds_DbOwnerInfo criteria = {
628 .dbID = "Cfoo123"
630 struct server_owner_info server_criteria = {
631 .dbID = criteria.dbID
634 const struct arguments_DS_df_FindDataBox service_arguments = {
635 .status_code = "0002",
636 .status_message = "No such box",
637 .criteria = &server_criteria,
638 .results_exists = 0,
639 .results = NULL
641 const struct service_configuration services[] = {
642 { SERVICE_DS_Dz_DummyOperation, NULL },
643 { SERVICE_DS_df_FindDataBox, &service_arguments },
644 { SERVICE_END, NULL }
646 const struct arguments_basic_authentication server_arguments = {
647 .username = username,
648 .password = password,
649 .isds_deviations = 1,
650 .services = services
652 error = start_server(&server_process, &url,
653 server_basic_authentication, &server_arguments, NULL);
654 if (error == -1) {
655 isds_ctx_free(&context);
656 isds_cleanup();
657 ABORT_UNIT(server_error);
659 TEST("login", test_login, IE_SUCCESS,
660 context, url, username, password, NULL, NULL);
661 free(url);
663 TEST("Report 0002 server error as IE_NOEXIST", test_isds_FindDataBox,
664 IE_NOEXIST, context, &criteria, NULL);
666 isds_logout(context);
667 if (stop_server(server_process)) {
668 isds_ctx_free(&context);
669 isds_cleanup();
670 ABORT_UNIT(server_error);
675 /* Report 5001 server error as IE_NOEXIST */
676 char *url = NULL;
678 struct isds_DbOwnerInfo criteria = {
679 .dbID = "Cfoo123"
681 struct server_owner_info server_criteria = {
682 .dbID = criteria.dbID
685 const struct arguments_DS_df_FindDataBox service_arguments = {
686 .status_code = "5001",
687 .status_message = "No such box",
688 .criteria = &server_criteria,
689 .results_exists = 0,
690 .results = NULL
692 const struct service_configuration services[] = {
693 { SERVICE_DS_Dz_DummyOperation, NULL },
694 { SERVICE_DS_df_FindDataBox, &service_arguments },
695 { SERVICE_END, NULL }
697 const struct arguments_basic_authentication server_arguments = {
698 .username = username,
699 .password = password,
700 .isds_deviations = 1,
701 .services = services
703 error = start_server(&server_process, &url,
704 server_basic_authentication, &server_arguments, NULL);
705 if (error == -1) {
706 isds_ctx_free(&context);
707 isds_cleanup();
708 ABORT_UNIT(server_error);
710 TEST("login", test_login, IE_SUCCESS,
711 context, url, username, password, NULL, NULL);
712 free(url);
714 TEST("Report 0002 server error as IE_NOEXIST", test_isds_FindDataBox,
715 IE_NOEXIST, context, &criteria, NULL);
717 isds_logout(context);
718 if (stop_server(server_process)) {
719 isds_ctx_free(&context);
720 isds_cleanup();
721 ABORT_UNIT(server_error);
728 isds_ctx_free(&context);
729 isds_cleanup();
730 SUM_TEST();