tests: Adapt utf8locale to musl
[libisds.git] / test / simline / isds_resign_message.c
blobf1ba6a86587772b2d89913bca4b0716a35039e1e
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 static int test_isds_resign_message(const isds_error error,
40 struct isds_ctx *context, const void *data, size_t length,
41 _Bool request_valid_to, const struct tm *expected_valid_to) {
42 isds_error err;
43 void *output = NULL;
44 size_t output_length = 0;
45 struct tm *valid_to = NULL;
47 err = isds_resign_message(context, data, length, &output, &output_length,
48 (request_valid_to) ? &valid_to : NULL);
49 if (error != err) {
50 free(output);
51 free(valid_to);
52 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
53 isds_strerror(error), isds_strerror(err),
54 isds_long_message(context));
56 if (!err) {
57 if (NULL == output) {
58 free(output);
59 free(valid_to);
60 FAIL_TEST("No message returned");
62 if (length != output_length) {
63 free(output);
64 free(valid_to);
65 FAIL_TEST("Wrong size of returned message: "
66 "expected=%zd, returned=%zd", length, output_length);
68 if (strcmp(data, output)) {
69 free(output);
70 free(valid_to);
71 FAIL_TEST("Returned message does not match input message: "
72 "expected=`%s', returned=`%s'", data, output);
74 } else {
75 if (0 != output_length) {
76 free(output);
77 free(valid_to);
78 FAIL_TEST("Output size is not 0 on error");
80 if (NULL != output) {
81 free(output);
82 free(valid_to);
83 FAIL_TEST("Output data pointer is not NULL on error");
86 free(output);
88 if (NULL == expected_valid_to) {
89 if (NULL != valid_to) {
90 free(valid_to);
91 FAIL_TEST("Valid_to pointer is not NULL");
93 } else {
94 if (NULL == valid_to) {
95 free(valid_to);
96 FAIL_TEST("Valid_to pointer should not be NULL");
98 if (
99 expected_valid_to->tm_year != valid_to->tm_year ||
100 expected_valid_to->tm_mon != valid_to->tm_mon ||
101 expected_valid_to->tm_mday != valid_to->tm_mday
103 char expected[32];
104 char returned[32];
105 strftime(expected, sizeof(expected), "%F", expected_valid_to);
106 strftime(returned, sizeof(returned), "%F", valid_to);
107 free(valid_to);
108 FAIL_TEST("Wrong valid_to date: expected=%s, returned=%s",
109 expected, returned);
112 free(valid_to);
114 PASS_TEST;
118 static void test_error(const char *code, const isds_error expected_error,
119 struct isds_ctx *context) {
120 struct arguments_DS_Dz_ResignISDSDocument service_arguments = {
121 .status_code = code,
122 .status_message = "some text",
123 .valid_to = NULL
125 const struct service_configuration services[] = {
126 { SERVICE_DS_Dz_DummyOperation, NULL },
127 { SERVICE_DS_Dz_ResignISDSDocument, &service_arguments },
128 { SERVICE_END, NULL }
130 const struct arguments_basic_authentication server_arguments = {
131 .username = username,
132 .password = password,
133 .isds_deviations = 1,
134 .services = services
136 const char data[] = "Hello world!";
137 pid_t server_process;
138 char *url = NULL;
139 int error;
141 error = start_server(&server_process, &url,
142 server_basic_authentication, &server_arguments, NULL);
143 if (error == -1) {
144 isds_ctx_free(&context);
145 isds_cleanup();
146 ABORT_UNIT(server_error);
149 TEST("login", test_login, IE_SUCCESS,
150 context, url, username, password, NULL, NULL);
151 TEST(code, test_isds_resign_message,
152 expected_error, context, data, sizeof(data), 1, NULL);
154 isds_logout(context);
155 if (stop_server(server_process)) {
156 isds_ctx_free(&context);
157 isds_cleanup();
158 ABORT_UNIT(server_error);
160 free(url);
161 url = NULL;
164 int main(void) {
165 int error;
166 pid_t server_process;
167 struct isds_ctx *context = NULL;
168 char *url = NULL;
170 INIT_TEST("isds_resing_message");
172 if (unsetenv("http_proxy")) {
173 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
175 if (isds_init()) {
176 isds_cleanup();
177 ABORT_UNIT("isds_init() failed\n");
179 context = isds_ctx_create();
180 if (!context) {
181 isds_cleanup();
182 ABORT_UNIT("isds_ctx_create() failed\n");
186 struct tm date;
187 const struct arguments_DS_Dz_ResignISDSDocument service_arguments = {
188 .status_code = "0000",
189 .status_message = "some text",
190 .valid_to = &date
192 const struct service_configuration services[] = {
193 { SERVICE_DS_Dz_DummyOperation, NULL },
194 { SERVICE_DS_Dz_ResignISDSDocument, &service_arguments },
195 { SERVICE_END, NULL }
197 const struct arguments_basic_authentication server_arguments = {
198 .username = username,
199 .password = password,
200 .isds_deviations = 1,
201 .services = services
203 memset(&date, 0, sizeof(date));
204 date.tm_year = 42;
205 date.tm_mon = 2;
206 date.tm_mday = 3;
207 const char data[] = "Hello world!";
209 error = start_server(&server_process, &url,
210 server_basic_authentication, &server_arguments, NULL);
211 if (error == -1) {
212 isds_ctx_free(&context);
213 isds_cleanup();
214 ABORT_UNIT(server_error);
217 TEST("prior logging in", test_isds_resign_message,
218 IE_CONNECTION_CLOSED, context, data, sizeof(data), 1, NULL);
219 TEST("login", test_login, IE_SUCCESS,
220 context, url, username, password, NULL, NULL);
221 TEST("NULL message", test_isds_resign_message,
222 IE_INVAL, context, NULL, 42, 1, NULL);
223 TEST("empty message", test_isds_resign_message,
224 IE_INVAL, context, "", 0, 1, NULL);
225 TEST("valid message", test_isds_resign_message,
226 IE_SUCCESS, context, data, sizeof(data), 1, &date);
227 TEST("valid message without date", test_isds_resign_message,
228 IE_SUCCESS, context, data, sizeof(data), 0, NULL);
230 isds_logout(context);
231 if (stop_server(server_process)) {
232 isds_ctx_free(&context);
233 isds_cleanup();
234 ABORT_UNIT(server_error);
236 free(url);
237 url = NULL;
240 test_error("2200", IE_INVAL, context);
241 test_error("2201", IE_NOTUNIQ, context);
242 test_error("2204", IE_INVAL, context);
243 test_error("2207", IE_ISDS, context);
245 isds_ctx_free(&context);
246 isds_cleanup();
247 SUM_TEST();