1 /*
2  * Copyright (c) 2023 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 "disableactiveinfocbstub_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "accesstoken_kit.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "securec.h"
25 #include "token_setproc.h"
26 
27 #include "sensor.h"
28 #include "sensor_service.h"
29 
30 namespace OHOS {
31 namespace Sensors {
32 using namespace Security::AccessToken;
33 using Security::AccessToken::AccessTokenID;
34 namespace {
35 constexpr size_t U32_AT_SIZE = 4;
36 auto g_service = SensorDelayedSpSingleton<SensorService>::GetInstance();
37 const std::u16string SENSOR_INTERFACE_TOKEN = u"ISensorService";
38 } // namespace
39 
SetUpTestCase()40 void SetUpTestCase()
41 {
42     const char **perms = new (std::nothrow) const char *[2];
43     if (perms == nullptr) {
44         return;
45     }
46     perms[0] = "ohos.permission.ACCELEROMETER";
47     perms[1] = "ohos.permission.MANAGE_SENSOR";
48     TokenInfoParams infoInstance = {
49         .dcapsNum = 0,
50         .permsNum = 2,
51         .aclsNum = 0,
52         .dcaps = nullptr,
53         .perms = perms,
54         .acls = nullptr,
55         .processName = "DisableActiveInfoCBStubFuzzTest",
56         .aplStr = "system_core",
57     };
58     uint64_t tokenId = GetAccessTokenId(&infoInstance);
59     SetSelfTokenID(tokenId);
60     AccessTokenKit::ReloadNativeTokenInfo();
61     delete[] perms;
62 }
63 
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)64 bool OnRemoteRequestFuzzTest(const uint8_t *data, size_t size)
65 {
66     SetUpTestCase();
67     MessageParcel datas;
68     datas.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN);
69     datas.WriteBuffer(data, size);
70     datas.RewindRead(0);
71     MessageParcel reply;
72     MessageOption option;
73     g_service->OnRemoteRequest(static_cast<uint32_t>(SensorInterfaceCode::DISABLE_ACTIVE_INFO_CB),
74         datas, reply, option);
75     return true;
76 }
77 }  // namespace Sensors
78 }  // namespace OHOS
79 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81 {
82     /* Run your code on data */
83     if (data == nullptr) {
84         return 0;
85     }
86 
87     /* Validate the length of size */
88     if (size < OHOS::Sensors::U32_AT_SIZE) {
89         return 0;
90     }
91 
92     OHOS::Sensors::OnRemoteRequestFuzzTest(data, size);
93     return 0;
94 }
95