Use _DEFAULT_SOURCE where _BSD_SOURCE macro presents
[libisds.git] / test / simline / totp_authentication.c
blob237b736c3a7aded396d4ff8a60af435e5773ac96
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 = "douglas";
21 static const char *password = "42";
22 static const char *otp_code = "314";
25 static int test_login(const isds_error error,
26 const isds_otp_resolution resolution, struct isds_ctx *context,
27 const char *url, const char *username, const char *password,
28 const struct isds_pki_credentials *pki_credentials,
29 struct isds_otp *otp) {
30 isds_error err;
32 err = isds_login(context, url, username, password, pki_credentials, otp);
33 if (error != err)
34 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
35 isds_strerror(error), isds_strerror(err),
36 isds_long_message(context));
37 if (otp != NULL && resolution != otp->resolution)
38 FAIL_TEST("Wrong OTP resolution: expected=%d, returned=%d (%s)",
39 resolution, otp->resolution, isds_long_message(context));
42 PASS_TEST;
46 static int test_logout(const isds_error error, struct isds_ctx *context) {
47 isds_error err;
49 err = isds_logout(context);
50 if (error != err)
51 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
52 isds_strerror(error), isds_strerror(err),
53 isds_long_message(context));
55 PASS_TEST;
58 static int test_ping(const isds_error error, struct isds_ctx *context) {
59 isds_error err;
61 err = isds_ping(context);
62 if (error != err)
63 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
64 isds_strerror(error), isds_strerror(err),
65 isds_long_message(context));
67 PASS_TEST;
70 int main(int argc, char **argv) {
71 int error;
72 pid_t server_process;
73 struct isds_ctx *context = NULL;
74 char *url = NULL;
76 struct isds_otp otp_credentials = {
77 .method = OTP_TIME
80 INIT_TEST("TOTP authentication");
82 if (unsetenv("http_proxy")) {
83 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
85 if (isds_init()) {
86 isds_cleanup();
87 ABORT_UNIT("isds_init() failed\n");
89 context = isds_ctx_create();
90 if (!context) {
91 isds_cleanup();
92 ABORT_UNIT("isds_ctx_create() failed\n");
96 const struct service_configuration services[] = {
97 { SERVICE_DS_Dz_DummyOperation, NULL },
98 { SERVICE_END, NULL }
100 const struct arguments_otp_authentication server_arguments = {
101 .method = AUTH_OTP_TIME,
102 .username = username,
103 .password = password,
104 .otp = (char *) otp_code,
105 .isds_deviations = 1,
106 .services = services
108 error = start_server(&server_process, &url,
109 server_otp_authentication, &server_arguments, NULL);
110 if (error == -1) {
111 isds_ctx_free(&context);
112 isds_cleanup();
113 ABORT_UNIT(server_error);
116 otp_credentials.otp_code = NULL;
117 TEST("First phase with invalid password", test_login,
118 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
119 url, "7777777", "nbuusr1", NULL, &otp_credentials);
120 isds_logout(context);
122 otp_credentials.otp_code = NULL;
123 TEST("First phase with valid password", test_login,
124 IE_PARTIAL_SUCCESS, OTP_RESOLUTION_TOTP_SENT, context,
125 url, username, password, NULL, &otp_credentials);
126 isds_logout(context);
128 otp_credentials.otp_code = (char *) otp_code;
129 TEST("Second phase with invalid password", test_login,
130 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
131 url, "7777777", "nbuusr1", NULL, &otp_credentials);
132 isds_logout(context);
134 otp_credentials.otp_code = "666";
135 TEST("Second phase with valid password but invalid OTP code", test_login,
136 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
137 url, username, password, NULL, &otp_credentials);
138 isds_logout(context);
140 otp_credentials.otp_code = (char *) otp_code;
141 TEST("Second phase with valid password and valid OTP code", test_login,
142 IE_SUCCESS, OTP_RESOLUTION_SUCCESS, context,
143 url, username, password, NULL, &otp_credentials);
144 TEST("Ping after succesfull OTP log-in", test_ping,
145 IE_SUCCESS, context);
146 TEST("Log-out after successfull log-in", test_logout,
147 IE_SUCCESS, context);
149 TEST("Ping after log-out after succesfull OTP log-in", test_ping,
150 IE_CONNECTION_CLOSED, context);
152 if (stop_server(server_process)) {
153 ABORT_UNIT(server_error);
156 free(url);
157 url = NULL;
161 error = start_server(&server_process, &url,
162 server_out_of_order, NULL, NULL);
163 if (error == -1) {
164 isds_ctx_free(&context);
165 isds_cleanup();
166 ABORT_UNIT(server_error);
169 otp_credentials.otp_code = "666";
170 TEST("log into out-of-order server", test_login,
171 IE_SOAP, OTP_RESOLUTION_UNKNOWN, context,
172 url, username, password, NULL, &otp_credentials);
173 isds_logout(context);
175 if (stop_server(server_process)) {
176 ABORT_UNIT(server_error);
179 free(url);
180 url = NULL;
183 isds_ctx_free(&context);
184 isds_cleanup();
185 SUM_TEST();