13f085823Sopenharmony_ci/* 23f085823Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 33f085823Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f085823Sopenharmony_ci * you may not use this file except in compliance with the License. 53f085823Sopenharmony_ci * You may obtain a copy of the License at 63f085823Sopenharmony_ci * 73f085823Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f085823Sopenharmony_ci * 93f085823Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f085823Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f085823Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f085823Sopenharmony_ci * See the License for the specific language governing permissions and 133f085823Sopenharmony_ci * limitations under the License. 143f085823Sopenharmony_ci */ 153f085823Sopenharmony_ci 163f085823Sopenharmony_ci#ifndef AW_CXX_DISTRIBUTED_DISTRIBUTED_MAJOR_H_ 173f085823Sopenharmony_ci#define AW_CXX_DISTRIBUTED_DISTRIBUTED_MAJOR_H_ 183f085823Sopenharmony_ci 193f085823Sopenharmony_ci#include <vector> 203f085823Sopenharmony_ci#include <map> 213f085823Sopenharmony_ci#include <cstdio> 223f085823Sopenharmony_ci#include <iostream> 233f085823Sopenharmony_ci#include <thread> 243f085823Sopenharmony_ci 253f085823Sopenharmony_ci#include <sys/socket.h> 263f085823Sopenharmony_ci#include "securec.h" 273f085823Sopenharmony_ci 283f085823Sopenharmony_ci#include "distributed.h" 293f085823Sopenharmony_ci#include "distributed_cfg.h" 303f085823Sopenharmony_ci 313f085823Sopenharmony_ci#include "gtest/gtest.h" 323f085823Sopenharmony_ci#include "hilog/log.h" 333f085823Sopenharmony_ci 343f085823Sopenharmony_cinamespace OHOS { 353f085823Sopenharmony_cinamespace DistributeSystemTest { 363f085823Sopenharmony_ci 373f085823Sopenharmony_ciclass DistributeTestEnvironment : public testing::Environment { 383f085823Sopenharmony_cipublic: 393f085823Sopenharmony_ci DistributeTestEnvironment(); 403f085823Sopenharmony_ci explicit DistributeTestEnvironment(std::string cfgFile); 413f085823Sopenharmony_ci virtual ~DistributeTestEnvironment(); 423f085823Sopenharmony_ci 433f085823Sopenharmony_ci bool RunTestCmd(size_t devNo, const std::string &strCommand, int cmdLen, const std::string &strExpectValue, 443f085823Sopenharmony_ci int expectValueLen, std::function<bool(const std::string &, int)> onProcessReturn); 453f085823Sopenharmony_ci 463f085823Sopenharmony_ci bool SendMessage(size_t devNo, const std::string &strMsg, int msgLen, 473f085823Sopenharmony_ci std::function<bool(const std::string &, int)> onProcessReturnMsg) ; 483f085823Sopenharmony_ci 493f085823Sopenharmony_ci bool Notify(size_t devNo, const std::string &strMsg, int msgLen); 503f085823Sopenharmony_ci 513f085823Sopenharmony_ci virtual void SetUp(); 523f085823Sopenharmony_ci virtual void TearDown(); 533f085823Sopenharmony_ci 543f085823Sopenharmony_ci void Init(std::string fileName); 553f085823Sopenharmony_ci 563f085823Sopenharmony_ci std::vector<DistDeviceInfo> clientList_; 573f085823Sopenharmony_ci 583f085823Sopenharmony_ci static constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD001690, "DistributeTestEnvironment"}; 593f085823Sopenharmony_ci 603f085823Sopenharmony_ciprivate: 613f085823Sopenharmony_ci DistributedCfg clientCfg_; 623f085823Sopenharmony_ci int serverPort_; 633f085823Sopenharmony_ci 643f085823Sopenharmony_ci int AddClient(std::string ipAddr); 653f085823Sopenharmony_ci int ConnectAgent(size_t devNo); 663f085823Sopenharmony_ci int GetSerial(); 673f085823Sopenharmony_ci bool SendToAgent(size_t devNo, int cmdType, void *pstrMsg, int len, 683f085823Sopenharmony_ci std::function<bool(const std::string &, int)> onProcessReturn); 693f085823Sopenharmony_ci 703f085823Sopenharmony_ci}; 713f085823Sopenharmony_ci 723f085823Sopenharmony_ciextern DistributeTestEnvironment *g_pDistributetestEnv; 733f085823Sopenharmony_ciclass DistributeTest : public testing::Test { 743f085823Sopenharmony_cipublic: 753f085823Sopenharmony_ci DistributeTest(); 763f085823Sopenharmony_ci virtual ~DistributeTest(); 773f085823Sopenharmony_ci 783f085823Sopenharmony_ciprotected: 793f085823Sopenharmony_ci bool RunCmdOnAgent(AGENT_NO devNo, const std::string &strCommand, int cmdLen, 803f085823Sopenharmony_ci const std::string &strExpectValue, int expectValueLen); 813f085823Sopenharmony_ci 823f085823Sopenharmony_ci bool RunCmdOnAgent(AGENT_NO devNo, const std::string &strCmd, const std::string &strArgs, 833f085823Sopenharmony_ci const std::string &strExpectValue); 843f085823Sopenharmony_ci bool RunCmdOnAgent(AGENT_NO devNo, const std::string &strCmd, const std::string &strArgs, 853f085823Sopenharmony_ci const std::string &strExpectValue, std::function<bool(const std::string &, int)> onReturnCall); 863f085823Sopenharmony_ci 873f085823Sopenharmony_ci bool SendMessage(AGENT_NO devNo, const std::string &msg, int len); 883f085823Sopenharmony_ci bool SendMessage(AGENT_NO devNo, const std::string &msg, int len, 893f085823Sopenharmony_ci std::function<bool(const std::string &, int)> onProcessReturnMsg); 903f085823Sopenharmony_ci bool Notify(AGENT_NO devNo, const std::string ¬ifyType, const std::string &msg, int msgLen); 913f085823Sopenharmony_ci bool NotifyTestInfo(AGENT_NO devNo, const std::string &status); 923f085823Sopenharmony_ci 933f085823Sopenharmony_ci // monitor the status of Agent 943f085823Sopenharmony_ci int CheckStatus(); 953f085823Sopenharmony_ci int GetReturnVal(); 963f085823Sopenharmony_ci 973f085823Sopenharmony_ciprivate: 983f085823Sopenharmony_ci void OnProcessNotify() {} 993f085823Sopenharmony_ci int returnVal_; 1003f085823Sopenharmony_ci 1013f085823Sopenharmony_ci static void SetUpTestCase(void); 1023f085823Sopenharmony_ci static void TearDownTestCase(void); 1033f085823Sopenharmony_ci 1043f085823Sopenharmony_ci // init network environment, obtain all the address of agent and connect 1053f085823Sopenharmony_ci int InitEnv(std::vector<std::string> clientList); 1063f085823Sopenharmony_ci 1073f085823Sopenharmony_ci virtual void SetUp() {} 1083f085823Sopenharmony_ci virtual void TearDown() {} 1093f085823Sopenharmony_ci 1103f085823Sopenharmony_ci virtual void OnNotify() {} 1113f085823Sopenharmony_ci virtual bool OnProcessValue(const std::string &szbuf, int len); 1123f085823Sopenharmony_ci virtual bool OnMsgProc(const std::string &szbuf, int len); 1133f085823Sopenharmony_ci}; 1143f085823Sopenharmony_ci} // namespace DistributeSystemTest 1153f085823Sopenharmony_ci} // namespace OHOS 1163f085823Sopenharmony_ci 1173f085823Sopenharmony_ci#endif // AW_CXX_DISTRIBUTED_DISTRIBUTED_MAJOR_H_ 118