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 #include "hicollie_collector_test.h"
16
17 #include <fstream>
18 #include <iostream>
19 #include <memory>
20 #include <set>
21 #include <unistd.h>
22
23 #include "event.h"
24 #include "file_util.h"
25 #include "time_util.h"
26
27 #include "hiview_platform.h"
28 #include "hisysevent.h"
29 #include "plugin.h"
30 #include "sys_event.h"
31
32 using namespace testing::ext;
33 namespace OHOS {
34 namespace HiviewDFX {
35 namespace {
makeEvent(const std::string & name,const std::string & domain,const std::string & eventName,const std::string & packageName,const std::string & logPath)36 std::shared_ptr<SysEvent> makeEvent(const std::string& name,
37 const std::string& domain,
38 const std::string& eventName,
39 const std::string& packageName,
40 const std::string& logPath)
41 {
42 auto time = TimeUtil::GetMilliseconds();
43 std::string str = "******************\n";
44 str += "this is test " + eventName + " at " + std::to_string(time) + "\n";
45 str += "******************\n";
46 FileUtil::SaveStringToFile(logPath, str);
47
48 auto jsonStr = "{\"domain_\":\"" + domain + "\"}";
49 auto sysEvent = std::make_shared<SysEvent>(name, nullptr, jsonStr);
50 sysEvent->SetEventValue("name_", eventName);
51 sysEvent->SetEventValue("type_", 1);
52 sysEvent->SetEventValue("time_", time);
53 sysEvent->SetEventValue("pid_", getpid());
54 sysEvent->SetEventValue("tid_", gettid());
55 sysEvent->SetEventValue("uid_", getuid());
56 sysEvent->SetEventValue("tz_", TimeUtil::GetTimeZone());
57 sysEvent->SetEventValue("PID", getpid());
58 sysEvent->SetEventValue("UID", getuid());
59 sysEvent->SetEventValue("MSG", "test " + eventName + " event");
60 sysEvent->SetEventValue("PACKAGE_NAME", packageName);
61 sysEvent->SetEventValue("PROCESS_NAME", packageName);
62
63 std::string tmpStr = R"~(logPath:)~" + logPath;
64 sysEvent->SetEventValue("info_", tmpStr);
65 if (sysEvent->ParseJson() < 0) {
66 printf("Failed to parse from queryResult file name %s.\n", logPath.c_str());
67 return nullptr;
68 }
69 return sysEvent;
70 }
71
GetFreezeDectorTestFile(const std::string & eventName,const std::string & packageName,uint64_t time)72 bool GetFreezeDectorTestFile(const std::string& eventName,
73 const std::string& packageName,
74 uint64_t time)
75 {
76 int count = 0;
77 std::string decLogPath = "";
78 while (count < 10) { // 10: 最大等待10s
79 sleep(1);
80 std::vector<std::string> files;
81 FileUtil::GetDirFiles("/data/log/faultlog/faultlogger/", files);
82 ++count;
83 for (auto& i : files) {
84 if (i.find(packageName) == std::string::npos) {
85 continue;
86 }
87 std::string content;
88 FileUtil::LoadStringFromFile(i, content);
89 if (content.find(std::to_string(time)) == std::string::npos) {
90 printf("time is not match.\n");
91 FileUtil::RemoveFile(i);
92 continue;
93 }
94
95 if (content.find(eventName) == std::string::npos) {
96 printf("Not %s.\n", eventName.c_str());
97 FileUtil::RemoveFile(i);
98 continue;
99 }
100
101 if (content.find(packageName) == std::string::npos) {
102 printf("Not %s.\n", packageName.c_str());
103 FileUtil::RemoveFile(i);
104 continue;
105 }
106 decLogPath = i;
107 break;
108 }
109
110 if (decLogPath != "") {
111 break;
112 }
113 }
114
115 if (decLogPath == "") {
116 printf("Not find files.\n");
117 return false;
118 }
119
120 FileUtil::RemoveFile(decLogPath);
121 return true;
122 }
123 }
124
SetUp()125 void HicollieCollectorTest::SetUp()
126 {
127 /**
128 * @tc.setup: create work directories
129 */
130 printf("SetUp.\n");
131 }
SetUpTestCase()132 void HicollieCollectorTest::SetUpTestCase()
133 {
134 /**
135 * @tc.setup: all first
136 */
137 printf("SetUpTestCase.\n");
138 HiviewPlatform &platform = HiviewPlatform::GetInstance();
139 if (!platform.InitEnvironment("/data/test/test_data/hiview_platform_config")) {
140 printf("Fail to init environment.\n");
141 }
142 }
143
TearDownTestCase()144 void HicollieCollectorTest::TearDownTestCase()
145 {
146 /**
147 * @tc.setup: all end
148 */
149 printf("TearDownTestCase.\n");
150 }
151
TearDown()152 void HicollieCollectorTest::TearDown()
153 {
154 /**
155 * @tc.teardown: destroy the event loop we have created
156 */
157 printf("TearDown.\n");
158 }
159
160 /**
161 * @tc.name: HicollieCollectorTest001
162 * @tc.desc: HicollieCollector send SERVICE_BLOCK
163 * @tc.type: FUNC
164 * @tc.require: AR000H3T5D
165 */
166 HWTEST_F(HicollieCollectorTest, HicollieCollectorTest001, TestSize.Level3)
167 {
168 HiviewPlatform &platform = HiviewPlatform::GetInstance();
169 std::shared_ptr<Plugin> plugin = platform.GetPluginByName("HiCollieCollector");
170 if (plugin == nullptr) {
171 printf("Get HiCollieCollector, failed\n");
172 FAIL();
173 }
174
175 std::string logPath = "/data/test/test_data/LOG001.log";
176 FileUtil::CreateFile(logPath);
177 if (!FileUtil::FileExists(logPath)) {
178 printf("CreateFile file, failed\n");
179 FAIL();
180 }
181
182 auto sysEvent = makeEvent("HicollieCollectorTest001", "FRAMEWORK", "SERVICE_TIMEOUT",
183 "HicollieCollectorTest001", logPath);
184 if (sysEvent == nullptr) {
185 printf("GetFreezeDectorTest001File, failed\n");
186 FAIL();
187 }
188 uint64_t time = sysEvent->GetEventIntValue("time_");
189 std::shared_ptr<OHOS::HiviewDFX::Event> event = std::static_pointer_cast<Event>(sysEvent);
190 std::shared_ptr<HiCollieCollector> hiCollieCollector = std::static_pointer_cast<HiCollieCollector>(plugin);
191 hiCollieCollector->OnUnorderedEvent(*(event.get()));
192
193 sleep(3);
194 if (!GetFreezeDectorTestFile("SERVICE_TIMEOUT",
195 "HicollieCollectorTest001",
196 time)) {
197 printf("GetFreezeDectorTest001File, failed\n");
198 FAIL();
199 }
200 }
201
202 /**
203 * @tc.name: HicollieCollectorTest002
204 * @tc.desc: HicollieCollector send SERVICE_TIMEOUT
205 * @tc.type: FUNC
206 * @tc.require: AR000H3T5D
207 */
208 HWTEST_F(HicollieCollectorTest, HicollieCollectorTest002, TestSize.Level3)
209 {
210 HiviewPlatform &platform = HiviewPlatform::GetInstance();
211 std::shared_ptr<Plugin> plugin = platform.GetPluginByName("HiCollieCollector");
212 if (plugin == nullptr) {
213 printf("Get HiCollieCollector, failed\n");
214 FAIL();
215 }
216
217 std::string logPath = "/data/test/test_data/LOG002.log";
218 FileUtil::CreateFile(logPath);
219 if (!FileUtil::FileExists(logPath)) {
220 printf("CreateFile file, failed\n");
221 FAIL();
222 }
223
224 auto sysEvent = makeEvent("HicollieCollectorTest002", "FRAMEWORK", "SERVICE_BLOCK",
225 "HicollieCollectorTest002", logPath);
226 if (sysEvent == nullptr) {
227 printf("GetFreezeDectorTest001File, failed\n");
228 FAIL();
229 }
230 std::shared_ptr<OHOS::HiviewDFX::Event> event = std::static_pointer_cast<Event>(sysEvent);
231 std::shared_ptr<HiCollieCollector> hiCollieCollector = std::static_pointer_cast<HiCollieCollector>(plugin);
232 hiCollieCollector->OnUnorderedEvent(*(event.get()));
233
234 sleep(3);
235 ASSERT_EQ(plugin->GetPluginInfo(), "HiCollieCollector");
236 }
237 }
238 }
239