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_db_mgr_test.h"
17
18 #include <limits>
19
20 #include "export_db_manager.h"
21 #include "hiview_global.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace {
26 constexpr char TEST_MODULE_NAME[] = "test_module";
27 constexpr char TEST_MODULE_NAME2[] = "test_module2";
28 constexpr char TEST_MODULE_NAME3[] = "test_module3";
29 constexpr char TEST_MODULE_NAME4[] = "test_module4";
30 constexpr int64_t FIRST_ENABLED_SEQ = 10;
31 constexpr int64_t SECOND_ENABLED_SEQ = 100;
32 constexpr int64_t THIRD_ENABLED_SEQ = 350;
33 constexpr int64_t FIRST_FINISH_SEQ = 150;
34 constexpr int64_t SECOND_FINISH_SEQ = 200;
35 constexpr int64_t THIRD_FINISH_SEQ = 400;
36
37 class EventExportDbMgrTestContext : public HiviewContext {
38 public:
EventExportDbMgrTestContext(std::string & dbDir)39 explicit EventExportDbMgrTestContext(std::string& dbDir): testDbDir_(dbDir) {}
40
41 public:
GetHiViewDirectory(DirectoryType type __UNUSED)42 std::string GetHiViewDirectory(DirectoryType type __UNUSED)
43 {
44 return testDbDir_;
45 }
46
47 private:
48 std::string testDbDir_;
49 };
50 }
51
SetUpTestCase()52 void EventExportDbMgrTest::SetUpTestCase()
53 {
54 }
55
TearDownTestCase()56 void EventExportDbMgrTest::TearDownTestCase()
57 {
58 }
59
SetUp()60 void EventExportDbMgrTest::SetUp()
61 {
62 }
63
TearDown()64 void EventExportDbMgrTest::TearDown()
65 {
66 }
67
68 /**
69 * @tc.name: EventExportDbMgrTest001
70 * @tc.desc: ExportDbManager test with normal steps
71 * @tc.type: FUNC
72 * @tc.require: issueI9G6TM
73 */
74 HWTEST_F(EventExportDbMgrTest, EventExportDbMgrTest001, testing::ext::TestSize.Level3)
75 {
76 std::string testDbDir("/data/test/test_data/db_dir1/");
77 EventExportDbMgrTestContext context(testDbDir);
78 HiviewGlobal::CreateInstance(context);
79 auto& manager = ExportDbManager::GetInstance();
80 ASSERT_EQ(manager.GetEventInheritFlagPath("module_name"),
81 "/data/test/test_data/db_dir1/sys_event_export/event_inherit_flag_module_name");
82 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME), INVALID_SEQ_VAL);
83 // export switch on at 10
84 manager.HandleExportSwitchChanged(TEST_MODULE_NAME, FIRST_ENABLED_SEQ);
85 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME), FIRST_ENABLED_SEQ);
86 // export task finish at 150
87 manager.HandleExportTaskFinished(TEST_MODULE_NAME, FIRST_FINISH_SEQ);
88 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME), FIRST_FINISH_SEQ);
89 // export switch off
90 manager.HandleExportSwitchChanged(TEST_MODULE_NAME, INVALID_SEQ_VAL);
91 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME), INVALID_SEQ_VAL);
92 // export switch on at 100
93 manager.HandleExportSwitchChanged(TEST_MODULE_NAME, SECOND_ENABLED_SEQ);
94 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME), FIRST_FINISH_SEQ);
95 // export task finish at 200
96 manager.HandleExportTaskFinished(TEST_MODULE_NAME, SECOND_FINISH_SEQ);
97 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME), SECOND_FINISH_SEQ);
98 // export switch off
99 manager.HandleExportSwitchChanged(TEST_MODULE_NAME, INVALID_SEQ_VAL);
100 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME), INVALID_SEQ_VAL);
101 // export switch on at 350
102 manager.HandleExportSwitchChanged(TEST_MODULE_NAME, THIRD_ENABLED_SEQ);
103 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME), THIRD_ENABLED_SEQ);
104 // export task finish at 400
105 manager.HandleExportTaskFinished(TEST_MODULE_NAME, THIRD_FINISH_SEQ);
106 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME), THIRD_FINISH_SEQ);
107 }
108
109 /**
110 * @tc.name: EventExportDbMgrTest002
111 * @tc.desc: ExportDbManager test with unnormal steps: switch on->init->switch on
112 * @tc.type: FUNC
113 * @tc.require: issueI9G6TM
114 */
115 HWTEST_F(EventExportDbMgrTest, EventExportDbMgrTest002, testing::ext::TestSize.Level3)
116 {
117 std::string testDbDir("/data/test/test_data/db_dir2/");
118 EventExportDbMgrTestContext context(testDbDir);
119 HiviewGlobal::CreateInstance(context);
120 auto& manager = ExportDbManager::GetInstance();
121 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME2), INVALID_SEQ_VAL);
122 // export switch on at 10
123 manager.HandleExportSwitchChanged(TEST_MODULE_NAME2, FIRST_ENABLED_SEQ);
124 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME2), FIRST_ENABLED_SEQ);
125 // export switch on at 100
126 manager.HandleExportSwitchChanged(TEST_MODULE_NAME2, SECOND_ENABLED_SEQ);
127 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME2), SECOND_ENABLED_SEQ);
128 }
129
130 /**
131 * @tc.name: EventExportDbMgrTest003
132 * @tc.desc: ExportDbManager test with unnormal steps: switch off->init->switch->switch off->switch on
133 * @tc.type: FUNC
134 * @tc.require: issueI9G6TM
135 */
136 HWTEST_F(EventExportDbMgrTest, EventExportDbMgrTest003, testing::ext::TestSize.Level3)
137 {
138 std::string testDbDir("/data/test/test_data/db_dir3/");
139 EventExportDbMgrTestContext context(testDbDir);
140 HiviewGlobal::CreateInstance(context);
141 auto& manager = ExportDbManager::GetInstance();
142 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME3), INVALID_SEQ_VAL);
143 // export switch off
144 manager.HandleExportSwitchChanged(TEST_MODULE_NAME3, INVALID_SEQ_VAL);
145 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME3), INVALID_SEQ_VAL);
146 // export switch off
147 manager.HandleExportSwitchChanged(TEST_MODULE_NAME3, INVALID_SEQ_VAL);
148 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME3), INVALID_SEQ_VAL);
149 // export switch on at 350
150 manager.HandleExportSwitchChanged(TEST_MODULE_NAME3, THIRD_ENABLED_SEQ);
151 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME3), THIRD_ENABLED_SEQ);
152 }
153
154 /**
155 * @tc.name: EventExportDbMgrTest004
156 * @tc.desc: ExportDbManager test with unnormal steps: task finished->init->task finished->switch on
157 * @tc.type: FUNC
158 * @tc.require: issueI9G6TM
159 */
160 HWTEST_F(EventExportDbMgrTest, EventExportDbMgrTest004, testing::ext::TestSize.Level3)
161 {
162 std::string testDbDir("/data/test/test_data/db_dir4/");
163 EventExportDbMgrTestContext context(testDbDir);
164 auto& manager = ExportDbManager::GetInstance();
165 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME4), INVALID_SEQ_VAL);
166 // export task finish at 150
167 manager.HandleExportTaskFinished(TEST_MODULE_NAME4, FIRST_FINISH_SEQ);
168 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME4), INVALID_SEQ_VAL);
169 // export task finish at 200
170 manager.HandleExportTaskFinished(TEST_MODULE_NAME4, SECOND_FINISH_SEQ);
171 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME4), INVALID_SEQ_VAL);
172 // export switch on at 350
173 manager.HandleExportSwitchChanged(TEST_MODULE_NAME4, THIRD_ENABLED_SEQ);
174 ASSERT_EQ(manager.GetExportBeginSeq(TEST_MODULE_NAME4), THIRD_ENABLED_SEQ);
175 }
176 } // namespace HiviewDFX
177 } // namespace OHOS