1 /*
2 * Copyright (c) 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_write_test.h"
17
18 #include <memory>
19
20 #include "event_export_util.h"
21 #include "event_write_strategy_factory.h"
22 #include "export_event_packager.h"
23 #include "export_file_writer.h"
24 #include "export_json_file_builder.h"
25 #include "file_util.h"
26 #include "parameter.h"
27 #include "parameter_ex.h"
28
29 namespace OHOS {
30 namespace HiviewDFX {
31 namespace {
32 const std::string MODULE_NAME = "TEST_MODULE";
33 const std::string EXPORT_DIR = "/data/test/";
34 const std::string TEST_DOMAIN = "TEST_DOMAIN";
35 const std::string TEST_EVENT_NAME = "TEST_NAME";
36 const EventVersion EVENT_VER {
37 "SV0.0.1",
38 "PV0.0.2",
39 };
40 constexpr int64_t MAX_FILE_SIZE = 2000000;
41 constexpr int32_t UID = 100;
42
BuildEventStr()43 std::string BuildEventStr()
44 {
45 return R"~({"domain_":"TEST_DOMAIN","name_":"TEST_NAME","type_":4,"time_":1746415896989,"tz_":"+0000",
46 "pid_":1,"tid_":1,"uid_":0,"log_":0,"id_":"17708195254699639651","PARAM1":"teecd","PARAM2":2317,
47 "period_seq_":"","level_":"CRITICAL","seq_":76258})~";
48 }
49
BuildCacheEvent()50 std::shared_ptr<CachedEvent> BuildCacheEvent()
51 {
52 std::string eventStr = BuildEventStr();
53 return std::make_shared<CachedEvent>(EVENT_VER, TEST_DOMAIN, TEST_EVENT_NAME, eventStr, UID);
54 }
55
BuildWriteStrategyParam()56 WriteStrategyParam BuildWriteStrategyParam()
57 {
58 return WriteStrategyParam {
59 MODULE_NAME,
60 EXPORT_DIR,
61 EVENT_VER,
62 UID,
63 };
64 }
65
BuildCachedEventMap(CachedEventMap & cachedEventMap)66 void BuildCachedEventMap(CachedEventMap& cachedEventMap)
67 {
68 cachedEventMap.emplace(TEST_DOMAIN, std::vector<std::pair<std::string, std::string>> {
69 std::make_pair(TEST_EVENT_NAME, BuildEventStr()),
70 });
71 }
72
AssertWroteFiles(const std::string & srcFile,const std::string & destFile)73 void AssertWroteFiles(const std::string& srcFile, const std::string& destFile)
74 {
75 ASSERT_NE(srcFile.find("U100"), std::string::npos);
76 ASSERT_NE(destFile.find("U100"), std::string::npos);
77 ASSERT_EQ(FileUtil::ExtractFileName(srcFile).size(), FileUtil::ExtractFileName(destFile).size());
78 }
79
GenerateDevId()80 std::string GenerateDevId()
81 {
82 constexpr int32_t idLen = 65;
83 char id[idLen] = {0};
84 if (GetDevUdid(id, idLen) == 0) {
85 return std::string(id);
86 }
87 return "";
88 }
89 }
90
SetUpTestCase()91 void EventExportWriteTest::SetUpTestCase()
92 {
93 }
94
TearDownTestCase()95 void EventExportWriteTest::TearDownTestCase()
96 {
97 }
98
SetUp()99 void EventExportWriteTest::SetUp()
100 {
101 }
102
TearDown()103 void EventExportWriteTest::TearDown()
104 {
105 }
106
107 /**
108 * @tc.name: EventExportWriteTest001
109 * @tc.desc: Test apis of ExportEventPackager
110 * @tc.type: FUNC
111 * @tc.require: issueIC607P
112 */
113 HWTEST_F(EventExportWriteTest, EventExportWriteTest001, testing::ext::TestSize.Level3)
114 {
115 ExportEventPackager packager(MODULE_NAME, EXPORT_DIR, EVENT_VER, UID, MAX_FILE_SIZE);
116 std::string eventStr = BuildEventStr();
117 ASSERT_TRUE(packager.AppendEvent(TEST_DOMAIN, TEST_EVENT_NAME, eventStr));
118 packager.ClearPackagedFiles();
119 ASSERT_TRUE(packager.AppendEvent(TEST_DOMAIN, TEST_EVENT_NAME, eventStr));
120 ASSERT_TRUE(packager.Package());
121 packager.HandlePackagedFiles();
122 ASSERT_TRUE(packager.Package());
123 }
124
125 /**
126 * @tc.name: EventExportWriteTest002
127 * @tc.desc: Test apis of EventWriteStrategyFactory & WriteZipFileStrategy
128 * @tc.type: FUNC
129 * @tc.require: issueIC607P
130 */
131 HWTEST_F(EventExportWriteTest, EventExportWriteTest002, testing::ext::TestSize.Level3)
132 {
133 auto strategy = EventWriteStrategyFactory::GetWriteStrategy(StrategyType::ZIP_JSON_FILE);
134 ASSERT_NE(strategy, nullptr);
135 ASSERT_EQ(strategy->GetPackagerKey(BuildCacheEvent()), "SV0.0.1_PV0.0.2_100");
136 auto eventStr = BuildEventStr();
137 ASSERT_FALSE(strategy->Write(eventStr, nullptr));
138
139 auto strategyParam = BuildWriteStrategyParam();
140 strategy->SetWriteStrategyParam(strategyParam);
141 ASSERT_TRUE(strategy->Write(eventStr, [this] (const std::string& srcPath,
__anonaede03600202(const std::string& srcPath, const std::string& destPath) 142 const std::string& destPath) {
143 AssertWroteFiles(srcPath, destPath);
144 }));
145 }
146
147 /**
148 * @tc.name: EventExportWriteTest003
149 * @tc.desc: Test apis of ExportJsonFileBuilder
150 * @tc.type: FUNC
151 * @tc.require: issueIC607P
152 */
153 HWTEST_F(EventExportWriteTest, EventExportWriteTest003, testing::ext::TestSize.Level3)
154 {
155 std::shared_ptr<ExportFileBaseBuilder> builder = std::make_shared<ExportJsonFileBuilder>(EVENT_VER);
156 ASSERT_NE(builder, nullptr);
157 CachedEventMap events;
158 BuildCachedEventMap(events);
159 std::string buildStr;
160 ASSERT_TRUE(builder->Build(events, buildStr));
161 ASSERT_GT(buildStr.size(), 0);
162 }
163
164
165 /**
166 * @tc.name: EventExportWriteTest004
167 * @tc.desc: Test apis of ExportFileWriter
168 * @tc.type: FUNC
169 * @tc.require: issueIC607P
170 */
171 HWTEST_F(EventExportWriteTest, EventExportWriteTest004, testing::ext::TestSize.Level3)
172 {
173 ExportFileWriter fileWriter;
174 fileWriter.SetExportFileWroteListener([this] (const std::string& srcPath,
__anonaede03600302(const std::string& srcPath, const std::string& destPath) 175 const std::string& destPath) {
176 AssertWroteFiles(srcPath, destPath);
177 });
178 CachedEventMap events;
179 BuildCachedEventMap(events);
180 auto strategyParam = BuildWriteStrategyParam();
181 ASSERT_FALSE(fileWriter.Write(nullptr, events, strategyParam));
182 ASSERT_TRUE(fileWriter.Write(std::make_shared<ExportJsonFileBuilder>(EVENT_VER), events,
183 strategyParam));
184 }
185
186 /**
187 * @tc.name: EventExportWriteTest005
188 * @tc.desc: Test apis of EventExportUtil
189 * @tc.type: FUNC
190 * @tc.require: issueIC607P
191 */
192 HWTEST_F(EventExportWriteTest, EventExportWriteTest005, testing::ext::TestSize.Level3)
193 {
194 auto deviceId = EventExportUtil::GetDeviceId();
195 if (Parameter::GetUserType() == Parameter::USER_TYPE_OVERSEA_COMMERCIAL) {
196 ASSERT_EQ(deviceId, Parameter::GetString("persist.hiviewdfx.priv.packid", ""));
197 } else {
198 ASSERT_EQ(deviceId, GenerateDevId());
199 }
200 }
201 } // namespace HiviewDFX
202 } // namespace OHOS