1 /*
2 * Copyright (c) 2022-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 "addaccountimplicitly_fuzzer.h"
17
18 #include <string>
19 #include <vector>
20 #include "account_log_wrapper.h"
21 #define private public
22 #include "app_account_authenticator_callback_stub.h"
23 #include "app_account_manager.h"
24 #undef private
25 #include "fuzz_data.h"
26 #include "want.h"
27
28 using namespace std;
29 using namespace OHOS::AccountSA;
30 const int CONSTANTS_NUMBER_ONE = 1;
31 const int CONSTANTS_NUMBER_TWO = 2;
32 const int CONSTANTS_NUMBER_THREE = 3;
33
34 class MockAuthenticatorCallback : public OHOS::AccountSA::IAppAccountAuthenticatorCallback {
35 public:
OnResult(int32_t resultCode,const OHOS::AAFwk::Want & result)36 OHOS::ErrCode OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result) override
37 {
38 return OHOS::ERR_OK;
39 }
OnRequestRedirected(const OHOS::AAFwk::Want & request)40 OHOS::ErrCode OnRequestRedirected(const OHOS::AAFwk::Want& request) override
41 {
42 return OHOS::ERR_OK;
43 }
OnRequestContinued()44 OHOS::ErrCode OnRequestContinued() override
45 {
46 return OHOS::ERR_OK;
47 }
AsObject()48 OHOS::sptr<OHOS::IRemoteObject> AsObject() override
49 {
50 return nullptr;
51 }
52 };
53
54 class MockAuthenticatorCallbackStub final : public AppAccountAuthenticatorCallbackStub {
55 public:
OnResult(int32_t resultCode,const OHOS::AAFwk::Want & result)56 OHOS::ErrCode OnResult(int32_t resultCode, const OHOS::AAFwk::Want &result)
57 {
58 return OHOS::ERR_OK;
59 }
60
OnRequestRedirected(const OHOS::AAFwk::Want & request)61 OHOS::ErrCode OnRequestRedirected(const OHOS::AAFwk::Want &request)
62 {
63 return OHOS::ERR_OK;
64 }
65
OnRequestContinued()66 OHOS::ErrCode OnRequestContinued()
67 {
68 return OHOS::ERR_OK;
69 }
CallbackEnter(uint32_t code)70 OHOS::ErrCode CallbackEnter([[maybe_unused]] uint32_t code)
71 {
72 return OHOS::ERR_OK;
73 }
CallbackExit(uint32_t code,int32_t result)74 OHOS::ErrCode CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result)
75 {
76 return OHOS::ERR_OK;
77 }
78 };
79 namespace OHOS {
AddAccountImplicitlyFuzzTest(const uint8_t * data,size_t size)80 bool AddAccountImplicitlyFuzzTest(const uint8_t* data, size_t size)
81 {
82 bool result = false;
83 if (size > 0) {
84 FuzzData fuzzData(data, size);
85 std::string testOwner(fuzzData.GenerateString());
86 std::string testAuthType(fuzzData.GenerateString());
87 std::string testKey(fuzzData.GenerateString());
88 std::string testValue(fuzzData.GenerateString());
89 AAFwk::Want options;
90 options.SetParam(testKey, testValue);
91 sptr<IAppAccountAuthenticatorCallback> callback = nullptr;
92 uint32_t number = fuzzData.GetData<uint32_t>() % CONSTANTS_NUMBER_THREE;
93 if (number == CONSTANTS_NUMBER_ONE) {
94 callback = new (std::nothrow) MockAuthenticatorCallback();
95 } else if (number == CONSTANTS_NUMBER_TWO) {
96 callback = new (std::nothrow) MockAuthenticatorCallbackStub();
97 }
98 result = AppAccountManager::AddAccountImplicitly(testOwner, testAuthType, options, callback);
99 }
100 return result;
101 }
102 }
103
104 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)105 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
106 {
107 /* Run your code on data */
108 OHOS::AddAccountImplicitlyFuzzTest(data, size);
109 return 0;
110 }
111
112