Use _DEFAULT_SOURCE where _BSD_SOURCE macro presents
[libisds.git] / test / simline / isds_resign_message.c
blob82306cdac66a18fd3b474635a783ddd0fd3f36bb
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 ABORT_UNIT(server_error);
158 free(url);
159 url = NULL;
162 int main(int argc, char **argv) {
163 int error;
164 pid_t server_process;
165 struct isds_ctx *context = NULL;
166 char *url = NULL;
168 INIT_TEST("isds_resing_message");
170 if (unsetenv("http_proxy")) {
171 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
173 if (isds_init()) {
174 isds_cleanup();
175 ABORT_UNIT("isds_init() failed\n");
177 context = isds_ctx_create();
178 if (!context) {
179 isds_cleanup();
180 ABORT_UNIT("isds_ctx_create() failed\n");
184 struct tm date;
185 const struct arguments_DS_Dz_ResignISDSDocument service_arguments = {
186 .status_code = "0000",
187 .status_message = "some text",
188 .valid_to = &date
190 const struct service_configuration services[] = {
191 { SERVICE_DS_Dz_DummyOperation, NULL },
192 { SERVICE_DS_Dz_ResignISDSDocument, &service_arguments },
193 { SERVICE_END, NULL }
195 const struct arguments_basic_authentication server_arguments = {
196 .username = username,
197 .password = password,
198 .isds_deviations = 1,
199 .services = services
201 memset(&date, 0, sizeof(date));
202 date.tm_year = 42;
203 date.tm_mon = 2;
204 date.tm_mday = 3;
205 const char data[] = "Hello world!";
207 error = start_server(&server_process, &url,
208 server_basic_authentication, &server_arguments, NULL);
209 if (error == -1) {
210 isds_ctx_free(&context);
211 isds_cleanup();
212 ABORT_UNIT(server_error);
215 TEST("prior logging in", test_isds_resign_message,
216 IE_CONNECTION_CLOSED, context, data, sizeof(data), 1, NULL);
217 TEST("login", test_login, IE_SUCCESS,
218 context, url, username, password, NULL, NULL);
219 TEST("NULL message", test_isds_resign_message,
220 IE_INVAL, context, NULL, 42, 1, NULL);
221 TEST("empty message", test_isds_resign_message,
222 IE_INVAL, context, "", 0, 1, NULL);
223 TEST("valid message", test_isds_resign_message,
224 IE_SUCCESS, context, data, sizeof(data), 1, &date);
225 TEST("valid message without date", test_isds_resign_message,
226 IE_SUCCESS, context, data, sizeof(data), 0, NULL);
228 isds_logout(context);
229 if (stop_server(server_process)) {
230 ABORT_UNIT(server_error);
232 free(url);
233 url = NULL;
236 test_error("2200", IE_INVAL, context);
237 test_error("2201", IE_NOTUNIQ, context);
238 test_error("2204", IE_INVAL, context);
239 test_error("2207", IE_ISDS, context);
241 isds_ctx_free(&context);
242 isds_cleanup();
243 SUM_TEST();