2 #define _POSIX_SOURCE /* For getaddrinfo(3) */
6 #define _BSD_SOURCE /* For NI_MAXHOST */
10 #define _XOPEN_SOURCE 600 /* For unsetenv(3) */
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
) {
29 err
= isds_login(context
, url
, username
, password
, pki_credentials
, otp
);
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
));
43 static int test_logout(const isds_error error
, struct isds_ctx
*context
) {
46 err
= isds_logout(context
);
48 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
49 isds_strerror(error
), isds_strerror(err
),
50 isds_long_message(context
));
55 static int test_ping(const isds_error error
, struct isds_ctx
*context
) {
58 err
= isds_ping(context
);
60 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
61 isds_strerror(error
), isds_strerror(err
),
62 isds_long_message(context
));
67 int main(int argc
, char **argv
) {
70 char *server_address
= NULL
;
71 struct isds_ctx
*context
= NULL
;
74 struct isds_otp otp_credentials
= {
78 INIT_TEST("TOTP authentication");
80 if (unsetenv("http_proxy")) {
81 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
85 ABORT_UNIT("isds_init() failed\n");
87 context
= isds_ctx_create();
90 ABORT_UNIT("isds_ctx_create() failed\n");
94 const struct service_configuration services
[] = {
95 { SERVICE_DS_Dz_DummyOperation
, NULL
},
98 const struct arguments_otp_authentication server_arguments
= {
99 .method
= AUTH_OTP_TIME
,
100 .username
= username
,
101 .password
= password
,
102 .otp
= (char *) otp_code
,
103 .isds_deviations
= 1,
106 error
= start_server(&server_process
, &server_address
,
107 server_otp_authentication
, &server_arguments
);
109 isds_ctx_free(&context
);
111 ABORT_UNIT(server_error
);
113 if (-1 == test_asprintf(&url
, "http://%s/", server_address
)) {
114 free(server_address
);
115 stop_server(server_process
);
116 isds_ctx_free(&context
);
118 ABORT_UNIT("Could not format ISDS URL");
120 free(server_address
);
122 otp_credentials
.otp_code
= NULL
;
123 TEST("First phase with invalid password", test_login
,
124 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
125 url
, "7777777", "nbuusr1", NULL
, &otp_credentials
);
126 isds_logout(context
);
128 otp_credentials
.otp_code
= NULL
;
129 TEST("First phase with valid password", test_login
,
130 IE_PARTIAL_SUCCESS
, OTP_RESOLUTION_TOTP_SENT
, context
,
131 url
, username
, password
, NULL
, &otp_credentials
);
132 isds_logout(context
);
134 otp_credentials
.otp_code
= (char *) otp_code
;
135 TEST("Second phase with invalid password", test_login
,
136 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
137 url
, "7777777", "nbuusr1", NULL
, &otp_credentials
);
138 isds_logout(context
);
140 otp_credentials
.otp_code
= "666";
141 TEST("Second phase with valid password but invalid OTP code", test_login
,
142 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
143 url
, username
, password
, NULL
, &otp_credentials
);
144 isds_logout(context
);
146 otp_credentials
.otp_code
= (char *) otp_code
;
147 TEST("Second phase with valid password and valid OTP code", test_login
,
148 IE_SUCCESS
, OTP_RESOLUTION_SUCCESS
, context
,
149 url
, username
, password
, NULL
, &otp_credentials
);
150 TEST("Ping after succesfull OTP log-in", test_ping
,
151 IE_SUCCESS
, context
);
152 TEST("Log-out after successfull log-in", test_logout
,
153 IE_SUCCESS
, context
);
155 TEST("Ping after log-out after succesfull OTP log-in", test_ping
,
156 IE_CONNECTION_CLOSED
, context
);
158 if (-1 == stop_server(server_process
)) {
159 ABORT_UNIT(server_error
);
167 error
= start_server(&server_process
, &server_address
,
168 server_out_of_order
, NULL
);
170 isds_ctx_free(&context
);
172 ABORT_UNIT(server_error
);
174 if (-1 == test_asprintf(&url
, "http://%s/", server_address
)) {
175 free(server_address
);
176 stop_server(server_process
);
177 isds_ctx_free(&context
);
179 ABORT_UNIT("Could not format ISDS URL");
181 free(server_address
);
183 otp_credentials
.otp_code
= "666";
184 TEST("log into out-of-order server", test_login
,
185 IE_SOAP
, OTP_RESOLUTION_UNKNOWN
, context
,
186 url
, username
, password
, NULL
, &otp_credentials
);
187 isds_logout(context
);
189 if (-1 == stop_server(server_process
)) {
190 ABORT_UNIT(server_error
);
197 isds_ctx_free(&context
);