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