0.6.1 bump
[libisds.git] / test / simline / isds_change_password.c
blobf5e7402052d1ee32b97af9ddd1f8397eac4620ee
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 = "Doug1as$";
18 static const char *password = "42aA#bc8";
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, char **refnum) {
39 isds_error err;
41 err = isds_change_password(context, old_password, new_password, otp,
42 refnum);
43 if (error != err)
44 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
45 isds_strerror(error), isds_strerror(err),
46 isds_long_message(context));
48 PASS_TEST;
51 int main(int argc, char **argv) {
52 int error;
53 pid_t server_process;
54 char *server_address = NULL;
55 struct isds_ctx *context = NULL;
56 char *url = NULL;
58 INIT_TEST("isds_change_password");
60 if (unsetenv("http_proxy")) {
61 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
63 if (isds_init()) {
64 isds_cleanup();
65 ABORT_UNIT("isds_init() failed\n");
67 context = isds_ctx_create();
68 if (!context) {
69 isds_cleanup();
70 ABORT_UNIT("isds_ctx_create() failed\n");
74 const struct arguments_DS_DsManage_ChangeISDSPassword service_arguments = {
75 .username = username,
76 .current_password = password
78 const struct service_configuration services[] = {
79 { SERVICE_DS_Dz_DummyOperation, NULL },
80 { SERVICE_DS_DsManage_ChangeISDSPassword, &service_arguments },
81 { SERVICE_END, NULL }
83 const struct arguments_basic_authentication server_arguments = {
84 .username = username,
85 .password = password,
86 .isds_deviations = 1,
87 .services = services
89 error = start_server(&server_process, &server_address,
90 server_basic_authentication, &server_arguments);
91 if (error == -1) {
92 isds_ctx_free(&context);
93 isds_cleanup();
94 ABORT_UNIT(server_error);
96 if (-1 == test_asprintf(&url, "http://%s/", server_address)) {
97 free(server_address);
98 stop_server(server_process);
99 isds_ctx_free(&context);
100 isds_cleanup();
101 ABORT_UNIT("Could not format ISDS URL");
103 free(server_address);
105 TEST("login", test_login, IE_SUCCESS,
106 context, url, username, password, NULL, NULL);
107 TEST("bad old password", test_isds_change_password, IE_INVAL,
108 context, "bad old password", "h2k$Aana", NULL, NULL);
109 TEST("too short (7 characters)", test_isds_change_password, IE_INVAL,
110 context, password, "aB34567", NULL, NULL);
111 TEST("too long (33 characters)", test_isds_change_password, IE_INVAL,
112 context, password, "aB3456789112345678921234567893123", NULL, NULL);
113 TEST("no upper case letter", test_isds_change_password, IE_INVAL,
114 context, password, "1bcdefgh", NULL, NULL);
115 TEST("no lower case letter", test_isds_change_password, IE_INVAL,
116 context, password, "1BCDEFGH", NULL, NULL);
117 TEST("no digit", test_isds_change_password, IE_INVAL,
118 context, password, "aBCDEFGH", NULL, NULL);
119 TEST("forbidden space", test_isds_change_password, IE_INVAL,
120 context, password, " h2k$Aan", NULL, NULL);
121 TEST("reused password", test_isds_change_password, IE_INVAL,
122 context, password, password, NULL, NULL);
123 TEST("password contains user ID", test_isds_change_password, IE_INVAL,
124 context, password, username, NULL, NULL);
125 TEST("sequence of the same characters", test_isds_change_password,
126 IE_INVAL, context, password, "h222k$Aa", NULL, NULL);
127 TEST("forbiden prefix qwert", test_isds_change_password,
128 IE_INVAL, context, password, "qwert$A8", NULL, NULL);
129 TEST("forbiden prefix asdgf", test_isds_change_password,
130 IE_INVAL, context, password, "asdgf$A8", NULL, NULL);
131 TEST("forbiden prefix 12345", test_isds_change_password,
132 IE_INVAL, context, password, "12345$Aa", NULL, NULL);
133 TEST("valid request", test_isds_change_password, IE_SUCCESS,
134 context, password, "h2k$Aana", NULL, NULL);
136 if (-1 == stop_server(server_process)) {
137 ABORT_UNIT(server_error);
140 free(url);
141 url = NULL;
145 isds_logout(context);
146 isds_ctx_free(&context);
147 isds_cleanup();
148 SUM_TEST();