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