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 <gtest/gtest.h>
17 #include <regex>
18 #include <string>
19 #include "FileDescriptor.h"
20 #include "sp_utils.h"
21 using namespace testing::ext;
22 using namespace std;
23
24 namespace OHOS {
25 namespace SmartPerf {
26 class SPdaemonFdsTest : public testing::Test {
27 public:
SetUpTestCase()28 static void SetUpTestCase() {}
TearDownTestCase()29 static void TearDownTestCase() {}
30
SetUp()31 void SetUp() {}
TearDown()32 void TearDown() {}
33 };
34
VerifResult(const std::string & result)35 static bool VerifResult(const std::string &result)
36 {
37 int fdTotalInt = 0;
38 std::regex fdTotalRegex(R"(fdTotal=(\d+))");
39 std::smatch match;
40 std::string::const_iterator searchStart(result.cbegin());
41 while (std::regex_search(searchStart, result.cend(), match, fdTotalRegex)) {
42 std::cout << "Match found: " << match.str(0) << std::endl;
43 std::string fdTotalStr = match.str(1);
44 fdTotalInt = std::stoi(fdTotalStr);
45 std::cout << "fdTotalInt as integer: " << fdTotalInt << std::endl;
46 searchStart = match.suffix().first;
47 }
48 return fdTotalInt > 0;
49 }
50
51 /**
52 * @tc.name: FdsTestCase01
53 * @tc.desc: Test Fds by packagename
54 * @tc.type: FUNC
55 */
56 HWTEST_F(SPdaemonFdsTest, FdsTestCase01, TestSize.Level1)
57 {
58 std::string cmd = "SP_daemon -N 1 -fds -PKG sh";
59 std::string result = "";
60 bool ret = SPUtils::LoadCmd(cmd, result);
61 EXPECT_EQ(ret, true);
62 ret = VerifResult(result);
63 EXPECT_EQ(ret, true);
64 }
65
66 /**
67 * @tc.name: FdsTestCase02
68 * @tc.desc: Test Fds by pid
69 * @tc.type: FUNC
70 */
71 HWTEST_F(SPdaemonFdsTest, FdsTestCase02, TestSize.Level1)
72 {
73 std::string cmd = "SP_daemon -N 1 -fds -PID 1";
74 std::string result = "";
75 bool ret = SPUtils::LoadCmd(cmd, result);
76 EXPECT_EQ(ret, true);
77 ret = VerifResult(result);
78 EXPECT_EQ(ret, true);
79 }
80
81 /**
82 * @tc.name: FdsTestCase03
83 * @tc.desc: Test Fds by pid not exit
84 * @tc.type: FUNC
85 */
86 HWTEST_F(SPdaemonFdsTest, FdsTestCase03, TestSize.Level1)
87 {
88 std::string cmd = "SP_daemon -N 1 -fds -PID 88888888"; // 88888888 is not exit
89 std::string result = "";
90 bool ret = SPUtils::LoadCmd(cmd, result);
91 EXPECT_EQ(ret, true);
92 ret = VerifResult(result);
93 EXPECT_EQ(ret, false);
94 }
95
96 /**
97 * @tc.name: FdsTestCase04
98 * @tc.desc: Test Fds
99 * @tc.type: FUNC
100 */
101 HWTEST_F(SPdaemonFdsTest, FdsTestCase04, TestSize.Level1)
102 {
103 FileDescriptor &fdsInstance = FileDescriptor::GetInstance();
104 std::string packName = "init";
105 fdsInstance.SetPackageName(packName);
106 fdsInstance.SetProcessId("1");
107 std::map<std::string, std::string> fdsItemData = fdsInstance.ItemData();
108 std::string fdTotal = fdsItemData["fdTotal"];
109 std::string fds = fdsItemData["fds"];
110 EXPECT_EQ(fdTotal.empty(), false);
111 EXPECT_EQ(fds.empty(), false);
112 }
113 } // namespace OHOS
114 } // namespace SmartPerf