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 "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 const int MORE_VECTOR_MAX_SIZE = 102401;
32 class AppAccountSubscriberTest : public AppAccountSubscriber {
33 public:
AppAccountSubscriberTest(const AppAccountSubscribeInfo & subscribeInfo)34 explicit AppAccountSubscriberTest(const AppAccountSubscribeInfo &subscribeInfo)
35 : AppAccountSubscriber(subscribeInfo)
36 {
37 ACCOUNT_LOGI("enter");
38 }
39
~AppAccountSubscriberTest()40 ~AppAccountSubscriberTest()
41 {}
42
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)43 virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
44 {
45 ACCOUNT_LOGI("enter");
46 }
47 };
48 namespace OHOS {
49 const std::u16string APPACCOUNT_TOKEN = u"OHOS.AccountSA.IAppAccount";
UnSubscribeAppAccountStubFuzzTest(const uint8_t * data,size_t size)50 bool UnSubscribeAppAccountStubFuzzTest(const uint8_t* data, size_t size)
51 {
52 if ((data == nullptr) || (size == 0)) {
53 return false;
54 }
55 MessageParcel dataTemp;
56 if (!dataTemp.WriteInterfaceToken(APPACCOUNT_TOKEN)) {
57 return false;
58 }
59 FuzzData fuzzData(data, size);
60 bool isWriteSubscribeInfo = fuzzData.GetData<bool>();
61 if (isWriteSubscribeInfo) {
62 AppAccountSubscribeInfo subscribeInfo;
63 subscribeInfo.SetOwners({ fuzzData.GenerateString() });
64 std::shared_ptr<AppAccountSubscriberTest> appAccountSubscriberPtr =
65 std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
66 if (!dataTemp.WriteRemoteObject(AppAccountEventListener::GetInstance()->AsObject())) {
67 return false;
68 }
69 }
70 uint32_t ownerSize = fuzzData.GetData<uint32_t>() % MORE_VECTOR_MAX_SIZE;
71 auto isMoreSize = fuzzData.GetData<bool>();
72 if (isMoreSize) {
73 ownerSize = MORE_VECTOR_MAX_SIZE;
74 }
75 if (!dataTemp.WriteInt32(ownerSize)) {
76 return false;
77 }
78 for (size_t i = 0; i < ownerSize; i++) {
79 if (!dataTemp.WriteString(fuzzData.GenerateString())) {
80 return false;
81 }
82 }
83 MessageParcel reply;
84 MessageOption option;
85 uint32_t code = static_cast<uint32_t>(IAppAccountIpcCode::COMMAND_UNSUBSCRIBE_APP_ACCOUNT);
86 auto appAccountManagerService = std::make_shared<AppAccountManagerService>();
87 appAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
88 return true;
89 }
90 }
91
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
94 {
95 /* Run your code on data */
96 OHOS::UnSubscribeAppAccountStubFuzzTest(data, size);
97 return 0;
98 }
99
100