• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_service_action_test.h"
17 
18 #include <vector>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <fstream>
24 #include "event_json_parser.h"
25 #include "event.h"
26 #include "hiview_global.h"
27 #include "hiview_platform.h"
28 #include "sys_event.h"
29 #include "sys_event_stat.h"
30 #include "sys_event_db_mgr.h"
31 #include "sys_event_dao.h"
32 #include "sys_event_db_backup.h"
33 #include "sys_event_service.h"
34 #include "flat_json_parser.h"
35 
36 namespace OHOS {
37 namespace HiviewDFX {
SetUpTestCase()38 void EventServiceActionTest::SetUpTestCase()
39 {
40     HiviewPlatform &platform = HiviewPlatform::GetInstance();
41     std::string defaultDir = "/data/test/test_data/hiview_platform_config";
42     if (!platform.InitEnvironment(defaultDir)) {
43         std::cout << "fail to init environment" << std::endl;
44     } else {
45         std::cout << "init environment successful" << std::endl;
46     }
47 }
48 
TearDownTestCase()49 void EventServiceActionTest::TearDownTestCase() {}
50 
SetUp()51 void EventServiceActionTest::SetUp() {}
52 
TearDown()53 void EventServiceActionTest::TearDown() {}
54 
55 /**
56  * @tc.name: EventJsonParserTest001
57  * @tc.desc: parse a event and check Json info
58  * @tc.type: FUNC
59  * @tc.require: SR000GGSVB
60  */
61 HWTEST_F(EventServiceActionTest, EventJsonParserTest001, testing::ext::TestSize.Level3)
62 {
63     printf("start EventJsonParserTest001\n");
64     constexpr char JSON_STR[] = "{\"domain_\":\"DEMO\",\"name_\":\"EVENT_NAME_A\",\"type_\":4,\
65         \"PARAM_A\":\"param a\",\"PARAM_B\":\"param b\"}";
66     auto sysEvent = std::make_shared<SysEvent>("SysEventService", nullptr, JSON_STR);
67     std::string yamlFile =
68         HiviewGlobal::GetInstance()->GetHiViewDirectory(HiviewContext::DirectoryType::CONFIG_DIRECTORY);
69     yamlFile = (yamlFile.back() != '/') ? (yamlFile + "/hisysevent.def") : (yamlFile + "hisysevent.def");
70     auto sysEventParser = std::make_unique<EventJsonParser>(yamlFile);
71     ASSERT_TRUE(sysEventParser->HandleEventJson(sysEvent));
72     ASSERT_TRUE(sysEventParser->GetTagByDomainAndName("abc", "abc") == "");
73     ASSERT_TRUE(sysEventParser->GetTagByDomainAndName("DEMO", "abc") == "");
74     ASSERT_TRUE(sysEventParser->GetTypeByDomainAndName("DEMO", "abc") == 0);
75     DuplicateIdFilter filter;
76     filter.IsDuplicateEvent("aaa");
77     filter.IsDuplicateEvent("a1");
78     filter.IsDuplicateEvent("a2");
79     filter.IsDuplicateEvent("a3");
80     filter.IsDuplicateEvent("a4");
81     filter.IsDuplicateEvent("a5");
82     ASSERT_TRUE(filter.IsDuplicateEvent("a5"));
83     FlatJsonParser parser("{\"level\":[abc]}");
84     std::cout << "FlatJsonParser:" << parser.Print() << std::endl;
85     ASSERT_TRUE(parser.Print()=="{\"level\":[abc]}");
86 }
87 
OpenTestFile(const char * filename)88 int OpenTestFile(const char *filename)
89 {
90     int fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
91     if (fd <= 0) {
92         printf("[ NET ]:open file error\n");
93         return -1;
94     }
95     return fd;
96 }
97 
GetFileContent(const std::string & filename)98 std::string GetFileContent(const std::string& filename)
99 {
100     std::ifstream inputfile(filename);
101     std::string contents;
102     std::string temp;
103     while (inputfile >> temp) {
104         contents.append(temp);
105     }
106     return contents;
107 }
108 
109 /**
110  * @tc.name: SysEventStatTest002
111  * @tc.desc: check event state
112  * @tc.type: FUNC
113  * @tc.require: issueI62WJT
114  */
115 HWTEST_F(EventServiceActionTest, SysEventStatTest002, testing::ext::TestSize.Level3)
116 {
117     SysEventStat sysEventStat_;
118     sysEventStat_.AccumulateEvent(true);
119     sysEventStat_.AccumulateEvent(false);
120     sysEventStat_.AccumulateEvent("domain1", "eventName_1");
121     sysEventStat_.AccumulateEvent("domain1", "eventName_2");
122     sysEventStat_.AccumulateEvent("domain2", "eventName_3");
123     sysEventStat_.AccumulateEvent("domain4", "eventName_4", false);
124     sysEventStat_.AccumulateEvent("domain4", "eventName_5", false);
125     sysEventStat_.AccumulateEvent("domain5", "eventName_6", false);
126     int fd1 = OpenTestFile("./fd1.txt");
127     ASSERT_FALSE(fd1 == -1);
128     sysEventStat_.StatSummary(fd1);
129     close(fd1);
130     std::string result1 = GetFileContent("./fd1.txt");
131     ASSERT_TRUE(result1.size() > 0);
132 
133     int fd2 = OpenTestFile("./fd2.txt");
134     sysEventStat_.StatDetail(fd2);
135     close(fd2);
136     std::string result2 = GetFileContent("./fd2.txt");
137     ASSERT_TRUE(result2.size() > 0);
138 
139     int fd3 = OpenTestFile("./fd3.txt");
140     sysEventStat_.StatInvalidDetail(fd3);
141     close(fd3);
142     std::string result3 = GetFileContent("./fd3.txt");
143     ASSERT_TRUE(result3.size() > 0);
144 
145     int fd4 = OpenTestFile("./fd4.txt");
146     sysEventStat_.Clear(fd4);
147     close(fd4);
148     std::string result4 = GetFileContent("./fd4.txt");
149     std::cout << "result4:" << result4 << std::endl;
150     ASSERT_TRUE(result4 == "cleanallstatinfo");
151 }
152 
153 /**
154  * @tc.name: SysEventDao003
155  * @tc.desc: check event Dao
156  * @tc.type: FUNC
157  * @tc.require:issueI64VNS
158  */
159 HWTEST_F(EventServiceActionTest, SysEventDao003, testing::ext::TestSize.Level3)
160 {
161     sysEventDbMgrPtr = std::make_unique<SysEventDbMgr>();
162     currentLooper_ = std::make_shared<EventLoop>("EventServiceActionTest");
163     currentLooper_->StartLoop();
164     std::string jsonStr1 = R"~({"domain_":"demo","name_":"SysEventDaoTest_003","type_":1,"tz_":8,"time_":162027129110,
165         "pid_":6527,"tid_":6527,"traceid_":"f0ed5160bb2df4b","spanid_":"10","pspanid_":"20","trace_flag_":4,
166         "keyBool":1})~";
167     auto sysEvent = std::make_shared<SysEvent>("SysEventSource", nullptr, jsonStr1);
168     ASSERT_TRUE(sysEvent->ParseJson() == 0);
169     sysEventDbMgrPtr->SaveToStore(sysEvent);
170     auto sysEventQuery = EventStore::SysEventDao::BuildQuery(EventStore::StoreType::FAULT);
171     std::vector<std::string> selections { EventStore::EventCol::NAME };
172     EventStore::ResultSet resultSet = (*sysEventQuery).Select(selections).
173         Where(EventStore::EventCol::NAME, EventStore::Op::EQ, "SysEventDaoTest_003").Execute();
174     int count = 0;
175     while (resultSet.HasNext()) {
176         EventStore::ResultSet::RecordIter it = resultSet.Next();
177         std::cout << "seq=" << it->GetSeq() << std::endl;
178         count++;
179     }
180     ASSERT_TRUE(count > 0);
181     std::cout <<" count:" << count << std::endl;
182     for (int i = 0; i < 50; i++) {
183         SysEventCreator sysEventCreator("domain1", "test1", SysEventCreator::FAULT);
184         sysEventCreator.SetKeyValue("KEY_INT", i);
185         auto sysEvent1 = std::make_shared<SysEvent>("SysEventSource", nullptr, sysEventCreator);
186         EventStore::SysEventDao::Insert(sysEvent1);
187     }
188     auto testPlugin = std::make_shared<SysEventService>();
189     sysEventDbMgrPtr->StartCheckStoreTask(nullptr);
190     sysEventDbMgrPtr->StartCheckStoreTask(currentLooper_);
191     sysEventDbMgrPtr->CheckStore();
192     SysEventDbBackup dbBackup(EventStore::StoreType::FAULT);
193     dbBackup.Recover();
194     ASSERT_TRUE(dbBackup.IsBroken() == 0);
195     if (currentLooper_ != nullptr) {
196         currentLooper_->StopLoop();
197         currentLooper_.reset();
198     }
199 }
200 
201 /**
202  * @tc.name: SysEventService004
203  * @tc.desc: check sysEvent service
204  * @tc.type: FUNC
205  * @tc.require:issueI64VNS
206  */
207 HWTEST_F(EventServiceActionTest, SysEventService004, testing::ext::TestSize.Level3)
208 {
209     auto testPlugin = std::make_shared<SysEventService>();
210     std::shared_ptr<Event> nullEvent = nullptr;
211     std::cout << "ASSERT1:" << testPlugin->OnEvent(nullEvent) << std::endl;
212     ASSERT_FALSE(testPlugin->OnEvent(nullEvent));
213     testPlugin->OnUnload();
214     std::string jsonStr = R"~({"domain_":"demo","name_":"SysEventDaoTest_003","type_":1,"tz_":8,
215         "time_":1620271291200,"pid_":6527,"tid_":6527,"traceid_":"f0ed5160bb2df4b","spanid_":"10","pspanid_":"20",
216         "trace_flag_":4,"keyBool":1,"keyChar":97})~";
217     auto sysEventTemp = std::make_shared<SysEvent>("SysEventSource", nullptr, jsonStr);
218     sysEventTemp->ParseJson();
219     std::shared_ptr<Event> event1 = std::static_pointer_cast<Event>(sysEventTemp);
220     event1->messageType_ = Event::MessageType::UE_EVENT;
221     ASSERT_FALSE(testPlugin->OnEvent(event1));
222     SysEventCreator sysEventCreator("domain1", "test1", SysEventCreator::FAULT);
223     auto sysEvent1 = std::make_shared<SysEvent>("SysEventSource", nullptr, sysEventCreator);
224     std::shared_ptr<Event> event2 = std::static_pointer_cast<Event>(sysEvent1);
225     std::cout << "ASSERT3:" << testPlugin->OnEvent(event2) << std::endl;
226     int fd5 = OpenTestFile("./fd5.txt");
227     testPlugin->Dump(fd5, {"start", "detail"});
228     testPlugin->Dump(fd5, {"start", "invalid"});
229     testPlugin->Dump(fd5, {"start", "clear"});
230     testPlugin->Dump(fd5, {"start", "aaa"});
231     testPlugin->Dump(fd5, {"start"});
232     std::string result = GetFileContent("./fd5.txt");
233     ASSERT_TRUE(result.size() > 0);
234 }
235 } // namespace HiviewDFX
236 } // namespace OHOS