Implement GetDataBoxActivityStatus as isds_get_box_state_history()
[libisds.git] / test / simline / isds_get_box_state_history.c
blob4845a97b4c026726dc4ccef17f3fd5c1b64d3ffa
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 "test_DbOwnerInfo.h"
18 #include "server.h"
19 #include "isds.h"
21 static const char *username = "Doug1as$";
22 static const char *password = "42aA#bc8";
25 static int test_login(const isds_error error, struct isds_ctx *context,
26 const char *url, const char *username, const char *password,
27 const struct isds_pki_credentials *pki_credentials,
28 struct isds_otp *otp) {
29 isds_error err;
31 err = isds_login(context, url, username, password, pki_credentials, otp);
32 if (error != err)
33 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
34 isds_strerror(error), isds_strerror(err),
35 isds_long_message(context));
37 PASS_TEST;
40 /* Compare isds_box_state_period.
41 * @return 0 if equaled, 1 otherwise. */
42 static int compare_isds_box_state_period(
43 const struct isds_box_state_period *expected_result,
44 const struct isds_box_state_period *result) {
46 TEST_POINTER_DUPLICITY(expected_result, result);
47 if (NULL == expected_result)
48 return 0;
50 TEST_TIMEVALPTR_DUPLICITY(&expected_result->from, &result->from);
51 TEST_TIMEVALPTR_DUPLICITY(&expected_result->to, &result->to);
52 TEST_INT_DUPLICITY(expected_result->dbState, result->dbState);
54 return 0;
57 /* Compare list of isds_box_state_period structures.
58 * @return 0 if equaled, 1 otherwise and set failure reason. */
59 static int compare_isds_box_state_period_lists(const struct isds_list *expected_list,
60 const struct isds_list *list) {
61 const struct isds_list *expected_item, *item;
62 int i;
64 for (i = 1, expected_item = expected_list, item = list;
65 NULL != expected_item && NULL != item;
66 i++, expected_item = expected_item->next, item = item->next) {
67 if (compare_isds_box_state_period(
68 (struct isds_box_state_period *)expected_item->data,
69 (struct isds_box_state_period *)item->data))
70 return 1;
72 if (NULL != expected_item && NULL == item)
73 FAIL_TEST("Result list is missing %d. item", i);
74 if (NULL == expected_item && NULL != item)
75 FAIL_TEST("Result list has superfluous %d. item", i);
77 return 0;
80 static int test_isds_get_box_state_history(const isds_error expected_error,
81 struct isds_ctx *context,
82 const char *dbID, const struct timeval *from, const struct timeval *to,
83 const struct isds_list *expected_results) {
84 isds_error error;
85 struct isds_list *results;
86 TEST_CALLOC(results);
88 error = isds_get_box_state_history(context, dbID, from, to, &results);
89 TEST_DESTRUCTOR((void(*)(void*))isds_list_free, &results);
91 if (expected_error != error) {
92 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
93 isds_strerror(expected_error), isds_strerror(error),
94 isds_long_message(context));
97 if (IE_SUCCESS != error) {
98 TEST_POINTER_IS_NULL(results);
99 PASS_TEST;
102 if (compare_isds_box_state_period_lists(expected_results, results))
103 return 1;
106 PASS_TEST;
109 int main(void) {
110 int error;
111 pid_t server_process;
112 struct isds_ctx *context = NULL;
114 INIT_TEST("isds_get_box_state_history");
116 if (unsetenv("http_proxy")) {
117 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
119 if (isds_init()) {
120 isds_cleanup();
121 ABORT_UNIT("isds_init() failed\n");
123 context = isds_ctx_create();
124 if (!context) {
125 isds_cleanup();
126 ABORT_UNIT("isds_ctx_create() failed\n");
130 /* Full response with two results */
131 char *url = NULL;
133 char *input_dbID = "A123456";
134 struct timeval input_from = {
135 .tv_sec = 5,
136 .tv_usec = 0
138 struct timeval input_to = {
139 .tv_sec = 6,
140 .tv_usec = 0
143 struct isds_box_state_period period1 = {
144 .from = {
145 .tv_sec = 1,
146 .tv_usec = 0
148 .to = {
149 .tv_sec = 2,
150 .tv_usec = 0
152 .dbState = 1
155 struct isds_box_state_period period2 = {
156 .from = {
157 .tv_sec = 3,
158 .tv_usec = 0
160 .to = {
161 .tv_sec = 4,
162 .tv_usec = 0
164 .dbState = 2
166 struct isds_list results2 = {
167 .next = NULL,
168 .data = &period2,
169 .destructor = NULL
171 struct isds_list results = {
172 .next = &results2,
173 .data = &period1,
174 .destructor = NULL
177 struct server_box_state_period server_period1 = {
178 .from = &period1.from,
179 .to = &period1.to,
180 .dbState = period1.dbState
182 struct server_box_state_period server_period2 = {
183 .from = &period2.from,
184 .to = &period2.to,
185 .dbState = period2.dbState
187 struct server_list server_results2 = {
188 .next = NULL,
189 .data = &server_period2,
190 .destructor = NULL
192 struct server_list server_results = {
193 .next = &server_results2,
194 .data = &server_period1,
195 .destructor = NULL
198 const struct arguments_DS_df_GetDataBoxActivityStatus
199 service_arguments = {
200 .status_code = "0000",
201 .status_message = "Ok.",
202 .box_id = input_dbID,
203 .from = &input_from,
204 .to = &input_to,
205 .result_box_id = "B123456",
206 .results_exists = 0,
207 .results = &server_results
209 const struct service_configuration services[] = {
210 { SERVICE_DS_Dz_DummyOperation, NULL },
211 { SERVICE_DS_df_GetDataBoxActivityStatus, &service_arguments },
212 { SERVICE_END, NULL }
214 const struct arguments_basic_authentication server_arguments = {
215 .username = username,
216 .password = password,
217 .isds_deviations = 1,
218 .services = services
220 error = start_server(&server_process, &url,
221 server_basic_authentication, &server_arguments, NULL);
222 if (error == -1) {
223 isds_ctx_free(&context);
224 isds_cleanup();
225 ABORT_UNIT(server_error);
227 TEST("login", test_login, IE_SUCCESS,
228 context, url, username, password, NULL, NULL);
229 free(url);
231 TEST("All data", test_isds_get_box_state_history, IE_SUCCESS,
232 context, input_dbID, &input_from, &input_to, &results);
234 isds_logout(context);
235 if (stop_server(server_process)) {
236 isds_ctx_free(&context);
237 isds_cleanup();
238 ABORT_UNIT(server_error);
243 /* Some error */
244 char *url = NULL;
246 char *input_dbID = "A123456";
247 struct timeval input_from = {
248 .tv_sec = 5,
249 .tv_usec = 0
251 struct timeval input_to = {
252 .tv_sec = 6,
253 .tv_usec = 0
256 const struct arguments_DS_df_GetDataBoxActivityStatus
257 service_arguments = {
258 .status_code = "0002",
259 .status_message = "No such box",
260 .box_id = input_dbID,
261 .from = &input_from,
262 .to = &input_to,
263 .result_box_id = NULL,
264 .results_exists = 0,
265 .results = NULL
267 const struct service_configuration services[] = {
268 { SERVICE_DS_Dz_DummyOperation, NULL },
269 { SERVICE_DS_df_GetDataBoxActivityStatus, &service_arguments },
270 { SERVICE_END, NULL }
272 const struct arguments_basic_authentication server_arguments = {
273 .username = username,
274 .password = password,
275 .isds_deviations = 1,
276 .services = services
278 error = start_server(&server_process, &url,
279 server_basic_authentication, &server_arguments, NULL);
280 if (error == -1) {
281 isds_ctx_free(&context);
282 isds_cleanup();
283 ABORT_UNIT(server_error);
285 TEST("login", test_login, IE_SUCCESS,
286 context, url, username, password, NULL, NULL);
287 free(url);
289 TEST("An error", test_isds_get_box_state_history, IE_ISDS,
290 context, input_dbID, &input_from, &input_to, NULL);
292 isds_logout(context);
293 if (stop_server(server_process)) {
294 isds_ctx_free(&context);
295 isds_cleanup();
296 ABORT_UNIT(server_error);
301 isds_ctx_free(&context);
302 isds_cleanup();
303 SUM_TEST();