1 /*
2 * Copyright (c) 2021 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 "hiview_plugin_platform_module_test.h"
17
18 #include "string_util.h"
19
20 using namespace testing::ext;
21 using namespace OHOS::HiviewDFX;
22
23 namespace {
GetCmdResultFromPopen(const std::string & cmd)24 std::string GetCmdResultFromPopen(const std::string& cmd)
25 {
26 if (cmd.empty()) {
27 return "";
28 }
29
30 FILE* fp = popen(cmd.c_str(), "r");
31 if (fp == nullptr) {
32 return "";
33 }
34 const int bufSize = 128;
35 char buffer[bufSize];
36 std::string result = "";
37 while (!feof(fp)) {
38 if (fgets(buffer, bufSize - 1, fp) != NULL) {
39 result += buffer;
40 }
41 }
42 pclose(fp);
43 return result;
44 }
45
GetServicePid(const std::string & serviceName)46 int GetServicePid(const std::string& serviceName)
47 {
48 std::string cmd = "pidof " + serviceName;
49 std::string pidStr = GetCmdResultFromPopen(cmd);
50 int32_t pid = 0;
51 OHOS::HiviewDFX::StringUtil::ConvertStringTo<int32_t>(pidStr, pid);
52 printf("the pid of service(%s) is %s \n", serviceName.c_str(), pidStr.c_str());
53 return pid;
54 }
55
WaitForServiceReady(const std::string & serviceName)56 void WaitForServiceReady(const std::string& serviceName)
57 {
58 int pid = GetServicePid(serviceName);
59 if (pid <= 0) {
60 std::string cmd = "start " + serviceName;
61 GetCmdResultFromPopen(cmd);
62 const int sleepTime = 10; // 10 seconds
63 sleep(sleepTime);
64 pid = GetServicePid(serviceName);
65 }
66 ASSERT_GT(pid, 0);
67 }
68 }
69
70 /**
71 * @tc.name: HiviewStatusTest001
72 * @tc.desc: check hiview running status and ensure it has been started
73 * @tc.type: FUNC
74 * @tc.require: SR000DPTSQ
75 */
76 HWTEST_F(HiviewPluginPlatformModuleTest, HiviewStatusTest001, TestSize.Level0)
77 {
78 WaitForServiceReady("hiview");
79 }
80