test: Add tests for isds_find_box_by_fulltext()
[libisds.git] / test / simline / isds_get_commercial_credit.c
bloba656591b7e67100a736abc59694e5b4386803a60
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 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(int argc, char **argv) {
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.new_capacity = 41,
248 .details.storage_set.new_valid_from = &new_valid_from,
249 .details.storage_set.new_valid_to = &new_valid_to,
250 .details.storage_set.old_capacity = &old_capacity,
251 .details.storage_set.old_valid_from = &old_valid_from,
252 .details.storage_set.old_valid_to = &old_valid_to,
253 .details.storage_set.initiator = "Foo",
255 struct server_credit_event server_event_credit_storage_set = {
256 .time = &event_time,
257 .credit_change = 133,
258 .new_credit = 244,
259 .type = SERVER_CREDIT_STORAGE_SET,
260 .details.storage_set.new_capacity = 41,
261 .details.storage_set.new_valid_from = &new_valid_from,
262 .details.storage_set.new_valid_to = &new_valid_to,
263 .details.storage_set.old_capacity = &old_capacity,
264 .details.storage_set.old_valid_from = &old_valid_from,
265 .details.storage_set.old_valid_to = &old_valid_to,
266 .details.storage_set.initiator = "Foo",
268 struct isds_list history_storage_set = {
269 .next = &history_expired,
270 .data = &event_credit_storage_set,
271 .destructor = NULL
273 struct server_list server_history_storage_set = {
274 .next = &server_history_expired,
275 .data = &server_event_credit_storage_set,
276 .destructor = NULL
279 struct isds_credit_event event_credit_message_sent = {
280 .time = &event_time,
281 .credit_change = 133,
282 .new_credit = 244,
283 .type = ISDS_CREDIT_MESSAGE_SENT,
284 .details.message_sent.recipient = "Foo",
285 .details.message_sent.message_id = "ijklmnop",
287 struct server_credit_event server_event_credit_message_sent = {
288 .time = &event_time,
289 .credit_change = 133,
290 .new_credit = 244,
291 .type = SERVER_CREDIT_MESSAGE_SENT,
292 .details.message_sent.recipient = "Foo",
293 .details.message_sent.message_id = "ijklmnop",
295 struct isds_list history_message_sent = {
296 .next = &history_storage_set,
297 .data = &event_credit_message_sent,
298 .destructor = NULL
300 struct server_list server_history_message_sent = {
301 .next = &server_history_storage_set,
302 .data = &server_event_credit_message_sent,
303 .destructor = NULL
306 struct isds_credit_event event_credit_discharged = {
307 .time = &event_time,
308 .credit_change = 133,
309 .new_credit = 244,
310 .type = ISDS_CREDIT_DISCHARGED,
311 .details.discharged.transaction = "Foo"
313 struct server_credit_event server_event_credit_discharged = {
314 .time = &event_time,
315 .credit_change = 133,
316 .new_credit = 244,
317 .type = SERVER_CREDIT_DISCHARGED,
318 .details.discharged.transaction = "Foo"
320 struct isds_list history_discharged = {
321 .next = &history_message_sent,
322 .data = &event_credit_discharged,
323 .destructor = NULL
325 struct server_list server_history_discharged = {
326 .next = &server_history_message_sent,
327 .data = &server_event_credit_discharged,
328 .destructor = NULL
331 struct isds_credit_event event_credit_charged = {
332 .time = &event_time,
333 .credit_change = 133,
334 .new_credit = 244,
335 .type = ISDS_CREDIT_CHARGED,
336 .details.charged.transaction = "Foo"
338 struct server_credit_event server_event_credit_charged = {
339 .time = &event_time,
340 .credit_change = 133,
341 .new_credit = 244,
342 .type = SERVER_CREDIT_CHARGED,
343 .details.charged.transaction = "Foo"
345 const struct isds_list history = {
346 .next = &history_discharged,
347 .data = &event_credit_charged,
348 .destructor = NULL
350 const struct server_list server_history = {
351 .next = &server_history_discharged,
352 .data = &server_event_credit_charged,
353 .destructor = NULL
357 const struct arguments_DS_df_DataBoxCreditInfo service_arguments = {
358 .status_code = "0000",
359 .status_message = "Ok.",
360 .box_id = box_id,
361 .from_date = &from_date,
362 .to_date = &to_date,
363 .current_credit = credit,
364 .email = email,
365 .history = &server_history
367 const struct service_configuration services[] = {
368 { SERVICE_DS_Dz_DummyOperation, NULL },
369 { SERVICE_DS_df_DataBoxCreditInfo, &service_arguments },
370 { SERVICE_END, NULL }
372 const struct arguments_basic_authentication server_arguments = {
373 .username = username,
374 .password = password,
375 .isds_deviations = 1,
376 .services = services
378 error = start_server(&server_process, &url,
379 server_basic_authentication, &server_arguments, NULL);
380 if (error == -1) {
381 isds_ctx_free(&context);
382 isds_cleanup();
383 ABORT_UNIT(server_error);
385 TEST("login", test_login, IE_SUCCESS,
386 context, url, username, password, NULL, NULL);
387 free(url);
389 TEST("NULL box_id", test_isds_get_commercial_credit, IE_INVAL,
390 context, NULL, NULL, NULL, NULL, NULL, NULL);
391 TEST("All data", test_isds_get_commercial_credit, IE_SUCCESS,
392 context, box_id, &from_date, &to_date, &credit, &email,
393 &history);
394 TEST("Wrong box_id", test_isds_get_commercial_credit, IE_ISDS,
395 context, "1", &from_date, &to_date, &credit, &email,
396 &history);
397 TEST("No history", test_isds_get_commercial_credit, IE_SUCCESS,
398 context, box_id, &from_date, &to_date, &credit, &email, NULL);
400 isds_logout(context);
401 if (stop_server(server_process)) {
402 isds_ctx_free(&context);
403 isds_cleanup();
404 ABORT_UNIT(server_error);
410 char *url = NULL;
411 const char *box_id = "abcefgh";
412 const long int credit = 42;
413 const char *email = "joe@example.com";
415 const struct arguments_DS_df_DataBoxCreditInfo service_arguments = {
416 .status_code = "0000",
417 .status_message = "Ok.",
418 .box_id = box_id,
419 .from_date = NULL,
420 .to_date = NULL,
421 .current_credit = credit,
422 .email = email,
423 .history = NULL
425 const struct service_configuration services[] = {
426 { SERVICE_DS_Dz_DummyOperation, NULL },
427 { SERVICE_DS_df_DataBoxCreditInfo, &service_arguments },
428 { SERVICE_END, NULL }
430 const struct arguments_basic_authentication server_arguments = {
431 .username = username,
432 .password = password,
433 .isds_deviations = 1,
434 .services = services
436 error = start_server(&server_process, &url,
437 server_basic_authentication, &server_arguments, NULL);
438 if (error == -1) {
439 isds_ctx_free(&context);
440 isds_cleanup();
441 ABORT_UNIT(server_error);
443 TEST("login", test_login, IE_SUCCESS,
444 context, url, username, password, NULL, NULL);
445 free(url);
447 TEST("No dates", test_isds_get_commercial_credit, IE_SUCCESS,
448 context, box_id, NULL, NULL, &credit, &email, NULL);
450 isds_logout(context);
451 if (stop_server(server_process)) {
452 isds_ctx_free(&context);
453 isds_cleanup();
454 ABORT_UNIT(server_error);
459 isds_ctx_free(&context);
460 isds_cleanup();
461 SUM_TEST();