• 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 "authenticate_fuzzer.h"
17 
18 #include <cstddef>
19 #include <string>
20 #include <vector>
21 #include "app_account_authenticator_callback_stub.h"
22 #include "app_account_manager.h"
23 #include "account_log_wrapper.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 {
AuthenticateFuzzTest(const uint8_t * data,size_t size)78     bool AuthenticateFuzzTest(const uint8_t* data, size_t size)
79     {
80         bool result = false;
81         if (size > 0) {
82             FuzzData fuzzData(data, size);
83             std::string testName(fuzzData.GenerateString());
84             std::string testOwner(fuzzData.GenerateString());
85             std::string testAuthType(fuzzData.GenerateString());
86             std::string testKey(fuzzData.GenerateString());
87             std::string testValue(fuzzData.GenerateString());
88             AAFwk::Want options;
89             options.SetParam(testKey, testValue);
90             sptr<IAppAccountAuthenticatorCallback> callback = nullptr;
91             uint32_t number = fuzzData.GetData<uint32_t>() % CONSTANTS_NUMBER_THREE;
92             if (number == CONSTANTS_NUMBER_ONE) {
93                 callback = new (std::nothrow) MockAuthenticatorCallback();
94             } else if (number == CONSTANTS_NUMBER_TWO) {
95                 callback = new (std::nothrow) MockAuthenticatorCallbackStub();
96             }
97             result = AppAccountManager::Authenticate(testName, testOwner, testAuthType, options, callback);
98         }
99         return result;
100     }
101 }
102 
103 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)104 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
105 {
106     /* Run your code on data */
107     OHOS::AuthenticateFuzzTest(data, size);
108     return 0;
109 }
110 
111