test: Add tests for isd_get_commercial_credit()
[libisds.git] / test / simline / totp_authentication.c
blob0020a842c5b9583dea4f94b2ce10799837ec8a2b
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 struct isds_ctx *context = NULL;
71 char *url = NULL;
73 struct isds_otp otp_credentials = {
74 .method = OTP_TIME
77 INIT_TEST("TOTP 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_TIME,
99 .username = username,
100 .password = password,
101 .otp = (char *) otp_code,
102 .isds_deviations = 1,
103 .services = services
105 error = start_server(&server_process, &url,
106 server_otp_authentication, &server_arguments, NULL);
107 if (error == -1) {
108 isds_ctx_free(&context);
109 isds_cleanup();
110 ABORT_UNIT(server_error);
113 otp_credentials.otp_code = NULL;
114 TEST("First phase with invalid password", test_login,
115 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
116 url, "7777777", "nbuusr1", NULL, &otp_credentials);
117 isds_logout(context);
119 otp_credentials.otp_code = NULL;
120 TEST("First phase with valid password", test_login,
121 IE_PARTIAL_SUCCESS, OTP_RESOLUTION_TOTP_SENT, context,
122 url, username, password, NULL, &otp_credentials);
123 isds_logout(context);
125 otp_credentials.otp_code = (char *) otp_code;
126 TEST("Second phase with invalid password", test_login,
127 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
128 url, "7777777", "nbuusr1", NULL, &otp_credentials);
129 isds_logout(context);
131 otp_credentials.otp_code = "666";
132 TEST("Second phase with valid password but invalid OTP code", test_login,
133 IE_NOT_LOGGED_IN, OTP_RESOLUTION_BAD_AUTHENTICATION, context,
134 url, username, password, NULL, &otp_credentials);
135 isds_logout(context);
137 otp_credentials.otp_code = (char *) otp_code;
138 TEST("Second phase with valid password and valid OTP code", test_login,
139 IE_SUCCESS, OTP_RESOLUTION_SUCCESS, context,
140 url, username, password, NULL, &otp_credentials);
141 TEST("Ping after succesfull OTP log-in", test_ping,
142 IE_SUCCESS, context);
143 TEST("Log-out after successfull log-in", test_logout,
144 IE_SUCCESS, context);
146 TEST("Ping after log-out after succesfull OTP log-in", test_ping,
147 IE_CONNECTION_CLOSED, context);
149 if (stop_server(server_process)) {
150 ABORT_UNIT(server_error);
153 free(url);
154 url = NULL;
158 error = start_server(&server_process, &url,
159 server_out_of_order, NULL, NULL);
160 if (error == -1) {
161 isds_ctx_free(&context);
162 isds_cleanup();
163 ABORT_UNIT(server_error);
166 otp_credentials.otp_code = "666";
167 TEST("log into out-of-order server", test_login,
168 IE_SOAP, OTP_RESOLUTION_UNKNOWN, context,
169 url, username, password, NULL, &otp_credentials);
170 isds_logout(context);
172 if (stop_server(server_process)) {
173 ABORT_UNIT(server_error);
176 free(url);
177 url = NULL;
180 isds_ctx_free(&context);
181 isds_cleanup();
182 SUM_TEST();