• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 
17 #include "res2_fuzzer.h"
18 
19 #include <stddef.h>
20 #include <stdint.h>
21 #include "adapter/ohos/entrance/file_asset_provider_impl.h"
22 #include "core/common/asset_manager_impl.h"
23 #include "frameworks/core/components/theme/theme_constants.h"
24 #include "test/mock/core/common/theme_mock.h"
25 
26 const uint32_t u16m = 65535;
27 
28 namespace OHOS {
29 using namespace OHOS::Ace;
30 using namespace std;
31 
WriteDataToFile(const string & path,const char * data,size_t size)32 int32_t WriteDataToFile(const string &path, const char* data, size_t size)
33 {
34     FILE *file = nullptr;
35     file = fopen(path.c_str(), "w+");
36     if (file == nullptr) {
37         return -1;
38     }
39     if (fwrite(data, 1, size, file) != size) {
40         (void)fclose(file);
41         return -1;
42     }
43     (void)fclose(file);
44     return 0;
45 }
46 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)47 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
48 {
49     bool result = false;
50     string cmd = "mkdir -p /data/test/arkui";
51     auto ri = size % u16m;
52     std::string s (reinterpret_cast<const char*>(data), ri);
53     WriteDataToFile("/data/test/arkui/123.png", s.c_str(), ri);
54     auto res = AceType::MakeRefPtr<ResourceAdapterMock>();
55     auto theme = AceType::MakeRefPtr<ThemeConstants>(res);
56     auto file = AceType::MakeRefPtr<FileAssetProviderImpl>();
57     auto asset = AceType::MakeRefPtr<AssetManagerImpl>();
58     string pack = "/data/test/arkui";
59     vector<string> files  = {""};
60     if(file->Initialize(pack,files)){
61         asset->PushBack(file);
62     }
63     theme->LoadCustomStyle(asset);
64     return result;
65 }
66 
67 }
68 
69 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
71 {
72     /* Run your code on data */
73     OHOS::DoSomethingInterestingWithMyAPI(data, size);
74     return 0;
75 }
76 
77