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 "freevibratorpackage_fuzzer.h"
17
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "securec.h"
21 #include "token_setproc.h"
22
23 #include "vibrator_agent.h"
24
25 namespace OHOS {
26 using namespace Security::AccessToken;
27 using Security::AccessToken::AccessTokenID;
28 namespace {
29 constexpr size_t DATA_MIN_SIZE = 20;
30 } // namespace
31
32 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)33 size_t GetObject(const uint8_t *data, size_t size, T &object)
34 {
35 size_t objectSize = sizeof(object);
36 if (objectSize > size) {
37 return 0;
38 }
39 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
40 if (ret != EOK) {
41 return 0;
42 }
43 return objectSize;
44 }
45
SetUpTestCase()46 void SetUpTestCase()
47 {
48 const char **perms = new (std::nothrow) const char *[1];
49 if (perms == nullptr) {
50 return;
51 }
52 perms[0] = "ohos.permission.VIBRATE";
53 TokenInfoParams infoInstance = {
54 .dcapsNum = 0,
55 .permsNum = 1,
56 .aclsNum = 0,
57 .dcaps = nullptr,
58 .perms = perms,
59 .acls = nullptr,
60 .processName = "FreeVibratorPackageTest",
61 .aplStr = "system_core",
62 };
63 uint64_t tokenId = GetAccessTokenId(&infoInstance);
64 SetSelfTokenID(tokenId);
65 AccessTokenKit::ReloadNativeTokenInfo();
66 delete[] perms;
67 }
68
FreeVibratorPackageFuzzTest(const uint8_t * data,size_t size)69 void FreeVibratorPackageFuzzTest(const uint8_t *data, size_t size)
70 {
71 if (data == nullptr || size < DATA_MIN_SIZE) {
72 return;
73 }
74 SetUpTestCase();
75 VibratorPackage package { 0 };
76 size_t startPos = 0;
77 startPos += GetObject<int32_t>(data + startPos, size - startPos, package.patternNum);
78 GetObject<int32_t>(data + startPos, size - startPos, package.packageDuration);
79 OHOS::Sensors::FreeVibratorPackage(package);
80 }
81 } // namespace OHOS
82
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
84 {
85 OHOS::FreeVibratorPackageFuzzTest(data, size);
86 return 0;
87 }