• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 -PKG ohos.samples.ecg -fds";
59     std::string result = "";
60     bool flag = false;
61     bool ret = SPUtils::LoadCmd(cmd, result);
62     std::string::size_type strOne = result.find("command exec finished!");
63     if ((strOne != result.npos)) {
64         flag = true;
65     }
66     EXPECT_EQ(ret, true);
67     EXPECT_TRUE(flag);
68 }
69 
70 /**
71  * @tc.name: FdsTestCase03
72  * @tc.desc: Test Fds by pid not exit
73  * @tc.type: FUNC
74  */
75 HWTEST_F(SPdaemonFdsTest, FdsTestCase03, TestSize.Level1)
76 {
77     std::string cmd = "SP_daemon -N 1 -fds -PID 88888888"; // 88888888 is not exit
78     std::string result = "";
79     bool ret = SPUtils::LoadCmd(cmd, result);
80     EXPECT_EQ(ret, true);
81     ret = VerifResult(result);
82     EXPECT_EQ(ret, false);
83 }
84 
85 /**
86  * @tc.name: FdsTestCase04
87  * @tc.desc: Test Fds
88  * @tc.type: FUNC
89  */
90 HWTEST_F(SPdaemonFdsTest, FdsTestCase04, TestSize.Level1)
91 {
92     FileDescriptor &fdsInstance = FileDescriptor::GetInstance();
93     std::string packName = "init";
94     fdsInstance.SetPackageName(packName);
95     fdsInstance.SetProcessId("1");
96     std::map<std::string, std::string> fdsItemData = fdsInstance.ItemData();
97     std::string fdTotal = fdsItemData["fdTotal"];
98     std::string fds = fdsItemData["fds"];
99     EXPECT_EQ(fdTotal.empty(), false);
100     EXPECT_EQ(fds.empty(), false);
101 }
102 } // namespace OHOS
103 } // namespace SmartPerf