Use _DEFAULT_SOURCE where _BSD_SOURCE macro presents
[libisds.git] / test / simline / isds_get_commercial_credit.c
blobead4f63ddac60e74fa8f724b845fb16bd08191bd
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 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 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);
115 break;
116 case ISDS_CREDIT_DISCHARGED:
117 TEST_STRING_DUPLICITY(event->details.discharged.transaction,
118 returned_event->details.discharged.transaction);
119 break;
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);
125 break;
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);
141 break;
142 case ISDS_CREDIT_EXPIRED:
143 break;
144 default:
145 FAIL_TEST("Uknown credit event type returned");
148 if (NULL != returned_item)
149 FAIL_TEST("Returned history has too many items");
152 PASS_TEST;
155 int main(int argc, char **argv) {
156 int error;
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");
165 if (isds_init()) {
166 isds_cleanup();
167 ABORT_UNIT("isds_init() failed\n");
169 context = isds_ctx_create();
170 if (!context) {
171 isds_cleanup();
172 ABORT_UNIT("isds_ctx_create() failed\n");
176 char *url = NULL;
177 const char *box_id = "abcefgh";
178 const struct tm from_date = {
179 .tm_year = 1,
180 .tm_mon = 1,
181 .tm_mday = 3
183 const struct tm to_date = {
184 .tm_year = 4,
185 .tm_mon = 4,
186 .tm_mday = 6
188 struct tm new_valid_from = {
189 .tm_year = 2,
190 .tm_mon = 2,
191 .tm_mday = 7
193 struct tm new_valid_to = {
194 .tm_year = 3,
195 .tm_mon = 3,
196 .tm_mday = 8
198 struct tm old_valid_from = {
199 .tm_year = 5,
200 .tm_mon = 5,
201 .tm_mday = 9
203 struct tm old_valid_to = {
204 .tm_year = 6,
205 .tm_mon = 6,
206 .tm_mday = 10
208 const long int credit = 42;
209 long int old_capacity = 43;
210 const char *email = "joe@example.com";
211 struct timeval event_time = {
212 .tv_sec = 981173106,
213 .tv_usec = 123456
216 struct isds_credit_event event_credit_expired = {
217 .time = &event_time,
218 .credit_change = 133,
219 .new_credit = 244,
220 .type = ISDS_CREDIT_EXPIRED,
222 struct server_credit_event server_event_credit_expired = {
223 .time = &event_time,
224 .credit_change = 133,
225 .new_credit = 244,
226 .type = SERVER_CREDIT_EXPIRED,
228 struct isds_list history_expired = {
229 .next = NULL,
230 .data = &event_credit_expired,
231 .destructor = NULL
233 struct server_list server_history_expired = {
234 .next = NULL,
235 .data = &server_event_credit_expired,
236 .destructor = NULL
239 struct isds_credit_event event_credit_storage_set = {
240 .time = &event_time,
241 .credit_change = 133,
242 .new_credit = 244,
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 = {
253 .time = &event_time,
254 .credit_change = 133,
255 .new_credit = 244,
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,
268 .destructor = NULL
270 struct server_list server_history_storage_set = {
271 .next = &server_history_expired,
272 .data = &server_event_credit_storage_set,
273 .destructor = NULL
276 struct isds_credit_event event_credit_message_sent = {
277 .time = &event_time,
278 .credit_change = 133,
279 .new_credit = 244,
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 = {
285 .time = &event_time,
286 .credit_change = 133,
287 .new_credit = 244,
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,
295 .destructor = NULL
297 struct server_list server_history_message_sent = {
298 .next = &server_history_storage_set,
299 .data = &server_event_credit_message_sent,
300 .destructor = NULL
303 struct isds_credit_event event_credit_discharged = {
304 .time = &event_time,
305 .credit_change = 133,
306 .new_credit = 244,
307 .type = ISDS_CREDIT_DISCHARGED,
308 .details.discharged.transaction = "Foo"
310 struct server_credit_event server_event_credit_discharged = {
311 .time = &event_time,
312 .credit_change = 133,
313 .new_credit = 244,
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,
320 .destructor = NULL
322 struct server_list server_history_discharged = {
323 .next = &server_history_message_sent,
324 .data = &server_event_credit_discharged,
325 .destructor = NULL
328 struct isds_credit_event event_credit_charged = {
329 .time = &event_time,
330 .credit_change = 133,
331 .new_credit = 244,
332 .type = ISDS_CREDIT_CHARGED,
333 .details.charged.transaction = "Foo"
335 struct server_credit_event server_event_credit_charged = {
336 .time = &event_time,
337 .credit_change = 133,
338 .new_credit = 244,
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,
345 .destructor = NULL
347 const struct server_list server_history = {
348 .next = &server_history_discharged,
349 .data = &server_event_credit_charged,
350 .destructor = NULL
354 const struct arguments_DS_df_DataBoxCreditInfo service_arguments = {
355 .status_code = "0000",
356 .status_message = "Ok.",
357 .box_id = box_id,
358 .from_date = &from_date,
359 .to_date = &to_date,
360 .current_credit = credit,
361 .email = email,
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,
373 .services = services
375 error = start_server(&server_process, &url,
376 server_basic_authentication, &server_arguments, NULL);
377 if (error == -1) {
378 isds_ctx_free(&context);
379 isds_cleanup();
380 ABORT_UNIT(server_error);
382 TEST("login", test_login, IE_SUCCESS,
383 context, url, username, password, NULL, NULL);
384 free(url);
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,
390 &history);
391 TEST("Wrong box_id", test_isds_get_commercial_credit, IE_ISDS,
392 context, "1", &from_date, &to_date, &credit, &email,
393 &history);
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);
405 char *url = NULL;
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.",
413 .box_id = box_id,
414 .from_date = NULL,
415 .to_date = NULL,
416 .current_credit = credit,
417 .email = email,
418 .history = NULL
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,
429 .services = services
431 error = start_server(&server_process, &url,
432 server_basic_authentication, &server_arguments, NULL);
433 if (error == -1) {
434 isds_ctx_free(&context);
435 isds_cleanup();
436 ABORT_UNIT(server_error);
438 TEST("login", test_login, IE_SUCCESS,
439 context, url, username, password, NULL, NULL);
440 free(url);
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);
453 isds_cleanup();
454 SUM_TEST();