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