• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 #include "event_export_config_test.h"
17 
18 #include <vector>
19 
20 #include "export_config_manager.h"
21 #include "export_event_list_parser.h"
22 #include "hiview_global.h"
23 #include "parameter_ex.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace {
28 // all value is from resource file: test_event_export_config.json and test_events.json
29 constexpr char TEST_CONFIG_DIR[] = "/data/test/test_data/";
30 constexpr char TEST_CONFIG_FILE[] = "/data/test/test_data/sys_event_export/test_event_export_config.json";
31 constexpr char TEST_MODULE_NAME[] = "test";
32 constexpr char TEST_SETTING_DB_PARAM_NAME[] = "test_param_key";
33 constexpr char TEST_SETTING_DB_PARAM_ENABLED_VAL[] = "1";
34 constexpr size_t TEST_MODULE_CNT = 2;
35 constexpr int64_t TEST_CAPACITY = 100;
36 constexpr int64_t TEST_SIZE = 2;
37 constexpr int64_t TEST_CYCLE = 3600;
38 constexpr int64_t TEST_FILE_STORE_DAY_CNT = 7;
39 constexpr size_t TEST_EXPORT_NAME_CNT = 3;
40 constexpr size_t TEST_EXPORT_CFG_FILE_CNT = 2;
41 constexpr int64_t TEST_EXPORT_CFG_VERSION = 202404061011;
42 constexpr char TEST_EXPORT_CFG_FILE[] = "/data/test/test_data/test_events_v2.json";
43 constexpr char INVALID_TEST_EXPORT_CFG_FILE1[] = "/data/test/test_data/invalid_test_events1.json";
44 constexpr char INVALID_TEST_EXPORT_CFG_FILE2[] = "/data/test/test_data/invalid_test_events2.json";
45 constexpr char INVALID_TEST_EXPORT_CFG_FILE3[] = "/data/test/test_data/invalid_test_events3.json";
46 constexpr int64_t DEFAULT_VERSION = 0;
47 constexpr char TEST_CONFIG_FILE1[] = "/data/test/test_data/sys_event_export/test1_event_export_config.json";
48 constexpr char TEST_CONFIG_FILE2[] = "/data/test/test_data/sys_event_export/test2_event_export_config.json";
49 
50 class EventExportConfigTestContext : public HiviewContext {
51 public:
GetHiViewDirectory(DirectoryType type __UNUSED)52     std::string GetHiViewDirectory(DirectoryType type __UNUSED)
53     {
54         return TEST_CONFIG_DIR;
55     }
56 };
57 }
SetUpTestCase()58 void EventExportConfigParseTest::SetUpTestCase()
59 {
60 }
61 
TearDownTestCase()62 void EventExportConfigParseTest::TearDownTestCase()
63 {
64 }
65 
SetUp()66 void EventExportConfigParseTest::SetUp()
67 {
68 }
69 
TearDown()70 void EventExportConfigParseTest::TearDown()
71 {
72 }
73 
74 /**
75  * @tc.name: EventExportConfigParseTest001
76  * @tc.desc: EventConfigManager test
77  * @tc.type: FUNC
78  * @tc.require: issueI9E8HA
79  */
80 HWTEST_F(EventExportConfigParseTest, EventExportConfigParseTest001, testing::ext::TestSize.Level3)
81 {
82     EventExportConfigTestContext context;
83     HiviewGlobal::CreateInstance(context);
84     auto& manager = ExportConfigManager::GetInstance();
85     std::vector<std::string> moduleNames;
86     manager.GetModuleNames(moduleNames);
87     ASSERT_EQ(moduleNames.size(), TEST_MODULE_CNT);
88     auto testModuleName = moduleNames.at(0);
89     ASSERT_EQ(testModuleName, TEST_MODULE_NAME);
90     auto exportConfig = manager.GetExportConfig(testModuleName);
91     ASSERT_NE(exportConfig, nullptr);
92     std::vector<std::shared_ptr<ExportConfig>> configs;
93     manager.GetAllExportConfigs(configs);
94     ASSERT_EQ(configs.size(), TEST_MODULE_CNT);
95 }
96 
97 /**
98  * @tc.name: EventExportConfigParseTest002
99  * @tc.desc: EventConfigParser test
100  * @tc.type: FUNC
101  * @tc.require: issueI9E8HA
102  */
103 HWTEST_F(EventExportConfigParseTest, EventExportConfigParseTest002, testing::ext::TestSize.Level3)
104 {
105     ExportConfigParser parser(TEST_CONFIG_FILE, TEST_MODULE_NAME);
106     auto exportConfig = parser.Parse();
107     ASSERT_NE(exportConfig, nullptr);
108     ASSERT_EQ(exportConfig->moduleName, TEST_MODULE_NAME);
109     ASSERT_EQ(exportConfig->exportDir, "/data/test/test_data/sys_event_export/");
110     ASSERT_EQ(exportConfig->maxCapcity, TEST_CAPACITY);
111     ASSERT_EQ(exportConfig->maxSize, TEST_SIZE);
112     ASSERT_EQ(exportConfig->taskCycle, TEST_CYCLE);
113     ASSERT_EQ(exportConfig->dayCnt, TEST_FILE_STORE_DAY_CNT);
114     ExportConfigParser parser1(TEST_CONFIG_FILE1, TEST_MODULE_NAME);
115     exportConfig = parser1.Parse();
116     ASSERT_NE(exportConfig, nullptr);
117     ASSERT_EQ(exportConfig->moduleName, TEST_MODULE_NAME);
118     ASSERT_NE(exportConfig->exportDir, "/data/test/test_data/sys_event_export/");
119 }
120 
121 /**
122  * @tc.name: EventExportConfigParseTest003
123  * @tc.desc: EventExportList test
124  * @tc.type: FUNC
125  * @tc.require: issueI9E8HA
126  */
127 HWTEST_F(EventExportConfigParseTest, EventExportConfigParseTest003, testing::ext::TestSize.Level3)
128 {
129     ExportConfigParser parser(TEST_CONFIG_FILE, TEST_MODULE_NAME);
130     auto exportConfig = parser.Parse();
131     ASSERT_NE(exportConfig, nullptr);
132     ASSERT_EQ(exportConfig->eventsConfigFiles.size(), TEST_EXPORT_CFG_FILE_CNT);
133 }
134 
135 /**
136  * @tc.name: EventExportConfigParseTest004
137  * @tc.desc: SettingDbParam test
138  * @tc.type: FUNC
139  * @tc.require: issueI9E8HA
140  */
141 HWTEST_F(EventExportConfigParseTest, EventExportConfigParseTest004, testing::ext::TestSize.Level3)
142 {
143     ExportConfigParser parser(TEST_CONFIG_FILE, TEST_MODULE_NAME);
144     auto exportConfig = parser.Parse();
145     ASSERT_NE(exportConfig, nullptr);
146     ASSERT_EQ(exportConfig->exportSwitchParam.name, TEST_SETTING_DB_PARAM_NAME);
147     ASSERT_EQ(exportConfig->exportSwitchParam.enabledVal, TEST_SETTING_DB_PARAM_ENABLED_VAL);
148     ASSERT_EQ(exportConfig->sysUpgradeParam.name, TEST_SETTING_DB_PARAM_NAME);
149     ASSERT_EQ(exportConfig->sysUpgradeParam.enabledVal, TEST_SETTING_DB_PARAM_ENABLED_VAL);
150 }
151 
152 /**
153  * @tc.name: EventExportConfigParseTest005
154  * @tc.desc: EventExportConfigParseTest005 test
155  * @tc.type: FUNC
156  * @tc.require: issueICI4CO
157  */
158 HWTEST_F(EventExportConfigParseTest, EventExportConfigParseTest005, testing::ext::TestSize.Level3)
159 {
160     ExportConfigParser parser1(TEST_CONFIG_FILE1, TEST_MODULE_NAME);
161     auto exportConfig = parser1.Parse();
162     ASSERT_NE(exportConfig, nullptr);
163     if (Parameter::IsOversea()) {
164         if (Parameter::IsBetaVersion()) {
165             ASSERT_EQ(exportConfig->exportDir, "/data/test/test_data/sys_event_export/180/");
166             ASSERT_EQ(exportConfig->taskType, 180);    // 180 is expected value
167             ASSERT_EQ(exportConfig->taskCycle, 2000);  // 2000 is expected value
168         } else {
169             ASSERT_EQ(exportConfig->exportDir, "/data/test/test_data/sys_event_export/3000/");
170             ASSERT_EQ(exportConfig->taskType, 3000);   // 3000 is expected value
171             ASSERT_EQ(exportConfig->taskCycle, 3000);  // 3000 is expected value
172         }
173     } else {
174         if (Parameter::IsBetaVersion()) {
175             ASSERT_EQ(exportConfig->exportDir, "/data/test/test_data/sys_event_export/0/");
176             ASSERT_EQ(exportConfig->taskType, ALL_EVENT_TASK_TYPE);
177             ASSERT_EQ(exportConfig->taskCycle, 1000);  // 1000 is expected value
178         } else {
179             ASSERT_EQ(exportConfig->exportDir, "/data/test/test_data/sys_event_export/2000/");
180             ASSERT_EQ(exportConfig->taskType, 2000);   // 2000 is expected value
181             ASSERT_EQ(exportConfig->taskCycle, 2000);  // 2000 is expected value
182         }
183     }
184     ASSERT_EQ(exportConfig->maxCapcity, TEST_CAPACITY);
185     ASSERT_EQ(exportConfig->maxSize, TEST_SIZE);
186     ASSERT_EQ(exportConfig->dayCnt, TEST_FILE_STORE_DAY_CNT);
187     ASSERT_EQ(exportConfig->inheritedModule, "inherited_test_module");
188 
189     ExportConfigParser parser2(TEST_CONFIG_FILE2, TEST_MODULE_NAME);
190     exportConfig = parser2.Parse();
191     ASSERT_EQ(exportConfig, nullptr);
192 }
193 
194 /**
195  * @tc.name: ExportEventListParserTest005
196  * @tc.desc: ExportEventListParser test
197  * @tc.type: FUNC
198  * @tc.require: issueIAC4BC
199  */
200 HWTEST_F(EventExportConfigParseTest, ExportEventListParserTest005, testing::ext::TestSize.Level3)
201 {
202     ExportEventListParser parser(TEST_EXPORT_CFG_FILE);
203     ExportEventList list;
204     parser.GetExportEventList(list);
205     ASSERT_GT(list.size(), 0);
206     auto iter = list.find("DOMAIN1");
207     ASSERT_NE(iter, list.end());
208     iter = list.find("DOMAIN2");
209     ASSERT_NE(iter, list.end());
210     ASSERT_EQ(iter->second.size(), TEST_EXPORT_NAME_CNT);
211     ASSERT_EQ(parser.GetConfigurationVersion(), TEST_EXPORT_CFG_VERSION);
212 }
213 
214 /**
215  * @tc.name: ExportEventListParserTest006
216  * @tc.desc: ExportEventListParser test
217  * @tc.type: FUNC
218  * @tc.require: issueIAC4BC
219  */
220 HWTEST_F(EventExportConfigParseTest, ExportEventListParserTest006, testing::ext::TestSize.Level3)
221 {
222     ExportEventListParser parser1(INVALID_TEST_EXPORT_CFG_FILE1);
223     ExportEventList list;
224     parser1.GetExportEventList(list);
225     ASSERT_EQ(list.size(), 0);
226     ASSERT_EQ(parser1.GetConfigurationVersion(), DEFAULT_VERSION);
227     ExportEventListParser parser2(INVALID_TEST_EXPORT_CFG_FILE2);
228     parser2.GetExportEventList(list);
229     ASSERT_EQ(list.size(), 0);
230     ASSERT_EQ(parser2.GetConfigurationVersion(), TEST_EXPORT_CFG_VERSION);
231     ExportEventListParser parser3(INVALID_TEST_EXPORT_CFG_FILE3);
232     parser3.GetExportEventList(list);
233     ASSERT_EQ(list.size(), 0);
234 }
235 } // namespace HiviewDFX
236 } // namespace OHOS