1/*
2 * Copyright (c) 2023-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 "hisysevent_delay_test.h"
17
18#include <gtest/gtest.h>
19
20#include <iosfwd>
21#include <string>
22#include <thread>
23#include <unistd.h>
24#include <vector>
25
26#include "gtest/gtest-message.h"
27#include "gtest/gtest-test-part.h"
28#include "gtest/hwext/gtest-ext.h"
29#include "gtest/hwext/gtest-tag.h"
30
31#include "hisysevent.h"
32#include "string_ex.h"
33#include "string_util.h"
34
35using namespace testing::ext;
36using namespace OHOS;
37using namespace OHOS::HiviewDFX;
38
39namespace {
40constexpr int WROTE_TOTAL_CNT = 30;
41
42void WriteStringWithLength(const std::string testCaseName, const std::string testCaseDescription, int cnt)
43{
44    string param;
45    param.append(cnt, 'a');
46    std::vector<int> wroteRet;
47    int ret = SUCCESS;
48    for (int i = 0; i < WROTE_TOTAL_CNT; ++i) {
49        ret = HiSysEventWrite(HiSysEvent::Domain::AAFWK, "LIFECYCLE_TIMEOUT", HiSysEvent::EventType::FAULT, "key",
50            param);
51        wroteRet.emplace_back(ret);
52    }
53    ASSERT_EQ(wroteRet.size(), WROTE_TOTAL_CNT);
54    ASSERT_EQ(std::count(wroteRet.begin(), wroteRet.end(), SUCCESS), WROTE_TOTAL_CNT);
55}
56
57template<typename T>
58void WriteSingleValue(const std::string testCaseName, const std::string testCaseDescription, T val)
59{
60    std::vector<int> wroteRet;
61    int ret = SUCCESS;
62    for (int i = 0; i < WROTE_TOTAL_CNT; ++i) {
63        ret = HiSysEventWrite(HiSysEvent::Domain::AAFWK, "LIFECYCLE_TIMEOUT", HiSysEvent::EventType::FAULT, "key",
64            val);
65        wroteRet.emplace_back(ret);
66    }
67    ASSERT_EQ(wroteRet.size(), WROTE_TOTAL_CNT);
68    ASSERT_EQ(std::count(wroteRet.begin(), wroteRet.end(), SUCCESS), WROTE_TOTAL_CNT);
69}
70}
71
72void HiSysEventDelayTest::SetUpTestCase(void)
73{
74}
75
76void HiSysEventDelayTest::TearDownTestCase(void)
77{
78}
79
80void HiSysEventDelayTest::SetUp(void)
81{
82}
83
84void HiSysEventDelayTest::TearDown(void)
85{
86}
87
88/**
89 * @tc.name: HiSysEventDelayTest001
90 * @tc.desc: Write a sysevent without any parameter
91 * @tc.type: FUNC
92 * @tc.require: issueI76V6J
93 */
94HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest001, TestSize.Level1)
95{
96    std::vector<int> wroteRet;
97    int ret = SUCCESS;
98    for (int i = 0; i < WROTE_TOTAL_CNT; ++i) {
99        ret = HiSysEventWrite(HiSysEvent::Domain::AAFWK, "LIFECYCLE_TIMEOUT", HiSysEvent::EventType::FAULT);
100        wroteRet.emplace_back(ret);
101    }
102    ASSERT_EQ(wroteRet.size(), WROTE_TOTAL_CNT);
103    ASSERT_EQ(std::count(wroteRet.begin(), wroteRet.end(), SUCCESS), WROTE_TOTAL_CNT);
104}
105
106/**
107 * @tc.name: HiSysEventDelayTest002
108 * @tc.desc: Write sysevent with a bool parameter
109 * @tc.type: FUNC
110 * @tc.require: issueI76V6J
111 */
112HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest002, TestSize.Level1)
113{
114    bool value = true;
115    WriteSingleValue("HiSysEventDelayTest002", "Write sysevent with a bool parameter", value);
116}
117
118/**
119 * @tc.name: HiSysEventDelayTest003
120 * @tc.desc: Write sysevent with a char parameter
121 * @tc.type: FUNC
122 * @tc.require: issueI76V6J
123 */
124HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest003, TestSize.Level1)
125{
126    char value = 'a';
127    WriteSingleValue("HiSysEventDelayTest003", "Write sysevent with a char parameter", value);
128}
129
130/**
131 * @tc.name: HiSysEventDelayTest004
132 * @tc.desc: Write sysevent with a double parameter
133 * @tc.type: FUNC
134 * @tc.require: issueI76V6J
135 */
136HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest004, TestSize.Level1)
137{
138    double value = 30949.374;
139    WriteSingleValue("HiSysEventDelayTest004", "Write sysevent with a double parameter", value);
140}
141
142/**
143 * @tc.name: HiSysEventDelayTest005
144 * @tc.desc: Write sysevent with a float parameter
145 * @tc.type: FUNC
146 * @tc.require: issueI76V6J
147 */
148HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest005, TestSize.Level1)
149{
150    float value = 230.47;
151    WriteSingleValue("HiSysEventDelayTest005", "Write sysevent with a float parameter", value);
152}
153
154/**
155 * @tc.name: HiSysEventDelayTest006
156 * @tc.desc: Write sysevent with a integer parameter
157 * @tc.type: FUNC
158 * @tc.require: issueI76V6J
159 */
160HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest006, TestSize.Level1)
161{
162    int value = 100;
163    WriteSingleValue("HiSysEventDelayTest006", "Write sysevent with a integer parameter", value);
164}
165
166/**
167 * @tc.name: HiSysEventDelayTest007
168 * @tc.desc: Write sysevent with a long parameter
169 * @tc.type: FUNC
170 * @tc.require: issueI76V6J
171 */
172HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest007, TestSize.Level1)
173{
174    long value = 1000000;
175    WriteSingleValue("HiSysEventDelayTest007", "Write sysevent with a long parameter", value);
176}
177
178/**
179 * @tc.name: HiSysEventDelayTest008
180 * @tc.desc: Write sysevent with a short parameter
181 * @tc.type: FUNC
182 * @tc.require: issueI76V6J
183 */
184HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest008, TestSize.Level1)
185{
186    short value = 10;
187    WriteSingleValue("HiSysEventDelayTest008", "Write sysevent with a short parameter", value);
188}
189
190/**
191 * @tc.name: HiSysEventDelayTest009
192 * @tc.desc: Write a sysevent with a string param whose length is 32
193 * @tc.type: FUNC
194 * @tc.require: issueI76V6J
195 */
196HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest009, TestSize.Level1)
197{
198    WriteStringWithLength("HiSysEventDelayTest009", "Write a sysevent with a string param whose length is 32",
199        32);
200}
201
202/**
203 * @tc.name: HiSysEventDelayTest010
204 * @tc.desc: Write a sysevent with a string param whose length is 64
205 * @tc.type: FUNC
206 * @tc.require: issueI76V6J
207 */
208HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest010, TestSize.Level1)
209{
210    WriteStringWithLength("HiSysEventDelayTest010", "Write a sysevent with a string param whose length is 64",
211        64);
212}
213
214/**
215 * @tc.name: HiSysEventDelayTest011
216 * @tc.desc: Write a sysevent with a string param whose length is 128
217 * @tc.type: FUNC
218 * @tc.require: issueI76V6J
219 */
220HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest011, TestSize.Level1)
221{
222    WriteStringWithLength("HiSysEventDelayTest011", "Write a sysevent with a string param whose length is 128",
223        128);
224}
225
226/**
227 * @tc.name: HiSysEventDelayTest012
228 * @tc.desc: Write a sysevent with a string param whose length is 256
229 * @tc.type: FUNC
230 * @tc.require: issueI76V6J
231 */
232HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest012, TestSize.Level1)
233{
234    WriteStringWithLength("HiSysEventDelayTest012", "Write a sysevent with a string param whose length is 256",
235        256);
236}
237
238
239/**
240 * @tc.name: HiSysEventDelayTest013
241 * @tc.desc: Write a sysevent with a string param whose length is 512
242 * @tc.type: FUNC
243 * @tc.require: issueI76V6J
244 */
245HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest013, TestSize.Level1)
246{
247    WriteStringWithLength("HiSysEventDelayTest013", "Write a sysevent with a string param whose length is 512",
248        512);
249}
250
251/**
252 * @tc.name: HiSysEventDelayTest014
253 * @tc.desc: Write a sysevent with a string param whose length is 1024
254 * @tc.type: FUNC
255 * @tc.require: issueI76V6J
256 */
257HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest014, TestSize.Level1)
258{
259    WriteStringWithLength("HiSysEventDelayTest014", "Write a sysevent with a string param whose length is 1024",
260        1024);
261}
262
263/**
264 * @tc.name: HiSysEventDelayTest015
265 * @tc.desc: Write a sysevent with a string param whose length is 2048
266 * @tc.type: FUNC
267 * @tc.require: issueI76V6J
268 */
269HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest015, TestSize.Level1)
270{
271    WriteStringWithLength("HiSysEventDelayTest015", "Write a sysevent with a string param whose length is 2048",
272        2048);
273}
274
275/**
276 * @tc.name: HiSysEventDelayTest016
277 * @tc.desc: Write a sysevent with a string param whose length is 3072
278 * @tc.type: FUNC
279 * @tc.require: issueI76V6J
280 */
281HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest016, TestSize.Level1)
282{
283    WriteStringWithLength("HiSysEventDelayTest016", "Write a sysevent with a string param whose length is 3072",
284        3072);
285}
286
287/**
288 * @tc.name: HiSysEventDelayTest017
289 * @tc.desc: Write a sysevent with a string param whose length is 4096
290 * @tc.type: FUNC
291 * @tc.require: issueI76V6J
292 */
293HWTEST_F(HiSysEventDelayTest, HiSysEventDelayTest017, TestSize.Level1)
294{
295    WriteStringWithLength("HiSysEventDelayTest017", "Write a sysevent with a string param whose length is 4096",
296        4096);
297}