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 "subscribeappaccountstub_fuzzer.h"
17 #include <string>
18 #include <vector>
19
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
27 using namespace std;
28 using namespace OHOS::AccountSA;
29 class AppAccountSubscriberTest : public AppAccountSubscriber {
30 public:
AppAccountSubscriberTest(const AppAccountSubscribeInfo & subscribeInfo)31 explicit AppAccountSubscriberTest(const AppAccountSubscribeInfo &subscribeInfo)
32 : AppAccountSubscriber(subscribeInfo)
33 {
34 ACCOUNT_LOGI("enter");
35 }
36
~AppAccountSubscriberTest()37 ~AppAccountSubscriberTest()
38 {}
39
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)40 virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
41 {
42 ACCOUNT_LOGI("enter");
43 }
44 };
45 namespace OHOS {
46 const std::u16string APPACCOUNT_TOKEN = u"ohos.accountfwk.IAppAccount";
SubscribeAppAccountStubFuzzTest(const uint8_t * data,size_t size)47 bool SubscribeAppAccountStubFuzzTest(const uint8_t* data, size_t size)
48 {
49 if ((data == nullptr) || (size == 0)) {
50 return false;
51 }
52 MessageParcel dataTemp;
53 if (!dataTemp.WriteInterfaceToken(APPACCOUNT_TOKEN)) {
54 return false;
55 }
56 AppAccountSubscribeInfo subscribeInfo;
57 if (!dataTemp.WriteParcelable(&subscribeInfo)) {
58 return false;
59 }
60
61 std::shared_ptr<AppAccountSubscriberTest> appAccountSubscriberPtr =
62 std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
63 auto appAccountEventListenerSptr = new (std::nothrow) AppAccountEventListener(appAccountSubscriberPtr);
64
65 if (!dataTemp.WriteRemoteObject(appAccountEventListenerSptr->AsObject())) {
66 return false;
67 }
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
75 return true;
76 }
77 }
78
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81 {
82 /* Run your code on data */
83 OHOS::SubscribeAppAccountStubFuzzTest(data, size);
84 return 0;
85 }
86
87