test: Introduce isds_change_password
[libisds.git] / test / simline / totp_authentication.c
blob562ac78aa51851eee1242a126be382218b10cbba
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 = "douglas";
18 static const char *password = "42";
19 static const char *otp_code = "314";
22 static int test_login(const isds_error error,
23 const isds_otp_resolution resolution, struct isds_ctx *context,
24 const char *url, const char *username, const char *password,
25 const struct isds_pki_credentials *pki_credentials,
26 struct isds_otp *otp) {
27 isds_error err;
29 err = isds_login(context, url, username, password, pki_credentials, otp);
30 if (error != err)
31 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
32 isds_strerror(error), isds_strerror(err),
33 isds_long_message(context));
34 if (otp != NULL && resolution != otp->resolution)
35 FAIL_TEST("Wrong OTP resolution: expected=%d, returned=%d (%s)",
36 resolution, otp->resolution, isds_long_message(context));
39 PASS_TEST;
43 static int test_logout(const isds_error error, struct isds_ctx *context) {
44 isds_error err;
46 err = isds_logout(context);
47 if (error != err)
48 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
49 isds_strerror(error), isds_strerror(err),
50 isds_long_message(context));
52 PASS_TEST;
55 static int test_ping(const isds_error error, struct isds_ctx *context) {
56 isds_error err;
58 err = isds_ping(context);
59 if (error != err)
60 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
61 isds_strerror(error), isds_strerror(err),
62 isds_long_message(context));
64 PASS_TEST;
67 int main(int argc, char **argv) {
68 int error;
69 pid_t server_process;
70 char *server_address = NULL;
71 struct isds_ctx *context = NULL;
72 char *url = NULL;
74 struct isds_otp otp_credentials = {
75 .method = OTP_TIME
78 INIT_TEST("TOTP authentication");
80 if (unsetenv("http_proxy")) {
81 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
83 if (isds_init()) {
84 isds_cleanup();
85 ABORT_UNIT("isds_init() failed\n");
87 context = isds_ctx_create();
88 if (!context) {
89 isds_cleanup();
90 ABORT_UNIT("isds_ctx_create() failed\n");
94 const struct service_configuration services[] = {
95 { SERVICE_DS_Dz_DummyOperation, NULL },
96 { SERVICE_END, NULL }
98 const struct arguments_otp_authentication server_arguments = {
99 .method = AUTH_OTP_TIME,
100 .username = username,
101 .password = password,
102 .otp = (char *) otp_code,
103 .isds_deviations = 1,
104 .services = services
106 error = start_server(&server_process, &server_address,
107 server_otp_authentication, &server_arguments);
108 if (error == -1) {
109 isds_ctx_free(&context);
110 isds_cleanup();
111 ABORT_UNIT(server_error);
113 if (-1 == test_asprintf(&url, "http://%s/", server_address)) {
114 free(server_address);
115 stop_server(server_process);
116 isds_ctx_free(&context);
117 isds_cleanup();
118 ABORT_UNIT("Could not format ISDS URL");
120 free(server_address);
122 otp_credentials.otp_code = NULL;
123 TEST("First phase with invalid password", test_login,
124 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
125 url, "7777777", "nbuusr1", NULL, &otp_credentials);
126 isds_logout(context);
128 otp_credentials.otp_code = NULL;
129 TEST("First phase with valid password", test_login,
130 IE_PARTIAL_SUCCESS, OTP_RESOLUTION_TOTP_SENT, context,
131 url, username, password, NULL, &otp_credentials);
132 isds_logout(context);
134 otp_credentials.otp_code = (char *) otp_code;
135 TEST("Second phase with invalid password", test_login,
136 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
137 url, "7777777", "nbuusr1", NULL, &otp_credentials);
138 isds_logout(context);
140 otp_credentials.otp_code = "666";
141 TEST("Second phase with valid password but invalid OTP code", test_login,
142 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
143 url, username, password, NULL, &otp_credentials);
144 isds_logout(context);
146 otp_credentials.otp_code = (char *) otp_code;
147 TEST("Second phase with valid password and valid OTP code", test_login,
148 IE_SUCCESS, OTP_RESOLUTION_SUCCESS, context,
149 url, username, password, NULL, &otp_credentials);
150 TEST("Ping after succesfull OTP log-in", test_ping,
151 IE_SUCCESS, context);
152 TEST("Log-out after successfull log-in", test_logout,
153 IE_SUCCESS, context);
155 TEST("Ping after log-out after succesfull OTP log-in", test_ping,
156 IE_CONNECTION_CLOSED, context);
158 if (-1 == stop_server(server_process)) {
159 ABORT_UNIT(server_error);
162 free(url);
163 url = NULL;
167 error = start_server(&server_process, &server_address,
168 server_out_of_order, NULL);
169 if (error == -1) {
170 isds_ctx_free(&context);
171 isds_cleanup();
172 ABORT_UNIT(server_error);
174 if (-1 == test_asprintf(&url, "http://%s/", server_address)) {
175 free(server_address);
176 stop_server(server_process);
177 isds_ctx_free(&context);
178 isds_cleanup();
179 ABORT_UNIT("Could not format ISDS URL");
181 free(server_address);
183 otp_credentials.otp_code = "666";
184 TEST("log into out-of-order server", test_login,
185 IE_SOAP, OTP_RESOLUTION_UNKNOWN, context,
186 url, username, password, NULL, &otp_credentials);
187 isds_logout(context);
189 if (-1 == stop_server(server_process)) {
190 ABORT_UNIT(server_error);
193 free(url);
194 url = NULL;
197 isds_ctx_free(&context);
198 isds_cleanup();
199 SUM_TEST();