38fba25c098dd9cc818569afcfd8488ff6f0f464
[platform/core/system/dlog.git] / src / tests / fdi_logger.c
1 // C
2 #include <assert.h>
3
4 // DLog
5 #include <dlog_ioctl.h>
6 #include <fdi_logger.h>
7
8 static int open_buf_ret;
9 static const char *open_buf_path;
10 int __wrap_logger_open_buffer_from_config_get_path(int buf_id, const struct log_config *conf, int open_flags, int *fd, char *actual_path, size_t path_size)
11 {
12         assert(buf_id == LOG_ID_RADIO);
13         assert(conf == (struct log_config *) 0xABDUL);
14         assert(open_flags == O_RDWR);
15
16         if (open_buf_path)
17                 strncpy(actual_path, open_buf_path, path_size);
18         if (open_buf_ret > 0)
19                 *fd = 0xFD;
20         return open_buf_ret;
21 }
22
23 static size_t filters_added;
24 static char *const filter_strs[] = {"H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne"};
25 int __wrap_dlogutil_filter_options_set_filterspec(dlogutil_filter_options_s *p_filter, const char *filterString)
26 {
27         assert(filterString);
28         assert(p_filter == (dlogutil_filter_options_s *) 0xCA5CADE);
29
30         ++filters_added;
31         for (size_t i = 0; i < NELEMS(filter_strs); ++i)
32                 if (!strcmp(filter_strs[i], filterString))
33                         return 0;
34
35         assert(false);
36 }
37
38 void __wrap_parse_androidlogger_message(struct android_logger_entry *ale, dlogutil_entry_s *le, size_t dgram_size) { }
39 void __wrap_copy_recv_timestamp(dlogutil_entry_s *le) { }
40
41 static bool use_real_close;
42 int __real_close(int fd);
43 int __wrap_close(int fd)
44 {
45         if (use_real_close)
46                 return __real_close(fd);
47
48         assert(fd == 0xFD);
49         return 0;
50 }
51
52 static const size_t READS[] = {
53         113, 259, 325, 207, 130, 281, 261, 169, 179, 379, 394, 390, 155, 228, 399,
54         288, 341, 1, 320, 221, 185, 255, 88, 108, 391, 299, 243, 382, 7, 41, 163, 7,
55         62, 288, 279, 350, 139, 29, 167, 28, 230, 172, 383, 329, 369, 263, 391, 37,
56         383, 186, 48, 369, 238, 227, 99, 217, 261, 313, 294, 253, 151, 325, 326, 21,
57         210, 350, 388, 97, 347, 348, 340, 3, 185, 98, 212, 74, 144, 52, 110, 202, 2,
58         202, 282, 103, 90, 150, 182, 145, 104, 159, 49, 69, 69, 69, 163, 184, 358,
59         50, 124, 205, 187, 139, 329, 361, 224, 263, 345, 99, 87, 127, 184, 231, 290,
60         192, 31, 239, 370, 324, 362, 58, 54, 214, 115, 352, 228, 161, 137, 240, 207,
61         273, 200, 154, 364, 310, 38, 332, 92, 334, 238, 52, 181, 205, 343, 312, 230,
62         164, 306, 187, 181, 346, 233, 274, 325, 129, 79, 282, 82, 304, 48, 95, 312,
63         151, 87, 212, 308, 96, 201, 274, 92, 212, 283, 173, 74, 237, 137, 49, 239,
64         155, 192, 222, 88, 267, 229, 383, 96, 3, 46, 146, 182, 182, 155, 151, 106,
65         195, 226, 112, 231, 294, 84, 115, 386, 116, 243, 54, 396, 324, 240, 203, 89,
66 };
67 static size_t read_cnt = 0;
68 static bool use_real_read;
69 static bool fail_read;
70 ssize_t __real_read(int fd, void *buf, size_t count);
71 ssize_t __wrap_read(int fd, void *buf, size_t count)
72 {
73         if (use_real_read)
74                 return __real_read(fd, buf, count);
75
76         if (fail_read) {
77                 errno = 334;
78                 return -1;
79         }
80
81         if (read_cnt == NELEMS(READS))
82                 return 0;
83
84         const size_t current_read = READS[read_cnt++];
85         assert(current_read < count);
86         ((char *)buf)[current_read] = '\0';
87         return current_read;
88 }
89
90 static int fail_malloc;
91 void *__real_malloc(size_t size);
92 void *__wrap_malloc(size_t size)
93 {
94         void *const ret = (fail_malloc & 1) ? NULL : __real_malloc(size);
95         fail_malloc >>= 1;
96         return ret;
97 }
98
99 static bool fail_calloc;
100 void *__real_calloc(size_t nmemb, size_t size);
101 void *__wrap_calloc(size_t nmemb, size_t size)
102 {
103         return fail_calloc ? NULL : __real_calloc(nmemb, size);
104 }
105
106 static int expected_ioctl;
107 static int ioctl_ret;
108 int __real_ioctl(int fd, int request, char *argp);
109 int __wrap_ioctl(int fd, int request, char *argp)
110 {
111         assert(fd == 0xFD);
112         assert(expected_ioctl == request);
113
114         if (ioctl_ret < 0) {
115                 errno = -ioctl_ret;
116                 return -1;
117         }
118
119         return ioctl_ret;
120 }
121
122 int main()
123 {
124         struct fd_info fdi = {
125                 .fd = -1,
126                 .priv_data = NULL,
127         };
128         list_head used_paths = NULL;
129
130         ops_logger.destroy(&fdi);
131
132         fail_malloc = 1;
133         assert(-ENOMEM == ops_logger.create(&fdi, (struct log_config *) 0xABDUL, "radio", &used_paths));
134
135         open_buf_ret = -536;
136         assert(-536 == ops_logger.create(&fdi, (struct log_config *) 0xABDUL, "radio", &used_paths));
137         open_buf_ret = 0;
138         assert(-EINVAL == ops_logger.create(&fdi, (struct log_config *) 0xABDUL, "radio", &used_paths));
139
140         open_buf_path = "abc";
141         open_buf_ret = 1;
142         fail_malloc = 2;
143         assert(-ENOMEM == ops_logger.create(&fdi, (struct log_config *) 0xABDUL, "radio", &used_paths));
144         assert(fdi.fd < 0);
145         assert(!fdi.priv_data);
146
147         fail_calloc = true;
148         assert(-ENOMEM == ops_logger.create(&fdi, (struct log_config *) 0xABDUL, "radio", &used_paths));
149         assert(fdi.fd < 0);
150         assert(!fdi.priv_data);
151         fail_calloc = false;
152
153         assert(!ops_logger.create(&fdi, (struct log_config *) 0xABDUL, "radio", &used_paths));
154         assert(fdi.fd == 0xFD);
155         assert(fdi.priv_data);
156
157         ops_logger.destroy(&fdi);
158         assert(!fdi.priv_data);
159         fdi.fd = -1;
160
161         assert(-EALREADY == ops_logger.create(&fdi, (struct log_config *) 0xABDUL, "radio", &used_paths));
162         assert(fdi.fd == -1);
163         assert(!fdi.priv_data);
164
165         open_buf_path = "def";
166         assert(!ops_logger.create(&fdi, (struct log_config *) 0xABDUL, "radio", &used_paths));
167
168         expected_ioctl = LOGGER_FLUSH_LOG;
169         ioctl_ret = -77;
170         assert(-77 == ops_logger.clear(&fdi));
171         ioctl_ret = 15;
172         assert(0 == ops_logger.clear(&fdi));
173
174         unsigned int sz;
175         expected_ioctl = LOGGER_GET_LOG_BUF_SIZE;
176         ioctl_ret = -EALREADY;
177         assert(-EALREADY == ops_logger.getsize(&fdi, &sz));
178         ioctl_ret = 2019;
179         assert(0 == ops_logger.getsize(&fdi, &sz));
180         assert(2019 == sz);
181
182         expected_ioctl = LOGGER_GET_LOG_LEN;
183         ioctl_ret = -67;
184         assert(-67 == ops_logger.prepare_print(&fdi, 123, false, (dlogutil_filter_options_s *) 0xCA5CADE));
185         expected_ioctl = LOGGER_GET_LOG_LEN;
186         ioctl_ret = 0;
187         assert(1 == ops_logger.prepare_print(&fdi, 123, false, (dlogutil_filter_options_s *) 0xCA5CADE));
188
189         expected_ioctl = -1;
190         assert(!ops_logger.prepare_print(&fdi, DLOGUTIL_MODE_CONTINUOUS, false, (dlogutil_filter_options_s *) 0xCA5CADE));
191
192         fail_read = true;
193         assert(-334 == ops_logger.read(&fdi));
194         fail_read = false;
195
196         for (size_t i = 0; i < NELEMS(READS) + 15; ++i) {
197                 assert(!ops_logger.has_log(&fdi));
198                 assert(NULL == ops_logger.extract_entry(&fdi));
199                 assert(NULL == ops_logger.peek_entry(&fdi));
200                 assert(NULL == ops_logger.extract_entry(&fdi));
201                 fail_malloc = !(rand() % 4) * 3;
202                 ops_logger.read(&fdi);
203                 if (!fail_malloc && i < NELEMS(READS) && READS[i] > sizeof(struct android_logger_entry)) {
204                         assert(ops_logger.has_log(&fdi));
205                         assert(-EAGAIN == ops_logger.read(&fdi));
206                         const dlogutil_entry_s *const le = ops_logger.peek_entry(&fdi);
207                         assert(le);
208                         assert(le == ops_logger.peek_entry(&fdi));
209                         assert(le == ops_logger.extract_entry(&fdi));
210                         free((dlogutil_entry_s *) le);
211                 }
212                 fail_malloc = 0;
213                 assert(!ops_logger.has_log(&fdi));
214                 assert(NULL == ops_logger.peek_entry(&fdi));
215                 assert(NULL == ops_logger.extract_entry(&fdi));
216         }
217
218         ops_logger.destroy(&fdi);
219 }