1 /*
2  * Copyright (c) 2023-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 "subscribeappaccountstub_fuzzer.h"
17 
18 #include <string>
19 #include <vector>
20 #include "account_log_wrapper.h"
21 #include "app_account_event_listener.h"
22 #include "app_account_manager_service.h"
23 #include "app_account_subscribe_info.h"
24 #include "app_account_subscriber.h"
25 #include "iapp_account.h"
26 #include "fuzz_data.h"
27 
28 using namespace std;
29 using namespace OHOS::AccountSA;
30 class AppAccountSubscriberTest : public AppAccountSubscriber {
31 public:
AppAccountSubscriberTest(const AppAccountSubscribeInfo & subscribeInfo)32     explicit AppAccountSubscriberTest(const AppAccountSubscribeInfo &subscribeInfo)
33         : AppAccountSubscriber(subscribeInfo)
34     {
35         ACCOUNT_LOGI("enter");
36     }
37 
~AppAccountSubscriberTest()38     ~AppAccountSubscriberTest()
39     {}
40 
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)41     virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
42     {
43         ACCOUNT_LOGI("enter");
44     }
45 };
46 namespace OHOS {
47 const std::u16string APPACCOUNT_TOKEN = u"ohos.accountfwk.IAppAccount";
SubscribeAppAccountStubFuzzTest(const uint8_t * data,size_t size)48 bool SubscribeAppAccountStubFuzzTest(const uint8_t* data, size_t size)
49 {
50     if ((data == nullptr) || (size == 0)) {
51         return false;
52     }
53     MessageParcel dataTemp;
54     if (!dataTemp.WriteInterfaceToken(APPACCOUNT_TOKEN)) {
55         return false;
56     }
57     FuzzData fuzzData(data, size);
58     AppAccountSubscribeInfo subscribeInfo;
59     subscribeInfo.SetOwners({fuzzData.GenerateRandomString()});
60     if (!dataTemp.WriteParcelable(&subscribeInfo)) {
61         return false;
62     }
63     std::shared_ptr<AppAccountSubscriberTest> appAccountSubscriberPtr =
64         std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
65     auto appAccountEventListenerSptr = new (std::nothrow) AppAccountEventListener(appAccountSubscriberPtr);
66     if (!dataTemp.WriteRemoteObject(appAccountEventListenerSptr->AsObject())) {
67         return false;
68     }
69     MessageParcel reply;
70     MessageOption option;
71     uint32_t code = static_cast<uint32_t>(AppAccountInterfaceCode::SUBSCRIBE_ACCOUNT);
72     auto appAccountManagerService = std::make_shared<AppAccountManagerService>();
73     appAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
74     return true;
75 }
76 }
77 
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80 {
81     /* Run your code on data */
82     OHOS::SubscribeAppAccountStubFuzzTest(data, size);
83     return 0;
84 }
85 
86