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 "registerpermactivestatuscallbackstub_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 "perm_active_status_change_callback.h"
26 #include "perm_active_status_customized_cbk.h"
27 #include "privacy_manager_service.h"
28 
29 using namespace std;
30 using namespace OHOS::Security::AccessToken;
31 
32 class RegisterActiveFuzzTest : public PermActiveStatusCustomizedCbk {
33 public:
RegisterActiveFuzzTest(const std::vector<std::string> & permList)34     explicit RegisterActiveFuzzTest(const std::vector<std::string> &permList)
35         : PermActiveStatusCustomizedCbk(permList)
36     {}
37 
~RegisterActiveFuzzTest()38     ~RegisterActiveFuzzTest()
39     {}
40 
ActiveStatusChangeCallback(ActiveChangeResponse & result)41     virtual void ActiveStatusChangeCallback(ActiveChangeResponse& result)
42     {
43         type_ = result.type;
44         return;
45     }
46 
47     ActiveChangeType type_ = PERM_INACTIVE;
48 };
49 
50 namespace OHOS {
RegisterPermActiveStatusCallbackStubFuzzTest(const uint8_t * data,size_t size)51     bool RegisterPermActiveStatusCallbackStubFuzzTest(const uint8_t* data, size_t size)
52     {
53         if ((data == nullptr) || (size == 0)) {
54             return false;
55         }
56 
57         AccessTokenFuzzData fuzzData(data, size);
58 
59         std::vector<std::string> permList = {fuzzData.GenerateRandomString()};
60         auto callback = std::make_shared<RegisterActiveFuzzTest>(permList);
61         callback->type_ = PERM_INACTIVE;
62         sptr<PermActiveStatusChangeCallback> callbackWrap = nullptr;
63         callbackWrap = new (std::nothrow) PermActiveStatusChangeCallback(callback);
64 
65         MessageParcel datas;
66         datas.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
67         if (!datas.WriteString(permList[0])) {
68             return false;
69         }
70         if (!datas.WriteInt32(permList.size())) {
71             return false;
72         }
73         if (!datas.WriteRemoteObject(callbackWrap->AsObject())) {
74             return false;
75         }
76 
77         uint32_t code = static_cast<uint32_t>(PrivacyInterfaceCode::REGISTER_PERM_ACTIVE_STATUS_CHANGE_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::RegisterPermActiveStatusCallbackStubFuzzTest(data, size);
92     return 0;
93 }
94