test: Introduce isds_change_password
[libisds.git] / test / simline / isds_change_password.c
blobdad751383d260ee6df97d3b81ef2d4783be78326
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";
21 static int test_login(const isds_error error, struct isds_ctx *context,
22 const char *url, const char *username, const char *password,
23 const struct isds_pki_credentials *pki_credentials,
24 struct isds_otp *otp) {
25 isds_error err;
27 err = isds_login(context, url, username, password, pki_credentials, otp);
28 if (error != err)
29 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
30 isds_strerror(error), isds_strerror(err),
31 isds_long_message(context));
33 PASS_TEST;
36 static int test_isds_change_password(const isds_error error,
37 struct isds_ctx *context, const char *old_password,
38 const char *new_password, struct isds_otp *otp) {
39 isds_error err;
41 err = isds_change_password(context, old_password, new_password, otp);
42 if (error != err)
43 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
44 isds_strerror(error), isds_strerror(err),
45 isds_long_message(context));
47 PASS_TEST;
50 int main(int argc, char **argv) {
51 int error;
52 pid_t server_process;
53 char *server_address = NULL;
54 struct isds_ctx *context = NULL;
55 char *url = NULL;
57 INIT_TEST("isds_change_password");
59 if (unsetenv("http_proxy")) {
60 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
62 if (isds_init()) {
63 isds_cleanup();
64 ABORT_UNIT("isds_init() failed\n");
66 context = isds_ctx_create();
67 if (!context) {
68 isds_cleanup();
69 ABORT_UNIT("isds_ctx_create() failed\n");
73 const struct service_configuration services[] = {
74 { SERVICE_DS_Dz_DummyOperation, NULL },
75 { SERVICE_DS_DsManage_ChangeISDSPassword, password },
76 { SERVICE_END, NULL }
78 const struct arguments_basic_authentication server_arguments = {
79 .username = username,
80 .password = password,
81 .isds_deviations = 1,
82 .services = services
84 error = start_server(&server_process, &server_address,
85 server_basic_authentication, &server_arguments);
86 if (error == -1) {
87 isds_ctx_free(&context);
88 isds_cleanup();
89 ABORT_UNIT(server_error);
91 if (-1 == test_asprintf(&url, "http://%s/", server_address)) {
92 free(server_address);
93 stop_server(server_process);
94 isds_ctx_free(&context);
95 isds_cleanup();
96 ABORT_UNIT("Could not format ISDS URL");
98 free(server_address);
100 TEST("login", test_login, IE_SUCCESS, context,
101 url, username, password, NULL, NULL);
102 TEST("bad old password", test_isds_change_password, IE_INVAL, context,
103 "bad old password", "h2k$aana", NULL);
104 TEST("valid request", test_isds_change_password, IE_SUCCESS, context,
105 password, "h2k$aana", NULL);
107 if (-1 == stop_server(server_process)) {
108 ABORT_UNIT(server_error);
111 free(url);
112 url = NULL;
116 isds_logout(context);
117 isds_ctx_free(&context);
118 isds_cleanup();
119 SUM_TEST();