test: Clean context and library globals when aborting simline tests
[libisds.git] / test / simline / isds_delete_message_from_storage.c
blob029ec89e85191d377c0c2ff34e68444d6dce9b32
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_delete_message_from_storage(const isds_error error,
40 struct isds_ctx *context, const char *message_id, _Bool incoming) {
41 isds_error err;
43 err = isds_delete_message_from_storage(context, message_id, incoming);
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));
49 PASS_TEST;
52 int main(int argc, char **argv) {
53 int error;
54 pid_t server_process;
55 struct isds_ctx *context = NULL;
56 char *url = NULL;
58 INIT_TEST("isds_delete_message_from_storage");
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_Dx_EraseMessage service_arguments = {
75 .message_id = "1234567",
76 .incoming = 1
78 const struct service_configuration services[] = {
79 { SERVICE_DS_Dz_DummyOperation, NULL },
80 { SERVICE_DS_Dx_EraseMessage, &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, &url,
90 server_basic_authentication, &server_arguments, NULL);
91 if (error == -1) {
92 isds_ctx_free(&context);
93 isds_cleanup();
94 ABORT_UNIT(server_error);
97 TEST("prior logging in", test_isds_delete_message_from_storage,
98 IE_CONNECTION_CLOSED, context, "1234567", 1);
99 TEST("login", test_login, IE_SUCCESS,
100 context, url, username, password, NULL, NULL);
101 TEST("bad message ID", test_isds_delete_message_from_storage, IE_INVAL,
102 context, "1000000", 1);
103 TEST("bad direction", test_isds_delete_message_from_storage, IE_INVAL,
104 context, "1234567", 0);
105 TEST("good ID and direction", test_isds_delete_message_from_storage,
106 IE_SUCCESS, context, "1234567", 1);
108 if (stop_server(server_process)) {
109 isds_ctx_free(&context);
110 isds_cleanup();
111 ABORT_UNIT(server_error);
114 free(url);
115 url = NULL;
119 isds_logout(context);
120 isds_ctx_free(&context);
121 isds_cleanup();
122 SUM_TEST();