• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "sys_event_stat_test.h"
16 
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <fstream>
22 
23 #include "sys_event_stat.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
SetUpTestCase()27 void SysEventStatTest::SetUpTestCase() {}
28 
TearDownTestCase()29 void SysEventStatTest::TearDownTestCase() {}
30 
SetUp()31 void SysEventStatTest::SetUp() {}
32 
TearDown()33 void SysEventStatTest::TearDown() {}
34 
OpenTestFile(const char * filename)35 static int OpenTestFile(const char *filename)
36 {
37     int fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
38     if (fd <= 0) {
39         printf("[ NET ]:open file error\n");
40         return -1;
41     }
42     return fd;
43 }
44 
GetFileContent(const std::string & filename)45 static std::string GetFileContent(const std::string& filename)
46 {
47     std::ifstream inputfile(filename);
48     std::string contents;
49     std::string temp;
50     while (inputfile >> temp) {
51         contents.append(temp);
52     }
53     return contents;
54 }
55 
56 /**
57  * @tc.name: SysEventStatTest001
58  * @tc.desc: check the sys event statistics function.
59  * @tc.type: FUNC
60  * @tc.require: issueI62WJT
61  */
62 HWTEST_F(SysEventStatTest, SysEventStatTest001, testing::ext::TestSize.Level0)
63 {
64     SysEventStat sysEventStat_;
65     sysEventStat_.AccumulateEvent(true);
66     sysEventStat_.AccumulateEvent(false);
67     sysEventStat_.AccumulateEvent("domain1", "eventName_1");
68     sysEventStat_.AccumulateEvent("domain1", "eventName_2");
69     sysEventStat_.AccumulateEvent("domain2", "eventName_3");
70     sysEventStat_.AccumulateEvent("domain4", "eventName_4", false);
71     sysEventStat_.AccumulateEvent("domain4", "eventName_5", false);
72     sysEventStat_.AccumulateEvent("domain5", "eventName_6", false);
73     int fd1 = OpenTestFile("./fd1.txt");
74     ASSERT_FALSE(fd1 == -1);
75     sysEventStat_.StatSummary(fd1);
76     close(fd1);
77     std::string result1 = GetFileContent("./fd1.txt");
78     ASSERT_TRUE(result1.size() > 0);
79 
80     int fd2 = OpenTestFile("./fd2.txt");
81     sysEventStat_.StatDetail(fd2);
82     close(fd2);
83     std::string result2 = GetFileContent("./fd2.txt");
84     ASSERT_TRUE(result2.size() > 0);
85 
86     int fd3 = OpenTestFile("./fd3.txt");
87     sysEventStat_.StatInvalidDetail(fd3);
88     close(fd3);
89     std::string result3 = GetFileContent("./fd3.txt");
90     ASSERT_TRUE(result3.size() > 0);
91 
92     int fd4 = OpenTestFile("./fd4.txt");
93     sysEventStat_.Clear(fd4);
94     close(fd4);
95     std::string result4 = GetFileContent("./fd4.txt");
96     std::cout << "result4:" << result4 << std::endl;
97     ASSERT_TRUE(result4 == "cleanallstatinfo");
98 }
99 } // namespace HiviewDFX
100 } // namespace OHOS
101