test: starting server returns URL instead of socket address
[libisds.git] / test / simline / hotp_authentication.c
blob6d65ab86b0f66d3edb451a727506caa12f89a65e
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 struct isds_ctx *context = NULL;
70 char *url = NULL;
72 struct isds_otp otp_credentials = {
73 .method = OTP_HMAC
76 INIT_TEST("HOTP authentication");
78 if (unsetenv("http_proxy")) {
79 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
81 if (isds_init()) {
82 isds_cleanup();
83 ABORT_UNIT("isds_init() failed\n");
85 context = isds_ctx_create();
86 if (!context) {
87 isds_cleanup();
88 ABORT_UNIT("isds_ctx_create() failed\n");
92 const struct service_configuration services[] = {
93 { SERVICE_DS_Dz_DummyOperation, NULL },
94 { SERVICE_END, NULL }
96 const struct arguments_otp_authentication server_arguments = {
97 .method = AUTH_OTP_HMAC,
98 .username = username,
99 .password = password,
100 .otp = otp_code,
101 .isds_deviations = 1,
102 .services = services
104 error = start_server(&server_process, &url,
105 server_otp_authentication, &server_arguments, NULL);
106 if (error == -1) {
107 isds_ctx_free(&context);
108 isds_cleanup();
109 ABORT_UNIT(server_error);
112 otp_credentials.otp_code = NULL;
113 TEST("Invalid password and missing OTP code", test_login,
114 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
115 url, "7777777", "nbuusr1", NULL, &otp_credentials);
116 isds_logout(context);
118 otp_credentials.otp_code = (char *) otp_code;
119 TEST("Invalid password and valid OTP code", test_login,
120 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
121 url, "7777777", "nbuusr1", NULL, &otp_credentials);
122 isds_logout(context);
124 otp_credentials.otp_code = NULL;
125 TEST("Valid password but missing OTP code", test_login,
126 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
127 url, username, password, NULL, &otp_credentials);
128 isds_logout(context);
130 otp_credentials.otp_code = "666";
131 TEST("Valid password but invalid OTP code", test_login,
132 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
133 url, username, password, NULL, &otp_credentials);
134 isds_logout(context);
136 otp_credentials.otp_code = (char *) otp_code;
137 TEST("Valid password and valid OTP code", test_login,
138 IE_SUCCESS, OTP_RESOLUTION_SUCCESS, context,
139 url, username, password, NULL, &otp_credentials);
140 TEST("Ping after succesfull OTP log-in", test_ping,
141 IE_SUCCESS, context);
142 TEST("Log-out after successfull log-in", test_logout,
143 IE_SUCCESS, context);
145 TEST("Ping after log-out after succesfull OTP log-in", test_ping,
146 IE_CONNECTION_CLOSED, context);
148 if (stop_server(server_process)) {
149 ABORT_UNIT(server_error);
152 free(url);
153 url = NULL;
157 error = start_server(&server_process, &url,
158 server_out_of_order, NULL, NULL);
159 if (error == -1) {
160 isds_ctx_free(&context);
161 isds_cleanup();
162 ABORT_UNIT(server_error);
165 otp_credentials.otp_code = "666";
166 TEST("log into out-of-order server", test_login,
167 IE_SOAP, OTP_RESOLUTION_UNKNOWN, context,
168 url, username, password, NULL, &otp_credentials);
169 isds_logout(context);
171 if (stop_server(server_process)) {
172 ABORT_UNIT(server_error);
175 free(url);
176 url = NULL;
179 isds_ctx_free(&context);
180 isds_cleanup();
181 SUM_TEST();