tests: Adapt utf8locale to musl
[libisds.git] / test / simline / isds_get_commercial_credit.c
blob60a6aa2007201bb74ee3966c04dc7f294394c621
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) and strdup(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 struct test_destructor_argument {
40 char *returned_email;
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);
47 isds_list_free(
48 &((struct test_destructor_argument *)argument)->returned_history
49 );
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,
71 from_date, to_date,
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");
88 PASS_TEST;
91 if (NULL != email)
92 TEST_STRING_DUPLICITY(*email, allocated.returned_email);
93 if (NULL != credit)
94 TEST_INT_DUPLICITY(*credit, returned_credit);
95 for (item = history, returned_item = allocated.returned_history;
96 NULL != item;
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;
102 if (NULL == event) {
103 isds_ctx_free(&context);
104 isds_cleanup();
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);
118 break;
119 case ISDS_CREDIT_DISCHARGED:
120 TEST_STRING_DUPLICITY(event->details.discharged.transaction,
121 returned_event->details.discharged.transaction);
122 break;
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);
128 break;
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);
144 break;
145 case ISDS_CREDIT_EXPIRED:
146 break;
147 default:
148 FAIL_TEST("Uknown credit event type returned");
151 if (NULL != returned_item)
152 FAIL_TEST("Returned history has too many items");
155 PASS_TEST;
158 int main(void) {
159 int error;
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");
168 if (isds_init()) {
169 isds_cleanup();
170 ABORT_UNIT("isds_init() failed\n");
172 context = isds_ctx_create();
173 if (!context) {
174 isds_cleanup();
175 ABORT_UNIT("isds_ctx_create() failed\n");
179 char *url = NULL;
180 const char *box_id = "abcefgh";
181 const struct tm from_date = {
182 .tm_year = 1,
183 .tm_mon = 1,
184 .tm_mday = 3
186 const struct tm to_date = {
187 .tm_year = 4,
188 .tm_mon = 4,
189 .tm_mday = 6
191 struct tm new_valid_from = {
192 .tm_year = 2,
193 .tm_mon = 2,
194 .tm_mday = 7
196 struct tm new_valid_to = {
197 .tm_year = 3,
198 .tm_mon = 3,
199 .tm_mday = 8
201 struct tm old_valid_from = {
202 .tm_year = 5,
203 .tm_mon = 5,
204 .tm_mday = 9
206 struct tm old_valid_to = {
207 .tm_year = 6,
208 .tm_mon = 6,
209 .tm_mday = 10
211 const long int credit = 42;
212 long int old_capacity = 43;
213 const char *email = "joe@example.com";
214 struct timeval event_time = {
215 .tv_sec = 981173106,
216 .tv_usec = 123456
219 struct isds_credit_event event_credit_expired = {
220 .time = &event_time,
221 .credit_change = 133,
222 .new_credit = 244,
223 .type = ISDS_CREDIT_EXPIRED,
225 struct server_credit_event server_event_credit_expired = {
226 .time = &event_time,
227 .credit_change = 133,
228 .new_credit = 244,
229 .type = SERVER_CREDIT_EXPIRED,
231 struct isds_list history_expired = {
232 .next = NULL,
233 .data = &event_credit_expired,
234 .destructor = NULL
236 struct server_list server_history_expired = {
237 .next = NULL,
238 .data = &server_event_credit_expired,
239 .destructor = NULL
242 struct isds_credit_event event_credit_storage_set = {
243 .time = &event_time,
244 .credit_change = 133,
245 .new_credit = 244,
246 .type = ISDS_CREDIT_STORAGE_SET,
247 .details.storage_set = {
248 .new_capacity = 41,
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,
254 .initiator = "Foo",
257 struct server_credit_event server_event_credit_storage_set = {
258 .time = &event_time,
259 .credit_change = 133,
260 .new_credit = 244,
261 .type = SERVER_CREDIT_STORAGE_SET,
262 .details.storage_set = {
263 .new_capacity = 41,
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,
269 .initiator = "Foo",
272 struct isds_list history_storage_set = {
273 .next = &history_expired,
274 .data = &event_credit_storage_set,
275 .destructor = NULL
277 struct server_list server_history_storage_set = {
278 .next = &server_history_expired,
279 .data = &server_event_credit_storage_set,
280 .destructor = NULL
283 struct isds_credit_event event_credit_message_sent = {
284 .time = &event_time,
285 .credit_change = 133,
286 .new_credit = 244,
287 .type = ISDS_CREDIT_MESSAGE_SENT,
288 .details.message_sent = {
289 .recipient = "Foo",
290 .message_id = "ijklmnop",
293 struct server_credit_event server_event_credit_message_sent = {
294 .time = &event_time,
295 .credit_change = 133,
296 .new_credit = 244,
297 .type = SERVER_CREDIT_MESSAGE_SENT,
298 .details.message_sent = {
299 .recipient = "Foo",
300 .message_id = "ijklmnop",
303 struct isds_list history_message_sent = {
304 .next = &history_storage_set,
305 .data = &event_credit_message_sent,
306 .destructor = NULL
308 struct server_list server_history_message_sent = {
309 .next = &server_history_storage_set,
310 .data = &server_event_credit_message_sent,
311 .destructor = NULL
314 struct isds_credit_event event_credit_discharged = {
315 .time = &event_time,
316 .credit_change = 133,
317 .new_credit = 244,
318 .type = ISDS_CREDIT_DISCHARGED,
319 .details.discharged.transaction = "Foo"
321 struct server_credit_event server_event_credit_discharged = {
322 .time = &event_time,
323 .credit_change = 133,
324 .new_credit = 244,
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,
331 .destructor = NULL
333 struct server_list server_history_discharged = {
334 .next = &server_history_message_sent,
335 .data = &server_event_credit_discharged,
336 .destructor = NULL
339 struct isds_credit_event event_credit_charged = {
340 .time = &event_time,
341 .credit_change = 133,
342 .new_credit = 244,
343 .type = ISDS_CREDIT_CHARGED,
344 .details.charged.transaction = "Foo"
346 struct server_credit_event server_event_credit_charged = {
347 .time = &event_time,
348 .credit_change = 133,
349 .new_credit = 244,
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,
356 .destructor = NULL
358 const struct server_list server_history = {
359 .next = &server_history_discharged,
360 .data = &server_event_credit_charged,
361 .destructor = NULL
365 const struct arguments_DS_df_DataBoxCreditInfo service_arguments = {
366 .status_code = "0000",
367 .status_message = "Ok.",
368 .box_id = box_id,
369 .from_date = &from_date,
370 .to_date = &to_date,
371 .current_credit = credit,
372 .email = email,
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,
384 .services = services
386 error = start_server(&server_process, &url,
387 server_basic_authentication, &server_arguments, NULL);
388 if (error == -1) {
389 isds_ctx_free(&context);
390 isds_cleanup();
391 ABORT_UNIT(server_error);
393 TEST("login", test_login, IE_SUCCESS,
394 context, url, username, password, NULL, NULL);
395 free(url);
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,
401 &history);
402 TEST("Wrong box_id", test_isds_get_commercial_credit, IE_ISDS,
403 context, "1", &from_date, &to_date, &credit, &email,
404 &history);
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);
411 isds_cleanup();
412 ABORT_UNIT(server_error);
418 char *url = NULL;
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.",
426 .box_id = box_id,
427 .from_date = NULL,
428 .to_date = NULL,
429 .current_credit = credit,
430 .email = email,
431 .history = NULL
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,
442 .services = services
444 error = start_server(&server_process, &url,
445 server_basic_authentication, &server_arguments, NULL);
446 if (error == -1) {
447 isds_ctx_free(&context);
448 isds_cleanup();
449 ABORT_UNIT(server_error);
451 TEST("login", test_login, IE_SUCCESS,
452 context, url, username, password, NULL, NULL);
453 free(url);
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);
461 isds_cleanup();
462 ABORT_UNIT(server_error);
467 isds_ctx_free(&context);
468 isds_cleanup();
469 SUM_TEST();