Added new user api unit test for the new fields.
[libgcal.git] / utests / utest_debug.c
blob25f0be475c95f2b46234fb7a6d51503dfe027f54
1 /*
2 * @file utest_debug.c
3 * @author Adenilson Cavalcanti
4 * @date Fri Jun 13 14:53:21 2008
6 * @brief Module for libgcal debug functions.
9 */
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <string.h>
18 #include "utest_debug.h"
19 #include "gcal.h"
20 #include "gcontact.h"
21 #include "gcal_status.h"
22 #include "utils.h"
24 static struct gcal_resource *ptr_gcal = NULL;
26 static void setup(void)
28 /* here goes any common data allocation */
29 ptr_gcal = gcal_construct(GCONTACT);
32 static void teardown(void)
34 /* and here we clean up */
35 gcal_destroy(ptr_gcal);
38 START_TEST (test_debug_authenticate)
41 int result, code;
43 result = gcal_get_authentication(ptr_gcal, "gcal4tester", "66libgcal");
44 fail_if(result != 0, "Authentication should work!");
46 code = gcal_status_httpcode(ptr_gcal);
47 fail_if(code != 200, "Reported HTTP code should be 200!");
48 fail_if(gcal_status_msg(ptr_gcal) != NULL, "There should be no msg!");
50 result = gcal_get_authentication(ptr_gcal, "gcal4tester", "failfail");
51 fail_if(result == 0, "Authentication must fail!");
53 code = gcal_status_httpcode(ptr_gcal);
54 fail_if(code != 403, "Reported HTTP code should be 403!");
57 END_TEST
59 START_TEST (test_debug_logfile)
61 int result;
62 size_t length;
63 char *file_path = "/tmp/libgcal.log";
64 char *error_line = "code: 403";
65 char *file_content = NULL;
66 int fd;
68 result = gcal_status_setlog(ptr_gcal, file_path);
69 fail_if(result == -1, "Failed setting log file!");
71 result = gcal_get_authentication(ptr_gcal, "gcal4tester", "failfail");
72 fail_if(result == 0, "Authentication must fail!");
74 /* This triggers the file closing */
75 gcal_destroy(ptr_gcal);
76 ptr_gcal = NULL;
78 /* Read file and check for error msg */
79 fd = open(file_path, O_RDONLY);
80 fail_if(fd == -1, "Cannot open log file!");
81 result = read_file(fd, &file_content, &length);
82 fail_if(result, "Failed reading log file!");
84 fail_if((!(strstr(file_content, error_line))), "Cannot find HTTP error"
85 " message!");
87 free(file_content);
88 close(fd);
90 END_TEST
93 TCase *gcaldebug_tcase_create(void)
95 TCase *tc = NULL;
96 int timeout_seconds = 50;
98 tc = tcase_create("gcaldebug");
99 tcase_add_checked_fixture(tc, setup, teardown);
100 tcase_set_timeout (tc, timeout_seconds);
102 tcase_add_test(tc, test_debug_authenticate);
103 tcase_add_test(tc, test_debug_logfile);
105 return tc;