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 #include "admin_manager_fuzzer.h"
17 
18 #include "common_fuzzer.h"
19 #include "edm_ipc_interface_code.h"
20 #include "func_code.h"
21 #include "get_data_template.h"
22 #include "message_parcel.h"
23 #define private public
24 #include "admin_manager.h"
25 #undef private
26 #include "ienterprise_device_mgr.h"
27 
28 
29 namespace OHOS {
30 namespace EDM {
31 constexpr size_t MIN_SIZE = 64;
32 
InitAdminParam(Admin & admin,std::string fuzzString,EntInfo entInfo,ManagedEvent event)33 void InitAdminParam(Admin &admin, std::string fuzzString, EntInfo entInfo, ManagedEvent event)
34 {
35     AdminInfo fuzzAdminInfo;
36     fuzzAdminInfo.packageName_ = fuzzString;
37     fuzzAdminInfo.className_ = fuzzString;
38     fuzzAdminInfo.entInfo_ = entInfo;
39     fuzzAdminInfo.permission_ = { fuzzString };
40     fuzzAdminInfo.managedEvents_ = { event };
41     fuzzAdminInfo.parentAdminName_ = fuzzString;
42     admin.adminInfo_ = fuzzAdminInfo;
43 }
44 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)45 void DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
46 {
47     std::shared_ptr<AdminManager> adminManager = AdminManager::GetInstance();
48     adminManager->Init();
49     std::string fuzzString(reinterpret_cast<const char*>(data), size);
50     std::vector<std::string> permissions = { fuzzString };
51     ManagedEvent event = GetData<ManagedEvent>();
52     Admin admin;
53     EntInfo entInfo;
54     entInfo.enterpriseName = fuzzString;
55     entInfo.description = fuzzString;
56     InitAdminParam(admin, fuzzString, entInfo, event);
57     std::shared_ptr<Admin> adminPtr = std::make_shared<Admin>(admin);
58     std::vector<std::shared_ptr<Admin>> adminPtrVec = { adminPtr };
59     int32_t eventId = CommonFuzzer::GetU32Data(data);
60     std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> subscribeAdmins;
61     subscribeAdmins[eventId] = adminPtrVec;
62     int32_t userId = CommonFuzzer::GetU32Data(data);
63     std::string bundleName(reinterpret_cast<const char*>(data), size);
64     AdminType role = GetData<AdminType>();
65     std::string packageName(reinterpret_cast<const char*>(data), size);
66     std::vector<std::string> packageNameList = { packageName };
67     std::string subAdminName(reinterpret_cast<const char*>(data), size);
68     std::shared_ptr<Admin> subOrSuperAdmin = std::make_shared<Admin>(admin);
69     std::string parentName(reinterpret_cast<const char*>(data), size);
70     std::vector<std::string> subAdmins = { fuzzString };
71     uint32_t fuzzEvent = GetData<uint32_t>();
72     std::vector<uint32_t> events = { fuzzEvent };
73     adminManager->GetAdminBySubscribeEvent(event, subscribeAdmins);
74     adminManager->SetAdminValue(userId, admin);
75     adminManager->GetAdminByPkgName(packageName, userId);
76     adminManager->DeleteAdmin(packageName, userId);
77     adminManager->IsSuperAdminExist();
78     adminManager->IsSuperAdmin(bundleName);
79     adminManager->IsSuperOrSubSuperAdmin(bundleName);
80     adminManager->GetEnabledAdmin(role, packageNameList, userId);
81     adminManager->GetSubOrSuperAdminByPkgName(subAdminName, subOrSuperAdmin);
82     adminManager->GetSubSuperAdminsByParentName(parentName, subAdmins);
83     adminManager->GetEntInfo(packageName, entInfo, userId);
84     adminManager->SetEntInfo(packageName, entInfo, userId);
85     adminManager->SaveSubscribeEvents(events, bundleName, userId);
86     adminManager->RemoveSubscribeEvents(events, bundleName, userId);
87     adminManager->GetSuperAdmin();
88 }
89 
90 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93     if (data == nullptr) {
94         return 0;
95     }
96     if (size < MIN_SIZE) {
97         return 0;
98     }
99     g_data = data;
100     g_size = size;
101     g_pos = 0;
102 
103     DoSomethingInterestingWithMyAPI(data, size);
104     return 0;
105 }
106 } // namespace EDM
107 } // namespace OHOS