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 char *server_address
= NULL
;
70 struct isds_ctx
*context
= NULL
;
73 struct isds_otp otp_credentials
= {
77 INIT_TEST("HOTP 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_HMAC
,
100 .password
= password
,
102 .isds_deviations
= 1,
105 error
= start_server(&server_process
, &server_address
,
106 server_otp_authentication
, &server_arguments
);
108 isds_ctx_free(&context
);
110 ABORT_UNIT(server_error
);
112 if (-1 == test_asprintf(&url
, "http://%s/", server_address
)) {
113 free(server_address
);
114 stop_server(server_process
);
115 isds_ctx_free(&context
);
117 ABORT_UNIT("Could not format ISDS URL");
119 free(server_address
);
121 otp_credentials
.otp_code
= NULL
;
122 TEST("Invalid password and missing OTP code", test_login
,
123 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
124 url
, "7777777", "nbuusr1", NULL
, &otp_credentials
);
125 isds_logout(context
);
127 otp_credentials
.otp_code
= (char *) otp_code
;
128 TEST("Invalid password and valid OTP code", test_login
,
129 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
130 url
, "7777777", "nbuusr1", NULL
, &otp_credentials
);
131 isds_logout(context
);
133 otp_credentials
.otp_code
= NULL
;
134 TEST("Valid password but missing OTP code", test_login
,
135 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
136 url
, username
, password
, NULL
, &otp_credentials
);
137 isds_logout(context
);
139 otp_credentials
.otp_code
= "666";
140 TEST("Valid password but invalid OTP code", test_login
,
141 IE_NOT_LOGGED_IN
, OTP_RESOLUTION_BAD_AUTHENTICATION
, context
,
142 url
, username
, password
, NULL
, &otp_credentials
);
143 isds_logout(context
);
145 otp_credentials
.otp_code
= (char *) otp_code
;
146 TEST("Valid password and valid OTP code", test_login
,
147 IE_SUCCESS
, OTP_RESOLUTION_SUCCESS
, context
,
148 url
, username
, password
, NULL
, &otp_credentials
);
149 TEST("Ping after succesfull OTP log-in", test_ping
,
150 IE_SUCCESS
, context
);
151 TEST("Log-out after successfull log-in", test_logout
,
152 IE_SUCCESS
, context
);
154 TEST("Ping after log-out after succesfull OTP log-in", test_ping
,
155 IE_CONNECTION_CLOSED
, context
);
157 if (-1 == stop_server(server_process
)) {
158 ABORT_UNIT(server_error
);
166 error
= start_server(&server_process
, &server_address
,
167 server_out_of_order
, NULL
);
169 isds_ctx_free(&context
);
171 ABORT_UNIT(server_error
);
173 if (-1 == test_asprintf(&url
, "http://%s/", server_address
)) {
174 free(server_address
);
175 stop_server(server_process
);
176 isds_ctx_free(&context
);
178 ABORT_UNIT("Could not format ISDS URL");
180 free(server_address
);
182 otp_credentials
.otp_code
= "666";
183 TEST("log into out-of-order server", test_login
,
184 IE_SOAP
, OTP_RESOLUTION_UNKNOWN
, context
,
185 url
, username
, password
, NULL
, &otp_credentials
);
186 isds_logout(context
);
188 if (-1 == stop_server(server_process
)) {
189 ABORT_UNIT(server_error
);
196 isds_ctx_free(&context
);