• 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 "svcrestoredepsmanager_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 #include "securec.h"
22 
23 #include "module_ipc/svc_restore_deps_manager.h"
24 
25 using namespace OHOS::FileManagement::Backup;
26 using namespace std;
27 
28 namespace OHOS {
29 constexpr size_t U32_AT_SIZE = 3;
30 constexpr int SPLITE_SIZE = 5;
31 constexpr size_t REDUNDENT_SIZE = 2;
32 constexpr size_t DISTINCT_SIZE = 2;
GetInfo(const uint8_t * data,size_t size,vector<BJsonEntityCaps::BundleInfo> & info)33 void GetInfo(const uint8_t *data, size_t size, vector<BJsonEntityCaps::BundleInfo> &info)
34 {
35     size_t base = size;
36     if (size >= U32_AT_SIZE) {
37         base = (size + 1) / REDUNDENT_SIZE;
38     }
39 
40     for (size_t i = 0; i < size; i++) {
41         BJsonEntityCaps::BundleInfo bundleInfo;
42         int len = size / SPLITE_SIZE;
43         int pos = 0;
44         bundleInfo.name = string(reinterpret_cast<const char*>(data + pos), len) + to_string(i % base);
45         pos += len;
46         bundleInfo.versionName = string(reinterpret_cast<const char*>(data + pos), len) + to_string(i);
47         pos += len;
48         bundleInfo.extensionName = string(reinterpret_cast<const char*>(data + pos), len) + to_string(i);
49         pos += len;
50 
51         if (i % DISTINCT_SIZE == 0) {
52             bundleInfo.restoreDeps = string(reinterpret_cast<const char*>(data + pos), len) + to_string(i);
53         } else {
54             bundleInfo.restoreDeps = string(reinterpret_cast<const char*>(data + pos), len) + "," + to_string(i);
55         }
56 
57         pos += len;
58         bundleInfo.supportScene = string(reinterpret_cast<const char*>(data + pos), len) + to_string(i);
59 
60         pos += len;
61         if (size >= pos + sizeof(bool)) {
62             bundleInfo.allToBackup = *(reinterpret_cast<const bool*>(data + pos));
63         }
64 
65         pos += sizeof(bool);
66         if (size >= pos + sizeof(int64_t)) {
67             bundleInfo.versionCode = *(reinterpret_cast<const int64_t*>(data + pos));
68         }
69 
70         pos += sizeof(int64_t);
71         if (size >= pos + sizeof(int64_t)) {
72             bundleInfo.spaceOccupied = *(reinterpret_cast<const int64_t*>(data + pos));
73         }
74         info.push_back(bundleInfo);
75     }
76 }
77 
SvcRestoreDepsManagerFuzzTest(const uint8_t * data,size_t size)78 bool SvcRestoreDepsManagerFuzzTest(const uint8_t *data, size_t size)
79 {
80     if (data == nullptr) {
81         return 0;
82     }
83 
84     vector<BJsonEntityCaps::BundleInfo> bundleInfos;
85     RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND;
86     if (size >= sizeof(RestoreTypeEnum)) {
87         restoreType = *(reinterpret_cast<const RestoreTypeEnum*>(data));
88     }
89     SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(bundleInfos, restoreType);
90     GetInfo(data, size, bundleInfos);
91 
92     BJsonEntityCaps::BundleInfo bundleInfo;
93     bundleInfo.name = string(reinterpret_cast<const char*>(data), size);
94     bundleInfos.push_back(bundleInfo);
95 
96     SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(bundleInfos, restoreType);
97     SvcRestoreDepsManager::GetInstance().GetRestoreBundleMap();
98 
99     size_t pos = size >> 1;
100     string bundleName(string(reinterpret_cast<const char*>(data), pos));
101     string fileName(string(reinterpret_cast<const char*>(data + pos), size - pos));
102     SvcRestoreDepsManager::GetInstance().AddRestoredBundles(bundleName);
103     SvcRestoreDepsManager::GetInstance().GetAllBundles();
104 
105     SvcRestoreDepsManager::GetInstance().UpdateToRestoreBundleMap(bundleName, fileName);
106     SvcRestoreDepsManager::GetInstance().UpdateToRestoreBundleMap(fileName, bundleName);
107     SvcRestoreDepsManager::GetInstance().IsAllBundlesRestored();
108 
109     SvcRestoreDepsManager::GetInstance().depsMap_.clear();
110     SvcRestoreDepsManager::GetInstance().allBundles_.clear();
111     SvcRestoreDepsManager::GetInstance().toRestoreBundleMap_.clear();
112     SvcRestoreDepsManager::GetInstance().restoredBundles_.clear();
113 
114     return true;
115 }
116 } // namespace OHOS
117 
118 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)119 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
120 {
121     /* Run your code on data */
122     OHOS::SvcRestoreDepsManagerFuzzTest(data, size);
123     return 0;
124 }