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#ifndef PARAMS_PARSE_FUZZER_H
17#define PARAMS_PARSE_FUZZER_H
18
19#include <string>
20#include <vector>
21#include <fstream>
22#include <filesystem>
23#include <gtest/gtest.h>
24
25namespace fuzztest {
26    static std::string g_currDir = "";
27    static std::string g_currFile = "";
28
29    class Param {
30    public:
31        Param(std::string name, std::vector<std::string> values) : name(name), values(values) {}
32        std::string name;
33        std::vector<std::string> values;
34    };
35
36    class ParamsParse {
37    public:
38        void ParamsParseFuzzTest();
39    private:
40        void CallParamsParseFunc(const std::vector<std::string>& args);
41        void SetTestArgs(std::vector<std::string>& args);
42        std::vector<Param> paramList = {
43            Param("-j", { g_currDir }),
44            Param("-n", { "entry" }),
45            Param("-d", { "" }),
46            Param("-p", { "8888" }),
47            Param("-s", { "phone_1676450550023_1" }),
48            Param("-or", { "1080", "2340" }),
49            Param("-cr", { "1080", "2340" }),
50            Param("-f", { g_currFile }),
51            Param("-hs", { "102400" }),
52            Param("-hf", { "true" }),
53            Param("-shape", { "rect" }),
54            Param("-device", { "phone" }),
55            Param("-url", { "pages/Index" }),
56            Param("-refresh", { "region" }),
57            Param("-card", { "true" }),
58            Param("-projectID", { "985150866" }),
59            Param("-ts", { "trace_93488_commandPipe" }),
60            Param("-cm", { "light" }),
61            Param("-o", { "portrait" }),
62            Param("-lws", { "40000" }),
63            Param("-av", { "ACE_2_0" }),
64            Param("-l", { "zh_CN" }),
65            Param("-sd", { "480" }),
66            Param("-sm", { "dynamic" }),
67            Param("-arp", { g_currDir }),
68            Param("-pm", { "Stage" }),
69            Param("-pages", { "main_pages" }),
70            Param("-hsp", { g_currDir }),
71            Param("-cpm", { "true" }),
72            Param("-abp", { "ets/entryability/EntryAbility.abc" }),
73            Param("-abn", { "EntryAbility" }),
74            Param("-staticCard", { "true" }),
75            Param("-foldable", { "true" }),
76            Param("-foldStatus", { "unfold" }),
77            Param("-fr", { "1080", "2340" }),
78            Param("-ljPath", { g_currFile })
79        };
80    };
81
82    class ParamsParseFuzzTest : public testing::Test {
83    protected:
84        // 在整个测试夹具类执行前执行一次初始化操作
85        static void SetUpTestCase()
86        {
87            // 使用空格分割字符串,并存入vector
88            char buffer[FILENAME_MAX];
89            if (getcwd(buffer, FILENAME_MAX) != nullptr) {
90                g_currDir = std::string(buffer);
91                g_currDir += "/MyApplication";
92                int status = mkdir(g_currDir.c_str(), 0777); // 0777 表示所有用户有读、写、执行权限
93                if (status != 0) {
94                    printf("Error creating folder!\n");
95                }
96                g_currFile = g_currDir + "/test.json";
97                // 创建文件流对象并打开文件
98                std::ofstream file(g_currFile);
99                // 检查文件是否成功打开
100                if (file.is_open()) {
101                    file.close();
102                } else {
103                    printf("Error creating file!\n");
104                }
105            } else {
106                printf("error: getcwd failed\n");
107            }
108        }
109
110        // 在整个测试夹具类执行后执行一次清理操作
111        static void TearDownTestCase()
112        {
113            if (std::remove(g_currFile.c_str()) != 0) {
114                printf("Error deleting file!\n");
115            }
116            std::filesystem::remove(g_currDir.c_str());
117        }
118
119        virtual void SetUp()
120        {
121            std::cout << "--> ParamsParseFuzzTest." << __func__ << " <--" <<std::endl;
122        }
123
124        virtual void TearDown()
125        {
126            std::cout << "--> ParamsParseFuzzTest." << __func__ << " <--" <<std::endl;
127        }
128
129        ParamsParse parse;
130    };
131}
132
133#endif