test: Move tls_authentication argument from server implementations to start_server()
[libisds.git] / test / simline / hotp_authentication.c
blobbdbec1bcf5e9c3157df2affbc7378768d1a2dce1
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;
42 static int test_logout(const isds_error error, struct isds_ctx *context) {
43 isds_error err;
45 err = isds_logout(context);
46 if (error != err)
47 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
48 isds_strerror(error), isds_strerror(err),
49 isds_long_message(context));
51 PASS_TEST;
54 static int test_ping(const isds_error error, struct isds_ctx *context) {
55 isds_error err;
57 err = isds_ping(context);
58 if (error != err)
59 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
60 isds_strerror(error), isds_strerror(err),
61 isds_long_message(context));
63 PASS_TEST;
66 int main(int argc, char **argv) {
67 int error;
68 pid_t server_process;
69 char *server_address = NULL;
70 struct isds_ctx *context = NULL;
71 char *url = NULL;
73 struct isds_otp otp_credentials = {
74 .method = OTP_HMAC
77 INIT_TEST("HOTP authentication");
79 if (unsetenv("http_proxy")) {
80 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
82 if (isds_init()) {
83 isds_cleanup();
84 ABORT_UNIT("isds_init() failed\n");
86 context = isds_ctx_create();
87 if (!context) {
88 isds_cleanup();
89 ABORT_UNIT("isds_ctx_create() failed\n");
93 const struct service_configuration services[] = {
94 { SERVICE_DS_Dz_DummyOperation, NULL },
95 { SERVICE_END, NULL }
97 const struct arguments_otp_authentication server_arguments = {
98 .method = AUTH_OTP_HMAC,
99 .username = username,
100 .password = password,
101 .otp = otp_code,
102 .isds_deviations = 1,
103 .services = services
105 error = start_server(&server_process, &server_address,
106 server_otp_authentication, &server_arguments, NULL);
107 if (error == -1) {
108 isds_ctx_free(&context);
109 isds_cleanup();
110 ABORT_UNIT(server_error);
112 if (-1 == test_asprintf(&url, "http://%s/", server_address)) {
113 free(server_address);
114 stop_server(server_process);
115 isds_ctx_free(&context);
116 isds_cleanup();
117 ABORT_UNIT("Could not format ISDS URL");
119 free(server_address);
121 otp_credentials.otp_code = NULL;
122 TEST("Invalid password and missing OTP code", test_login,
123 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
124 url, "7777777", "nbuusr1", NULL, &otp_credentials);
125 isds_logout(context);
127 otp_credentials.otp_code = (char *) otp_code;
128 TEST("Invalid password and valid OTP code", test_login,
129 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
130 url, "7777777", "nbuusr1", NULL, &otp_credentials);
131 isds_logout(context);
133 otp_credentials.otp_code = NULL;
134 TEST("Valid password but missing OTP code", test_login,
135 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
136 url, username, password, NULL, &otp_credentials);
137 isds_logout(context);
139 otp_credentials.otp_code = "666";
140 TEST("Valid password but invalid OTP code", test_login,
141 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
142 url, username, password, NULL, &otp_credentials);
143 isds_logout(context);
145 otp_credentials.otp_code = (char *) otp_code;
146 TEST("Valid password and valid OTP code", test_login,
147 IE_SUCCESS, OTP_RESOLUTION_SUCCESS, context,
148 url, username, password, NULL, &otp_credentials);
149 TEST("Ping after succesfull OTP log-in", test_ping,
150 IE_SUCCESS, context);
151 TEST("Log-out after successfull log-in", test_logout,
152 IE_SUCCESS, context);
154 TEST("Ping after log-out after succesfull OTP log-in", test_ping,
155 IE_CONNECTION_CLOSED, context);
157 if (-1 == stop_server(server_process)) {
158 ABORT_UNIT(server_error);
161 free(url);
162 url = NULL;
166 error = start_server(&server_process, &server_address,
167 server_out_of_order, NULL, NULL);
168 if (error == -1) {
169 isds_ctx_free(&context);
170 isds_cleanup();
171 ABORT_UNIT(server_error);
173 if (-1 == test_asprintf(&url, "http://%s/", server_address)) {
174 free(server_address);
175 stop_server(server_process);
176 isds_ctx_free(&context);
177 isds_cleanup();
178 ABORT_UNIT("Could not format ISDS URL");
180 free(server_address);
182 otp_credentials.otp_code = "666";
183 TEST("log into out-of-order server", test_login,
184 IE_SOAP, OTP_RESOLUTION_UNKNOWN, context,
185 url, username, password, NULL, &otp_credentials);
186 isds_logout(context);
188 if (-1 == stop_server(server_process)) {
189 ABORT_UNIT(server_error);
192 free(url);
193 url = NULL;
196 isds_ctx_free(&context);
197 isds_cleanup();
198 SUM_TEST();