1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifdef OHOS_ENABLE_PARAMETER
16 #undef _GNU_SOURCE
17 #include <hilog/hilog_adapter.c>
18 #define _GNU_SOURCE
19 #include <musl_log.h>
20 #include <test.h>
21 #endif
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <stdio.h>
25
26 #define OVERWRITE 1
27 #define ZERO 0
28 #define INVALID_ENV (-1)
29 #define VALID_ENV 0
30 #define ENABLE_LOG "param set musl.log.enable true"
31 #define LOG_LEVEL_FATAL "param set musl.log.level FATAL"
32 #define LOG_LEVEL_WARN "param set musl.log.level WARN"
33 #define LOG_LEVEL_ERROR "param set musl.log.level ERROR"
34 #define LOG_LEVEL_DEBUG "param set musl.log.level DEBUG"
35 #define LOG_LEVEL_INFO "param set musl.log.level INFO"
36
TestValidEnv(const char *str)37 int TestValidEnv(const char *str)
38 {
39 FILE *res = popen(str, "r");
40 if (res == NULL) {
41 return INVALID_ENV;
42 }
43 char path[1035];
44 while (fgets(path, sizeof(path), res) != NULL) {
45 if (strstr(path, "fail")) {
46 return INVALID_ENV;
47 }
48 }
49 pclose(res);
50 return VALID_ENV;
51 }
52
TestFatalLevelnull53 void TestFatalLevel()
54 {
55 int result = TestValidEnv(LOG_LEVEL_FATAL);
56 if (result == INVALID_ENV) {
57 return;
58 }
59 musl_log_reset();
60 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_FATAL)) {
61 t_error("LOG_LEVEL_FATAL level LOG_FATAL print failed \n");
62 }
63 if (HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_ERROR)) {
64 t_error("LOG_LEVEL_FATAL level LOG_ERROR print failed \n");
65 }
66 if (HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_WARN)) {
67 t_error("LOG_LEVEL_FATAL level LOG_WARN print failed \n");
68 }
69 if (HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_INFO)) {
70 t_error("LOG_LEVEL_FATAL level LOG_INFO print failed \n");
71 }
72 if (HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_DEBUG)) {
73 t_error("LOG_LEVEL_FATAL level LOG_DEBUG print failed \n");
74 }
75 }
76
TestErrorLevelnull77 void TestErrorLevel()
78 {
79 int result = TestValidEnv(LOG_LEVEL_ERROR);
80 if (result == INVALID_ENV) {
81 return;
82 }
83 musl_log_reset();
84 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_FATAL)) {
85 t_error("LOG_LEVEL_ERROR level LOG_FATAL print failed \n");
86 }
87 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_ERROR)) {
88 t_error("LOG_LEVEL_ERROR level LOG_ERROR print failed \n");
89 }
90 if (HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_WARN)) {
91 t_error("LOG_LEVEL_ERROR level LOG_WARN print failed \n");
92 }
93 if (HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_INFO)) {
94 t_error("LOG_LEVEL_ERROR level LOG_INFO print failed \n");
95 }
96 if (HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_DEBUG)) {
97 t_error("LOG_LEVEL_ERROR level LOG_DEBUG print failed \n");
98 }
99 }
100
TestWarnLevelnull101 void TestWarnLevel()
102 {
103 int result = TestValidEnv(LOG_LEVEL_WARN);
104 if (result == INVALID_ENV) {
105 return;
106 }
107 musl_log_reset();
108 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_FATAL)) {
109 t_error("LOG_LEVEL_WARN level LOG_FATAL print failed \n");
110 }
111 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_ERROR)) {
112 t_error("LOG_LEVEL_WARN level LOG_ERROR print failed \n");
113 }
114 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_WARN)) {
115 t_error("LOG_LEVEL_WARN level LOG_WARN print failed \n");
116 }
117 if (HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_INFO)) {
118 t_error("LOG_LEVEL_WARN level LOG_INFO print failed \n");
119 }
120 if (HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_DEBUG)) {
121 t_error("LOG_LEVEL_WARN level LOG_DEBUG print failed \n");
122 }
123 }
124
TestInfoLevelnull125 void TestInfoLevel()
126 {
127 int result = TestValidEnv(LOG_LEVEL_INFO);
128 if (result == INVALID_ENV) {
129 return;
130 }
131 musl_log_reset();
132 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_FATAL)) {
133 t_error("LOG_LEVEL_INFO level LOG_FATAL print failed \n");
134 }
135 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_ERROR)) {
136 t_error("LOG_LEVEL_INFO level LOG_ERROR print failed \n");
137 }
138 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_WARN)) {
139 t_error("LOG_LEVEL_INFO level LOG_WARN print failed \n");
140 }
141 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_INFO)) {
142 t_error("LOG_LEVEL_INFO level LOG_INFO print failed \n");
143 }
144 if (HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_DEBUG)) {
145 t_error("LOG_LEVEL_INFO level LOG_DEBUG print failed \n");
146 }
147 }
148
TestDebugLevelnull149 void TestDebugLevel()
150 {
151 int result = TestValidEnv(LOG_LEVEL_DEBUG);
152 if (result == INVALID_ENV) {
153 return;
154 }
155 musl_log_reset();
156 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_FATAL)) {
157 t_error("LOG_LEVEL_DEBUG level LOG_FATAL print failed \n");
158 }
159 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_ERROR)) {
160 t_error("LOG_LEVEL_DEBUG level LOG_ERROR print failed \n");
161 }
162 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_WARN)) {
163 t_error("LOG_LEVEL_DEBUG level LOG_WARN print failed \n");
164 }
165 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_INFO)) {
166 t_error("LOG_LEVEL_DEBUG level LOG_INFO print failed \n");
167 }
168 if (!HiLogAdapterIsLoggable(MUSL_LOG_DOMAIN, MUSL_LOG_TAG, LOG_DEBUG)) {
169 t_error("LOG_LEVEL_DEBUG level LOG_DEBUG print failed \n");
170 }
171 }
172
mainnull173 int main()
174 {
175 #ifdef OHOS_ENABLE_PARAMETER
176 int result = TestValidEnv(ENABLE_LOG);
177 if (result == INVALID_ENV) {
178 return ZERO;
179 }
180 TestFatalLevel();
181 TestErrorLevel();
182 TestWarnLevel();
183 TestInfoLevel();
184 TestDebugLevel();
185 #endif
186 return t_status;
187 }