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