tests: zlogger nominally runs the daemon
[platform/core/system/dlog.git] / tests / test_filters.c
1 #define LOG_TAG "XXX"
2
3 // C
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 // POSIX
9 #include <fcntl.h>
10 #include <pthread.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13
14 // DLOG
15 #include <dlog.h>
16 #include <dynamic_config.h>
17 #include <logcommon.h>
18 #include <loglimiter.h>
19
20 #define DYNAMIC_CONF "/tmp/dlog-filters/" DYNAMIC_CONFIG_FILENAME
21 #define DYNAMIC_CONF_ALTER "/tmp/" DYNAMIC_CONFIG_FILENAME "_ALTER" // Weird but ok
22 #define BUF_LEN (MAX_CONF_KEY_LEN + MAX_CONF_VAL_LEN + 3) // +3 because KEY=VAL\n\0
23
24 #define FILTER_ALLOW (__LOG_LIMITER_LIMIT_MAX + 1)
25 #define FILTER_DENY 0
26
27 // limit values same as in loglimiter
28 int set_config(const char *filename, int flag, char prio, const char *tag, int limit)
29 {
30         assert(tag);
31         assert(flag == O_APPEND || flag == O_TRUNC);
32
33         char buf[BUF_LEN];
34         const int flags = O_CREAT | O_WRONLY | flag;
35         int r;
36         ssize_t s;
37
38         __attribute__((cleanup(close_fd))) const int fd = open(filename, flags, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
39         if (fd < 0) {
40                 int ret = -errno;
41                 perror("error opening dynamic conf file");
42                 return ret;
43         }
44
45         if (limit == FILTER_ALLOW)
46                 r = snprintf(buf, sizeof buf, "limiter|%s|%c=allow\n", tag, prio);
47         else if (limit == FILTER_DENY)
48                 r = snprintf(buf, sizeof buf, "limiter|%s|%c=deny\n", tag, prio);
49         else
50                 r = snprintf(buf, sizeof buf, "limiter|%s|%c=%d\n", tag, prio, limit);
51         if (r < 0) {
52                 r = -errno;
53                 perror("snprintf error");
54                 return r;
55         }
56
57         s = write(fd, buf, r);
58         if (s < 0) {
59                 r = -errno;
60                 perror("error writing config file");
61         } else
62                 r = 0;
63
64         return r;
65 }
66
67 int erase_file(const char *filename)
68 {
69         const int fd = open(filename, O_RDWR|O_TRUNC);
70         if (fd < 0) {
71                 int ret = -errno;
72                 perror("error opening dynamic conf file");
73                 return ret;
74         }
75
76         assert(!close(fd));
77
78         return 0;
79 }
80
81 int main(int argc, char **argv)
82 {
83         // test allow all filter
84         if (set_config(DYNAMIC_CONF, O_TRUNC, '*', "*", FILTER_ALLOW) < 0)
85                 return EXIT_FAILURE;
86         for (int i = 0; i < 3; i++)
87                 RLOGE("[1] ALLOWED message %d\n", i);
88
89         // test deny all filter
90         if (set_config(DYNAMIC_CONF, O_TRUNC, '*', "*", FILTER_DENY) < 0)
91                 return EXIT_FAILURE;
92         for (int i = 0; i < 3; i++)
93                 RLOGE("[2] DENIED message %d\n", i);
94
95         // still deny all, but allow by tag filter
96         if (set_config(DYNAMIC_CONF, O_APPEND, '*', "YYY", FILTER_ALLOW) < 0)
97                 return EXIT_FAILURE;
98         if (set_config(DYNAMIC_CONF_ALTER, O_TRUNC, '*', "ZZZ", FILTER_ALLOW) < 0)
99                 return EXIT_FAILURE;
100
101         for (int i = 0; i < 3; i++)
102                 RLOG(LOG_ERROR, "YYY", "[3] ALLOWED message %d\n", i);
103         for (int i = 0; i < 3; i++)
104                 RLOG(LOG_ERROR, "ZZZ", "[4] DENIED message %d\n", i);
105         for (int i = 0; i < 3; i++)
106                 RLOGE("[5] DENIED message %d\n", i);
107
108
109         // test deny all but allow by priority filter
110         if (set_config(DYNAMIC_CONF, O_TRUNC,  '*', "*", FILTER_DENY)  < 0
111         ||  set_config(DYNAMIC_CONF, O_APPEND, 'W', "*", FILTER_ALLOW) < 0)
112                 return EXIT_FAILURE;
113         for (int i = 0; i < 3; i++)
114                 RLOG(LOG_WARN, "ZZZ", "[6] ALLOWED message %d\n", i);
115         for (int i = 0; i < 3; i++)
116                 RLOG(LOG_DEBUG, "ZZZ", "[7] DENIED message %d\n", i);
117
118         // test time based limiter
119         static const int TIMED_LIMIT = 2;
120         if (set_config(DYNAMIC_CONF, O_TRUNC, '*', "*", TIMED_LIMIT) < 0)
121                 return EXIT_FAILURE;
122         for (int i = 0; i < 10; i++)
123                 RLOG(LOG_DEBUG, "AAA", "[8] %s message %d/%d\n", i < TIMED_LIMIT ? "ALLOWED" : "DENIED", i + 1, TIMED_LIMIT);
124
125         if (erase_file(DYNAMIC_CONF_ALTER) < 0)
126                 return EXIT_FAILURE;
127         if (erase_file(DYNAMIC_CONF) < 0)
128                 return EXIT_FAILURE;
129         return EXIT_SUCCESS;
130 }