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_query_wrapper_builder_test.h"
17
18 #include "event_query_wrapper_builder.h"
19 #include "ret_code.h"
20 #include "sys_event_dao.h"
21 #include "sys_event_query.h"
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 namespace {
26 }
27
SetUpTestCase()28 void EventQueryWrapperBuilderTest::SetUpTestCase() {}
29
TearDownTestCase()30 void EventQueryWrapperBuilderTest::TearDownTestCase() {}
31
SetUp()32 void EventQueryWrapperBuilderTest::SetUp() {}
33
TearDown()34 void EventQueryWrapperBuilderTest::TearDown() {}
35
36 /**
37 * @tc.name: EventQueryWrapperBuilderTest001
38 * @tc.desc: Test ParseCondition api of correct condition string literal
39 * @tc.type: FUNC
40 * @tc.require: issueI8YEQI
41 */
42 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest001, testing::ext::TestSize.Level3)
43 {
44 ConditionParser parser;
45 EventStore::Cond condition;
46 std::string condListeral = "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op\":\"=\","
47 "\"value\":1000},{\"param\":\"PACKAGE_NAME\",\"op\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
48 auto ret = parser.ParseCondition(condListeral, condition);
49 ASSERT_TRUE(ret);
50 }
51
52 /**
53 * @tc.name: EventQueryWrapperBuilderTest002
54 * @tc.desc: Test ParseCondition api with string literal with invalid json format
55 * @tc.type: FUNC
56 * @tc.require: issueI8YEQI
57 */
58 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest002, testing::ext::TestSize.Level3)
59 {
60 ConditionParser parser;
61 EventStore::Cond condition;
62 std::string condListeral = "-1";
63 auto ret = parser.ParseCondition(condListeral, condition);
64 ASSERT_TRUE(!ret);
65 }
66
67 /**
68 * @tc.name: EventQueryWrapperBuilderTest003
69 * @tc.desc: Test ParseCondition api with string literal with invalid version key
70 * @tc.type: FUNC
71 * @tc.require: issueI8YEQI
72 */
73 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest003, testing::ext::TestSize.Level3)
74 {
75 ConditionParser parser;
76 EventStore::Cond condition;
77 std::string condListeral = "{\"version1\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op\":\"=\","
78 "\"value\":1000},{\"param\":\"PACKAGE_NAME\",\"op\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
79 auto ret = parser.ParseCondition(condListeral, condition);
80 ASSERT_TRUE(!ret);
81 }
82
83 /**
84 * @tc.name: EventQueryWrapperBuilderTest004
85 * @tc.desc: Test ParseCondition api with string literal with invalid condition key
86 * @tc.type: FUNC
87 * @tc.require: issueI8YEQI
88 */
89 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest004, testing::ext::TestSize.Level3)
90 {
91 ConditionParser parser;
92 EventStore::Cond condition;
93 std::string condListeral = "{\"version\":\"V1\",\"condition1\":{\"and\":[{\"param\":\"PID\",\"op\":\"=\","
94 "\"value\":1000},{\"param\":\"PACKAGE_NAME\",\"op\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
95 auto ret = parser.ParseCondition(condListeral, condition);
96 ASSERT_TRUE(!ret);
97 }
98
99 /**
100 * @tc.name: EventQueryWrapperBuilderTest005
101 * @tc.desc: Test ParseCondition api with string literal with invalid operation key
102 * @tc.type: FUNC
103 * @tc.require: issueI8YEQI
104 */
105 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest005, testing::ext::TestSize.Level3)
106 {
107 ConditionParser parser;
108 EventStore::Cond condition;
109 std::string condListeral = "{\"version\":\"V1\",\"condition\":{\"and1\":[{\"param\":\"PID\",\"op\":\"=\","
110 "\"value\":1000},{\"param\":\"PACKAGE_NAME\",\"op\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
111 auto ret = parser.ParseCondition(condListeral, condition);
112 ASSERT_TRUE(!ret);
113 }
114
115 /**
116 * @tc.name: EventQueryWrapperBuilderTest006
117 * @tc.desc: Test ParseCondition api with string literal with invalid param key
118 * @tc.type: FUNC
119 * @tc.require: issueI8YEQI
120 */
121 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest006, testing::ext::TestSize.Level3)
122 {
123 ConditionParser parser;
124 EventStore::Cond condition;
125 std::string condListeral = "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param1\":\"PID\",\"op\":\"=\","
126 "\"value\":1000},{\"param1\":\"PACKAGE_NAME\",\"op\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
127 auto ret = parser.ParseCondition(condListeral, condition);
128 ASSERT_TRUE(!ret);
129 }
130
131 /**
132 * @tc.name: EventQueryWrapperBuilderTest007
133 * @tc.desc: Test ParseCondition api with string literal with invalid param option key
134 * @tc.type: FUNC
135 * @tc.require: issueI8YEQI
136 */
137 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest007, testing::ext::TestSize.Level3)
138 {
139 ConditionParser parser;
140 EventStore::Cond condition;
141 std::string condListeral = "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op1\":\"=\","
142 "\"value\":1000},{\"param\":\"PACKAGE_NAME\",\"op1\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
143 auto ret = parser.ParseCondition(condListeral, condition);
144 ASSERT_TRUE(!ret);
145 }
146
147 /**
148 * @tc.name: EventQueryWrapperBuilderTest008
149 * @tc.desc: Test ParseCondition api with string literal with invalid param value key
150 * @tc.type: FUNC
151 * @tc.require: issueI8YEQI
152 */
153 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest008, testing::ext::TestSize.Level3)
154 {
155 ConditionParser parser;
156 EventStore::Cond condition;
157 std::string condListeral = "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op\":\"=\","
158 "\"value1\":1000},{\"param\":\"PACKAGE_NAME\",\"op\":\"=\",\"value1\":\"com.ohos.testHiSysEvent2\"}]}}";
159 auto ret = parser.ParseCondition(condListeral, condition);
160 ASSERT_TRUE(!ret);
161 }
162
163 /**
164 * @tc.name: EventQueryWrapperBuilderTest009
165 * @tc.desc: Test query events by TimeStampEventQueryWrapper
166 * @tc.type: FUNC
167 * @tc.require: issueIBX8TW
168 */
169 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest009, testing::ext::TestSize.Level3)
170 {
171 auto now = std::chrono::system_clock::now();
172 auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
173 QueryArgument queryArgument(0, millisecs.count(), 10); // 10 is a test value
174 auto builder = std::make_shared<EventQueryWrapperBuilder>(queryArgument);
175 builder->Append("HIVIEWDFX", "PLUGIN_UNLOAD", 4, "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op\":\">\","
176 "\"value\":0}]}}");
177 builder->Append("HIVIEWDFX", "PLUGIN_LOAD", 4, "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op\":\">\","
178 "\"value\":0}]}}");
179 ASSERT_TRUE(builder->IsValid());
180 auto queryWrapper = builder->Build();
181 ASSERT_NE(queryWrapper, nullptr);
182 }
183
184 /**
185 * @tc.name: EventQueryWrapperBuilderTest010
186 * @tc.desc: Test query events by SeqEventQueryWrapper
187 * @tc.type: FUNC
188 * @tc.require: issueIBX8TW
189 */
190 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest010, testing::ext::TestSize.Level3)
191 {
192 QueryArgument queryArgument(0, 0, 10, 1000, 2000); // 10 is a test query count, 1000-2000 is test query range
193 auto builder = std::make_shared<EventQueryWrapperBuilder>(queryArgument);
194 builder->Append("HIVIEWDFX", "PLUGIN_LOAD", 4, "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op\":\">\","
195 "\"value\":0}]}}");
196 builder->Append("HIVIEWDFX", "PLUGIN_UNLOAD", 4, "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op\":\">\","
197 "\"value\":0}]}}");
198 ASSERT_TRUE(builder->IsValid());
199 auto queryWrapper = builder->Build();
200 ASSERT_NE(queryWrapper, nullptr);
201 }
202 }
203 }