1 /*
2 * Copyright (c) 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 #include <iostream>
16
17 #include <gtest/gtest.h>
18
19 #include "hiappevent_base.h"
20 #include "hiappevent_config.h"
21 #include "hiappevent_verify.h"
22
23 using namespace testing::ext;
24 using namespace OHOS::HiviewDFX;
25
26 namespace {
27 const std::string TEST_DIR = "/data/test/hiappevent/";
28
29 class HiAppEventVerifyTest : public testing::Test {
30 public:
31 void SetUp();
TearDown()32 void TearDown() {}
33 };
34
SetUp()35 void HiAppEventVerifyTest::SetUp()
36 {
37 HiAppEventConfig::GetInstance().SetStorageDir(TEST_DIR);
38 }
39 }
40
41 /**
42 * @tc.name: HiAppEventVerifyTest001
43 * @tc.desc: check the param isValid.
44 * @tc.type: FUNC
45 */
46 HWTEST_F(HiAppEventVerifyTest, HiAppEventVerifyTest001, TestSize.Level0)
47 {
48 std::cout << "HiAppEventVerifyTest001 start" << std::endl;
49 std::string testName = "testName";
50 bool isValid = IsValidUserIdValue(testName);
51 EXPECT_TRUE(isValid);
52 isValid = IsValidUserPropValue(testName);
53 EXPECT_TRUE(isValid);
54 isValid = IsValidWatcherName(testName);
55 EXPECT_TRUE(isValid);
56
57 testName = "";
58 isValid = IsValidUserIdValue(testName);
59 EXPECT_FALSE(isValid);
60 isValid = IsValidUserPropValue(testName);
61 EXPECT_FALSE(isValid);
62 isValid = IsValidWatcherName(testName);
63 EXPECT_FALSE(isValid);
64
65 isValid = IsValidEventType(0); // 1-4: value range of event type
66 EXPECT_FALSE(isValid);
67 isValid = IsValidEventType(1);
68 EXPECT_TRUE(isValid);
69 std::cout << "HiAppEventVerifyTest001 end" << std::endl;
70 }
71
72 /**
73 * @tc.name: HiAppEventVerifyTest002
74 * @tc.desc: check the VerifyCustomEventParam func.
75 * @tc.type: FUNC
76 */
77 HWTEST_F(HiAppEventVerifyTest, HiAppEventVerifyTest002, TestSize.Level0)
78 {
79 std::cout << "HiAppEventVerifyTest002 start" << std::endl;
80 std::shared_ptr<AppEventPack> event = std::make_shared<AppEventPack>();
81 bool setRes = HiAppEventConfig::GetInstance().SetConfigurationItem("disable", "true");
82 EXPECT_TRUE(setRes);
83 int result = VerifyCustomEventParams(event);
84 EXPECT_EQ(result, ErrorCode::ERROR_HIAPPEVENT_DISABLE);
85
86 setRes = HiAppEventConfig::GetInstance().SetConfigurationItem("disable", "false");
87 EXPECT_TRUE(setRes);
88 event->SetDomain("a%$321");
89 result = VerifyCustomEventParams(event);
90 EXPECT_EQ(result, ErrorCode::ERROR_INVALID_EVENT_DOMAIN);
91
92 event->SetDomain("testDomain");
93 event->SetName("123456");
94 result = VerifyCustomEventParams(event);
95 EXPECT_EQ(result, ErrorCode::ERROR_INVALID_EVENT_NAME);
96
97 event->SetName("testName");
98 result = VerifyCustomEventParams(event);
99 EXPECT_EQ(result, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
100
101 event->AddParam("testParam");
102 result = VerifyCustomEventParams(event);
103 EXPECT_EQ(result, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
104
105 for (int i = 0; i < 64; i++) {
106 event->AddParam(std::to_string(i));
107 }
108 result = VerifyCustomEventParams(event);
109 EXPECT_EQ(result, ErrorCode::ERROR_INVALID_CUSTOM_PARAM_NUM);
110 std::cout << "HiAppEventVerifyTest002 end" << std::endl;
111 }
112
113 /**
114 * @tc.name: HiAppEventVerifyTest003
115 * @tc.desc: check the VerifyAppCustomEventParam func.
116 * @tc.type: FUNC
117 */
118 HWTEST_F(HiAppEventVerifyTest, HiAppEventVerifyTest003, TestSize.Level0)
119 {
120 std::cout << "HiAppEventVerifyTest003 start" << std::endl;
121 std::shared_ptr<AppEventPack> event = std::make_shared<AppEventPack>();
122 bool setRes = HiAppEventConfig::GetInstance().SetConfigurationItem("disable", "false");
123 EXPECT_TRUE(setRes);
124 event->SetDomain("testDomain");
125 event->SetName("testName");
126 int result = VerifyCustomEventParams(event);
127 EXPECT_EQ(result, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
128
129 event->AddParam("testKey", "testValue\\1\2\b3\f4\n5\r6\t7");
130 result = VerifyCustomEventParams(event);
131 EXPECT_EQ(result, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
132
133 std::vector<std::string> strs(3, "testStr");
134 event->AddParam("testVectorParam", strs);
135 result = VerifyCustomEventParams(event);
136 EXPECT_EQ(result, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
137
138 event->AddParam("testParam");
139 result = VerifyCustomEventParams(event);
140 EXPECT_EQ(result, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
141 event->AddParam("testParam");
142 result = VerifyCustomEventParams(event);
143 EXPECT_EQ(result, 0);
144 std::cout << "HiAppEventVerifyTest003 end" << std::endl;
145 }
146
147 /**
148 * @tc.name: HiAppEventVerifyTest004
149 * @tc.desc: check the VerifyAppCustomEventParam func.
150 * @tc.type: FUNC
151 */
152 HWTEST_F(HiAppEventVerifyTest, HiAppEventVerifyTest004, TestSize.Level0)
153 {
154 std::cout << "HiAppEventVerifyTest004 start" << std::endl;
155 std::shared_ptr<AppEventPack> event = std::make_shared<AppEventPack>();
156 bool setRes = HiAppEventConfig::GetInstance().SetConfigurationItem("disable", "false");
157 EXPECT_TRUE(setRes);
158 event->SetDomain("testDomain");
159 event->SetName("testName");
160 int result = VerifyCustomEventParams(event);
161 EXPECT_EQ(result, ErrorCode::HIAPPEVENT_VERIFY_SUCCESSFUL);
162
163 event->AddParam("a%$321");
164 result = VerifyCustomEventParams(event);
165 EXPECT_EQ(result, ErrorCode::ERROR_INVALID_PARAM_NAME);
166 std::cout << "HiAppEventVerifyTest004 end" << std::endl;
167 }