Implement Re-signISDSDocument
[libisds.git] / test / simline / isds_resign_message.c
blob3c3573ef648d4cfb1732a2d3e2f38790233b8817
1 #ifndef _POSIX_SOURCE
2 #define _POSIX_SOURCE /* For getaddrinfo(3) */
3 #endif
5 #ifndef _BSD_SOURCE
6 #define _BSD_SOURCE /* For NI_MAXHOST */
7 #endif
9 #ifndef _XOPEN_SOURCE
10 #define _XOPEN_SOURCE 600 /* For unsetenv(3) */
11 #endif
13 #include "../test.h"
14 #include "server.h"
15 #include "isds.h"
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) {
25 isds_error err;
27 err = isds_login(context, url, username, password, pki_credentials, otp);
28 if (error != err)
29 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
30 isds_strerror(error), isds_strerror(err),
31 isds_long_message(context));
33 PASS_TEST;
36 static int test_isds_resign_message(const isds_error error,
37 struct isds_ctx *context, const void *data, size_t length,
38 _Bool request_valid_to, const struct tm *expected_valid_to) {
39 isds_error err;
40 void *output = NULL;
41 size_t output_length = 0;
42 struct tm *valid_to = NULL;
44 err = isds_resign_message(context, data, length, &output, &output_length,
45 (request_valid_to) ? &valid_to : NULL);
46 if (error != err) {
47 free(output);
48 free(valid_to);
49 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
50 isds_strerror(error), isds_strerror(err),
51 isds_long_message(context));
53 if (!err) {
54 if (NULL == output) {
55 free(output);
56 free(valid_to);
57 FAIL_TEST("No message returned");
59 if (length != output_length) {
60 free(output);
61 free(valid_to);
62 FAIL_TEST("Wrong size of returned message: "
63 "expected=%zd, returned=%zd", length, output_length);
65 if (strcmp(data, output)) {
66 free(output);
67 free(valid_to);
68 FAIL_TEST("Returned message does not match input message: "
69 "expected=`%s', returned=`%s'", data, output);
71 } else {
72 if (0 != output_length) {
73 free(output);
74 free(valid_to);
75 FAIL_TEST("Output size is not 0 on error");
77 if (NULL != output) {
78 free(output);
79 free(valid_to);
80 FAIL_TEST("Output data pointer is not NULL on error");
83 free(output);
85 if (NULL == expected_valid_to) {
86 if (NULL != valid_to) {
87 free(valid_to);
88 FAIL_TEST("Valid_to pointer is not NULL");
90 } else {
91 if (NULL == valid_to) {
92 free(valid_to);
93 FAIL_TEST("Valid_to pointer should not be NULL");
95 if (
96 expected_valid_to->tm_year != valid_to->tm_year ||
97 expected_valid_to->tm_mon != valid_to->tm_mon ||
98 expected_valid_to->tm_mday != valid_to->tm_mday
99 ) {
100 char expected[32];
101 char returned[32];
102 strftime(expected, sizeof(expected), "%F", expected_valid_to);
103 strftime(returned, sizeof(returned), "%F", valid_to);
104 free(valid_to);
105 FAIL_TEST("Wrong valid_to date: expected=%s, returned=%s",
106 expected, returned);
109 free(valid_to);
111 PASS_TEST;
115 static void test_error(const char *code, const isds_error expected_error,
116 struct isds_ctx *context) {
117 struct arguments_DS_Dz_ResignISDSDocument service_arguments = {
118 .status_code = code,
119 .status_message = "some text",
120 .valid_to = NULL
122 const struct service_configuration services[] = {
123 { SERVICE_DS_Dz_DummyOperation, NULL },
124 { SERVICE_DS_Dz_ResignISDSDocument, &service_arguments },
125 { SERVICE_END, NULL }
127 const struct arguments_basic_authentication server_arguments = {
128 .username = username,
129 .password = password,
130 .isds_deviations = 1,
131 .services = services
133 const char data[] = "Hello world!";
134 pid_t server_process;
135 char *url = NULL;
136 int error;
138 error = start_server(&server_process, &url,
139 server_basic_authentication, &server_arguments, NULL);
140 if (error == -1) {
141 isds_ctx_free(&context);
142 isds_cleanup();
143 ABORT_UNIT(server_error);
146 TEST("login", test_login, IE_SUCCESS,
147 context, url, username, password, NULL, NULL);
148 TEST(code, test_isds_resign_message,
149 expected_error, context, data, sizeof(data), 1, NULL);
151 isds_logout(context);
152 if (stop_server(server_process)) {
153 ABORT_UNIT(server_error);
155 free(url);
156 url = NULL;
159 int main(int argc, char **argv) {
160 int error;
161 pid_t server_process;
162 struct isds_ctx *context = NULL;
163 char *url = NULL;
165 INIT_TEST("isds_resing_message");
167 if (unsetenv("http_proxy")) {
168 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
170 if (isds_init()) {
171 isds_cleanup();
172 ABORT_UNIT("isds_init() failed\n");
174 context = isds_ctx_create();
175 if (!context) {
176 isds_cleanup();
177 ABORT_UNIT("isds_ctx_create() failed\n");
181 struct tm date;
182 const struct arguments_DS_Dz_ResignISDSDocument service_arguments = {
183 .status_code = "0000",
184 .status_message = "some text",
185 .valid_to = &date
187 const struct service_configuration services[] = {
188 { SERVICE_DS_Dz_DummyOperation, NULL },
189 { SERVICE_DS_Dz_ResignISDSDocument, &service_arguments },
190 { SERVICE_END, NULL }
192 const struct arguments_basic_authentication server_arguments = {
193 .username = username,
194 .password = password,
195 .isds_deviations = 1,
196 .services = services
198 memset(&date, 0, sizeof(date));
199 date.tm_year = 42;
200 date.tm_mon = 2;
201 date.tm_mday = 3;
202 const char data[] = "Hello world!";
204 error = start_server(&server_process, &url,
205 server_basic_authentication, &server_arguments, NULL);
206 if (error == -1) {
207 isds_ctx_free(&context);
208 isds_cleanup();
209 ABORT_UNIT(server_error);
212 TEST("prior logging in", test_isds_resign_message,
213 IE_CONNECTION_CLOSED, context, data, sizeof(data), 1, NULL);
214 TEST("login", test_login, IE_SUCCESS,
215 context, url, username, password, NULL, NULL);
216 TEST("NULL message", test_isds_resign_message,
217 IE_INVAL, context, NULL, 42, 1, NULL);
218 TEST("empty message", test_isds_resign_message,
219 IE_INVAL, context, "", 0, 1, NULL);
220 TEST("valid message", test_isds_resign_message,
221 IE_SUCCESS, context, data, sizeof(data), 1, &date);
222 TEST("valid message without date", test_isds_resign_message,
223 IE_SUCCESS, context, data, sizeof(data), 0, NULL);
225 isds_logout(context);
226 if (stop_server(server_process)) {
227 ABORT_UNIT(server_error);
229 free(url);
230 url = NULL;
233 test_error("2200", IE_INVAL, context);
234 test_error("2201", IE_NOTUNIQ, context);
235 test_error("2204", IE_INVAL, context);
236 test_error("2207", IE_ISDS, context);
238 isds_ctx_free(&context);
239 isds_cleanup();
240 SUM_TEST();