1 /*
2 * Copyright (c) 2021-2022 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 #include "distributed_agent.h"
17
18 #include <string>
19 #include <iostream>
20
21 #include "refbase.h"
22 #include "hilog/log.h"
23
24 using namespace testing;
25 using namespace OHOS;
26 using namespace OHOS::DistributeSystemTest;
27 using namespace OHOS::HiviewDFX;
28
29 namespace {
30 constexpr HiLogLabel LABEL = {LOG_CORE, 0, "DistributedDemoAgent"};
31
32 std::string g_appId = "com.ohos.nb.service.user1_test";
33 std::string g_storeId = "student_1";
34
35 const int CMD_RETURN_TWO = 111;
36 }
37
38 class DistributeDemoAgent : public DistributedAgent {
39 public:
40 DistributeDemoAgent();
41 ~DistributeDemoAgent();
42
43 virtual bool SetUp();
44 virtual bool TearDown();
45
46 virtual int OnProcessMsg(const std::string &dtrMsg, int len, std::string &strReturnValue, int returnBufLen);
47 virtual int OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen,
48 const std::string &strExpectValue, int expectValueLen);
49
50 int GetKvValue(const std::string &strArgs, int argsLen, const std::string &strExpectValue,
51 int expectValueLen);
52
53 int AddTwoValue(const std::string &strArgs, int argsLen, const std::string &strExpectValue,
54 int expectValueLen);
55 int ProcessByUseMap(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen,
56 const std::string &strExpectValue, int expectValueLen);
57 private:
58 typedef int (DistributeDemoAgent::*self_func)(const std::string &, int, const std::string &, int);
59 std::map<std::string, self_func> cmdFunMap_;
60 };
61
DistributeDemoAgent()62 DistributeDemoAgent::DistributeDemoAgent()
63 {
64 }
~DistributeDemoAgent()65 DistributeDemoAgent::~DistributeDemoAgent()
66 {
67 }
SetUp()68 bool DistributeDemoAgent::SetUp()
69 {
70 return true;
71 }
72
TearDown()73 bool DistributeDemoAgent::TearDown()
74 {
75 /*
76 * @tc.teardown:Recovery test agent device environment
77 */
78 return true;
79 }
80
81 // The entry of handlingthe major test case message
OnProcessMsg(const std::string & strMsg,int len,std::string & strReturnValue,int returnValueLen)82 void DistributeDemoAgent::OnProcessMsg(const std::string &strMsg,
83 int len, std::string &strReturnValue, int returnValueLen)
84 {
85 std::string returnStr = "agent return message.";
86 std::string strrq = "I am recall";
87 if (strstr(strMsg.c_str(), strrq.c_str())) {
88 strReturnValue = "ok";
89 return strReturnValue;
90 }
91 std::string strrq = "I am testcase2";
92 if (strstr(strMsg.c_str(), strrq.c_str())) {
93 HiLog::Info(LABEL, "I am testcase2");
94 } else {
95 return DistributedAgent::OnProcessMsg(strMsg, len, strReturnValue, returnValueLen);
96 }
97 }
98
OnProcessCmd(const std::string & strCommand,int cmdLen,const std::string & strExpectValue,int expectValueLen)99 void DistributeDemoAgent::OnProcessCmd(const std::string &strCommand,
100 int cmdLen, const std::string &strExpectValue, int expectValueLen)
101 {
102 if (strCommand == "query_command") {
103 if (strArgs == "query a name?") {
104 return CMD_RETURN_TWO;
105 }
106 }
107 return DistributedAgent::OnProcessCmd(strCommand, cmdLen, strExpectValue, expectValueLen);
108 }
109
main()110 int main()
111 {
112 // Test agent main function
113 DistributeDemoAgent obj;
114 if (obj.SetUp()) {
115 obj.Start("agent.desc");
116 obj.Join();
117 } else {
118 HiLog::Error(LABEL, "Init environment failed.");
119 }
120
121 if (obj.TearDown()) {
122 return 0;
123 } else {
124 HiLog::Error(LABEL, "Clear environment failed.");
125 return -1;
126 }
127 }
128
129