1 /*
2  * Copyright (c) 2024 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 "sys_event_dao.h"
20 #include "sys_event_query.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace {
25 }
26 
SetUpTestCase()27 void EventQueryWrapperBuilderTest::SetUpTestCase() {}
28 
TearDownTestCase()29 void EventQueryWrapperBuilderTest::TearDownTestCase() {}
30 
SetUp()31 void EventQueryWrapperBuilderTest::SetUp() {}
32 
TearDown()33 void EventQueryWrapperBuilderTest::TearDown() {}
34 
35 /**
36  * @tc.name: EventQueryWrapperBuilderTest001
37  * @tc.desc: Test ParseCondition api of correct condition string literal
38  * @tc.type: FUNC
39  * @tc.require: issueI8YEQI
40  */
41 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest001, testing::ext::TestSize.Level3)
42 {
43     ConditionParser parser;
44     EventStore::Cond condition;
45     std::string condListeral = "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op\":\"=\","
46         "\"value\":1000},{\"param\":\"PACKAGE_NAME\",\"op\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
47     auto ret = parser.ParseCondition(condListeral, condition);
48     ASSERT_TRUE(ret);
49 }
50 
51 /**
52  * @tc.name: EventQueryWrapperBuilderTest002
53  * @tc.desc: Test ParseCondition api with string literal with invalid json format
54  * @tc.type: FUNC
55  * @tc.require: issueI8YEQI
56  */
57 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest002, testing::ext::TestSize.Level3)
58 {
59     ConditionParser parser;
60     EventStore::Cond condition;
61     std::string condListeral = "-1";
62     auto ret = parser.ParseCondition(condListeral, condition);
63     ASSERT_TRUE(!ret);
64 }
65 
66 /**
67  * @tc.name: EventQueryWrapperBuilderTest003
68  * @tc.desc: Test ParseCondition api with string literal with invalid version key
69  * @tc.type: FUNC
70  * @tc.require: issueI8YEQI
71  */
72 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest003, testing::ext::TestSize.Level3)
73 {
74     ConditionParser parser;
75     EventStore::Cond condition;
76     std::string condListeral = "{\"version1\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op\":\"=\","
77         "\"value\":1000},{\"param\":\"PACKAGE_NAME\",\"op\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
78     auto ret = parser.ParseCondition(condListeral, condition);
79     ASSERT_TRUE(!ret);
80 }
81 
82 /**
83  * @tc.name: EventQueryWrapperBuilderTest004
84  * @tc.desc: Test ParseCondition api with string literal with invalid condition key
85  * @tc.type: FUNC
86  * @tc.require: issueI8YEQI
87  */
88 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest004, testing::ext::TestSize.Level3)
89 {
90     ConditionParser parser;
91     EventStore::Cond condition;
92     std::string condListeral = "{\"version\":\"V1\",\"condition1\":{\"and\":[{\"param\":\"PID\",\"op\":\"=\","
93         "\"value\":1000},{\"param\":\"PACKAGE_NAME\",\"op\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
94     auto ret = parser.ParseCondition(condListeral, condition);
95     ASSERT_TRUE(!ret);
96 }
97 
98 /**
99  * @tc.name: EventQueryWrapperBuilderTest005
100  * @tc.desc: Test ParseCondition api with string literal with invalid operation key
101  * @tc.type: FUNC
102  * @tc.require: issueI8YEQI
103  */
104 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest005, testing::ext::TestSize.Level3)
105 {
106     ConditionParser parser;
107     EventStore::Cond condition;
108     std::string condListeral = "{\"version\":\"V1\",\"condition\":{\"and1\":[{\"param\":\"PID\",\"op\":\"=\","
109         "\"value\":1000},{\"param\":\"PACKAGE_NAME\",\"op\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
110     auto ret = parser.ParseCondition(condListeral, condition);
111     ASSERT_TRUE(!ret);
112 }
113 
114 /**
115  * @tc.name: EventQueryWrapperBuilderTest006
116  * @tc.desc: Test ParseCondition api with string literal with invalid param key
117  * @tc.type: FUNC
118  * @tc.require: issueI8YEQI
119  */
120 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest006, testing::ext::TestSize.Level3)
121 {
122     ConditionParser parser;
123     EventStore::Cond condition;
124     std::string condListeral = "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param1\":\"PID\",\"op\":\"=\","
125         "\"value\":1000},{\"param1\":\"PACKAGE_NAME\",\"op\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
126     auto ret = parser.ParseCondition(condListeral, condition);
127     ASSERT_TRUE(!ret);
128 }
129 
130 /**
131  * @tc.name: EventQueryWrapperBuilderTest007
132  * @tc.desc: Test ParseCondition api with string literal with invalid param option key
133  * @tc.type: FUNC
134  * @tc.require: issueI8YEQI
135  */
136 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest007, testing::ext::TestSize.Level3)
137 {
138     ConditionParser parser;
139     EventStore::Cond condition;
140     std::string condListeral = "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op1\":\"=\","
141         "\"value\":1000},{\"param\":\"PACKAGE_NAME\",\"op1\":\"=\",\"value\":\"com.ohos.testHiSysEvent2\"}]}}";
142     auto ret = parser.ParseCondition(condListeral, condition);
143     ASSERT_TRUE(!ret);
144 }
145 
146 /**
147  * @tc.name: EventQueryWrapperBuilderTest008
148  * @tc.desc: Test ParseCondition api with string literal with invalid param value key
149  * @tc.type: FUNC
150  * @tc.require: issueI8YEQI
151  */
152 HWTEST_F(EventQueryWrapperBuilderTest, EventQueryWrapperBuilderTest008, testing::ext::TestSize.Level3)
153 {
154     ConditionParser parser;
155     EventStore::Cond condition;
156     std::string condListeral = "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"PID\",\"op\":\"=\","
157         "\"value1\":1000},{\"param\":\"PACKAGE_NAME\",\"op\":\"=\",\"value1\":\"com.ohos.testHiSysEvent2\"}]}}";
158     auto ret = parser.ParseCondition(condListeral, condition);
159     ASSERT_TRUE(!ret);
160 }
161 }
162 }