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 struct isds_ctx
*context
= NULL
;
73 struct isds_otp otp_credentials
= {
77 INIT_TEST("TOTP authentication");
79 if (unsetenv("http_proxy")) {
80 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
84 ABORT_UNIT("isds_init() failed\n");
86 context
= isds_ctx_create();
89 ABORT_UNIT("isds_ctx_create() failed\n");
93 const struct service_configuration services
[] = {
94 { SERVICE_DS_Dz_DummyOperation
, NULL
},
97 const struct arguments_otp_authentication server_arguments
= {
98 .method
= AUTH_OTP_TIME
,
100 .password
= password
,
101 .otp
= (char *) otp_code
,
102 .isds_deviations
= 1,
105 error
= start_server(&server_process
, &url
,
106 server_otp_authentication
, &server_arguments
, NULL
);
108 isds_ctx_free(&context
);
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
);
158 error
= start_server(&server_process
, &url
,
159 server_out_of_order
, NULL
, NULL
);
161 isds_ctx_free(&context
);
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
);
180 isds_ctx_free(&context
);