12498b56bSopenharmony_ci/*
22498b56bSopenharmony_ci
32498b56bSopenharmony_ciCopyright (c) 2021 Huawei Device Co., Ltd.
42498b56bSopenharmony_ciLicensed under the Apache License, Version 2.0 (the "License");
52498b56bSopenharmony_ciyou may not use this file except in compliance with the License.
62498b56bSopenharmony_ciYou may obtain a copy of the License at
72498b56bSopenharmony_cihttp://www.apache.org/licenses/LICENSE-2.0
82498b56bSopenharmony_ciUnless required by applicable law or agreed to in writing, software
92498b56bSopenharmony_cidistributed under the License is distributed on an "AS IS" BASIS,
102498b56bSopenharmony_ciWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
112498b56bSopenharmony_ciSee the License for the specific language governing permissions and
122498b56bSopenharmony_cilimitations under the License.
132498b56bSopenharmony_ci*/
142498b56bSopenharmony_ci#include "hilogclient_fuzzer.h"
152498b56bSopenharmony_ci
162498b56bSopenharmony_ci#include <cstddef>
172498b56bSopenharmony_ci#include <cstdint>
182498b56bSopenharmony_ci#include <ctime>
192498b56bSopenharmony_ci#include <sys/types.h>
202498b56bSopenharmony_ci#include <unistd.h>
212498b56bSopenharmony_ci#include <sys/syscall.h>
222498b56bSopenharmony_ci#include "hilog/log.h"
232498b56bSopenharmony_ci#include "hilog_common.h"
242498b56bSopenharmony_ci#include "hilog_input_socket_client.h"
252498b56bSopenharmony_ci
262498b56bSopenharmony_cinamespace OHOS {
272498b56bSopenharmony_ci    static const char FUZZ_TEST[] = "fuzz test";
282498b56bSopenharmony_ci    static const uint32_t FUZZ_DOMAIN = 0xD002D01;
292498b56bSopenharmony_ci    bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
302498b56bSopenharmony_ci    {
312498b56bSopenharmony_ci        HilogMsg header = {0};
322498b56bSopenharmony_ci        struct timespec ts = {0};
332498b56bSopenharmony_ci        (void)clock_gettime(CLOCK_REALTIME, &ts);
342498b56bSopenharmony_ci        struct timespec tsMono = {0};
352498b56bSopenharmony_ci        (void)clock_gettime(CLOCK_MONOTONIC, &tsMono);
362498b56bSopenharmony_ci        header.tv_sec = static_cast<uint32_t>(ts.tv_sec);
372498b56bSopenharmony_ci        header.tv_nsec = static_cast<uint32_t>(ts.tv_nsec);
382498b56bSopenharmony_ci        header.mono_sec = static_cast<uint32_t>(tsMono.tv_sec);
392498b56bSopenharmony_ci        header.pid = getprocpid();
402498b56bSopenharmony_ci        header.tid = static_cast<uint32_t>(syscall(SYS_gettid));
412498b56bSopenharmony_ci        header.type = LOG_CORE;
422498b56bSopenharmony_ci        header.level = LOG_INFO;
432498b56bSopenharmony_ci        header.domain = FUZZ_DOMAIN;
442498b56bSopenharmony_ci        int ret = HilogWriteLogMessage(&header, FUZZ_TEST,
452498b56bSopenharmony_ci            strlen(FUZZ_TEST) + 1, reinterpret_cast<const char*>(data), size);
462498b56bSopenharmony_ci        if (ret < 0) {
472498b56bSopenharmony_ci            return false;
482498b56bSopenharmony_ci        }
492498b56bSopenharmony_ci        return true;
502498b56bSopenharmony_ci    }
512498b56bSopenharmony_ci}
522498b56bSopenharmony_ci
532498b56bSopenharmony_ci/* Fuzzer entry point */
542498b56bSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
552498b56bSopenharmony_ci{
562498b56bSopenharmony_ci    if (data == nullptr || size == 0) {
572498b56bSopenharmony_ci        std::cout << "invalid data" << std::endl;
582498b56bSopenharmony_ci        return 0;
592498b56bSopenharmony_ci    }
602498b56bSopenharmony_ci    /* Run your code on data */
612498b56bSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(data, size);
622498b56bSopenharmony_ci    return 0;
632498b56bSopenharmony_ci}