1 /*
2 * Copyright (c) 2025 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
16 #include "commoncomplog_fuzzer.h"
17 #include "common_components/log/log.h"
18 #include "test/fuzztest/containersdequecommon_fuzzer/containersdequecommon_fuzzer.h"
19
20 using namespace common;
21
22 namespace OHOS {
23
CommonCompFormatLog(const uint8_t * data,size_t size)24 void CommonCompFormatLog(const uint8_t* data, size_t size)
25 {
26 FormatLog("%d", data[size / 2]); // 2:Check size is even number
27 }
28
CommonCompPrettyOrderMathNano(const uint8_t * data,size_t size)29 void CommonCompPrettyOrderMathNano(const uint8_t* data, size_t size)
30 {
31 const char* orderOfMagnitudeFromNano[] = { "n", "u", "m" };
32 if (size == 0) {
33 return;
34 }
35 PrettyOrderMathNano(size, orderOfMagnitudeFromNano[data[size - 1] % 3]); // 3: len of orderOfMagnitudeFromNano
36 }
37
CommonCompPrettyOrderInfo(const uint8_t * data,size_t size)38 void CommonCompPrettyOrderInfo(const uint8_t* data, size_t size)
39 {
40 const char* orderOfMagnitude[] = { "", "K", "M", "G", "T", "P", "E" };
41 if (size == 0) {
42 return;
43 }
44 PrettyOrderInfo(size, orderOfMagnitude[data[size - 1] % 7]); // 7:len of orderOfMagnitude
45 }
46
CommonCompPretty(const uint8_t * data,size_t size)47 void CommonCompPretty(const uint8_t* data, size_t size)
48 {
49 if (size == 0) {
50 return;
51 }
52 Pretty(data[size - 1]);
53 }
54
CommonCompLevelToString(const uint8_t * data,size_t size)55 void CommonCompLevelToString(const uint8_t* data, size_t size)
56 {
57 if (size == 0) {
58 return;
59 }
60 Log log;
61 log.LevelToString(static_cast<Level>(data[size - 1] % 7)); // 7: FATAL + 1
62 }
63
CommonCompConvertFromRuntime(const uint8_t * data,size_t size)64 void CommonCompConvertFromRuntime(const uint8_t* data, size_t size)
65 {
66 if (size == 0) {
67 return;
68 }
69 Log log;
70 log.ConvertFromRuntime(static_cast<LOG_LEVEL>(data[size - 1] % 5)); // 5: LOG_LEVEL + 1
71 }
72 }
73
74 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77 // Run your code on data.
78 OHOS::CommonCompFormatLog(data, size);
79 OHOS::CommonCompPrettyOrderMathNano(data, size);
80 OHOS::CommonCompPrettyOrderInfo(data, size);
81 OHOS::CommonCompPretty(data, size);
82 OHOS::CommonCompLevelToString(data, size);
83 OHOS::CommonCompConvertFromRuntime(data, size);
84
85 return 0;
86 }