1 /*
2 * Copyright (c) 2023-2025 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.AccountSA.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 bool isWriteSubscribeInfo = fuzzData.GetData<bool>();
59 if (isWriteSubscribeInfo) {
60 AppAccountSubscribeInfo subscribeInfo;
61 subscribeInfo.SetOwners({ fuzzData.GenerateString() });
62 std::shared_ptr<AppAccountSubscriberTest> appAccountSubscriberPtr =
63 std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
64 if (!dataTemp.WriteParcelable(&subscribeInfo)) {
65 return false;
66 }
67 }
68 bool isWriteSubscriber = fuzzData.GetData<bool>();
69 if (isWriteSubscriber) {
70 if (!dataTemp.WriteRemoteObject(AppAccountEventListener::GetInstance()->AsObject())) {
71 return false;
72 }
73 }
74 MessageParcel reply;
75 MessageOption option;
76 uint32_t code = static_cast<uint32_t>(IAppAccountIpcCode::COMMAND_SUBSCRIBE_APP_ACCOUNT);
77 auto appAccountManagerService = std::make_shared<AppAccountManagerService>();
78 appAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
79 return true;
80 }
81 }
82
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
85 {
86 /* Run your code on data */
87 OHOS::SubscribeAppAccountStubFuzzTest(data, size);
88 return 0;
89 }
90
91