b7795b906e02bc1055cf896bf3c6351f50002747
[platform/core/system/dlog.git] / src / tests / libdlog_base_wrap.c
1 /*
2  * Copyright (c) 2018-2020, Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 // C
18 #include <assert.h>
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 // DLog
24 #include <dlog.h>
25 #include <libdlog.h>
26 #include <logconfig.h>
27 #include <loglimiter.h>
28
29 void __log_limiter_update(const struct log_config *config) { }
30 void __log_limiter_destroy(void) { }
31 void __dynamic_config_destroy() { }
32
33 static bool destroyed;
34 void destroy()
35 {
36         destroyed = true;
37 }
38
39 int wtl_android(log_id_t buf_id, log_priority pri, const char *tag, const char *msg, struct timespec *tp_mono) { return 45835705; }
40 int wtl_pipe(log_id_t buf_id, log_priority pri, const char *tag, const char *msg, struct timespec *tp_mono) { return 0xDECC16; }
41
42 void __dlog_init_pipe(const struct log_config *conf) { write_to_log = wtl_pipe; }
43 void __dlog_init_android(const struct log_config *conf)
44 {
45         write_to_log = wtl_android;
46         destroy_backend = destroy;
47 }
48
49 static struct log_config CONFIG;
50 static bool fail_conf_read;
51 static bool conf_read_called;
52 int __wrap_log_config_read(struct log_config *config)
53 {
54         conf_read_called = true;
55         config->begin = config->last = NULL;
56
57         if (fail_conf_read)
58                 return -1;
59
60         log_config_copy(config, &CONFIG);
61         return 0;
62 }
63
64 struct pass_log_result limiter_ret;
65 struct pass_log_result __log_limiter_pass_log(const char *tag, int prio) { return limiter_ret; }
66
67 static bool limiter_created;
68 int __log_limiter_create(const struct log_config *config)
69 {
70         limiter_created = true;
71         return 1;
72 }
73
74 static bool use_dynamic_conf;
75 bool __dynamic_config_create(struct log_config *config) { return use_dynamic_conf; }
76
77 static bool dynamic_config_called;
78 void __dynamic_config_update() { dynamic_config_called = true; }
79
80 bool fail_snprintf;
81 int __wrap_snprintf(char *str, size_t size, const char *format, ...)
82 {
83         if (fail_snprintf && !strcmp(format, "enable_%s")) {
84                 errno = -ENAMETOOLONG;
85                 return -1;
86         }
87
88         va_list va;
89         va_start(va, format);
90         const int ret = vsnprintf(str, size, format, va);
91         va_end(va);
92         return ret;
93 }
94
95 int dlog_vprint_wrap_va_list(log_priority pri, const char *tag, const char *msg, ...)
96 {
97         va_list ap;
98         va_start(ap, msg);
99
100         const int ret = dlog_vprint(pri, tag, msg, ap);
101
102         va_end(ap);
103         return ret;
104 }
105
106 void tct_tests()
107 {
108         /* These are a copy of TCT tests:
109          * repository: test/tct/native/api on tizen.org
110          * commit: aa0758c84abc5a10f60df68f4f86efe7dc1216c6, 2019-01-21
111          *
112          * Most of the TCT code for DLog is boilerplate setting up the
113          * framework and such. Since that is quite worthless, this file
114          * only copies the meritum. In particular, there are multiple
115          * calls to dlog_print for reporting test results in the TCT;
116          * it is important to keep this in mind when comparing how much
117          * dlog_print seems to get called in TCT code vs the handful of
118          * lines present in this file, since actual test cases consist
119          * of just the latter.
120          *
121          * The other checks earlier in this file should already catch
122          * everything the TCT tests do but it's good to work with the
123          * exact params used in the TCT tests to handle any edge cases
124          * that might appear and, more importantly, to have this done
125          * at build time since the actual TCT suite is a total hassle
126          * to work with, especially given its simplicity.
127          *
128          * Note that we still use the emulated functions since the real
129          * TCT test is done on a live system; the build environment has
130          * nowhere to actually send the logs and the tests only really
131          * check for parameter correctness. */
132
133         // src/itc/dlog/ITs-dlog.c : ITc_dlog_dlog_print_p
134         assert(dlog_print(DLOG_DEBUG, "DLOG_TEST", "[Dlog_ITC] Dlog Test Message\n") >= 0);
135         assert(dlog_print(DLOG_INFO , "DLOG_TEST", "[Dlog_ITC] Dlog Test Message\n") >= 0);
136         assert(dlog_print(DLOG_WARN , "DLOG_TEST", "[Dlog_ITC] Dlog Test Message\n") >= 0);
137         assert(dlog_print(DLOG_ERROR, "DLOG_TEST", "[Dlog_ITC] Dlog Test Message\n") >= 0);
138
139         // src/itc/dlog/ITs-dlog.c : ITc_dlog_dlog_vprint_p
140         assert(dlog_vprint_wrap_va_list(DLOG_DEBUG, "DLOG_TEST", "[Dlog_ITC] Dlog Test Message\n") >= 0);
141         assert(dlog_vprint_wrap_va_list(DLOG_INFO , "DLOG_TEST", "[Dlog_ITC] Dlog Test Message\n") >= 0);
142         assert(dlog_vprint_wrap_va_list(DLOG_WARN , "DLOG_TEST", "[Dlog_ITC] Dlog Test Message\n") >= 0);
143         assert(dlog_vprint_wrap_va_list(DLOG_ERROR, "DLOG_TEST", "[Dlog_ITC] Dlog Test Message\n") >= 0);
144
145         // src/utc/dlog/utc-SystemFW-dlog-print-func.c
146         assert(dlog_print(DLOG_INFO, "DLOG_TEST", "dlog test message for tct-mgr\n") > 0);
147         assert(dlog_print(-1, NULL, NULL) < 1);
148
149         // src/utc/dlog/utc-SystemFW-dlog-vprint-func.c
150         assert(dlog_vprint_wrap_va_list(DLOG_INFO, "DLOG_TEST", "dlog test message for tct-mgr\n") > 0);
151         assert(dlog_vprint_wrap_va_list(-1, NULL, NULL) < 1);
152 }