1/* 2 * Copyright (c) 2024 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#ifndef APPSPAWN_TEST_CMD_H 16#define APPSPAWN_TEST_CMD_H 17 18#include <cstdint> 19#include <cstdio> 20#include <cstdlib> 21#include <string> 22#include <vector> 23 24#include "appspawn.h" 25#include "appspawn_msg.h" 26#include "appspawn_utils.h" 27#include "cJSON.h" 28#include "json_utils.h" 29#include "securec.h" 30#include "thread_manager.h" 31 32typedef struct TagThreadContext { 33} ThreadContext; 34 35namespace OHOS { 36namespace AppSpawnModuleTest { 37class AppSpawnTestCommander : public ThreadContext { 38public: 39 AppSpawnTestCommander() 40 { 41 exit_ = 0; 42 appSpawn_ = 1; 43 dumpFlags = 0; 44 msgType_ = MAX_TYPE_INVALID; 45 terminatePid_ = 0; 46 } 47 explicit AppSpawnTestCommander(int serverType) 48 { 49 exit_ = 0; 50 appSpawn_ = serverType; 51 dumpFlags = 0; 52 msgType_ = MAX_TYPE_INVALID; 53 terminatePid_ = 0; 54 } 55 ~AppSpawnTestCommander() 56 { 57 if (ptyFd_ != -1) { 58 (void)close(ptyFd_); 59 ptyFd_ = -1; 60 } 61 if (appInfoConfig_) { 62 cJSON_Delete(appInfoConfig_); 63 appInfoConfig_ = nullptr; 64 } 65 } 66 67 int ProcessArgs(int argc, char *const argv[]); 68 int Run(); 69 70 int CreateOtherMsg(AppSpawnReqMsgHandle &reqHandle, pid_t pid); 71 int CreateMsg(AppSpawnReqMsgHandle &reqHandle, const char *defaultConfig, 72 uint32_t defMsgType = MAX_TYPE_INVALID); 73 int StartSendMsg(); 74 int SendMsg(); 75 AppSpawnClientHandle GetClientHandle() 76 { 77 return clientHandle_; 78 } 79 80private: 81 std::vector<std::string> split(const std::string &str, const std::string &pattern); 82 int InitPtyInterface(); 83 int ProcessInputCmd(std::string &cmd); 84 int AddExtTlv(const cJSON *appInfoConfig, AppSpawnReqMsgHandle reqHandle); 85 int BuildMsgFromJson(const cJSON *appInfoConfig, AppSpawnReqMsgHandle reqHandle); 86 int AddBundleInfoFromJson(const cJSON *appInfoConfig, AppSpawnReqMsgHandle reqHandle); 87 int AddDacInfoFromJson(const cJSON *appInfoConfig, AppSpawnReqMsgHandle reqHandle); 88 int AddInternetPermissionInfoFromJson(const cJSON *appInfoConfig, AppSpawnReqMsgHandle reqHandle); 89 int AddAccessTokenFromJson(const cJSON *appInfoConfig, AppSpawnReqMsgHandle reqHandle); 90 int AddDomainInfoFromJson(const cJSON *appInfoConfig, AppSpawnReqMsgHandle reqHandle); 91 uint32_t GetUint32ArrayFromJson(const cJSON *json, const char *name, uint32_t dataArray[], uint32_t maxCount); 92 static AppSpawnTestCommander *ConvertTo(const ThreadContext *context) 93 { 94 return const_cast<AppSpawnTestCommander *>(reinterpret_cast<const AppSpawnTestCommander *>(context)); 95 } 96 static void TaskExecutorProc(ThreadTaskHandle handle, const ThreadContext *context); 97 static void SendTaskFinish(ThreadTaskHandle handle, const ThreadContext *context); 98 static void InputThread(ThreadTaskHandle handle, const ThreadContext *context); 99 static void DumpThread(ThreadTaskHandle handle, const ThreadContext *context); 100 101 int ptyFd_{-1}; 102 uint32_t dumpFlags : 1; 103 uint32_t exit_ : 1; 104 uint32_t appSpawn_ : 2; 105 uint32_t msgType_; 106 pid_t terminatePid_; 107 uint32_t threadCount_{1}; 108 std::string ptyName_{}; 109 std::string testFileName_{}; 110 std::string processName_{}; 111 cJSON *appInfoConfig_{nullptr}; 112 AppSpawnClientHandle clientHandle_{nullptr}; 113 ThreadMgr threadMgr_{nullptr}; 114 ThreadTaskHandle inputHandle_{0}; 115 ThreadTaskHandle dumpHandle_{0}; 116}; 117} // namespace AppSpawnModuleTest 118} // namespace OHOS 119#endif // APPSPAWN_TEST_CMD_H