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 "risk_analysis_manager_fuzzer.h"
17 
18 #include <string>
19 
20 #include "risk_analysis_manager_callback_service.h"
21 #include "risk_analysis_manager_service.h"
22 #include "security_guard_log.h"
23 
24 
25 namespace OHOS::Security::SecurityGuard {
26 RiskAnalysisManagerService g_service(RISK_ANALYSIS_MANAGER_SA_ID, true);
27 constexpr int32_t REMAINDER_VALUE = 2;
28 
OnRemoteRequestFuzzTest(const uint8_t * data,size_t size)29 void OnRemoteRequestFuzzTest(const uint8_t* data, size_t size)
30 {
31     MessageParcel datas;
32     MessageParcel reply;
33     MessageOption option;
34     datas.WriteInterfaceToken(IRiskAnalysisManager::GetDescriptor());
35     if (size % REMAINDER_VALUE == 0) {
36         // handle get security model result cmd
37         uint32_t modelId = static_cast<uint32_t>(size);
38         std::string deviceId(reinterpret_cast<const char *>(data), size);
39         datas.WriteString(deviceId);
40         datas.WriteUint32(modelId);
41         ResultCallback func = [] (const std::string &devId, uint32_t modelId, const std::string &result) -> int32_t {
42             SGLOGI("RiskAnalysisManagerCallbackService called");
43             return 0;
44         };
45         sptr<IRemoteObject> callback = new (std::nothrow) RiskAnalysisManagerCallbackService(func);
46         datas.WriteRemoteObject(callback);
47         g_service.OnRemoteRequest(RiskAnalysisManagerStub::CMD_GET_SECURITY_MODEL_RESULT, datas, reply, option);
48         return;
49     }
50     // handle set model state cmd
51     uint32_t modelId = static_cast<uint32_t>(size);
52     datas.WriteUint32(modelId);
53     datas.WriteBool(size % REMAINDER_VALUE == 0);
54     g_service.OnRemoteRequest(RiskAnalysisManagerStub::CMD_SET_MODEL_STATE, datas, reply, option);
55 }
56 }  // namespace OHOS::Security::SecurityGuard
57 
58 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
60 {
61     /* Run your code on date */
62     OHOS::Security::SecurityGuard::OnRemoteRequestFuzzTest(data, size);
63     return 0;
64 }
65