1 /*
2  * Copyright (c) 2022-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 "subscribeappaccount_fuzzer.h"
17 
18 #include <string>
19 #include <vector>
20 #include "account_log_wrapper.h"
21 #include "app_account_manager.h"
22 #include "app_account_subscribe_info.h"
23 #include "fuzz_data.h"
24 
25 using namespace std;
26 using namespace OHOS::AccountSA;
27 class AppAccountSubscriberTest : public AppAccountSubscriber {
28 public:
AppAccountSubscriberTest(const AppAccountSubscribeInfo & subscribeInfo)29     explicit AppAccountSubscriberTest(const AppAccountSubscribeInfo &subscribeInfo)
30         : AppAccountSubscriber(subscribeInfo)
31     {
32         ACCOUNT_LOGI("enter");
33     }
34 
~AppAccountSubscriberTest()35     ~AppAccountSubscriberTest()
36     {}
37 
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts)38     virtual void OnAccountsChanged(const std::vector<AppAccountInfo> &accounts)
39     {
40         ACCOUNT_LOGI("enter");
41     }
42 };
43 namespace OHOS {
SubscribeAppAccountFuzzTest(const uint8_t * data,size_t size)44     bool SubscribeAppAccountFuzzTest(const uint8_t* data, size_t size)
45     {
46         bool result = false;
47         if (size > 0) {
48             FuzzData fuzzData(data, size);
49             std::string testOwner(fuzzData.GenerateRandomString());
50             std::vector<std::string> owners;
51             owners.emplace_back(testOwner);
52             AppAccountSubscribeInfo subscribeInfo(owners);
53             auto subscriberTestPtr = std::make_shared<AppAccountSubscriberTest>(subscribeInfo);
54             result = AppAccountManager::SubscribeAppAccount(subscriberTestPtr);
55         }
56         return result;
57     }
58 }
59 
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
62 {
63     /* Run your code on data */
64     OHOS::SubscribeAppAccountFuzzTest(data, size);
65     return 0;
66 }
67 
68