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 #ifndef SAMGR_TEST_FUZZTEST_SYSTEM_ABILITY_MANAGER_FUZZER_UTILS_H
17 #define SAMGR_TEST_FUZZTEST_SYSTEM_ABILITY_MANAGER_FUZZER_UTILS_H
18 #include <cstddef>
19 #include <cstdint>
20 #include "system_ability_status_change_stub.h"
21 #include "system_ability_load_callback_stub.h"
22 #include "system_process_status_change_stub.h"
23 namespace OHOS {
24 namespace Samgr {
25 class FuzzTestUtils {
26 public:
27     static void FuzzTestRemoteRequest(const uint8_t *rawData, size_t size, uint32_t code);
28     static void FuzzTestRemoteRequest(MessageParcel& data, uint32_t code);
BuildBoolFromData(const uint8_t * data,size_t size)29     static bool BuildBoolFromData(const uint8_t* data, size_t size)
30     {
31         if ((data == nullptr) || (size < sizeof(bool))) {
32             return false;
33         }
34         bool boolVal = static_cast<bool>(*data);
35         return boolVal;
36     }
BuildInt32FromData(const uint8_t * data,size_t size)37     static int32_t BuildInt32FromData(const uint8_t* data, size_t size)
38     {
39         if ((data == nullptr) || (size < sizeof(int32_t))) {
40             return 0;
41         }
42         int32_t int32Val = *reinterpret_cast<const int32_t *>(data);
43         return int32Val;
44     }
BuildStringFromData(const uint8_t * data,size_t size)45     static std::string BuildStringFromData(const uint8_t* data, size_t size)
46     {
47         if ((data == nullptr) || (size == 0)) {
48             return "";
49         }
50         std::string strVal(reinterpret_cast<const char *>(data), size);
51         return strVal;
52     }
53 private:
54     static bool IsDmReady();
55     static void AddDeviceManager();
56 };
57 
58 class MockSystemProcessStatusChange : public SystemProcessStatusChangeStub {
59 public:
OnSystemProcessStarted(SystemProcessInfo & systemProcessInfo)60     void OnSystemProcessStarted(SystemProcessInfo& systemProcessInfo) override {}
OnSystemProcessStopped(SystemProcessInfo & systemProcessInfo)61     void OnSystemProcessStopped(SystemProcessInfo& systemProcessInfo) override {}
62 };
63 
64 class MockSystemAbilityStatusChange : public SystemAbilityStatusChangeStub {
65 public:
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)66     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override {}
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)67     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override {}
68 };
69 
70 class MockSystemAbilityLoadCallback : public SystemAbilityLoadCallbackStub {
71 public:
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)72     void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject) override {}
73 
OnLoadSystemAbilityFail(int32_t systemAbilityId)74     void OnLoadSystemAbilityFail(int32_t systemAbilityId) override {}
75 };
76 }
77 } // namespace OHOS
78 
79 #endif
80