• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "telephony_call_policy_plugin_fuzzer.h"
17 #include <system_ability_definition.h>
18 
19 #define protected public
20 #define private public
21 #include "telephony_call_policy_plugin.h"
22 #undef protected
23 #undef private
24 #include "common_fuzzer.h"
25 #include "edm_ipc_interface_code.h"
26 #include "func_code.h"
27 #include "get_data_template.h"
28 #include "ienterprise_device_mgr.h"
29 #include "message_parcel.h"
30 #include "plugin_manager.h"
31 #include "usb_device_id.h"
32 #include "utils.h"
33 
34 namespace OHOS {
35 namespace EDM {
36 constexpr size_t MIN_SIZE = 24;
37 constexpr int32_t WITHOUT_USERID = 0;
38 
CheckPrivateFunction(const uint8_t * data,const size_t size,int32_t & pos)39 void CheckPrivateFunction(const uint8_t* data, const size_t size, int32_t &pos)
40 {
41     int32_t stringSize = size / 18;
42     int32_t policyFlag = size % 3;
43     TelephonyCallPolicyPlugin plugin;
44     std::vector<std::string> v1 {CommonFuzzer::GetString(data, pos, stringSize, size)};
45     std::vector<std::string> v2 {CommonFuzzer::GetString(data, pos, stringSize, size)};
46     plugin.MergeAndRemoveDuplicates(v1, v2);
47     TelephonyCallPolicyType info1 {v1, policyFlag};
48     TelephonyCallPolicyType info2 {v2, policyFlag};
49     std::string key = CommonFuzzer::GetString(data, pos, stringSize, size);
50     std::map<std::string, TelephonyCallPolicyType> dataMap1 = {{key, info1}};
51     std::map<std::string, TelephonyCallPolicyType> dataMap2 = {{key, info2}};
52     int32_t flag = CommonFuzzer::GetU32Data(data);
53     plugin.IsTrustBlockConflict(key, flag, dataMap1);
54     plugin.CheckIsLimit(key, flag, v2, dataMap1);
55     std::string adminName = CommonFuzzer::GetString(data, pos, stringSize, size);
56     std::string policyData = CommonFuzzer::GetString(data, pos, stringSize, size);
57     std::string mergeData = CommonFuzzer::GetString(data, pos, stringSize, size);
58     int32_t userId = CommonFuzzer::GetU32Data(data);
59     plugin.AddCurrentAndMergePolicy(dataMap1, dataMap2, key, flag, v1);
60     plugin.RemoveCurrentAndMergePolicy(dataMap1, dataMap2, key, flag, v1);
61     int32_t userId100 = 100;
62     plugin.GetOthersMergePolicyData(adminName, userId100, policyData);
63     plugin.OnAdminRemove(adminName, policyData, mergeData, userId);
64     plugin.OnOtherServiceStart(userId);
65 }
66 
67 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70     if (data == nullptr) {
71         return 0;
72     }
73     if (size < MIN_SIZE) {
74         return 0;
75     }
76     int32_t pos = 0;
77     int32_t stringSize = size / 18;
78     int32_t policyFlag = size % 3;
79     for (uint32_t operateType = static_cast<uint32_t>(FuncOperateType::GET);
80         operateType <= static_cast<uint32_t>(FuncOperateType::REMOVE); operateType++) {
81         uint32_t code = EdmInterfaceCode::DISALLOWED_SIM;
82         code = POLICY_FUNC_CODE(operateType, code);
83 
84         AppExecFwk::ElementName admin;
85         admin.SetBundleName(CommonFuzzer::GetString(data, pos, stringSize, size));
86         admin.SetAbilityName(CommonFuzzer::GetString(data, pos, stringSize, size));
87         std::string callType = CommonFuzzer::GetString(data, pos, stringSize, size);
88         MessageParcel parcel;
89         parcel.WriteInterfaceToken(IEnterpriseDeviceMgrIdl::GetDescriptor());
90         parcel.WriteInt32(WITHOUT_USERID);
91         if (operateType) {
92             parcel.WriteParcelable(&admin);
93             parcel.WriteString("");
94             parcel.WriteString(callType);
95             parcel.WriteInt32(policyFlag);
96             std::vector<std::string> numbers {CommonFuzzer::GetString(data, pos, stringSize, size)};
97             parcel.WriteStringVector(numbers);
98         } else {
99             parcel.WriteString("");
100             parcel.WriteInt32(0);
101             parcel.WriteParcelable(&admin);
102             parcel.WriteString(callType);
103             parcel.WriteInt32(policyFlag);
104         }
105         CommonFuzzer::OnRemoteRequestFuzzerTest(code, data, size, parcel);
106     }
107 
108     CheckPrivateFunction(data, size, pos);
109     return 0;
110 }
111 } // namespace EDM
112 } // namespace OHOS