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