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