56ee1b75e945b1c6665b734b855a619d76892ddf
[platform/core/system/dlog.git] / src / tests / log_file.c
1 // C
2 #include <assert.h>
3
4 // DLog
5 #include <log_file.h>
6
7 static bool fail_strdup;
8 static char *strdup_ret;
9 char *__real_strdup(const char *s);
10 char *__wrap_strdup(const char *s)
11 {
12         if (!fail_strdup)
13                 return __real_strdup(s);
14
15         errno = EINPROGRESS;
16         return strdup_ret;
17 }
18
19 static void *expect_free;
20 void __real_free(void *ptr);
21 void __wrap_free(void *ptr)
22 {
23         if (!expect_free)
24                 __real_free(ptr);
25         else
26                 assert(ptr == expect_free);
27 }
28
29 static size_t rename_calls;
30 int __wrap_rename(const char *oldpath, const char *newpath)
31 {
32         ++rename_calls;
33         errno = EDEADLOCK;
34         return -1;
35 }
36
37 int __wrap_dlogutil_entry_get_timestamp(const dlogutil_entry_s *le, dlogutil_sorting_order_e stamp_type, struct timespec *ts)
38 {
39         assert(stamp_type == DLOGUTIL_SORT_SENT_MONO);
40         assert(le == (dlogutil_entry_s *) 0xBA5EBALL);
41         *ts = (struct timespec) {
42                 .tv_sec = 3333,
43                 .tv_nsec = 9999,
44         };
45         return 0;
46 }
47
48 int __wrap_dlogutil_entry_get_tag(const dlogutil_entry_s *entry, const char **tag)
49 {
50         assert(entry == (dlogutil_entry_s *) 0xBA5EBALL);
51         *tag = "";
52         return 0;
53 }
54
55 static bool custom_memcpy;
56 void *__real_memcpy(void *dest, const void *src, size_t n);
57 void *__wrap_memcpy(void *dest, const void *src, size_t n)
58 {
59         if (!custom_memcpy)
60                 return __real_memcpy(dest, src, n);
61
62         assert(src == (void *) 0xBA5EBALL);
63         assert(n == sizeof(dlogutil_entry_s));
64         return dest;
65 }
66
67 static bool fail_snprintf;
68 int __wrap_snprintf(char *str, size_t size, const char *format, ...)
69 {
70         return -fail_snprintf;
71 }
72
73 static int log_print_log_line_ret;
74 static bool log_print_log_line_correct_color;
75 int __wrap_log_print_log_line(struct log_format p_format, int fd, const dlogutil_entry_s *entry)
76 {
77         assert(fd == 0xFD);
78         assert(p_format.color == log_print_log_line_correct_color);
79         return log_print_log_line_ret;
80 }
81
82 static bool fake_open;
83 static const char *open_pathname;
84 static int open_errno;
85 static size_t open_calls;
86 int __real_open(const char *pathname, int flags, mode_t mode);
87 int __wrap_open(const char *pathname, int flags, mode_t mode)
88 {
89         if (!fake_open)
90                 return __real_open(pathname, flags, mode);
91
92         assert(pathname == open_pathname);
93         ++open_calls;
94
95         if (!open_errno)
96                 return 0xFD;
97         errno = open_errno;
98         return -1;
99 }
100
101 int __wrap_open64(const char *pathname, int flags, mode_t mode)
102 {
103         return __wrap_open(pathname, flags, mode);
104 }
105
106 static bool fail_fstat;
107 int __wrap_fstat(int fd, struct stat *buf)
108 {
109         if (fail_fstat)
110                 return -1;
111         buf->st_size = 7654321;
112         return 0;
113 }
114 int __wrap_fstat64(int fd, struct stat *buf)
115 {
116         return __wrap_fstat(fd, buf);
117 }
118
119 static int isatty_ret;
120 int __wrap_isatty(int fd) {
121         return isatty_ret;
122         // We should also set errno, but we won't
123 }
124
125 int main()
126 {
127         struct log_file lf;
128         logfile_init(&lf);
129         assert(lf.fd == -1);
130         assert(!lf.path);
131
132         isatty_ret = 0;
133
134         logfile_set_fd(&lf, -10, 0);
135         assert(lf.fd == -10);
136         assert(!lf.isatty);
137         assert(!lf.should_close);
138
139         isatty_ret = 1;
140         logfile_set_fd(&lf, -10, 0);
141         assert(lf.fd == -10);
142         assert(lf.isatty);
143         assert(!lf.should_close);
144         isatty_ret = 0;
145
146         logfile_set_fd(&lf, 15, 3);
147         assert(lf.fd == 15);
148         assert(!lf.isatty);
149         assert(lf.should_close);
150
151         fail_strdup = true;
152         assert(-EINPROGRESS == logfile_set_path(&lf, "x"));
153         assert(!lf.path);
154
155         expect_free = lf.path = (char *) 0xDEAD50UL;
156         strdup_ret = (char *) 0xF007BALL;
157         assert(!logfile_set_path(&lf, "y"));
158         assert(lf.path == strdup_ret);
159         expect_free = strdup_ret;
160
161         fail_strdup = false;
162         assert(!logfile_set_path(&lf, "pathetic"));
163         assert(!strcmp(lf.path, "pathetic"));
164         expect_free = NULL;
165
166         open_pathname = lf.path;
167         fake_open = true;
168         open_errno = 456;
169         assert(-456 == logfile_open(&lf));
170
171         open_errno = 0;
172         fail_fstat = true;
173         lf.size = 9999;
174         open_calls = 0;
175         assert(!logfile_open(&lf));
176         assert(lf.fd == 0xFD);
177         assert(lf.size == 9999);
178         assert(open_calls == 1);
179         fail_fstat = false;
180
181         open_calls = 0;
182         lf.rotate_size_kbytes = 0;
183         assert(!logfile_open(&lf));
184         assert(lf.fd == 0xFD);
185         assert(lf.size == 7654321);
186         assert(open_calls == 1);
187
188         lf.rotate_size_kbytes = 10;
189         open_calls = 0;
190         lf.max_rotated = 3;
191         assert(1 == logfile_open(&lf));
192         assert(rename_calls == 3);
193         assert(open_calls == 2);
194
195         custom_memcpy = true;
196         lf.prev_sec = 4444;
197         lf.prev_nsec = 6666;
198         isatty_ret = lf.isatty = false;
199         log_print_log_line_ret = -1;
200         log_print_log_line_correct_color = false;
201         assert(logfile_write_with_rotation((dlogutil_entry_s *) 0xBA5EBALL, &lf, DLOGUTIL_SORT_SENT_MONO));
202         assert(lf.prev_sec == 4444);
203         assert(lf.prev_nsec == 6666);
204
205         log_print_log_line_ret = 1;
206         fail_snprintf = true;
207         assert(!logfile_write_with_rotation((dlogutil_entry_s *) 0xBA5EBALL, &lf, DLOGUTIL_SORT_SENT_MONO));
208         assert(lf.prev_sec == 3333);
209         assert(lf.prev_nsec == 9999);
210
211         lf.colors_auto = true;
212         assert(!logfile_write_with_rotation((dlogutil_entry_s *) 0xBA5EBALL, &lf, DLOGUTIL_SORT_SENT_MONO));
213         assert(lf.prev_sec == 3333);
214         assert(lf.prev_nsec == 9999);
215
216         isatty_ret = lf.isatty = true;
217         lf.colors_auto = false;
218         assert(!logfile_write_with_rotation((dlogutil_entry_s *) 0xBA5EBALL, &lf, DLOGUTIL_SORT_SENT_MONO));
219         assert(lf.prev_sec == 3333);
220         assert(lf.prev_nsec == 9999);
221
222         lf.colors_auto = true;
223         log_print_log_line_correct_color = true;
224         assert(!logfile_write_with_rotation((dlogutil_entry_s *) 0xBA5EBALL, &lf, DLOGUTIL_SORT_SENT_MONO));
225         assert(lf.prev_sec == 3333);
226         assert(lf.prev_nsec == 9999);
227
228         isatty_ret = lf.isatty = false;
229         lf.format.color = true;
230         lf.colors_auto = false;
231         assert(!logfile_write_with_rotation((dlogutil_entry_s *) 0xBA5EBALL, &lf, DLOGUTIL_SORT_SENT_MONO));
232         assert(lf.prev_sec == 3333);
233         assert(lf.prev_nsec == 9999);
234
235         lf.colors_auto = true;
236         log_print_log_line_correct_color = false;
237         assert(!logfile_write_with_rotation((dlogutil_entry_s *) 0xBA5EBALL, &lf, DLOGUTIL_SORT_SENT_MONO));
238         assert(lf.prev_sec == 3333);
239         assert(lf.prev_nsec == 9999);
240
241         fail_snprintf = false;
242         assert(!logfile_write_with_rotation((dlogutil_entry_s *) 0xBA5EBALL, &lf, DLOGUTIL_SORT_SENT_MONO));
243         custom_memcpy = false;
244
245         logfile_free(&lf);
246
247         // some extra work is done (by lcov?) after main finishes
248         fake_open = false;
249         expect_free = NULL;
250         open_pathname = NULL;
251 }