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