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 "dfx_test_util.h" 23 #include "procinfo.h" 24 25 using namespace OHOS::HiviewDFX; 26 using namespace testing::ext; 27 using namespace std; 28 29 namespace OHOS { 30 namespace HiviewDFX { 31 class ProcinfoTest : public testing::Test { 32 public: SetUpTestCase(void)33 static void SetUpTestCase(void) {} TearDownTestCase(void)34 static void TearDownTestCase(void) {} SetUp()35 void SetUp() {} TearDown()36 void TearDown() {} 37 }; 38 } // namespace HiviewDFX 39 } // namespace OHOS 40 41 /** 42 * @tc.name: ProcinfoTest001 43 * @tc.desc: test GetProcStatus 44 * @tc.type: FUNC 45 */ 46 HWTEST_F(ProcinfoTest, ProcinfoTest001, TestSize.Level2) 47 { 48 GTEST_LOG_(INFO) << "ProcinfoTest001: start."; 49 ProcInfo procInfo; 50 ASSERT_TRUE(GetProcStatus(procInfo)); 51 ASSERT_EQ(getpid(), procInfo.pid); 52 GTEST_LOG_(INFO) << "ProcinfoTest001: end."; 53 } 54 55 /** 56 * @tc.name: ProcinfoTest002 57 * @tc.desc: test GetTidsByPidWithFunc 58 * @tc.type: FUNC 59 */ 60 HWTEST_F(ProcinfoTest, ProcinfoTest002, TestSize.Level2) 61 { 62 GTEST_LOG_(INFO) << "ProcinfoTest002: start."; 63 std::vector<int> tids; 64 ASSERT_TRUE(GetTidsByPidWithFunc(getpid(), tids, nullptr)); 65 GTEST_LOG_(INFO) << "ProcinfoTest002: end."; 66 } 67 68 /** 69 * @tc.name: ProcinfoTest003 70 * @tc.desc: test GetProcStatusByPid, GetTidsByPid, IsThreadInPid 71 * @tc.type: FUNC 72 */ 73 HWTEST_F(ProcinfoTest, ProcinfoTest003, TestSize.Level0) 74 { 75 GTEST_LOG_(INFO) << "ProcinfoTest003: start."; 76 struct ProcInfo procInfo; 77 ASSERT_TRUE(GetProcStatusByPid(getpid(), procInfo)); 78 std::vector<int> tids; 79 std::vector<int> nstids; 80 ASSERT_TRUE(GetTidsByPid(getpid(), tids, nstids)); 81 for (size_t i = 0; i < nstids.size(); ++i) { 82 ASSERT_TRUE(IsThreadInPid(getpid(), nstids[i])); 83 if (procInfo.ns) { 84 int nstid = tids[i]; 85 TidToNstid(getpid(), tids[i], nstid); 86 ASSERT_EQ(nstid, nstids[i]); 87 } 88 } 89 GTEST_LOG_(INFO) << "ProcinfoTest003: end."; 90 } 91 92 /** 93 * @tc.name: ProcinfoTest004 94 * @tc.desc: test TidToNstid 95 * @tc.type: FUNC 96 */ 97 HWTEST_F(ProcinfoTest, ProcinfoTest004, TestSize.Level2) 98 { 99 GTEST_LOG_(INFO) << "ProcinfoTest004: start."; 100 int nstid = -1; 101 ASSERT_TRUE(TidToNstid(getpid(), gettid(), nstid)); 102 ASSERT_EQ(gettid(), nstid); 103 GTEST_LOG_(INFO) << "ProcinfoTest004: end."; 104 } 105 106 /** 107 * @tc.name: ProcinfoTest005 108 * @tc.desc: test ReadProcessStatus, ReadProcessWchan, ReadThreadWchan, ReadProcessName, ReadThreadName 109 * @tc.type: FUNC 110 */ 111 HWTEST_F(ProcinfoTest, ProcinfoTest005, TestSize.Level2) 112 { 113 GTEST_LOG_(INFO) << "ProcinfoTest005: start."; 114 std::string result; 115 ReadProcessStatus(result, getpid()); 116 GTEST_LOG_(INFO) << result; 117 ASSERT_TRUE(result.find("Name:") != std::string::npos); 118 ASSERT_TRUE(result.find("SigQ:") != std::string::npos); 119 ASSERT_TRUE(result.find("nonvoluntary_ctxt_switches") != std::string::npos); 120 ReadProcessWchan(result, getpid(), false, true); 121 GTEST_LOG_(INFO) << result; 122 ASSERT_TRUE(result.find("Process wchan:") != std::string::npos); 123 ReadThreadWchan(result, gettid(), true); 124 GTEST_LOG_(INFO) << result; 125 ASSERT_TRUE(result.find("Tid:") != std::string::npos); 126 ASSERT_TRUE(result.find("wchan:") != std::string::npos); 127 ReadProcessName(getpid(), result); 128 GTEST_LOG_(INFO) << result; 129 ASSERT_TRUE(result.find("test_procinfo") != std::string::npos); 130 ReadThreadName(getpid(), result); 131 GTEST_LOG_(INFO) << result; 132 ASSERT_TRUE(result.find("test_procinfo") != std::string::npos); 133 ReadThreadNameByPidAndTid(getpid(), gettid(), result); 134 GTEST_LOG_(INFO) << result; 135 ASSERT_TRUE(result.find("test_procinfo") != std::string::npos); 136 GTEST_LOG_(INFO) << "ProcinfoTest005: end."; 137 } 138 139 /** 140 * @tc.name: GetTidByThreadName006 141 * @tc.desc: test GetWatchdogTidByPid 142 * @tc.type: FUNC 143 */ 144 HWTEST_F(ProcinfoTest, GetTidByThreadName006, TestSize.Level2) 145 { 146 pid_t watchdogTid = GetTidByThreadName(-1, "OS_DfxWatchdog"); 147 ASSERT_TRUE(watchdogTid == -1); 148 149 watchdogTid = GetTidByThreadName(getpid(), "OS_DfxWatchdog"); 150 ASSERT_TRUE(watchdogTid == -1); 151 __anon998440de0102null152 std::thread watchdogThread([] { 153 pthread_setname_np(pthread_self(), "OS_DfxWatchdog"); 154 std::this_thread::sleep_for(std::chrono::seconds(2)); // 2 : sleep for 2 seconds 155 }); 156 usleep(5000); // 5 : sleep for 5 milliseconds 157 watchdogTid = GetTidByThreadName(getpid(), "OS_DfxWatchdog"); 158 ASSERT_TRUE(watchdogTid != -1); 159 160 watchdogThread.join(); 161 } 162