test: Report server crash in simline tests
[libisds.git] / test / simline / hotp_isds_change_password.c
blobb9287fce22afcd45bf0a6e9c6aba111379361070
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 = "Doug1as$";
18 static const char *password = "42aA#bc8";
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_isds_change_password(const isds_error error,
44 const isds_otp_resolution resolution, const char *reference_number,
45 struct isds_ctx *context, const char *old_password,
46 const char *new_password, struct isds_otp *otp, char **refnum) {
47 isds_error err;
49 err = isds_change_password(context, old_password, new_password, otp,
50 refnum);
51 if (error != err)
52 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
53 isds_strerror(error), isds_strerror(err),
54 isds_long_message(context));
55 if (otp != NULL && resolution != otp->resolution)
56 FAIL_TEST("Wrong OTP resolution: expected=%d, returned=%d (%s)",
57 resolution, otp->resolution, isds_long_message(context));
58 if (NULL != refnum)
59 TEST_STRING_DUPLICITY(reference_number, *refnum);
61 PASS_TEST;
64 int main(int argc, char **argv) {
65 int error;
66 pid_t server_process;
67 char *server_address = NULL;
68 struct isds_ctx *context = NULL;
69 char *url = NULL;
70 char *refnum = NULL;
71 struct isds_otp otp_credentials = {
72 .method = OTP_HMAC
76 INIT_TEST("isds_change_password with HOTP");
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 arguments_asws_changePassword_ChangePasswordOTP
93 passwd_arguments = {
94 .username = username,
95 .current_password = password,
96 .method = AUTH_OTP_HMAC,
97 .reference_number = "42"
99 const struct service_configuration services[] = {
100 { SERVICE_DS_Dz_DummyOperation, NULL },
101 { SERVICE_asws_changePassword_ChangePasswordOTP, &passwd_arguments },
102 { SERVICE_END, NULL }
104 const struct arguments_otp_authentication server_arguments = {
105 .method = AUTH_OTP_HMAC,
106 .username = username,
107 .password = password,
108 .otp = (char *) otp_code,
109 .isds_deviations = 1,
110 .services = services
112 error = start_server(&server_process, &server_address,
113 server_otp_authentication, &server_arguments, NULL);
114 if (error == -1) {
115 isds_ctx_free(&context);
116 isds_cleanup();
117 ABORT_UNIT(server_error);
119 if (-1 == test_asprintf(&url, "http://%s/", server_address)) {
120 free(server_address);
121 stop_server(server_process);
122 isds_ctx_free(&context);
123 isds_cleanup();
124 ABORT_UNIT("Could not format ISDS URL");
126 free(server_address);
128 otp_credentials.otp_code = (char *) otp_code;
129 TEST("login", test_login, IE_SUCCESS, OTP_RESOLUTION_SUCCESS,
130 context, url, username, password, NULL, &otp_credentials);
132 /* Second phase of authentication */
133 otp_credentials.otp_code = (char *) otp_code;
134 TEST("Second phase with invalid password", test_isds_change_password,
135 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, NULL,
136 context, "nbuusr1", "h2k$Aana", &otp_credentials, &refnum);
137 /* XXX: There is bug in curl < 7.28.0 when authorization header is not
138 * sent on second attempt after 401 response. Fixed by upstream commit
139 * ce8311c7e49eca93c136b58efa6763853541ec97. The only work-around is
140 * to use new CURL handle. */
141 TEST("Second phase with invalid password 2", test_isds_change_password,
142 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, NULL,
143 context, "nbuusr2", "h2k$Aana", &otp_credentials, &refnum);
144 otp_credentials.otp_code = "666";
145 TEST("Second phase with valid password but invalid OTP code",
146 test_isds_change_password,
147 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, NULL,
148 context, password, "h2k$Aana", &otp_credentials, &refnum);
150 /* Checks for new password */
151 otp_credentials.otp_code = (char *) otp_code;
152 TEST("too short (7 characters)", test_isds_change_password, IE_INVAL,
153 OTP_RESOLUTION_SUCCESS, "42",
154 context, password, "aB34567", &otp_credentials, &refnum);
155 TEST("too long (33 characters)", test_isds_change_password, IE_INVAL,
156 OTP_RESOLUTION_SUCCESS, "42",
157 context, password, "aB3456789112345678921234567893123",
158 &otp_credentials, &refnum);
159 TEST("no upper case letter", test_isds_change_password, IE_INVAL,
160 OTP_RESOLUTION_SUCCESS, "42",
161 context, password, "1bcdefgh", &otp_credentials, &refnum);
162 TEST("no lower case letter", test_isds_change_password, IE_INVAL,
163 OTP_RESOLUTION_SUCCESS, "42",
164 context, password, "1BCDEFGH", &otp_credentials, &refnum);
165 TEST("no digit", test_isds_change_password, IE_INVAL,
166 OTP_RESOLUTION_SUCCESS, "42",
167 context, password, "aBCDEFGH", &otp_credentials, &refnum);
168 TEST("forbidden space", test_isds_change_password, IE_INVAL,
169 OTP_RESOLUTION_SUCCESS, "42",
170 context, password, " h2k$Aan", &otp_credentials, &refnum);
171 TEST("reused password", test_isds_change_password, IE_INVAL,
172 OTP_RESOLUTION_SUCCESS, "42",
173 context, password, password, &otp_credentials, &refnum);
174 TEST("password contains user ID", test_isds_change_password, IE_INVAL,
175 OTP_RESOLUTION_SUCCESS, "42",
176 context, password, username, &otp_credentials, &refnum);
177 TEST("sequence of the same characters", test_isds_change_password,
178 IE_INVAL, OTP_RESOLUTION_SUCCESS, "42",
179 context, password, "h222k$Aa", &otp_credentials, &refnum);
180 TEST("forbiden prefix qwert", test_isds_change_password,
181 IE_INVAL, OTP_RESOLUTION_SUCCESS, "42",
182 context, password, "qwert$A8", &otp_credentials, &refnum);
183 TEST("forbiden prefix asdgf", test_isds_change_password,
184 IE_INVAL, OTP_RESOLUTION_SUCCESS, "42",
185 context, password, "asdgf$A8", &otp_credentials, &refnum);
186 TEST("forbiden prefix 12345", test_isds_change_password,
187 IE_INVAL, OTP_RESOLUTION_SUCCESS, "42",
188 context, password, "12345$Aa", &otp_credentials, &refnum);
189 TEST("valid request", test_isds_change_password, IE_SUCCESS,
190 OTP_RESOLUTION_SUCCESS, "42",
191 context, password, "h2k$Aana", &otp_credentials, &refnum);
193 free(refnum);
194 refnum = NULL;
195 isds_logout(context);
196 if (stop_server(server_process)) {
197 ABORT_UNIT(server_error);
200 free(url);
201 url = NULL;
205 isds_logout(context);
206 isds_ctx_free(&context);
207 isds_cleanup();
208 SUM_TEST();