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 "setpropertystub_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 
41 
SetPropertyStubFuzzTest(const uint8_t * data,size_t size)42 bool SetPropertyStubFuzzTest(const uint8_t *data, size_t size)
43 {
44     if ((data == nullptr) || (size == 0)) {
45         return false;
46     }
47     FuzzData fuzzData(data, size);
48     int32_t userId = fuzzData.GetData<int32_t>();
49     std::vector<uint8_t> attr = {fuzzData.GetData<uint8_t>()};
50     SetPropertyRequest request = {
51         .authType = static_cast<AuthType>(fuzzData.GenerateRandomEnmu(IAMAuthType::TYPE_END)),
52         .mode = fuzzData.GenerateRandomEnmu(PropertyMode::PROPERTY_MODE_NOTIFY_COLLECTOR_READY),
53         .attrs = Attributes(attr),
54     };
55     std::shared_ptr<GetSetPropCallback> ptr = make_shared<MockGetSetPropCallback>();
56     sptr<IGetSetPropCallback> callback = new (std::nothrow) GetSetPropCallbackService(ptr);
57 
58     MessageParcel dataTemp;
59     if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
60         return false;
61     }
62     if (!dataTemp.WriteInt32(userId)) {
63         return false;
64     }
65     if (!dataTemp.WriteInt32(request.authType)) {
66         return false;
67     }
68     auto buffer = request.attrs.Serialize();
69     if (!dataTemp.WriteUInt8Vector(buffer)) {
70         return false;
71     }
72     if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
73         return false;
74     }
75     MessageParcel reply;
76     MessageOption option;
77     uint32_t code = static_cast<uint32_t>(AccountIAMInterfaceCode::SET_PROPERTY);
78     auto iamAccountManagerService = std::make_shared<AccountIAMService>();
79     iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
80 
81     return true;
82 }
83 } // namespace OHOS
84 
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
87 {
88     /* Run your code on data */
89     OHOS::SetPropertyStubFuzzTest(data, size);
90     return 0;
91 }
92