• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "unsubscribeappaccountstub_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";
UnSubscribeAppAccountStubFuzzTest(const uint8_t * data,size_t size)47 bool UnSubscribeAppAccountStubFuzzTest(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 
57     AppAccountSubscribeInfo subscribeInfo;
58     std::shared_ptr<AppAccountSubscriberTest> appAccountSubscriberPtr =
59         std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
60     auto appAccountEventListenerSptr = new (std::nothrow) AppAccountEventListener(appAccountSubscriberPtr);
61 
62     if (!dataTemp.WriteRemoteObject(appAccountEventListenerSptr->AsObject())) {
63         return false;
64     }
65 
66     MessageParcel reply;
67     MessageOption option;
68     uint32_t code = static_cast<uint32_t>(AppAccountInterfaceCode::UNSUBSCRIBE_ACCOUNT);
69     auto appAccountManagerService = std::make_shared<AppAccountManagerService>();
70     appAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
71 
72     return true;
73 }
74 }
75 
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79     /* Run your code on data */
80     OHOS::UnSubscribeAppAccountStubFuzzTest(data, size);
81     return 0;
82 }
83 
84