test: Add test for isds_ping()
[libisds.git] / test / simline / isds_ping.c
blobc3908baf0fe04c3bb416f66ded9dcaae5cbce12f
1 #ifndef _POSIX_SOURCE
2 #define _POSIX_SOURCE /* For getaddrinfo(3) */
3 #endif
5 #ifndef _BSD_SOURCE
6 #define _BSD_SOURCE /* For NI_MAXHOST up to glibc-2.19 */
7 #endif
8 #ifndef _DEFAULT_SOURCE
9 #define _DEFAULT_SOURCE /* For NI_MAXHOST since glibc-2.20 */
10 #endif
12 #ifndef _XOPEN_SOURCE
13 #define _XOPEN_SOURCE 600 /* For unsetenv(3) */
14 #endif
16 #include "../test.h"
17 #include "server.h"
18 #include "isds.h"
20 static const char *username = "Doug1as$";
21 static const char *password = "42aA#bc8";
24 static int test_login(const isds_error error, struct isds_ctx *context,
25 const char *url, const char *username, const char *password,
26 const struct isds_pki_credentials *pki_credentials,
27 struct isds_otp *otp) {
28 isds_error err;
30 err = isds_login(context, url, username, password, pki_credentials, otp);
31 if (error != err)
32 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
33 isds_strerror(error), isds_strerror(err),
34 isds_long_message(context));
36 PASS_TEST;
39 static int test_isds_ping(const isds_error error, struct isds_ctx *context) {
40 isds_error err;
42 err = isds_ping(context);
44 if (error != err) {
45 FAIL_TEST("Wrong return code: expected=%s, returned=%s (%s)",
46 isds_strerror(error), isds_strerror(err),
47 isds_long_message(context));
50 PASS_TEST;
54 int main(int argc, char **argv) {
55 int error;
56 pid_t server_process;
57 struct isds_ctx *context = NULL;
58 char *url = NULL;
60 INIT_TEST("isds_ping");
62 if (unsetenv("http_proxy")) {
63 ABORT_UNIT("Could not remove http_proxy variable from environment\n");
65 if (isds_init()) {
66 isds_cleanup();
67 ABORT_UNIT("isds_init() failed\n");
69 context = isds_ctx_create();
70 if (!context) {
71 isds_cleanup();
72 ABORT_UNIT("isds_ctx_create() failed\n");
76 const struct service_configuration services[] = {
77 { SERVICE_DS_Dz_DummyOperation, NULL },
78 { SERVICE_END, NULL }
80 const struct arguments_basic_authentication server_arguments = {
81 .username = username,
82 .password = password,
83 .isds_deviations = 1,
84 .services = services
87 error = start_server(&server_process, &url,
88 server_basic_authentication, &server_arguments, NULL);
89 if (error == -1) {
90 isds_ctx_free(&context);
91 isds_cleanup();
92 ABORT_UNIT(server_error);
95 TEST("prior logging in", test_isds_ping,
96 IE_CONNECTION_CLOSED, context);
97 TEST("login", test_login, IE_SUCCESS,
98 context, url, username, password, NULL, NULL);
99 TEST("after logging in", test_isds_ping,
100 IE_SUCCESS, context);
102 /* Terminate server without loggin out, we will exhibit calls on the
103 * logged-in context later. */
104 if (stop_server(server_process)) {
105 ABORT_UNIT(server_error);
107 free(url);
108 url = NULL;
111 /* Shorten network timeout to make tests faster */
112 if (isds_set_timeout(context, 500) != IE_SUCCESS) {
113 ABORT_UNIT("isds_set_timeout() failed");
115 TEST("when server disappears", test_isds_ping,
116 IE_NETWORK, context);
118 isds_logout(context);
119 TEST("after logging out", test_isds_ping,
120 IE_CONNECTION_CLOSED, context);
122 isds_ctx_free(&context);
123 isds_cleanup();
124 SUM_TEST();