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 "getpropertystub_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 MockGetSetPropCallback : public OHOS::AccountSA::GetSetPropCallback {
31 public:
~MockGetSetPropCallback()32 virtual ~MockGetSetPropCallback() {}
OnResult(int32_t result,const Attributes & extraInfo)33 void OnResult(int32_t result, const Attributes &extraInfo) override
34 {
35 return;
36 }
37 };
38
GetPropertyStubFuzzTest(const uint8_t * data,size_t size)39 bool GetPropertyStubFuzzTest(const uint8_t *data, size_t size)
40 {
41 if ((data == nullptr) || (size == 0)) {
42 return false;
43 }
44
45 int32_t userId = static_cast<int32_t>(size);
46 AuthType authType = static_cast<AuthType>(size);
47 std::vector<Attributes::AttributeKey> keys = {static_cast<Attributes::AttributeKey>(size)};
48 GetPropertyRequest request = {
49 .authType = authType,
50 .keys = keys,
51 };
52 std::shared_ptr<GetSetPropCallback> ptr = make_shared<MockGetSetPropCallback>();
53 sptr<IGetSetPropCallback> callback = new (std::nothrow) GetSetPropCallbackService(ptr);
54
55 MessageParcel dataTemp;
56 if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
57 return false;
58 }
59
60 if (!dataTemp.WriteInt32(userId)) {
61 return false;
62 }
63
64 if (!dataTemp.WriteInt32(request.authType)) {
65 return false;
66 }
67 std::vector<uint32_t> attrKeys;
68 std::transform(request.keys.begin(), request.keys.end(), std::back_inserter(attrKeys),
69 [](const auto &key) { return static_cast<uint32_t>(key); });
70
71 if (!dataTemp.WriteUInt32Vector(attrKeys)) {
72 return false;
73 }
74 if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
75 return false;
76 }
77
78 MessageParcel reply;
79 MessageOption option;
80 uint32_t code = static_cast<uint32_t>(AccountIAMInterfaceCode::GET_PROPERTY);
81 auto iamAccountManagerService = std::make_shared<AccountIAMService>();
82 iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
83
84 return true;
85 }
86 } // namespace OHOS
87
88 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
90 {
91 /* Run your code on data */
92 OHOS::GetPropertyStubFuzzTest(data, size);
93 return 0;
94 }
95