2 #define _POSIX_SOURCE /* For getaddrinfo(3) */
6 #define _BSD_SOURCE /* For NI_MAXHOST */
10 #define _XOPEN_SOURCE 600 /* For unsetenv(3) */
17 static const char *username
= "Doug1as$";
18 static const char *password
= "42aA#bc8";
21 static int test_login(const isds_error error
, struct isds_ctx
*context
,
22 const char *url
, const char *username
, const char *password
,
23 const struct isds_pki_credentials
*pki_credentials
,
24 struct isds_otp
*otp
) {
27 err
= isds_login(context
, url
, username
, password
, pki_credentials
, otp
);
29 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
30 isds_strerror(error
), isds_strerror(err
),
31 isds_long_message(context
));
36 static int test_isds_get_commercial_credit(const isds_error error
,
37 struct isds_ctx
*context
, const char *box_id
,
38 const struct tm
*from_date
, const struct tm
*to_date
,
39 const long int *credit
, const char **email
,
40 const struct isds_list
*history
) {
41 isds_error returned_error
;
42 long int returned_credit
= -1;
43 char *returned_email
= strdup("foo");
44 struct isds_list
*returned_history
= NULL
;
45 const struct isds_list
*item
;
46 struct isds_list
*returned_item
;
47 const struct isds_credit_event
*event
;
48 struct isds_credit_event
*returned_event
;
50 returned_error
= isds_get_commercial_credit(context
, box_id
,
52 (NULL
== credit
) ? NULL
: &returned_credit
,
53 (NULL
== email
) ? NULL
: &returned_email
,
54 (NULL
== history
) ? NULL
: &returned_history
);
56 if (error
!= returned_error
) {
57 FAILURE_REASON("Wrong return code: expected=%s, returned=%s (%s)",
58 isds_strerror(error
), isds_strerror(returned_error
),
59 isds_long_message(context
));
63 if (IE_SUCCESS
!= returned_error
) {
64 if (NULL
!= email
&& NULL
!= returned_email
)
65 FAIL_TEST("email is not NULL on error");
66 if (NULL
!= history
&& NULL
!= returned_history
)
67 FAIL_TEST("history is not NULL on error");
72 TEST_STRING_DUPLICITY(*email
, returned_email
);
74 TEST_INT_DUPLICITY(*credit
, returned_credit
);
75 for (item
= history
, returned_item
= returned_history
;
77 item
= item
->next
, returned_item
= returned_item
->next
) {
78 if (NULL
== returned_item
)
79 FAIL_TEST("Returned history has too few items");
80 event
= (struct isds_credit_event
*)item
->data
;
81 returned_event
= (struct isds_credit_event
*)returned_item
->data
;
83 ABORT_UNIT("History event is not defined");
84 if (NULL
== returned_item
)
85 FAIL_TEST("Returned history event is NULL and it shouldn't be");
86 TEST_TIMEVALPTR_DUPLICITY(event
->time
, returned_event
->time
);
87 TEST_INT_DUPLICITY(event
->credit_change
, returned_event
->credit_change
);
88 TEST_INT_DUPLICITY(event
->new_credit
, returned_event
->new_credit
);
89 TEST_INT_DUPLICITY(event
->type
, returned_event
->type
);
91 switch (event
->type
) {
92 case ISDS_CREDIT_CHARGED
:
93 TEST_STRING_DUPLICITY(event
->details
.charged
.transaction
,
94 returned_event
->details
.charged
.transaction
);
96 case ISDS_CREDIT_DISCHARGED
:
97 TEST_STRING_DUPLICITY(event
->details
.discharged
.transaction
,
98 returned_event
->details
.discharged
.transaction
);
100 case ISDS_CREDIT_MESSAGE_SENT
:
101 TEST_STRING_DUPLICITY(event
->details
.message_sent
.recipient
,
102 returned_event
->details
.message_sent
.recipient
);
103 TEST_STRING_DUPLICITY(event
->details
.message_sent
.message_id
,
104 returned_event
->details
.message_sent
.message_id
);
106 case ISDS_CREDIT_STORAGE_SET
:
107 TEST_INT_DUPLICITY(event
->details
.storage_set
.new_capacity
,
108 returned_event
->details
.storage_set
.new_capacity
);
109 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.new_valid_from
,
110 returned_event
->details
.storage_set
.new_valid_from
);
111 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.new_valid_to
,
112 returned_event
->details
.storage_set
.new_valid_to
);
113 TEST_INTPTR_DUPLICITY(event
->details
.storage_set
.old_capacity
,
114 returned_event
->details
.storage_set
.old_capacity
);
115 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.old_valid_from
,
116 returned_event
->details
.storage_set
.old_valid_from
);
117 TEST_TMPTR_DUPLICITY(event
->details
.storage_set
.old_valid_to
,
118 returned_event
->details
.storage_set
.old_valid_to
);
119 TEST_STRING_DUPLICITY(event
->details
.storage_set
.initiator
,
120 returned_event
->details
.storage_set
.initiator
);
122 case ISDS_CREDIT_EXPIRED
:
125 FAIL_TEST("Uknown credit event type returned");
128 if (NULL
!= returned_item
)
129 FAIL_TEST("Returned history has too many items");
132 free(returned_email
);
133 isds_list_free(&returned_history
);
137 free(returned_email
);
138 isds_list_free(&returned_history
);
142 int main(int argc
, char **argv
) {
144 pid_t server_process
;
145 struct isds_ctx
*context
= NULL
;
148 INIT_TEST("isds_get_commercial_credit");
150 if (unsetenv("http_proxy")) {
151 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
155 ABORT_UNIT("isds_init() failed\n");
157 context
= isds_ctx_create();
160 ABORT_UNIT("isds_ctx_create() failed\n");
164 const char *box_id
= "abcefgh";
165 const struct tm from_date
= {
170 const struct tm to_date
= {
175 struct tm new_valid_from
= {
180 struct tm new_valid_to
= {
185 struct tm old_valid_from
= {
190 struct tm old_valid_to
= {
195 const long int credit
= 42;
196 long int old_capacity
= 43;
197 const char *email
= "joe@example.com";
198 struct timeval event_time
= {
203 struct isds_credit_event event_credit_expired
= {
205 .credit_change
= 133,
207 .type
= ISDS_CREDIT_EXPIRED
,
209 struct server_credit_event server_event_credit_expired
= {
211 .credit_change
= 133,
213 .type
= SERVER_CREDIT_EXPIRED
,
215 struct isds_list history_expired
= {
217 .data
= &event_credit_expired
,
220 struct server_list server_history_expired
= {
222 .data
= &server_event_credit_expired
,
226 struct isds_credit_event event_credit_storage_set
= {
228 .credit_change
= 133,
230 .type
= ISDS_CREDIT_STORAGE_SET
,
231 .details
.storage_set
.new_capacity
= 41,
232 .details
.storage_set
.new_valid_from
= &new_valid_from
,
233 .details
.storage_set
.new_valid_to
= &new_valid_to
,
234 .details
.storage_set
.old_capacity
= &old_capacity
,
235 .details
.storage_set
.old_valid_from
= &old_valid_from
,
236 .details
.storage_set
.old_valid_to
= &old_valid_to
,
237 .details
.storage_set
.initiator
= "Foo",
239 struct server_credit_event server_event_credit_storage_set
= {
241 .credit_change
= 133,
243 .type
= SERVER_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 isds_list history_storage_set
= {
253 .next
= &history_expired
,
254 .data
= &event_credit_storage_set
,
257 struct server_list server_history_storage_set
= {
258 .next
= &server_history_expired
,
259 .data
= &server_event_credit_storage_set
,
263 struct isds_credit_event event_credit_message_sent
= {
265 .credit_change
= 133,
267 .type
= ISDS_CREDIT_MESSAGE_SENT
,
268 .details
.message_sent
.recipient
= "Foo",
269 .details
.message_sent
.message_id
= "ijklmnop",
271 struct server_credit_event server_event_credit_message_sent
= {
273 .credit_change
= 133,
275 .type
= SERVER_CREDIT_MESSAGE_SENT
,
276 .details
.message_sent
.recipient
= "Foo",
277 .details
.message_sent
.message_id
= "ijklmnop",
279 struct isds_list history_message_sent
= {
280 .next
= &history_storage_set
,
281 .data
= &event_credit_message_sent
,
284 struct server_list server_history_message_sent
= {
285 .next
= &server_history_storage_set
,
286 .data
= &server_event_credit_message_sent
,
290 struct isds_credit_event event_credit_discharged
= {
292 .credit_change
= 133,
294 .type
= ISDS_CREDIT_DISCHARGED
,
295 .details
.discharged
.transaction
= "Foo"
297 struct server_credit_event server_event_credit_discharged
= {
299 .credit_change
= 133,
301 .type
= SERVER_CREDIT_DISCHARGED
,
302 .details
.discharged
.transaction
= "Foo"
304 struct isds_list history_discharged
= {
305 .next
= &history_message_sent
,
306 .data
= &event_credit_discharged
,
309 struct server_list server_history_discharged
= {
310 .next
= &server_history_message_sent
,
311 .data
= &server_event_credit_discharged
,
315 struct isds_credit_event event_credit_charged
= {
317 .credit_change
= 133,
319 .type
= ISDS_CREDIT_CHARGED
,
320 .details
.charged
.transaction
= "Foo"
322 struct server_credit_event server_event_credit_charged
= {
324 .credit_change
= 133,
326 .type
= SERVER_CREDIT_CHARGED
,
327 .details
.charged
.transaction
= "Foo"
329 const struct isds_list history
= {
330 .next
= &history_discharged
,
331 .data
= &event_credit_charged
,
334 const struct server_list server_history
= {
335 .next
= &server_history_discharged
,
336 .data
= &server_event_credit_charged
,
341 const struct arguments_DS_df_DataBoxCreditInfo service_arguments
= {
342 .status_code
= "0000",
343 .status_message
= "Ok.",
345 .from_date
= &from_date
,
347 .current_credit
= credit
,
349 .history
= &server_history
351 const struct service_configuration services
[] = {
352 { SERVICE_DS_Dz_DummyOperation
, NULL
},
353 { SERVICE_DS_df_DataBoxCreditInfo
, &service_arguments
},
354 { SERVICE_END
, NULL
}
356 const struct arguments_basic_authentication server_arguments
= {
357 .username
= username
,
358 .password
= password
,
359 .isds_deviations
= 1,
362 error
= start_server(&server_process
, &url
,
363 server_basic_authentication
, &server_arguments
, NULL
);
365 isds_ctx_free(&context
);
367 ABORT_UNIT(server_error
);
370 TEST("login", test_login
, IE_SUCCESS
,
371 context
, url
, username
, password
, NULL
, NULL
);
372 TEST("NULL box_id", test_isds_get_commercial_credit
, IE_INVAL
,
373 context
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
374 TEST("All data", test_isds_get_commercial_credit
, IE_SUCCESS
,
375 context
, box_id
, &from_date
, &to_date
, &credit
, &email
,
377 TEST("Wrong box_id", test_isds_get_commercial_credit
, IE_ISDS
,
378 context
, "1", &from_date
, &to_date
, &credit
, &email
,
381 if (stop_server(server_process
)) {
382 ABORT_UNIT(server_error
);
390 isds_logout(context
);
391 isds_ctx_free(&context
);