• 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 #define private public
17 #include <cstddef>
18 #include <cstdint>
19 
20 #include "app_service_fwk/app_service_fwk_installer.h"
21 
22 #include "appservicefwkinstallerupdateappservice_fuzzer.h"
23 #include "securec.h"
24 
25 using namespace OHOS::AppExecFwk;
26 namespace OHOS {
27     constexpr size_t U32_AT_SIZE = 4;
28     const int32_t VERSION_LOW = 0;
29     const std::string MODULE_NAME_TEST = "moduleName";
30     const std::string TEST_CREATE_FILE_PATH = "/data/test/resource/bms/app_service_test/test_create_dir/test.hap";
31     const std::string VERSION_ONE_LIBRARY_ONE_PATH =
32       "/data/test/resource/bms/app_service_test/test_create_dir/test.hap";
33 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)34     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
35     {
36         AppServiceFwkInstaller appServiceFwk;
37         InstallParam installParam;
38         installParam.copyHapToInstallPath = false;
39         std::unordered_map<std::string, InnerBundleInfo> infos;
40         InnerBundleInfo innerBundleInfo;
41         innerBundleInfo.currentPackage_ = MODULE_NAME_TEST;
42         infos.emplace(TEST_CREATE_FILE_PATH, innerBundleInfo);
43         appServiceFwk.UpdateAppService(innerBundleInfo, infos, installParam);
44         InnerBundleInfo oldInfo;
45         InnerBundleInfo newInfo;
46         std::string hspPath = ", path: ";
47         appServiceFwk.ProcessBundleUpdateStatus(oldInfo, newInfo, VERSION_ONE_LIBRARY_ONE_PATH, installParam);
48         bool isReplace = true;
49         appServiceFwk.ProcessModuleUpdate(innerBundleInfo, oldInfo, hspPath, installParam);
50         appServiceFwk.RemoveLowerVersionSoDir(VERSION_LOW);
51         std::string bundlePath(data, size);
52         std::string cpuAbi(data, size);
53         std::string targetSoPath(data, size);
54         bool isPreInstalledBundle = false;
55         appServiceFwk.VerifyCodeSignatureForNativeFiles(bundlePath, cpuAbi, targetSoPath);
56         std::vector<Security::Verify::HapVerifyResult> hapVerifyResults;
57         appServiceFwk.DeliveryProfileToCodeSign(hapVerifyResults);
58         appServiceFwk.GenerateOdid(infos, hapVerifyResults);
59         return true;
60     }
61 }
62 
63 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66     /* Run your code on data */
67     if (data == nullptr) {
68         return 0;
69     }
70 
71     if (size < OHOS::U32_AT_SIZE) {
72         return 0;
73     }
74 
75     char* ch = static_cast<char*>(malloc(size + 1));
76     if (ch == nullptr) {
77         return 0;
78     }
79 
80     (void)memset_s(ch, size + 1, 0x00, size + 1);
81     if (memcpy_s(ch, size, data, size) != EOK) {
82         free(ch);
83         ch = nullptr;
84         return 0;
85     }
86     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
87     free(ch);
88     ch = nullptr;
89     return 0;
90 }