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