1 /*
2 * Copyright (c) 2022 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 "test_utils.h"
17
18 #include <iostream>
19 #include <map>
20 #include <string>
21 #include <vector>
22 #include <list>
23 #include <thread>
24 #include <memory>
25 #include <fcntl.h>
26 #include <cerrno>
27 #include <sys/stat.h>
28
29 #include "parameters.h"
30 #include "service_control.h"
31
32 namespace initModuleTest {
33 namespace {
34 constexpr size_t MAX_BUFFER_SIZE = 4096;
35 }
36
37 // File operator
ReadFileContent(const std::string & fileName,std::string & content)38 int ReadFileContent(const std::string &fileName, std::string &content)
39 {
40 content.clear();
41 auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(fileName.c_str(), "r"), fclose);
42 if (fp == nullptr) {
43 std::cout << "Cannot open file " << fileName << std::endl;
44 return -1;
45 }
46
47 struct stat st {};
48 if (stat(fileName.c_str(), &st) < 0) {
49 std::cout << "Cannot get " << fileName << " stat\n";
50 return -1;
51 }
52
53 ssize_t n = 0;
54 char buffer[MAX_BUFFER_SIZE] = {};
55 while ((n = fread(buffer, 1, MAX_BUFFER_SIZE, fp.get())) > 0) {
56 content.append(buffer, n);
57 }
58 return feof(fp.get()) ? 0 : -1;
59 }
60
StartsWith(const std::string & str,const std::string & prefix)61 bool StartsWith(const std::string &str, const std::string &prefix)
62 {
63 return ((str.size() > prefix.size()) && (str.substr(0, prefix.size()) == prefix));
64 }
65
EndsWith(const std::string & str,const std::string & suffix)66 bool EndsWith(const std::string &str, const std::string &suffix)
67 {
68 return ((str.size() > suffix.size()) && (str.substr(str.size() - suffix.size(), suffix.size()) == suffix));
69 }
70
Trim(const std::string & str)71 std::string Trim(const std::string &str)
72 {
73 size_t start = 0;
74 size_t end = str.size() - 1;
75
76 while (start < str.size()) {
77 if (!isspace(str[start])) {
78 break;
79 }
80 start++;
81 }
82
83 while (start < end) {
84 if (!isspace(str[end])) {
85 break;
86 }
87 end--;
88 }
89
90 if (end < start) {
91 return "";
92 }
93
94 return str.substr(start, end - start + 1);
95 }
96
Split(const std::string & str,const std::string & pattern)97 std::vector<std::string> Split(const std::string &str, const std::string &pattern)
98 {
99 std::vector<std::string> result {};
100 size_t pos = std::string::npos;
101 size_t start = 0;
102 while (true) {
103 pos = str.find_first_of(pattern, start);
104 result.push_back(str.substr(start, pos - start));
105 if (pos == std::string::npos) {
106 break;
107 }
108 start = pos + 1;
109 }
110 return result;
111 }
112
113 static std::map<ServiceStatus, std::string> g_serviceStatusMap = {
114 { SERVICE_IDLE, "idle"},
115 { SERVICE_STARTING, "starting"},
116 { SERVICE_STARTED, "running"},
117 { SERVICE_READY, "ready"},
118 { SERVICE_STOPPING, "stopping"},
119 { SERVICE_STOPPED, "stopped"},
120 { SERVICE_ERROR, "error" },
121 { SERVICE_SUSPENDED, "suspended" },
122 { SERVICE_FREEZED, "freezed" },
123 { SERVICE_DISABLED, "disabled" },
124 { SERVICE_CRITICAL, "critical" },
125 };
126
ValidStatus(ServiceStatus status)127 static inline bool ValidStatus(ServiceStatus status)
128 {
129 return status >= SERVICE_IDLE && status <= SERVICE_CRITICAL;
130 }
131
GetServiceStatus(const std::string & serviceName)132 std::string GetServiceStatus(const std::string &serviceName)
133 {
134 if (serviceName.empty()) {
135 return "";
136 }
137 const std::string serviceCtlPrefix = "startup.service.ctl.";
138 const std::string serviceCtlName = serviceCtlPrefix + serviceName;
139 uint32_t ret = OHOS::system::GetUintParameter<uint32_t>(serviceCtlName, 0);
140 ServiceStatus status = static_cast<ServiceStatus>(ret);
141 if (!ValidStatus(status)) {
142 return "";
143 }
144 return g_serviceStatusMap[status];
145 }
146
147 } // initModuleTest
148