1 /*
2  * Copyright (c) 2022 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 "timereceivedmessage_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21 #include <string_ex.h>
22 
23 #include "accesstoken_kit.h"
24 #include "message_parcel.h"
25 #include "nativetoken_kit.h"
26 #include "time_system_ability.h"
27 #include "token_setproc.h"
28 
29 #define private public
30 #define protected public
31 #include "sntp_client.h"
32 
33 using namespace OHOS::MiscServices;
34 
35 namespace OHOS {
36 constexpr size_t THRESHOLD = 4;
37 constexpr int32_t NTP_PACKAGE_SIZE = 48;
38 const std::u16string TIMESERVICE_INTERFACE_TOKEN = u"ohos.miscservices.time.ITimeService";
39 
FuzzTimeDump(const uint8_t * rawData,size_t size)40 bool FuzzTimeDump(const uint8_t *rawData, size_t size)
41 {
42     std::vector<std::u16string> args;
43     std::string str(reinterpret_cast<const char *>(rawData), size);
44     args.push_back(Str8ToStr16(str));
45     int fd = 0;
46     TimeSystemAbility::GetInstance()->Dump(fd, args);
47     return true;
48 }
49 
FuzzTimeReceivedMessage(const uint8_t * data,size_t size)50 bool FuzzTimeReceivedMessage(const uint8_t *data, size_t size)
51 {
52     char buffer[NTP_PACKAGE_SIZE] = { 0 };
53     if (memcpy_s(buffer, NTP_PACKAGE_SIZE, data, size) != EOK) {
54         return true;
55     }
56     auto client = std::make_shared<SNTPClient>();
57     client->ReceivedMessage(buffer);
58     return true;
59 }
60 } // namespace OHOS
61 
62 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
64 {
65     if (size < OHOS::THRESHOLD) {
66         return 0;
67     }
68     /* Run your code on data */
69     OHOS::FuzzTimeDump(data, size);
70     OHOS::FuzzTimeReceivedMessage(data, size);
71     return 0;
72 }