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
16 #include "getdelaytime_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 #include <string>
22
23 #include "accesstoken_kit.h"
24 #include "nativetoken_kit.h"
25 #include "securec.h"
26 #include "token_setproc.h"
27
28 #include "vibrator_agent.h"
29
30 namespace OHOS {
31 using namespace Security::AccessToken;
32 using Security::AccessToken::AccessTokenID;
33
SetUpTestCase()34 void SetUpTestCase()
35 {
36 const char **perms = new (std::nothrow) const char *[1];
37 if (perms == nullptr) {
38 return;
39 }
40 perms[0] = "ohos.permission.VIBRATE";
41 TokenInfoParams infoInstance = {
42 .dcapsNum = 0,
43 .permsNum = 1,
44 .aclsNum = 0,
45 .dcaps = nullptr,
46 .perms = perms,
47 .acls = nullptr,
48 .processName = "GetDelayTimeFuzzTest",
49 .aplStr = "system_core",
50 };
51 uint64_t tokenId = GetAccessTokenId(&infoInstance);
52 SetSelfTokenID(tokenId);
53 AccessTokenKit::ReloadNativeTokenInfo();
54 delete[] perms;
55 }
56
GetDelayTimeFuzzTest(const uint8_t * data,size_t size)57 bool GetDelayTimeFuzzTest(const uint8_t *data, size_t size)
58 {
59 SetUpTestCase();
60 std::string argv(reinterpret_cast<const char *>(data), size);
61 int32_t delayTime = static_cast<int32_t>(std::atoi(argv.c_str()));
62 int32_t ret = OHOS::Sensors::GetDelayTime(delayTime);
63 if (ret == 0) {
64 return false;
65 }
66 return true;
67 }
68 } // namespace OHOS
69
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
71 {
72 OHOS::GetDelayTimeFuzzTest(data, size);
73 return 0;
74 }