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