l10n: Update translation catalogues
[libisds.git] / test / simline / hotp_authentication.c
blob6269c151f352a72c8377d9dacac8f5660df82874
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;
45 static int test_logout(const isds_error error, struct isds_ctx *context) {
46 isds_error err;
48 err = isds_logout(context);
49 if (error != err)
50 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
51 isds_strerror(error), isds_strerror(err),
52 isds_long_message(context));
54 PASS_TEST;
57 static int test_ping(const isds_error error, struct isds_ctx *context) {
58 isds_error err;
60 err = isds_ping(context);
61 if (error != err)
62 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
63 isds_strerror(error), isds_strerror(err),
64 isds_long_message(context));
66 PASS_TEST;
69 int main(void) {
70 int error;
71 pid_t server_process;
72 struct isds_ctx *context = NULL;
73 char *url = NULL;
75 struct isds_otp otp_credentials = {
76 .method = OTP_HMAC
79 INIT_TEST("HOTP authentication");
81 if (unsetenv("http_proxy")) {
82 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
84 if (isds_init()) {
85 isds_cleanup();
86 ABORT_UNIT("isds_init() failed\n");
88 context = isds_ctx_create();
89 if (!context) {
90 isds_cleanup();
91 ABORT_UNIT("isds_ctx_create() failed\n");
95 const struct service_configuration services[] = {
96 { SERVICE_DS_Dz_DummyOperation, NULL },
97 { SERVICE_END, NULL }
99 const struct arguments_otp_authentication server_arguments = {
100 .method = AUTH_OTP_HMAC,
101 .username = username,
102 .password = password,
103 .otp = otp_code,
104 .isds_deviations = 1,
105 .services = services
107 error = start_server(&server_process, &url,
108 server_otp_authentication, &server_arguments, NULL);
109 if (error == -1) {
110 isds_ctx_free(&context);
111 isds_cleanup();
112 ABORT_UNIT(server_error);
115 otp_credentials.otp_code = NULL;
116 TEST("Invalid password and missing OTP code", test_login,
117 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
118 url, "7777777", "nbuusr1", NULL, &otp_credentials);
119 isds_logout(context);
121 otp_credentials.otp_code = (char *) otp_code;
122 TEST("Invalid password and valid 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 = NULL;
128 TEST("Valid password but missing OTP code", test_login,
129 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
130 url, username, password, NULL, &otp_credentials);
131 isds_logout(context);
133 otp_credentials.otp_code = "666";
134 TEST("Valid password but invalid 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 = (char *) otp_code;
140 TEST("Valid password and valid OTP code", test_login,
141 IE_SUCCESS, OTP_RESOLUTION_SUCCESS, context,
142 url, username, password, NULL, &otp_credentials);
143 TEST("Ping after succesfull OTP log-in", test_ping,
144 IE_SUCCESS, context);
145 TEST("Log-out after successfull log-in", test_logout,
146 IE_SUCCESS, context);
148 TEST("Ping after log-out after succesfull OTP log-in", test_ping,
149 IE_CONNECTION_CLOSED, context);
151 if (stop_server(server_process)) {
152 isds_ctx_free(&context);
153 isds_cleanup();
154 ABORT_UNIT(server_error);
157 free(url);
158 url = NULL;
162 error = start_server(&server_process, &url,
163 server_out_of_order, NULL, NULL);
164 if (error == -1) {
165 isds_ctx_free(&context);
166 isds_cleanup();
167 ABORT_UNIT(server_error);
170 otp_credentials.otp_code = "666";
171 TEST("log into out-of-order server", test_login,
172 IE_SOAP, OTP_RESOLUTION_UNKNOWN, context,
173 url, username, password, NULL, &otp_credentials);
174 isds_logout(context);
176 if (stop_server(server_process)) {
177 isds_ctx_free(&context);
178 isds_cleanup();
179 ABORT_UNIT(server_error);
182 free(url);
183 url = NULL;
186 isds_ctx_free(&context);
187 isds_cleanup();
188 SUM_TEST();