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 #ifndef OHOS_IPC_TEST_HELPER_H
17 #define OHOS_IPC_TEST_HELPER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include <dirent.h>
23 #include <iostream>
24 #include <sys/types.h>
25 #include "ipc_debug.h"
26 #include "log_tags.h"
27 
28 namespace OHOS {
29 const static int INVALID_PID = -1;
30 
31 class IPCTestHelper {
32 public:
33     enum {
34         IPC_TEST_SAMGR,
35         IPC_TEST_SERVER,
36         IPC_TEST_CLIENT,
37         IPC_TEST_MSG_SERVER,
38         IPC_TEST_MSG_CLIENT,
39         IPC_TEST_SERVER_EXTRA,
40         IPC_TEST_NONE = 0xFF,
41     };
42 
43     IPCTestHelper();
44     ~IPCTestHelper();
45     static const std::string &GetTestAppName(int appId);
46     pid_t GetPidByName(std::string task_name);
47     bool  GetChildPids(std::vector<pid_t> &childPids);
48 
49     pid_t StartExecutable(std::string name, std::string args = "");
50 
51     bool StopExecutable(pid_t pid);
52     bool StopExecutable(std::string name);
53 
54     bool PrepareTestSuite();
55     bool TearDownTestSuite();
56 
57     void AddTestAppPid(const std::string &appName, const int &pid);
58     void RemoveTestAppPid(const std::string &appName);
59 
60     bool StartTestApp(int appId, const int &cmdId = 0);
61     bool StopTestApp(int appId);
62 
63     pid_t GetTestAppPid(int appId);
64 
65     pid_t GetPid();
66     uid_t GetUid();
67 
68     long GetCurrentTimeMs();
69 
70 private:
71     std::mutex mutex_;
72     std::map<std::string, pid_t> testPids_;
73     static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_TEST, "IPCTestHelper" };
74 };
75 } // namespace OHOS
76 #endif // OHOS_IPC_TEST_HELPER_H
77