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
));
42 static int test_logout(const isds_error error
, struct isds_ctx
*context
) {
45 err
= isds_logout(context
);
47 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
48 isds_strerror(error
), isds_strerror(err
),
49 isds_long_message(context
));
54 static int test_ping(const isds_error error
, struct isds_ctx
*context
) {
57 err
= isds_ping(context
);
59 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
60 isds_strerror(error
), isds_strerror(err
),
61 isds_long_message(context
));
66 int main(int argc
, char **argv
) {
69 struct isds_ctx
*context
= NULL
;
72 struct isds_otp otp_credentials
= {
76 INIT_TEST("HOTP authentication");
78 if (unsetenv("http_proxy")) {
79 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
83 ABORT_UNIT("isds_init() failed\n");
85 context
= isds_ctx_create();
88 ABORT_UNIT("isds_ctx_create() failed\n");
92 const struct service_configuration services
[] = {
93 { SERVICE_DS_Dz_DummyOperation
, NULL
},
96 const struct arguments_otp_authentication server_arguments
= {
97 .method
= AUTH_OTP_HMAC
,
101 .isds_deviations
= 1,
104 error
= start_server(&server_process
, &url
,
105 server_otp_authentication
, &server_arguments
, NULL
);
107 isds_ctx_free(&context
);
109 ABORT_UNIT(server_error
);
112 otp_credentials
.otp_code
= NULL
;
113 TEST("Invalid password and missing OTP code", test_login
,
114 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
115 url
, "7777777", "nbuusr1", NULL
, &otp_credentials
);
116 isds_logout(context
);
118 otp_credentials
.otp_code
= (char *) otp_code
;
119 TEST("Invalid password and valid OTP code", test_login
,
120 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
121 url
, "7777777", "nbuusr1", NULL
, &otp_credentials
);
122 isds_logout(context
);
124 otp_credentials
.otp_code
= NULL
;
125 TEST("Valid password but missing OTP code", test_login
,
126 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
127 url
, username
, password
, NULL
, &otp_credentials
);
128 isds_logout(context
);
130 otp_credentials
.otp_code
= "666";
131 TEST("Valid password but invalid OTP code", test_login
,
132 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
133 url
, username
, password
, NULL
, &otp_credentials
);
134 isds_logout(context
);
136 otp_credentials
.otp_code
= (char *) otp_code
;
137 TEST("Valid password and valid OTP code", test_login
,
138 IE_SUCCESS
, OTP_RESOLUTION_SUCCESS
, context
,
139 url
, username
, password
, NULL
, &otp_credentials
);
140 TEST("Ping after succesfull OTP log-in", test_ping
,
141 IE_SUCCESS
, context
);
142 TEST("Log-out after successfull log-in", test_logout
,
143 IE_SUCCESS
, context
);
145 TEST("Ping after log-out after succesfull OTP log-in", test_ping
,
146 IE_CONNECTION_CLOSED
, context
);
148 if (stop_server(server_process
)) {
149 ABORT_UNIT(server_error
);
157 error
= start_server(&server_process
, &url
,
158 server_out_of_order
, NULL
, NULL
);
160 isds_ctx_free(&context
);
162 ABORT_UNIT(server_error
);
165 otp_credentials
.otp_code
= "666";
166 TEST("log into out-of-order server", test_login
,
167 IE_SOAP
, OTP_RESOLUTION_UNKNOWN
, context
,
168 url
, username
, password
, NULL
, &otp_credentials
);
169 isds_logout(context
);
171 if (stop_server(server_process
)) {
172 ABORT_UNIT(server_error
);
179 isds_ctx_free(&context
);