1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License. 5800b99b8Sopenharmony_ci * You may obtain a copy of the License at 6800b99b8Sopenharmony_ci * 7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8800b99b8Sopenharmony_ci * 9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and 13800b99b8Sopenharmony_ci * limitations under the License. 14800b99b8Sopenharmony_ci */ 15800b99b8Sopenharmony_ci 16800b99b8Sopenharmony_ci#include <gtest/gtest.h> 17800b99b8Sopenharmony_ci#include <ctime> 18800b99b8Sopenharmony_ci#include <securec.h> 19800b99b8Sopenharmony_ci#include <string> 20800b99b8Sopenharmony_ci#include <vector> 21800b99b8Sopenharmony_ci#include "dfx_util.h" 22800b99b8Sopenharmony_ci#include "procinfo.h" 23800b99b8Sopenharmony_ci 24800b99b8Sopenharmony_ciusing namespace OHOS::HiviewDFX; 25800b99b8Sopenharmony_ciusing namespace testing::ext; 26800b99b8Sopenharmony_ciusing namespace std; 27800b99b8Sopenharmony_ci 28800b99b8Sopenharmony_cinamespace OHOS { 29800b99b8Sopenharmony_cinamespace HiviewDFX { 30800b99b8Sopenharmony_ciclass ProcinfoTest : public testing::Test { 31800b99b8Sopenharmony_cipublic: 32800b99b8Sopenharmony_ci static void SetUpTestCase(void) {} 33800b99b8Sopenharmony_ci static void TearDownTestCase(void) {} 34800b99b8Sopenharmony_ci void SetUp() {} 35800b99b8Sopenharmony_ci void TearDown() {} 36800b99b8Sopenharmony_ci}; 37800b99b8Sopenharmony_ci} // namespace HiviewDFX 38800b99b8Sopenharmony_ci} // namespace OHOS 39800b99b8Sopenharmony_ci 40800b99b8Sopenharmony_ci/** 41800b99b8Sopenharmony_ci * @tc.name: ProcinfoTest001 42800b99b8Sopenharmony_ci * @tc.desc: test GetProcStatus 43800b99b8Sopenharmony_ci * @tc.type: FUNC 44800b99b8Sopenharmony_ci */ 45800b99b8Sopenharmony_ciHWTEST_F(ProcinfoTest, ProcinfoTest001, TestSize.Level2) 46800b99b8Sopenharmony_ci{ 47800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ProcinfoTest001: start."; 48800b99b8Sopenharmony_ci ProcInfo procInfo; 49800b99b8Sopenharmony_ci ASSERT_TRUE(GetProcStatus(procInfo)); 50800b99b8Sopenharmony_ci ASSERT_EQ(getpid(), procInfo.pid); 51800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ProcinfoTest001: end."; 52800b99b8Sopenharmony_ci} 53800b99b8Sopenharmony_ci 54800b99b8Sopenharmony_ci/** 55800b99b8Sopenharmony_ci * @tc.name: ProcinfoTest002 56800b99b8Sopenharmony_ci * @tc.desc: test GetTidsByPidWithFunc 57800b99b8Sopenharmony_ci * @tc.type: FUNC 58800b99b8Sopenharmony_ci */ 59800b99b8Sopenharmony_ciHWTEST_F(ProcinfoTest, ProcinfoTest002, TestSize.Level2) 60800b99b8Sopenharmony_ci{ 61800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ProcinfoTest002: start."; 62800b99b8Sopenharmony_ci std::vector<int> tids; 63800b99b8Sopenharmony_ci ASSERT_TRUE(GetTidsByPidWithFunc(getpid(), tids, nullptr)); 64800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ProcinfoTest002: end."; 65800b99b8Sopenharmony_ci} 66800b99b8Sopenharmony_ci 67800b99b8Sopenharmony_ci/** 68800b99b8Sopenharmony_ci * @tc.name: ProcinfoTest003 69800b99b8Sopenharmony_ci * @tc.desc: test GetProcStatusByPid, GetTidsByPid, IsThreadInPid 70800b99b8Sopenharmony_ci * @tc.type: FUNC 71800b99b8Sopenharmony_ci */ 72800b99b8Sopenharmony_ciHWTEST_F(ProcinfoTest, ProcinfoTest003, TestSize.Level2) 73800b99b8Sopenharmony_ci{ 74800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ProcinfoTest003: start."; 75800b99b8Sopenharmony_ci struct ProcInfo procInfo; 76800b99b8Sopenharmony_ci ASSERT_TRUE(GetProcStatusByPid(getpid(), procInfo)); 77800b99b8Sopenharmony_ci std::vector<int> tids; 78800b99b8Sopenharmony_ci std::vector<int> nstids; 79800b99b8Sopenharmony_ci ASSERT_TRUE(GetTidsByPid(getpid(), tids, nstids)); 80800b99b8Sopenharmony_ci for (size_t i = 0; i < nstids.size(); ++i) { 81800b99b8Sopenharmony_ci ASSERT_TRUE(IsThreadInPid(getpid(), nstids[i])); 82800b99b8Sopenharmony_ci if (procInfo.ns) { 83800b99b8Sopenharmony_ci int nstid = tids[i]; 84800b99b8Sopenharmony_ci TidToNstid(getpid(), tids[i], nstid); 85800b99b8Sopenharmony_ci ASSERT_EQ(nstid, nstids[i]); 86800b99b8Sopenharmony_ci } 87800b99b8Sopenharmony_ci } 88800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ProcinfoTest003: end."; 89800b99b8Sopenharmony_ci} 90800b99b8Sopenharmony_ci 91800b99b8Sopenharmony_ci/** 92800b99b8Sopenharmony_ci * @tc.name: ProcinfoTest004 93800b99b8Sopenharmony_ci * @tc.desc: test TidToNstid 94800b99b8Sopenharmony_ci * @tc.type: FUNC 95800b99b8Sopenharmony_ci */ 96800b99b8Sopenharmony_ciHWTEST_F(ProcinfoTest, ProcinfoTest004, TestSize.Level2) 97800b99b8Sopenharmony_ci{ 98800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ProcinfoTest004: start."; 99800b99b8Sopenharmony_ci int nstid = -1; 100800b99b8Sopenharmony_ci ASSERT_TRUE(TidToNstid(getpid(), gettid(), nstid)); 101800b99b8Sopenharmony_ci ASSERT_EQ(gettid(), nstid); 102800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ProcinfoTest004: end."; 103800b99b8Sopenharmony_ci} 104800b99b8Sopenharmony_ci 105800b99b8Sopenharmony_ci/** 106800b99b8Sopenharmony_ci * @tc.name: ProcinfoTest005 107800b99b8Sopenharmony_ci * @tc.desc: test ReadProcessStatus, ReadProcessWchan, ReadThreadWchan, ReadProcessName, ReadThreadName 108800b99b8Sopenharmony_ci * @tc.type: FUNC 109800b99b8Sopenharmony_ci */ 110800b99b8Sopenharmony_ciHWTEST_F(ProcinfoTest, ProcinfoTest005, TestSize.Level2) 111800b99b8Sopenharmony_ci{ 112800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ProcinfoTest005: start."; 113800b99b8Sopenharmony_ci std::string result; 114800b99b8Sopenharmony_ci ReadProcessStatus(result, getpid()); 115800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << result; 116800b99b8Sopenharmony_ci ASSERT_TRUE(result.find("Name:") != std::string::npos); 117800b99b8Sopenharmony_ci ASSERT_TRUE(result.find("SigQ:") != std::string::npos); 118800b99b8Sopenharmony_ci ASSERT_TRUE(result.find("nonvoluntary_ctxt_switches") != std::string::npos); 119800b99b8Sopenharmony_ci ReadProcessWchan(result, getpid(), false, true); 120800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << result; 121800b99b8Sopenharmony_ci ASSERT_TRUE(result.find("Process wchan:") != std::string::npos); 122800b99b8Sopenharmony_ci ReadThreadWchan(result, gettid(), true); 123800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << result; 124800b99b8Sopenharmony_ci ASSERT_TRUE(result.find("Tid:") != std::string::npos); 125800b99b8Sopenharmony_ci ASSERT_TRUE(result.find("wchan:") != std::string::npos); 126800b99b8Sopenharmony_ci ReadProcessName(getpid(), result); 127800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << result; 128800b99b8Sopenharmony_ci ASSERT_TRUE(result.find("test_procinfo") != std::string::npos); 129800b99b8Sopenharmony_ci ReadThreadName(getpid(), result); 130800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << result; 131800b99b8Sopenharmony_ci ASSERT_TRUE(result.find("test_procinfo") != std::string::npos); 132800b99b8Sopenharmony_ci ReadThreadNameByPidAndTid(getpid(), gettid(), result); 133800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << result; 134800b99b8Sopenharmony_ci ASSERT_TRUE(result.find("test_procinfo") != std::string::npos); 135800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ProcinfoTest005: end."; 136800b99b8Sopenharmony_ci} 137