• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "getdelaytimeenhanced_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 
34 namespace {
35 constexpr size_t U32_AT_SIZE = 12;
36 } // namespace
37 
38 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)39 size_t GetObject(const uint8_t *data, size_t size, T &object)
40 {
41     size_t objectSize = sizeof(object);
42     if (objectSize > size) {
43         return 0;
44     }
45     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
46     if (ret != EOK) {
47         return 0;
48     }
49     return objectSize;
50 }
51 
SetUpTestCase()52 void SetUpTestCase()
53 {
54     const char **perms = new (std::nothrow) const char *[1];
55     if (perms == nullptr) {
56         return;
57     }
58     perms[0] = "ohos.permission.VIBRATE";
59     TokenInfoParams infoInstance = {
60         .dcapsNum = 0,
61         .permsNum = 1,
62         .aclsNum = 0,
63         .dcaps = nullptr,
64         .perms = perms,
65         .acls = nullptr,
66         .processName = "GetDelayTimeEnhancedFuzzTest",
67         .aplStr = "system_core",
68     };
69     uint64_t tokenId = GetAccessTokenId(&infoInstance);
70     SetSelfTokenID(tokenId);
71     AccessTokenKit::ReloadNativeTokenInfo();
72     delete[] perms;
73 }
74 
GetDelayTimeEnhancedFuzzTest(const uint8_t * data,size_t size)75 bool GetDelayTimeEnhancedFuzzTest(const uint8_t *data, size_t size)
76 {
77     if (data == nullptr || size < U32_AT_SIZE) {
78         return false;
79     }
80     SetUpTestCase();
81 
82     VibratorIdentifier identifier;
83     size_t startPos = 0;
84     int32_t delayTime = 0;
85     startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.deviceId);
86     startPos += GetObject<int32_t>(data + startPos, size - startPos, identifier.vibratorId);
87     GetObject<int32_t>(data + startPos, size - startPos, delayTime);
88 
89     int32_t ret = OHOS::Sensors::GetDelayTimeEnhanced(identifier, delayTime);
90     if (ret == 0) {
91         return false;
92     }
93     return true;
94 }
95 } // namespace OHOS
96 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
98 {
99     OHOS::GetDelayTimeEnhancedFuzzTest(data, size);
100     return 0;
101 }