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