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) and strdup(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 struct test_destructor_argument
{
41 struct isds_list
*returned_history
;
44 static void test_destructor(void *argument
) {
45 if (NULL
== argument
) return;
46 free(((struct test_destructor_argument
*)argument
)->returned_email
);
48 &((struct test_destructor_argument
*)argument
)->returned_history
52 static int test_isds_get_commercial_credit(const isds_error error
,
53 struct isds_ctx
*context
, const char *box_id
,
54 const struct tm
*from_date
, const struct tm
*to_date
,
55 const long int *credit
, const char **email
,
56 const struct isds_list
*history
) {
57 isds_error returned_error
;
58 long int returned_credit
= -1;
60 struct test_destructor_argument allocated
= {
61 .returned_email
= strdup("foo"),
62 .returned_history
= NULL
65 const struct isds_list
*item
;
66 struct isds_list
*returned_item
;
67 const struct isds_credit_event
*event
;
68 struct isds_credit_event
*returned_event
;
70 returned_error
= isds_get_commercial_credit(context
, box_id
,
72 (NULL
== credit
) ? NULL
: &returned_credit
,
73 (NULL
== email
) ? NULL
: &allocated
.returned_email
,
74 (NULL
== history
) ? NULL
: &allocated
.returned_history
);
75 TEST_DESTRUCTOR(test_destructor
, &allocated
);
77 if (error
!= returned_error
) {
78 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
79 isds_strerror(error
), isds_strerror(returned_error
),
80 isds_long_message(context
));
83 if (IE_SUCCESS
!= returned_error
) {
84 if (NULL
!= email
&& NULL
!= allocated
.returned_email
)
85 FAIL_TEST("email is not NULL on error");
86 if (NULL
!= history
&& NULL
!= allocated
.returned_history
)
87 FAIL_TEST("history is not NULL on error");
92 TEST_STRING_DUPLICITY(*email
, allocated
.returned_email
);
94 TEST_INT_DUPLICITY(*credit
, returned_credit
);
95 for (item
= history
, returned_item
= allocated
.returned_history
;
97 item
= item
->next
, returned_item
= returned_item
->next
) {
98 if (NULL
== returned_item
)
99 FAIL_TEST("Returned history has too few items");
100 event
= (struct isds_credit_event
*)item
->data
;
101 returned_event
= (struct isds_credit_event
*)returned_item
->data
;
103 isds_ctx_free(&context
);
105 ABORT_UNIT("History event is not defined");
107 if (NULL
== returned_item
)
108 FAIL_TEST("Returned history event is NULL and it shouldn't be");
109 TEST_TIMEVALPTR_DUPLICITY(event
->time
, returned_event
->time
);
110 TEST_INT_DUPLICITY(event
->credit_change
, returned_event
->credit_change
);
111 TEST_INT_DUPLICITY(event
->new_credit
, returned_event
->new_credit
);
112 TEST_INT_DUPLICITY(event
->type
, returned_event
->type
);
114 switch (event
->type
) {
115 case ISDS_CREDIT_CHARGED
:
116 TEST_STRING_DUPLICITY(event
->details
.charged
.transaction
,
117 returned_event
->details
.charged
.transaction
);
119 case ISDS_CREDIT_DISCHARGED
:
120 TEST_STRING_DUPLICITY(event
->details
.discharged
.transaction
,
121 returned_event
->details
.discharged
.transaction
);
123 case ISDS_CREDIT_MESSAGE_SENT
:
124 TEST_STRING_DUPLICITY(event
->details
.message_sent
.recipient
,
125 returned_event
->details
.message_sent
.recipient
);
126 TEST_STRING_DUPLICITY(event
->details
.message_sent
.message_id
,
127 returned_event
->details
.message_sent
.message_id
);
129 case ISDS_CREDIT_STORAGE_SET
:
130 TEST_INT_DUPLICITY(event
->details
.storage_set
.new_capacity
,
131 returned_event
->details
.storage_set
.new_capacity
);
132 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.new_valid_from
,
133 returned_event
->details
.storage_set
.new_valid_from
);
134 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.new_valid_to
,
135 returned_event
->details
.storage_set
.new_valid_to
);
136 TEST_INTPTR_DUPLICITY(event
->details
.storage_set
.old_capacity
,
137 returned_event
->details
.storage_set
.old_capacity
);
138 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.old_valid_from
,
139 returned_event
->details
.storage_set
.old_valid_from
);
140 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.old_valid_to
,
141 returned_event
->details
.storage_set
.old_valid_to
);
142 TEST_STRING_DUPLICITY(event
->details
.storage_set
.initiator
,
143 returned_event
->details
.storage_set
.initiator
);
145 case ISDS_CREDIT_EXPIRED
:
148 FAIL_TEST("Uknown credit event type returned");
151 if (NULL
!= returned_item
)
152 FAIL_TEST("Returned history has too many items");
160 pid_t server_process
;
161 struct isds_ctx
*context
= NULL
;
163 INIT_TEST("isds_get_commercial_credit");
165 if (unsetenv("http_proxy")) {
166 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
170 ABORT_UNIT("isds_init() failed\n");
172 context
= isds_ctx_create();
175 ABORT_UNIT("isds_ctx_create() failed\n");
180 const char *box_id
= "abcefgh";
181 const struct tm from_date
= {
186 const struct tm to_date
= {
191 struct tm new_valid_from
= {
196 struct tm new_valid_to
= {
201 struct tm old_valid_from
= {
206 struct tm old_valid_to
= {
211 const long int credit
= 42;
212 long int old_capacity
= 43;
213 const char *email
= "joe@example.com";
214 struct timeval event_time
= {
219 struct isds_credit_event event_credit_expired
= {
221 .credit_change
= 133,
223 .type
= ISDS_CREDIT_EXPIRED
,
225 struct server_credit_event server_event_credit_expired
= {
227 .credit_change
= 133,
229 .type
= SERVER_CREDIT_EXPIRED
,
231 struct isds_list history_expired
= {
233 .data
= &event_credit_expired
,
236 struct server_list server_history_expired
= {
238 .data
= &server_event_credit_expired
,
242 struct isds_credit_event event_credit_storage_set
= {
244 .credit_change
= 133,
246 .type
= ISDS_CREDIT_STORAGE_SET
,
247 .details
.storage_set
= {
249 .new_valid_from
= &new_valid_from
,
250 .new_valid_to
= &new_valid_to
,
251 .old_capacity
= &old_capacity
,
252 .old_valid_from
= &old_valid_from
,
253 .old_valid_to
= &old_valid_to
,
257 struct server_credit_event server_event_credit_storage_set
= {
259 .credit_change
= 133,
261 .type
= SERVER_CREDIT_STORAGE_SET
,
262 .details
.storage_set
= {
264 .new_valid_from
= &new_valid_from
,
265 .new_valid_to
= &new_valid_to
,
266 .old_capacity
= &old_capacity
,
267 .old_valid_from
= &old_valid_from
,
268 .old_valid_to
= &old_valid_to
,
272 struct isds_list history_storage_set
= {
273 .next
= &history_expired
,
274 .data
= &event_credit_storage_set
,
277 struct server_list server_history_storage_set
= {
278 .next
= &server_history_expired
,
279 .data
= &server_event_credit_storage_set
,
283 struct isds_credit_event event_credit_message_sent
= {
285 .credit_change
= 133,
287 .type
= ISDS_CREDIT_MESSAGE_SENT
,
288 .details
.message_sent
= {
290 .message_id
= "ijklmnop",
293 struct server_credit_event server_event_credit_message_sent
= {
295 .credit_change
= 133,
297 .type
= SERVER_CREDIT_MESSAGE_SENT
,
298 .details
.message_sent
= {
300 .message_id
= "ijklmnop",
303 struct isds_list history_message_sent
= {
304 .next
= &history_storage_set
,
305 .data
= &event_credit_message_sent
,
308 struct server_list server_history_message_sent
= {
309 .next
= &server_history_storage_set
,
310 .data
= &server_event_credit_message_sent
,
314 struct isds_credit_event event_credit_discharged
= {
316 .credit_change
= 133,
318 .type
= ISDS_CREDIT_DISCHARGED
,
319 .details
.discharged
.transaction
= "Foo"
321 struct server_credit_event server_event_credit_discharged
= {
323 .credit_change
= 133,
325 .type
= SERVER_CREDIT_DISCHARGED
,
326 .details
.discharged
.transaction
= "Foo"
328 struct isds_list history_discharged
= {
329 .next
= &history_message_sent
,
330 .data
= &event_credit_discharged
,
333 struct server_list server_history_discharged
= {
334 .next
= &server_history_message_sent
,
335 .data
= &server_event_credit_discharged
,
339 struct isds_credit_event event_credit_charged
= {
341 .credit_change
= 133,
343 .type
= ISDS_CREDIT_CHARGED
,
344 .details
.charged
.transaction
= "Foo"
346 struct server_credit_event server_event_credit_charged
= {
348 .credit_change
= 133,
350 .type
= SERVER_CREDIT_CHARGED
,
351 .details
.charged
.transaction
= "Foo"
353 const struct isds_list history
= {
354 .next
= &history_discharged
,
355 .data
= &event_credit_charged
,
358 const struct server_list server_history
= {
359 .next
= &server_history_discharged
,
360 .data
= &server_event_credit_charged
,
365 const struct arguments_DS_df_DataBoxCreditInfo service_arguments
= {
366 .status_code
= "0000",
367 .status_message
= "Ok.",
369 .from_date
= &from_date
,
371 .current_credit
= credit
,
373 .history
= &server_history
375 const struct service_configuration services
[] = {
376 { SERVICE_DS_Dz_DummyOperation
, NULL
},
377 { SERVICE_DS_df_DataBoxCreditInfo
, &service_arguments
},
378 { SERVICE_END
, NULL
}
380 const struct arguments_basic_authentication server_arguments
= {
381 .username
= username
,
382 .password
= password
,
383 .isds_deviations
= 1,
386 error
= start_server(&server_process
, &url
,
387 server_basic_authentication
, &server_arguments
, NULL
);
389 isds_ctx_free(&context
);
391 ABORT_UNIT(server_error
);
393 TEST("login", test_login
, IE_SUCCESS
,
394 context
, url
, username
, password
, NULL
, NULL
);
397 TEST("NULL box_id", test_isds_get_commercial_credit
, IE_INVAL
,
398 context
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
399 TEST("All data", test_isds_get_commercial_credit
, IE_SUCCESS
,
400 context
, box_id
, &from_date
, &to_date
, &credit
, &email
,
402 TEST("Wrong box_id", test_isds_get_commercial_credit
, IE_ISDS
,
403 context
, "1", &from_date
, &to_date
, &credit
, &email
,
405 TEST("No history", test_isds_get_commercial_credit
, IE_SUCCESS
,
406 context
, box_id
, &from_date
, &to_date
, &credit
, &email
, NULL
);
408 isds_logout(context
);
409 if (stop_server(server_process
)) {
410 isds_ctx_free(&context
);
412 ABORT_UNIT(server_error
);
419 const char *box_id
= "abcefgh";
420 const long int credit
= 42;
421 const char *email
= "joe@example.com";
423 const struct arguments_DS_df_DataBoxCreditInfo service_arguments
= {
424 .status_code
= "0000",
425 .status_message
= "Ok.",
429 .current_credit
= credit
,
433 const struct service_configuration services
[] = {
434 { SERVICE_DS_Dz_DummyOperation
, NULL
},
435 { SERVICE_DS_df_DataBoxCreditInfo
, &service_arguments
},
436 { SERVICE_END
, NULL
}
438 const struct arguments_basic_authentication server_arguments
= {
439 .username
= username
,
440 .password
= password
,
441 .isds_deviations
= 1,
444 error
= start_server(&server_process
, &url
,
445 server_basic_authentication
, &server_arguments
, NULL
);
447 isds_ctx_free(&context
);
449 ABORT_UNIT(server_error
);
451 TEST("login", test_login
, IE_SUCCESS
,
452 context
, url
, username
, password
, NULL
, NULL
);
455 TEST("No dates", test_isds_get_commercial_credit
, IE_SUCCESS
,
456 context
, box_id
, NULL
, NULL
, &credit
, &email
, NULL
);
458 isds_logout(context
);
459 if (stop_server(server_process
)) {
460 isds_ctx_free(&context
);
462 ABORT_UNIT(server_error
);
467 isds_ctx_free(&context
);