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 "playprimitiveeffect_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 namespace Sensors {
27 using namespace Security::AccessToken;
28 using Security::AccessToken::AccessTokenID;
29 
30 namespace {
31 constexpr size_t DATA_MIN_SIZE = 20;
32 constexpr char END_CHAR = '\0';
33 constexpr size_t LEN = 20;
34 } // namespace
35 
36 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)37 size_t GetObject(const uint8_t *data, size_t size, T &object)
38 {
39     size_t objectSize = sizeof(object);
40     if (objectSize > size) {
41         return 0;
42     }
43     errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
44     if (ret != EOK) {
45         return 0;
46     }
47     return objectSize;
48 }
49 
SetUpTestCase()50 void SetUpTestCase()
51 {
52     const char **perms = new (std::nothrow) const char *[1];
53     if (perms == nullptr) {
54         return;
55     }
56     perms[0] = "ohos.permission.VIBRATE";
57     TokenInfoParams infoInstance = {
58         .dcapsNum = 0,
59         .permsNum = 1,
60         .aclsNum = 0,
61         .dcaps = nullptr,
62         .perms = perms,
63         .acls = nullptr,
64         .processName = "PlayPrimitiveEffectFuzzTest",
65         .aplStr = "system_core",
66     };
67     uint64_t tokenId = GetAccessTokenId(&infoInstance);
68     SetSelfTokenID(tokenId);
69     AccessTokenKit::ReloadNativeTokenInfo();
70     delete[] perms;
71 }
72 
PlayPrimitiveEffectFuzzTest(const uint8_t * data,size_t size)73 void PlayPrimitiveEffectFuzzTest(const uint8_t *data, size_t size)
74 {
75     SetUpTestCase();
76     if (data == nullptr || size < DATA_MIN_SIZE) {
77         return;
78     }
79     size_t startPos = 0;
80     char effectId[LEN + 1];
81     effectId[LEN] = END_CHAR;
82     for (size_t i = 0; i < LEN; ++i) {
83         startPos += GetObject<char>(data + startPos, size - startPos, effectId[i]);
84     }
85     int32_t intensity { 0 };
86     OHOS::Sensors::PlayPrimitiveEffect(effectId, intensity);
87 }
88 } // namespace Sensors
89 } // namespace OHOS
90 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
92 {
93     OHOS::Sensors::PlayPrimitiveEffectFuzzTest(data, size);
94     return 0;
95 }