• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "setpropertystub_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 
39 
SetPropertyStubFuzzTest(const uint8_t * data,size_t size)40 bool SetPropertyStubFuzzTest(const uint8_t *data, size_t size)
41 {
42     if ((data == nullptr) || (size == 0)) {
43         return false;
44     }
45 
46     int32_t userId = static_cast<int32_t>(size);
47     std::vector<uint8_t> attr = {static_cast<uint8_t>(size)};
48     SetPropertyRequest request = {
49         .authType = static_cast<AuthType>(size),
50         .mode = static_cast<PropertyMode>(size),
51         .attrs = Attributes(attr),
52     };
53     std::shared_ptr<GetSetPropCallback> ptr = make_shared<MockGetSetPropCallback>();
54     sptr<IGetSetPropCallback> callback = new (std::nothrow) GetSetPropCallbackService(ptr);
55 
56     MessageParcel dataTemp;
57     if (!dataTemp.WriteInterfaceToken(IAMACCOUNT_TOKEN)) {
58         return false;
59     }
60 
61     if (!dataTemp.WriteInt32(userId)) {
62         return false;
63     }
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 
76     MessageParcel reply;
77     MessageOption option;
78     uint32_t code = static_cast<uint32_t>(AccountIAMInterfaceCode::SET_PROPERTY);
79     auto iamAccountManagerService = std::make_shared<AccountIAMService>();
80     iamAccountManagerService->OnRemoteRequest(code, dataTemp, reply, option);
81 
82     return true;
83 }
84 } // namespace OHOS
85 
86 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)87 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
88 {
89     /* Run your code on data */
90     OHOS::SetPropertyStubFuzzTest(data, size);
91     return 0;
92 }
93