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 "processdeathobserver_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 static sptr<IRemoteObject> g_remote = new (std::nothrow) IPCObjectStub();
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 = "CreateDataChannelStubFuzzTest",
56         .aplStr = "system_core",
57     };
58     uint64_t tokenId = GetAccessTokenId(&infoInstance);
59     SetSelfTokenID(tokenId);
60     AccessTokenKit::ReloadNativeTokenInfo();
61     delete[] perms;
62 }
63 
ProcessDeathObserverFuzzTest(const uint8_t * data,size_t size)64 bool ProcessDeathObserverFuzzTest(const uint8_t *data, size_t size)
65 {
66     SetUpTestCase();
67     if (g_remote == nullptr) {
68         return false;
69     }
70     g_service->ProcessDeathObserver(g_remote);
71     return true;
72 }
73 }  // namespace Sensors
74 }  // namespace OHOS
75 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78     /* Run your code on data */
79     if (data == nullptr) {
80         return 0;
81     }
82 
83     /* Validate the length of size */
84     if (size < OHOS::Sensors::U32_AT_SIZE) {
85         return 0;
86     }
87 
88     OHOS::Sensors::ProcessDeathObserverFuzzTest(data, size);
89     return 0;
90 }
91