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