test: online/login describes returned error code
[libisds.git] / client / getreceivedsigneddelivery.c
blob300253110473a9c9e3e105566a7b297614fa2c91
1 #define _XOPEN_SOURCE 500
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <locale.h>
5 #include <time.h>
6 #include <string.h>
7 #include <isds.h>
8 #include "common.h"
11 int main(int argc, char **argv) {
12 struct isds_ctx *ctx = NULL;
13 isds_error err;
14 char *last_message_id = NULL;
16 setlocale(LC_ALL, "");
18 err = isds_init();
19 if (err) {
20 printf("isds_init() failed: %s\n", isds_strerror(err));
21 exit(EXIT_FAILURE);
24 isds_set_logging(ILF_ALL & ~ILF_HTTP, ILL_ALL);
26 ctx = isds_ctx_create();
27 if (!ctx) {
28 printf("isds_ctx_create() failed");
31 err = isds_set_timeout(ctx, 10000);
32 if (err) {
33 printf("isds_set_timeout() failed: %s\n", isds_strerror(err));
36 /* err = isds_set_tls(ctx, ITLS_VERIFY_SERVER, 0);
37 if (err) {
38 printf("isds_set_tls(ITLS_VERIFY_SERVER) failed: %s\n",
39 isds_strerror(err));
42 err = isds_set_tls(ctx, ITLS_CA_FILE, "/etc/ssl/certs/ca-certificates.crt");
43 if (err) {
44 printf("isds_set_tls(ITLS_CA_FILE) failed: %s\n",
45 isds_strerror(err));
46 }*/
48 err = isds_login(ctx, url, username, password, NULL);
49 if (err) {
50 printf("isds_login() failed: %s: %s\n", isds_strerror(err),
51 isds_long_message(ctx));
52 } else {
53 printf("Logged in :)\n");
58 /* Get list of received messages */
60 struct tm from_time_tm = {
61 .tm_year = 2000 - 1900,
62 .tm_mon = 1 - 1,
63 .tm_mday = 1,
64 .tm_hour = 1,
65 .tm_min = 2,
66 .tm_sec = 3,
67 .tm_isdst = -1
69 time_t from_time_t = mktime(&from_time_tm);
70 struct timeval from_time = {
71 .tv_sec = from_time_t,
72 .tv_usec = 4000
74 unsigned long int number = 0;
75 struct isds_list *messages = NULL, *item;
76 struct isds_message *last_message = NULL;
78 /* TODO: Try different criteria */
79 printf("Getting list of received messages\n");
80 err = isds_get_list_of_received_messages(ctx, &from_time, NULL, NULL,
81 MESSAGESTATE_ANY, 0, &number, &messages);
82 if (err)
83 printf("isds_get_list_of_received_messages() failed: %s: %s\n",
84 isds_strerror(err), isds_long_message(ctx));
85 else {
86 printf("isds_get_list_of_received_messages() succeeded: "
87 "number of messages = %lu\n", number);
88 for(item = messages; item; item = item->next) {
89 last_message = (struct isds_message *) (item->data);
94 if (last_message) {
95 /*Save last message for latter refference */
96 if (last_message->envelope && last_message->envelope->dmID) {
97 last_message_id = strdup(last_message->envelope->dmID);
101 isds_list_free(&messages);
105 /* Download last message signed delivery info */
106 if (last_message_id) {
107 struct isds_message *message = NULL;
109 printf("Getting last received message signed delivery info with ID: "
110 "%s\n", last_message_id);
111 err = isds_get_signed_delivery_info(ctx, last_message_id, &message);
112 if (err)
113 printf("isds_get_signed_delivery_info() failed: %s: %s\n",
114 isds_strerror(err), isds_long_message(ctx));
115 else {
116 printf("isds_get_signed_delivery_info() succeeded:\n");
117 print_message(message);
118 save_data("Saving signed delivery info",
119 message->raw, message->raw_length);
122 isds_message_free(&message);
123 free(last_message_id);
127 /* Download signed delivery info with invalid ID */
129 struct isds_message *message = NULL;
130 char *id = "123456789112345678921";
132 printf("Getting signed delivery info with invalid ID: %s\n", id);
133 err = isds_get_signed_delivery_info(ctx, id, &message);
134 if (err)
135 printf("isds_get_signed_delivery_info() failed as assumed: %s: "
136 "%s\n", isds_strerror(err), isds_long_message(ctx));
137 else {
138 printf("isds_get_signed_delivery_info() succeeded. "
139 "This should not happen:\n");
140 print_message(message);
143 isds_message_free(&message);
147 /* Download nonexistent signed delivery info */
149 struct isds_message *message = NULL;
150 char *id = "7777777";
152 printf("Getting nonexistent signed delivery info with ID: %s\n", id);
153 err = isds_get_signed_delivery_info(ctx, id, &message);
154 if (err)
155 printf("isds_get_signed_delivery_info() failed as assumed: %s: "
156 "%s\n", isds_strerror(err), isds_long_message(ctx));
157 else {
158 printf("isds_get_signed_delivery_info() succeeded. "
159 "This should not happen:\n");
160 print_message(message);
163 isds_message_free(&message);
167 err = isds_logout(ctx);
168 if (err) {
169 printf("isds_logout() failed: %s\n", isds_strerror(err));
173 err = isds_ctx_free(&ctx);
174 if (err) {
175 printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
179 err = isds_cleanup();
180 if (err) {
181 printf("isds_cleanup() failed: %s\n", isds_strerror(err));
184 exit (EXIT_SUCCESS);