• 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         if (size >= sizeof(bool)) {
61             bundleInfo.allToBackup = *(reinterpret_cast<const bool*>(data));
62         }
63 
64         if (size >= sizeof(int64_t)) {
65             bundleInfo.versionCode = *(reinterpret_cast<const int64_t*>(data));
66         }
67 
68         if (size >= sizeof(int64_t)) {
69             bundleInfo.spaceOccupied = *(reinterpret_cast<const int64_t*>(data));
70         }
71         info.push_back(bundleInfo);
72     }
73 }
74 
SvcRestoreDepsManagerFuzzTest(const uint8_t * data,size_t size)75 bool SvcRestoreDepsManagerFuzzTest(const uint8_t *data, size_t size)
76 {
77     vector<BJsonEntityCaps::BundleInfo> bundleInfos;
78     RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND;
79     if (size >= sizeof(RestoreTypeEnum)) {
80         restoreType = *(reinterpret_cast<const RestoreTypeEnum*>(data));
81     }
82     SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(bundleInfos, restoreType);
83     GetInfo(data, size, bundleInfos);
84 
85     BJsonEntityCaps::BundleInfo bundleInfo;
86     bundleInfo.name = string(reinterpret_cast<const char*>(data), size);
87     bundleInfos.push_back(bundleInfo);
88 
89     SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(bundleInfos, restoreType);
90     SvcRestoreDepsManager::GetInstance().GetRestoreBundleMap();
91 
92     size_t pos = size >> 1;
93     string bundleName(string(reinterpret_cast<const char*>(data), pos));
94     string fileName(string(reinterpret_cast<const char*>(data + pos), size - pos));
95     SvcRestoreDepsManager::GetInstance().AddRestoredBundles(bundleName);
96     SvcRestoreDepsManager::GetInstance().GetAllBundles();
97 
98     SvcRestoreDepsManager::GetInstance().UpdateToRestoreBundleMap(bundleName, fileName);
99     SvcRestoreDepsManager::GetInstance().UpdateToRestoreBundleMap(fileName, bundleName);
100     SvcRestoreDepsManager::GetInstance().IsAllBundlesRestored();
101 
102     SvcRestoreDepsManager::GetInstance().depsMap_.clear();
103     SvcRestoreDepsManager::GetInstance().allBundles_.clear();
104     SvcRestoreDepsManager::GetInstance().toRestoreBundleMap_.clear();
105     SvcRestoreDepsManager::GetInstance().restoredBundles_.clear();
106 
107     return true;
108 }
109 } // namespace OHOS
110 
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
113 {
114     /* Run your code on data */
115     if (data == nullptr) {
116         return 0;
117     }
118 
119     OHOS::SvcRestoreDepsManagerFuzzTest(data, size);
120     return 0;
121 }