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 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 ABORT_UNIT("History event is not defined");
104 if (NULL
== returned_item
)
105 FAIL_TEST("Returned history event is NULL and it shouldn't be");
106 TEST_TIMEVALPTR_DUPLICITY(event
->time
, returned_event
->time
);
107 TEST_INT_DUPLICITY(event
->credit_change
, returned_event
->credit_change
);
108 TEST_INT_DUPLICITY(event
->new_credit
, returned_event
->new_credit
);
109 TEST_INT_DUPLICITY(event
->type
, returned_event
->type
);
111 switch (event
->type
) {
112 case ISDS_CREDIT_CHARGED
:
113 TEST_STRING_DUPLICITY(event
->details
.charged
.transaction
,
114 returned_event
->details
.charged
.transaction
);
116 case ISDS_CREDIT_DISCHARGED
:
117 TEST_STRING_DUPLICITY(event
->details
.discharged
.transaction
,
118 returned_event
->details
.discharged
.transaction
);
120 case ISDS_CREDIT_MESSAGE_SENT
:
121 TEST_STRING_DUPLICITY(event
->details
.message_sent
.recipient
,
122 returned_event
->details
.message_sent
.recipient
);
123 TEST_STRING_DUPLICITY(event
->details
.message_sent
.message_id
,
124 returned_event
->details
.message_sent
.message_id
);
126 case ISDS_CREDIT_STORAGE_SET
:
127 TEST_INT_DUPLICITY(event
->details
.storage_set
.new_capacity
,
128 returned_event
->details
.storage_set
.new_capacity
);
129 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.new_valid_from
,
130 returned_event
->details
.storage_set
.new_valid_from
);
131 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.new_valid_to
,
132 returned_event
->details
.storage_set
.new_valid_to
);
133 TEST_INTPTR_DUPLICITY(event
->details
.storage_set
.old_capacity
,
134 returned_event
->details
.storage_set
.old_capacity
);
135 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.old_valid_from
,
136 returned_event
->details
.storage_set
.old_valid_from
);
137 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.old_valid_to
,
138 returned_event
->details
.storage_set
.old_valid_to
);
139 TEST_STRING_DUPLICITY(event
->details
.storage_set
.initiator
,
140 returned_event
->details
.storage_set
.initiator
);
142 case ISDS_CREDIT_EXPIRED
:
145 FAIL_TEST("Uknown credit event type returned");
148 if (NULL
!= returned_item
)
149 FAIL_TEST("Returned history has too many items");
155 int main(int argc
, char **argv
) {
157 pid_t server_process
;
158 struct isds_ctx
*context
= NULL
;
160 INIT_TEST("isds_get_commercial_credit");
162 if (unsetenv("http_proxy")) {
163 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
167 ABORT_UNIT("isds_init() failed\n");
169 context
= isds_ctx_create();
172 ABORT_UNIT("isds_ctx_create() failed\n");
177 const char *box_id
= "abcefgh";
178 const struct tm from_date
= {
183 const struct tm to_date
= {
188 struct tm new_valid_from
= {
193 struct tm new_valid_to
= {
198 struct tm old_valid_from
= {
203 struct tm old_valid_to
= {
208 const long int credit
= 42;
209 long int old_capacity
= 43;
210 const char *email
= "joe@example.com";
211 struct timeval event_time
= {
216 struct isds_credit_event event_credit_expired
= {
218 .credit_change
= 133,
220 .type
= ISDS_CREDIT_EXPIRED
,
222 struct server_credit_event server_event_credit_expired
= {
224 .credit_change
= 133,
226 .type
= SERVER_CREDIT_EXPIRED
,
228 struct isds_list history_expired
= {
230 .data
= &event_credit_expired
,
233 struct server_list server_history_expired
= {
235 .data
= &server_event_credit_expired
,
239 struct isds_credit_event event_credit_storage_set
= {
241 .credit_change
= 133,
243 .type
= ISDS_CREDIT_STORAGE_SET
,
244 .details
.storage_set
.new_capacity
= 41,
245 .details
.storage_set
.new_valid_from
= &new_valid_from
,
246 .details
.storage_set
.new_valid_to
= &new_valid_to
,
247 .details
.storage_set
.old_capacity
= &old_capacity
,
248 .details
.storage_set
.old_valid_from
= &old_valid_from
,
249 .details
.storage_set
.old_valid_to
= &old_valid_to
,
250 .details
.storage_set
.initiator
= "Foo",
252 struct server_credit_event server_event_credit_storage_set
= {
254 .credit_change
= 133,
256 .type
= SERVER_CREDIT_STORAGE_SET
,
257 .details
.storage_set
.new_capacity
= 41,
258 .details
.storage_set
.new_valid_from
= &new_valid_from
,
259 .details
.storage_set
.new_valid_to
= &new_valid_to
,
260 .details
.storage_set
.old_capacity
= &old_capacity
,
261 .details
.storage_set
.old_valid_from
= &old_valid_from
,
262 .details
.storage_set
.old_valid_to
= &old_valid_to
,
263 .details
.storage_set
.initiator
= "Foo",
265 struct isds_list history_storage_set
= {
266 .next
= &history_expired
,
267 .data
= &event_credit_storage_set
,
270 struct server_list server_history_storage_set
= {
271 .next
= &server_history_expired
,
272 .data
= &server_event_credit_storage_set
,
276 struct isds_credit_event event_credit_message_sent
= {
278 .credit_change
= 133,
280 .type
= ISDS_CREDIT_MESSAGE_SENT
,
281 .details
.message_sent
.recipient
= "Foo",
282 .details
.message_sent
.message_id
= "ijklmnop",
284 struct server_credit_event server_event_credit_message_sent
= {
286 .credit_change
= 133,
288 .type
= SERVER_CREDIT_MESSAGE_SENT
,
289 .details
.message_sent
.recipient
= "Foo",
290 .details
.message_sent
.message_id
= "ijklmnop",
292 struct isds_list history_message_sent
= {
293 .next
= &history_storage_set
,
294 .data
= &event_credit_message_sent
,
297 struct server_list server_history_message_sent
= {
298 .next
= &server_history_storage_set
,
299 .data
= &server_event_credit_message_sent
,
303 struct isds_credit_event event_credit_discharged
= {
305 .credit_change
= 133,
307 .type
= ISDS_CREDIT_DISCHARGED
,
308 .details
.discharged
.transaction
= "Foo"
310 struct server_credit_event server_event_credit_discharged
= {
312 .credit_change
= 133,
314 .type
= SERVER_CREDIT_DISCHARGED
,
315 .details
.discharged
.transaction
= "Foo"
317 struct isds_list history_discharged
= {
318 .next
= &history_message_sent
,
319 .data
= &event_credit_discharged
,
322 struct server_list server_history_discharged
= {
323 .next
= &server_history_message_sent
,
324 .data
= &server_event_credit_discharged
,
328 struct isds_credit_event event_credit_charged
= {
330 .credit_change
= 133,
332 .type
= ISDS_CREDIT_CHARGED
,
333 .details
.charged
.transaction
= "Foo"
335 struct server_credit_event server_event_credit_charged
= {
337 .credit_change
= 133,
339 .type
= SERVER_CREDIT_CHARGED
,
340 .details
.charged
.transaction
= "Foo"
342 const struct isds_list history
= {
343 .next
= &history_discharged
,
344 .data
= &event_credit_charged
,
347 const struct server_list server_history
= {
348 .next
= &server_history_discharged
,
349 .data
= &server_event_credit_charged
,
354 const struct arguments_DS_df_DataBoxCreditInfo service_arguments
= {
355 .status_code
= "0000",
356 .status_message
= "Ok.",
358 .from_date
= &from_date
,
360 .current_credit
= credit
,
362 .history
= &server_history
364 const struct service_configuration services
[] = {
365 { SERVICE_DS_Dz_DummyOperation
, NULL
},
366 { SERVICE_DS_df_DataBoxCreditInfo
, &service_arguments
},
367 { SERVICE_END
, NULL
}
369 const struct arguments_basic_authentication server_arguments
= {
370 .username
= username
,
371 .password
= password
,
372 .isds_deviations
= 1,
375 error
= start_server(&server_process
, &url
,
376 server_basic_authentication
, &server_arguments
, NULL
);
378 isds_ctx_free(&context
);
380 ABORT_UNIT(server_error
);
382 TEST("login", test_login
, IE_SUCCESS
,
383 context
, url
, username
, password
, NULL
, NULL
);
386 TEST("NULL box_id", test_isds_get_commercial_credit
, IE_INVAL
,
387 context
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
388 TEST("All data", test_isds_get_commercial_credit
, IE_SUCCESS
,
389 context
, box_id
, &from_date
, &to_date
, &credit
, &email
,
391 TEST("Wrong box_id", test_isds_get_commercial_credit
, IE_ISDS
,
392 context
, "1", &from_date
, &to_date
, &credit
, &email
,
394 TEST("No history", test_isds_get_commercial_credit
, IE_SUCCESS
,
395 context
, box_id
, &from_date
, &to_date
, &credit
, &email
, NULL
);
397 isds_logout(context
);
398 if (stop_server(server_process
)) {
399 ABORT_UNIT(server_error
);
406 const char *box_id
= "abcefgh";
407 const long int credit
= 42;
408 const char *email
= "joe@example.com";
410 const struct arguments_DS_df_DataBoxCreditInfo service_arguments
= {
411 .status_code
= "0000",
412 .status_message
= "Ok.",
416 .current_credit
= credit
,
420 const struct service_configuration services
[] = {
421 { SERVICE_DS_Dz_DummyOperation
, NULL
},
422 { SERVICE_DS_df_DataBoxCreditInfo
, &service_arguments
},
423 { SERVICE_END
, NULL
}
425 const struct arguments_basic_authentication server_arguments
= {
426 .username
= username
,
427 .password
= password
,
428 .isds_deviations
= 1,
431 error
= start_server(&server_process
, &url
,
432 server_basic_authentication
, &server_arguments
, NULL
);
434 isds_ctx_free(&context
);
436 ABORT_UNIT(server_error
);
438 TEST("login", test_login
, IE_SUCCESS
,
439 context
, url
, username
, password
, NULL
, NULL
);
442 TEST("No dates", test_isds_get_commercial_credit
, IE_SUCCESS
,
443 context
, box_id
, NULL
, NULL
, &credit
, &email
, NULL
);
445 isds_logout(context
);
446 if (stop_server(server_process
)) {
447 ABORT_UNIT(server_error
);
452 isds_ctx_free(&context
);