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 16 #include <gtest/gtest.h> 17 #include <ctime> 18 #include <securec.h> 19 #include <string> 20 #include <vector> 21 #include "dfx_util.h" 22 #include "procinfo.h" 23 24 using namespace OHOS::HiviewDFX; 25 using namespace testing::ext; 26 using namespace std; 27 28 namespace OHOS { 29 namespace HiviewDFX { 30 class ProcinfoTest : public testing::Test { 31 public: SetUpTestCase(void)32 static void SetUpTestCase(void) {} TearDownTestCase(void)33 static void TearDownTestCase(void) {} SetUp()34 void SetUp() {} TearDown()35 void TearDown() {} 36 }; 37 } // namespace HiviewDFX 38 } // namespace OHOS 39 40 /** 41 * @tc.name: ProcinfoTest001 42 * @tc.desc: test GetProcStatus 43 * @tc.type: FUNC 44 */ 45 HWTEST_F(ProcinfoTest, ProcinfoTest001, TestSize.Level2) 46 { 47 GTEST_LOG_(INFO) << "ProcinfoTest001: start."; 48 ProcInfo procInfo; 49 ASSERT_TRUE(GetProcStatus(procInfo)); 50 ASSERT_EQ(getpid(), procInfo.pid); 51 GTEST_LOG_(INFO) << "ProcinfoTest001: end."; 52 } 53 54 /** 55 * @tc.name: ProcinfoTest002 56 * @tc.desc: test GetTidsByPidWithFunc 57 * @tc.type: FUNC 58 */ 59 HWTEST_F(ProcinfoTest, ProcinfoTest002, TestSize.Level2) 60 { 61 GTEST_LOG_(INFO) << "ProcinfoTest002: start."; 62 std::vector<int> tids; 63 ASSERT_TRUE(GetTidsByPidWithFunc(getpid(), tids, nullptr)); 64 GTEST_LOG_(INFO) << "ProcinfoTest002: end."; 65 } 66 67 /** 68 * @tc.name: ProcinfoTest003 69 * @tc.desc: test GetProcStatusByPid, GetTidsByPid, IsThreadInPid 70 * @tc.type: FUNC 71 */ 72 HWTEST_F(ProcinfoTest, ProcinfoTest003, TestSize.Level2) 73 { 74 GTEST_LOG_(INFO) << "ProcinfoTest003: start."; 75 struct ProcInfo procInfo; 76 ASSERT_TRUE(GetProcStatusByPid(getpid(), procInfo)); 77 std::vector<int> tids; 78 std::vector<int> nstids; 79 ASSERT_TRUE(GetTidsByPid(getpid(), tids, nstids)); 80 for (size_t i = 0; i < nstids.size(); ++i) { 81 ASSERT_TRUE(IsThreadInPid(getpid(), nstids[i])); 82 if (procInfo.ns) { 83 int nstid = tids[i]; 84 TidToNstid(getpid(), tids[i], nstid); 85 ASSERT_EQ(nstid, nstids[i]); 86 } 87 } 88 GTEST_LOG_(INFO) << "ProcinfoTest003: end."; 89 } 90 91 /** 92 * @tc.name: ProcinfoTest004 93 * @tc.desc: test TidToNstid 94 * @tc.type: FUNC 95 */ 96 HWTEST_F(ProcinfoTest, ProcinfoTest004, TestSize.Level2) 97 { 98 GTEST_LOG_(INFO) << "ProcinfoTest004: start."; 99 int nstid = -1; 100 ASSERT_TRUE(TidToNstid(getpid(), gettid(), nstid)); 101 ASSERT_EQ(gettid(), nstid); 102 GTEST_LOG_(INFO) << "ProcinfoTest004: end."; 103 } 104 105 /** 106 * @tc.name: ProcinfoTest005 107 * @tc.desc: test ReadProcessStatus, ReadProcessWchan, ReadThreadWchan, ReadProcessName, ReadThreadName 108 * @tc.type: FUNC 109 */ 110 HWTEST_F(ProcinfoTest, ProcinfoTest005, TestSize.Level2) 111 { 112 GTEST_LOG_(INFO) << "ProcinfoTest005: start."; 113 std::string result; 114 ReadProcessStatus(result, getpid()); 115 GTEST_LOG_(INFO) << result; 116 ASSERT_TRUE(result.find("Name:") != std::string::npos); 117 ASSERT_TRUE(result.find("SigQ:") != std::string::npos); 118 ASSERT_TRUE(result.find("nonvoluntary_ctxt_switches") != std::string::npos); 119 ReadProcessWchan(result, getpid(), false, true); 120 GTEST_LOG_(INFO) << result; 121 ASSERT_TRUE(result.find("Process wchan:") != std::string::npos); 122 ReadThreadWchan(result, gettid(), true); 123 GTEST_LOG_(INFO) << result; 124 ASSERT_TRUE(result.find("Tid:") != std::string::npos); 125 ASSERT_TRUE(result.find("wchan:") != std::string::npos); 126 ReadProcessName(getpid(), result); 127 GTEST_LOG_(INFO) << result; 128 ASSERT_TRUE(result.find("test_procinfo") != std::string::npos); 129 ReadThreadName(getpid(), result); 130 GTEST_LOG_(INFO) << result; 131 ASSERT_TRUE(result.find("test_procinfo") != std::string::npos); 132 ReadThreadNameByPidAndTid(getpid(), gettid(), result); 133 GTEST_LOG_(INFO) << result; 134 ASSERT_TRUE(result.find("test_procinfo") != std::string::npos); 135 GTEST_LOG_(INFO) << "ProcinfoTest005: end."; 136 } 137