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 "startusingpermissioncallbackstub_fuzzer.h"
17 
18 #include <string>
19 #include <thread>
20 #include <vector>
21 
22 #include "accesstoken_fuzzdata.h"
23 #undef private
24 #include "i_privacy_manager.h"
25 #include "state_change_callback.h"
26 #include "state_customized_cbk.h"
27 #include "privacy_manager_service.h"
28 
29 using namespace std;
30 using namespace OHOS::Security::AccessToken;
31 
32 class CbCustomizeTest : public StateCustomizedCbk {
33 public:
CbCustomizeTest()34     explicit CbCustomizeTest() : StateCustomizedCbk()
35     {
36     }
37 
~CbCustomizeTest()38     ~CbCustomizeTest()
39     {}
40 
StateChangeNotify(AccessTokenID tokenId,bool isShowing)41     virtual void StateChangeNotify(AccessTokenID tokenId,  bool isShowing)
42     {
43         isShowing_ = true;
44     }
45 
46     bool isShowing_ = false;
47 };
48 
49 namespace OHOS {
StartUsingPermissionCallbackStubFuzzTest(const uint8_t * data,size_t size)50     bool StartUsingPermissionCallbackStubFuzzTest(const uint8_t* data, size_t size)
51     {
52         if ((data == nullptr) || (size == 0)) {
53             return false;
54         }
55 
56         AccessTokenFuzzData fuzzData(data, size);
57 
58         sptr<StateChangeCallback> callbackWrap = nullptr;
59         auto callback = std::make_shared<CbCustomizeTest>();
60         callbackWrap = new (std::nothrow) StateChangeCallback(callback);
61 
62         MessageParcel datas;
63         datas.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
64         if (!datas.WriteUint32(static_cast<AccessTokenID>(fuzzData.GetData<uint32_t>()))) {
65             return false;
66         }
67         if (!datas.WriteInt32(fuzzData.GetData<int32_t>())) {
68             return false;
69         }
70         if (!datas.WriteString(fuzzData.GenerateRandomString())) {
71             return false;
72         }
73         if (!datas.WriteRemoteObject(callbackWrap->AsObject())) {
74             return false;
75         }
76 
77         uint32_t code = static_cast<uint32_t>(PrivacyInterfaceCode::START_USING_PERMISSION_CALLBACK);
78 
79         MessageParcel reply;
80         MessageOption option;
81         DelayedSingleton<PrivacyManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
82 
83         return true;
84     }
85 } // namespace OHOS
86 
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89 {
90     /* Run your code on data */
91     OHOS::StartUsingPermissionCallbackStubFuzzTest(data, size);
92     return 0;
93 }
94