1 /*
2 * Copyright (c) 2023-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 "authuserstub_fuzzer.h"
17
18 #include <string>
19 #include <vector>
20 #include "account_iam_callback_service.h"
21 #include "account_iam_client.h"
22 #include "account_iam_service.h"
23 #include "account_log_wrapper.h"
24 #include "fuzz_data.h"
25 #include "iaccount_iam.h"
26
27 using namespace std;
28 using namespace OHOS::AccountSA;
29 namespace OHOS {
30 const std::u16string IAMACCOUNT_TOKEN = u"ohos.accountfwk.IAccountIAM";
31
32 class MockIDMCallback : public OHOS::AccountSA::IDMCallback {
33 public:
~MockIDMCallback()34 virtual ~MockIDMCallback()
35 {
36 }
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const Attributes & extraInfo)37 void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo) override
38 {
39 return;
40 }
OnResult(int32_t result,const Attributes & extraInfo)41 void OnResult(int32_t result, const Attributes &extraInfo) override
42 {
43 return;
44 }
45 };
46
AuthUserStubFuzzTest(const uint8_t * data,size_t size)47 bool AuthUserStubFuzzTest(const uint8_t *data, size_t size)
48 {
49 if ((data == nullptr) || (size == 0)) {
50 return false;
51 }
52 FuzzData fuzzData(data, size);
53 int32_t userId = fuzzData.GetData<int32_t>();
54 std::vector<uint8_t> challenge = {fuzzData.GetData<uint8_t>()};
55 AuthType authType = static_cast<AuthType>(fuzzData.GenerateRandomEnmu(IAMAuthType::TYPE_END));
56 AuthTrustLevel authTrustLevel = fuzzData.GenerateRandomEnmu(AuthTrustLevel::ATL4);
57 std::shared_ptr<IDMCallback> ptr = make_shared<MockIDMCallback>();
58 sptr<IIDMCallback> callback = new (std::nothrow) IDMCallbackService(userId, ptr);
59
60 MessageParcel dataTemp;
61 if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
62 return false;
63 }
64 if (!dataTemp.WriteInt32(userId)) {
65 return false;
66 }
67 if (!dataTemp.WriteUInt8Vector(challenge)) {
68 return false;
69 }
70 if (!dataTemp.WriteInt32(authType)) {
71 return false;
72 }
73 if (!dataTemp.WriteUint32(authTrustLevel)) {
74 return false;
75 }
76 if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
77 return false;
78 }
79 MessageParcel reply;
80 MessageOption option;
81 uint32_t code = static_cast<uint32_t>(AccountIAMInterfaceCode::AUTH_USER);
82 auto iamAccountManagerService = std::make_shared<AccountIAMService>();
83 iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
84
85 return true;
86 }
87 } // namespace OHOS
88
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
91 {
92 /* Run your code on data */
93 OHOS::AuthUserStubFuzzTest(data, size);
94 return 0;
95 }
96