• 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 #include "audit_log_parser_test.h"
16 
17 #include "audit.h"
18 #include "audit_log_parser.h"
19 #include "pipeline.h"
20 
21 using namespace testing::ext;
22 using namespace OHOS::HiviewDFX;
23 
SetUp()24 void AuditLogParserTest::SetUp()
25 {
26     Audit::GetInstance().Init(true);
27     Audit::GetInstance().Clear();
28 }
29 
30 /**
31  * @tc.name: AuditLogParserTest001
32  * @tc.desc: write normal event log into audit log and then parse it
33  * @tc.type: FUNC
34  * @tc.require: AR000DPTT2
35  */
36 HWTEST_F(AuditLogParserTest, AuditLogParserTest001, TestSize.Level3)
37 {
38     Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_IN, 1585730002219098,
39                            "EventProcessorExample4|EventProcessorExample3|example-2@7166|10000");
40     Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_OUT, 1585730002219098);
41     Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_IN, 1585730009919825,
42                            "EventProcessorExample3|EventProcessorExample4|example-2@7166|10001");
43     Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_OUT, 1585730009919825);
44     AuditLogParser parser;
45     parser.StartParse();
46     auto threadEventList = parser.GetThreadSummary();
47     ASSERT_EQ(threadEventList.size(), 2ul);
48     auto pluginEventList = parser.GetPluginSummary();
49     ASSERT_EQ(pluginEventList.size(), 2ul);
50     auto pipelineEventList = parser.GetPipelineSummary();
51     ASSERT_EQ(pipelineEventList.size(), 0ul);
52     auto scope = parser.GetAuditLogTimeScope();
53     ASSERT_GT(scope.length(), 10ul);
54 }
55 
56 /**
57  * @tc.name: AuditLogParserTest002
58  * @tc.desc: write pipeline event log into audit log and then parse it
59  * @tc.type: FUNC
60  * @tc.require: AR000DPTT2
61  */
62 HWTEST_F(AuditLogParserTest, AuditLogParserTest002, TestSize.Level3)
63 {
64     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_CREATE, 1585730002219098, "EventSourceExample|901000000");
65     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_IN, 1585730002219098, "EventProcessorExample1");
66     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_OUT, 1585730002219098, "example-2@7166");
67     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_IN, 1585730002219098, "EventProcessorExample2");
68     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_OUT, 1585730002219098, "example-2@7167");
69     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_IN, 1585730002219098, "EventProcessorExample3");
70     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_OUT, 1585730002219098, "example-2@7168");
71     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_DONE, 1585730002219098);
72     AuditLogParser parser;
73     parser.StartParse();
74     auto threadEventList = parser.GetThreadSummary();
75     ASSERT_EQ(threadEventList.size(), 3ul);
76     for (auto& log : threadEventList) {
77         printf("thread log:%s \n", log.c_str());
78     }
79     auto pluginEventList = parser.GetPluginSummary();
80     for (auto& log : pluginEventList) {
81         printf("plugin log:%s \n", log.c_str());
82     }
83     ASSERT_EQ(pluginEventList.size(), 3ul);
84     auto pipelineEventList = parser.GetPipelineSummary();
85     for (auto& log : pipelineEventList) {
86         printf("pipeline log:%s \n", log.c_str());
87     }
88     ASSERT_EQ(pipelineEventList.size(), 1ul);
89     auto scope = parser.GetAuditLogTimeScope();
90     ASSERT_GT(scope.length(), 10ul);
91 }
92 
93 /**
94  * @tc.name: AuditLogParserTest003
95  * @tc.desc: write mixed event log into audit log and then parse it
96  * @tc.type: FUNC
97  * @tc.require: AR000DPTT2
98  */
99 HWTEST_F(AuditLogParserTest, AuditLogParserTest003, TestSize.Level3)
100 {
101     Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_IN, 1585730002219050,
102                            "EventProcessorExample4|EventProcessorExample3|example-2@7166|10000");
103     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_CREATE, 1585730002219099, "EventSourceExample|901000000");
104     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_IN, 1585730002219099, "EventProcessorExample1");
105     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_OUT, 1585730002219099, "example-2@7166");
106     Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_OUT, 1585730002219050);
107     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_IN, 1585730002219099, "EventProcessorExample2");
108     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_OUT, 1585730002219099, "example-2@7167");
109     Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_IN, 1585730002219070,
110                            "EventProcessorExample3|EventProcessorExample4|example-2@7166|10001");
111     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_IN, 1585730002219099, "EventProcessorExample3");
112     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_HANDLE_OUT, 1585730002219099, "example-2@7168");
113     Audit::WriteAuditEvent(Audit::StatsEvent::PIPELINE_EVENT_DONE, 1585730002219099);
114     Audit::WriteAuditEvent(Audit::StatsEvent::QUEUE_EVENT_OUT, 1585730002219070);
115     AuditLogParser parser;
116     parser.StartParse();
117     auto threadEventList = parser.GetThreadSummary();
118     ASSERT_EQ(threadEventList.size(), 5ul);
119     for (auto& log : threadEventList) {
120         printf("thread log:%s \n", log.c_str());
121     }
122     auto pluginEventList = parser.GetPluginSummary();
123     for (auto& log : pluginEventList) {
124         printf("plugin log:%s \n", log.c_str());
125     }
126     ASSERT_EQ(pluginEventList.size(), 5ul);
127     auto pipelineEventList = parser.GetPipelineSummary();
128     for (auto& log : pipelineEventList) {
129         printf("pipeline log:%s \n", log.c_str());
130     }
131     ASSERT_EQ(pipelineEventList.size(), 1ul);
132     auto scope = parser.GetAuditLogTimeScope();
133     ASSERT_GT(scope.length(), 10ul);
134 }
135 
136 /**
137  * @tc.name: AuditLogParserTest004
138  * @tc.desc: construct a pipeline event with a normal event info
139  * @tc.type: FUNC
140  * @tc.require: AR000DPTT2
141  */
142 HWTEST_F(AuditLogParserTest, AuditLogParserTest004, TestSize.Level3)
143 {
144     AuditLogParser* parser = new AuditLogParser();
145     ASSERT_NE(parser, nullptr);
146     AuditLogParser::EventInfo info;
147     AuditLogParser::PipelineEventInfo pipeInfo;
148     pipeInfo.eventSerialId = 1;
149     ASSERT_EQ(1ul, pipeInfo.eventSerialId);
150     info.eventSerialId = 1;
151     AuditLogParser::PipelineEventInfo pipeInfo2(info);
152     ASSERT_EQ(info.eventSerialId, pipeInfo2.eventSerialId);
153     delete parser;
154 }