• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     EdmPermission edmPermission;
52     edmPermission.permissionName_ = fuzzString;
53     std::vector<EdmPermission> reqPermissions = { edmPermission };
54     ManagedEvent event = GetData<ManagedEvent>();
55     Admin admin;
56     EntInfo entInfo;
57     entInfo.enterpriseName = fuzzString;
58     entInfo.description = fuzzString;
59     InitAdminParam(admin, fuzzString, entInfo, event);
60     std::shared_ptr<Admin> adminPtr = std::make_shared<Admin>(admin);
61     std::vector<std::shared_ptr<Admin>> adminPtrVec = { adminPtr };
62     int32_t eventId = CommonFuzzer::GetU32Data(data);
63     std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> subscribeAdmins;
64     subscribeAdmins[eventId] = adminPtrVec;
65     int32_t userId = CommonFuzzer::GetU32Data(data);
66     std::string bundleName(reinterpret_cast<const char*>(data), size);
67     AdminType role = GetData<AdminType>();
68     std::string packageName(reinterpret_cast<const char*>(data), size);
69     std::vector<std::string> packageNameList = { packageName };
70     std::string subAdminName(reinterpret_cast<const char*>(data), size);
71     std::shared_ptr<Admin> subOrSuperAdmin = std::make_shared<Admin>(admin);
72     std::string parentName(reinterpret_cast<const char*>(data), size);
73     std::vector<std::string> subAdmins = { fuzzString };
74     uint32_t fuzzEvent = GetData<uint32_t>();
75     std::vector<uint32_t> events = { fuzzEvent };
76     adminManager->GetAdminBySubscribeEvent(event, subscribeAdmins);
77     adminManager->SetAdminValue(userId, admin);
78     adminManager->GetReqPermission(permissions, reqPermissions);
79     adminManager->GetAdminByPkgName(packageName, userId);
80     adminManager->DeleteAdmin(packageName, userId);
81     adminManager->GetGrantedPermission(permissions, role);
82     adminManager->IsSuperAdminExist();
83     adminManager->IsSuperAdmin(bundleName);
84     adminManager->IsSuperOrSubSuperAdmin(bundleName);
85     adminManager->GetEnabledAdmin(role, packageNameList, userId);
86     adminManager->GetSubOrSuperAdminByPkgName(subAdminName, subOrSuperAdmin);
87     adminManager->GetSubSuperAdminsByParentName(parentName, subAdmins);
88     adminManager->GetEntInfo(packageName, entInfo, userId);
89     adminManager->SetEntInfo(packageName, entInfo, userId);
90     adminManager->SaveSubscribeEvents(events, bundleName, userId);
91     adminManager->RemoveSubscribeEvents(events, bundleName, userId);
92     adminManager->SaveAuthorizedAdmin(bundleName, permissions, parentName);
93     adminManager->GetSuperAdmin();
94 }
95 
96 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
98 {
99     if (data == nullptr) {
100         return 0;
101     }
102     if (size < MIN_SIZE) {
103         return 0;
104     }
105     g_data = data;
106     g_size = size;
107     g_pos = 0;
108 
109     DoSomethingInterestingWithMyAPI(data, size);
110     return 0;
111 }
112 } // namespace EDM
113 } // namespace OHOS