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 "resetsensors_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 "sensor_agent.h"
24 #include "sensor_agent_type.h"
25 #include "sensor_errors.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "ResetSensorsFuzzTest"
29
30 namespace OHOS {
31 namespace Sensors {
32 using namespace OHOS::HiviewDFX;
33 using namespace Security::AccessToken;
34 using Security::AccessToken::AccessTokenID;
35 namespace {
36 constexpr size_t DATA_MIN_SIZE = 4;
37 } // namespace
38
39 template<class T>
GetObject(const uint8_t * data,size_t size,T & object)40 void GetObject(const uint8_t *data, size_t size, T &object)
41 {
42 size_t objectSize = sizeof(object);
43 if (objectSize > size) {
44 return;
45 }
46 errno_t ret = memcpy_s(&object, objectSize, data, objectSize);
47 if (ret != EOK) {
48 return;
49 }
50 }
51
SetUpTestCase()52 void SetUpTestCase()
53 {
54 const char **perms = new (std::nothrow) const char *[2];
55 CHKPV(perms);
56 perms[0] = "ohos.permission.ACCELEROMETER";
57 perms[1] = "ohos.permission.MANAGE_SENSOR";
58 TokenInfoParams infoInstance = {
59 .dcapsNum = 0,
60 .permsNum = 2,
61 .aclsNum = 0,
62 .dcaps = nullptr,
63 .perms = perms,
64 .acls = nullptr,
65 .processName = "ResetSensorsFuzzTest",
66 .aplStr = "system_core",
67 };
68 uint64_t tokenId = GetAccessTokenId(&infoInstance);
69 SetSelfTokenID(tokenId);
70 AccessTokenKit::ReloadNativeTokenInfo();
71 delete[] perms;
72 }
73
ResetSensorsFuzzTest(const uint8_t * data,size_t size)74 void ResetSensorsFuzzTest(const uint8_t *data, size_t size)
75 {
76 if (data == nullptr || size < DATA_MIN_SIZE) {
77 return;
78 }
79 SetUpTestCase();
80 int32_t status { 0 };
81 GetObject<int32_t>(data, size, status);
82 ResetSensors();
83 SetDeviceStatus(status);
84 return;
85 }
86 } // namespace Sensors
87 } // namespace OHOS
88
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
90 {
91 OHOS::Sensors::ResetSensorsFuzzTest(data, size);
92 return 0;
93 }
94