• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AW_CXX_DISTRIBUTED_DISTRIBUTED_MAJOR_H_
17 #define AW_CXX_DISTRIBUTED_DISTRIBUTED_MAJOR_H_
18 
19 #include <vector>
20 #include <map>
21 #include <cstdio>
22 #include <iostream>
23 #include <thread>
24 
25 #include <sys/socket.h>
26 #include "securec.h"
27 
28 #include "distributed.h"
29 #include "distributed_cfg.h"
30 
31 #include "gtest/gtest.h"
32 
33 namespace OHOS {
34 namespace DistributeSystemTest {
35 
36 class DistributeTestEnvironment : public testing::Environment {
37 public:
38     DistributeTestEnvironment();
39     explicit DistributeTestEnvironment(std::string cfgFile);
40     virtual ~DistributeTestEnvironment();
41 
42     bool RunTestCmd(size_t devNo, const std::string &strCommand, int cmdLen, const std::string &strExpectValue,
43         int expectValueLen, std::function<bool(const std::string &, int)> onProcessReturn);
44 
45     bool SendMessage(size_t devNo, const std::string &strMsg, int msgLen,
46         std::function<bool(const std::string &, int)> onProcessReturnMsg) ;
47 
48     bool Notify(size_t devNo, const std::string &strMsg, int msgLen);
49 
50     virtual void SetUp();
51     virtual void TearDown();
52 
53     void Init(std::string fileName);
54 
55     std::vector<DistDeviceInfo> clientList_;
56 
57 private:
58     DistributedCfg clientCfg_;
59     int serverPort_;
60 
61     int AddClient(std::string ipAddr);
62     int ConnectAgent(size_t devNo);
63     int GetSerial();
64     bool SendToAgent(size_t devNo, int cmdType, void *pstrMsg, int len,
65         std::function<bool(const std::string &, int)> onProcessReturn);
66 
67 };
68 
69 extern DistributeTestEnvironment  *g_pDistributetestEnv;
70 class DistributeTest : public testing::Test {
71 public:
72     DistributeTest();
73     virtual ~DistributeTest();
74 
75 protected:
76     bool RunCmdOnAgent(AGENT_NO devNo, const std::string &strCommand, int cmdLen,
77             const std::string &strExpectValue, int expectValueLen);
78 
79     bool RunCmdOnAgent(AGENT_NO devNo, const std::string &strCmd, const std::string &strArgs,
80         const std::string &strExpectValue);
81     bool RunCmdOnAgent(AGENT_NO devNo, const std::string &strCmd, const std::string &strArgs,
82         const std::string &strExpectValue, std::function<bool(const std::string &, int)> onReturnCall);
83 
84     bool SendMessage(AGENT_NO devNo, const std::string &msg, int len);
85     bool SendMessage(AGENT_NO devNo, const std::string &msg, int len,
86         std::function<bool(const std::string &, int)> onProcessReturnMsg);
87     bool Notify(AGENT_NO devNo, const std::string &notifyType, const std::string &msg, int msgLen);
88     bool NotifyTestInfo(AGENT_NO devNo, const std::string &status);
89 
90     // monitor the status of Agent
91     int CheckStatus();
92     int GetReturnVal();
93 
94 private:
OnProcessNotify()95     void OnProcessNotify() {}
96     int returnVal_;
97 
98     static void SetUpTestCase(void);
99     static void TearDownTestCase(void);
100 
101     // init network environment, obtain all the address of agent and connect
102     int InitEnv(std::vector<std::string> clientList);
103 
SetUp()104     virtual void SetUp() {}
TearDown()105     virtual void TearDown() {}
106 
OnNotify()107     virtual void OnNotify() {}
108     virtual bool OnProcessValue(const std::string &szbuf, int len);
109     virtual bool OnMsgProc(const std::string &szbuf, int len);
110 };
111 } // namespace DistributeSystemTest
112 } // namespace OHOS
113 
114 #endif // AW_CXX_DISTRIBUTED_DISTRIBUTED_MAJOR_H_
115