• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "app_types.h"
22 #include "refbase.h"
23 #include "app_kvstore.h"
24 #include "app_distributed_kv_data_manager.h"
25 #include "hilog/log.h"
26 
27 using namespace testing;
28 using namespace OHOS;
29 using namespace OHOS::DistributeSystemTest;
30 using namespace OHOS::HiviewDFX;
31 using namespace OHOS::AppDistributedKv;
32 
33 namespace {
34     constexpr HiLogLabel LABEL = {LOG_CORE, 0, "DistributeDemoAgent"};
35     std::shared_ptr<AppDistributedKvDataManager> g_manager;
36     std::string g_appId = "com.ohos.nb.service.user1_test";
37     std::string g_storeId = "student_1";
38     std::unique_ptr<AppKvStore> g_kvStorePtr;
39     const int CMD_LEN = 4;
40     const int RETURN_HALF = 2;
41     const int MSG_CALL_LEN = 6;
42     const int CMD_RETURN_TWO = 111;
43 }
44 
45 class DistributeDemoAgent : public DistributedAgent {
46 public:
47     DistributeDemoAgent();
48     ~DistributeDemoAgent();
49 
50     virtual bool SetUp();
51     virtual bool TearDown();
52     virtual int OnProcessMsg(const std::string &strMsg, int len, std::string &strReturnValue, int returnBufLen);
53     virtual int OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen,
54         const std::string &strExpectValue, int expectValueLen);
55 
56     int GetKvValue(const std::string &strArgs, int argsLen, const std::string &strExpectValue,
57         int expectValueLen);
58     int AddTwoValue(const std::string &strArgs, int argsLen, const std::string &strExpectValue,
59         int expectValueLen);
60     int ProcessByUseMap(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen,
61         const std::string &strExpectValue, int expectValueLen);
62 
63 private:
64     typedef	int	(DistributeDemoAgent::*self_func)(const std::string &, int, const std::string &, int);
65     std::map<std::string, self_func> cmdFunMap_;
66 };
67 
DistributeDemoAgent()68 DistributeDemoAgent::DistributeDemoAgent()
69 {
70 }
~DistributeDemoAgent()71 DistributeDemoAgent::~DistributeDemoAgent()
72 {
73 }
74 
SetUp()75 bool DistributeDemoAgent::SetUp()
76 {
77     /**
78      * @tc.setup: Initialize test agent, start data management service
79      */
80     cmdFunMap_["getkvstore"] = &DistributeDemoAgent::GetKvValue;
81     cmdFunMap_["add_for_two_int"] = &DistributeDemoAgent::AddTwoValue;
82     g_manager = AppDistributedKvDataManager::GetInstance(g_appId, "/data/test");
83     Options options;
84     options.createIfMissing = true;
85     options.encrypt = false;
86     options.persistent = true;
87     std::string storeId = g_storeId;
88     Status status = g_manager->GetKvStore(options, storeId, [&](std::unique_ptr<AppKvStore> kvStore) {
89         g_kvStorePtr = std::move(kvStore);
90     });
91     if (status == Status::SUCCESS) {
92         return true;
93     }
94     return false;
95 }
96 
TearDown()97 bool DistributeDemoAgent::TearDown()
98 {
99     /**
100      * @tc.teardown: Recovery test agent device environment
101      */
102     return true;
103 }
104 
105 // The entry of handlingthe major test case message
OnProcessMsg(const std::string & strMsg,int len,std::string & strReturnValue,int returnBufLen)106 int DistributeDemoAgent::OnProcessMsg(const std::string &strMsg, int len,
107     std::string &strReturnValue, int returnBufLen)
108 {
109     int nret = 0;
110     std::string returnStr = "agent return message.";
111     if ((len > CMD_LEN) && (strMsg.find("\0\1\0\1") == 1)) {
112         for (int i = 0; i < returnBufLen; i++) {
113             strReturnValue  += std::to_string((i + 1) % RETURN_HALF);
114         }
115         nret = returnBufLen;
116     } else {
117         HiLog::Info(LABEL, "receive message=%s.", strMsg.c_str());
118         if (!strncmp(strMsg.c_str(), "recall", MSG_CALL_LEN)) {
119             returnStr = "I get recall message.";
120             int ptrlen = returnStr.size();
121             if (ptrlen > returnBufLen) {
122                 ptrlen = returnBufLen - 1;
123             }
124             strReturnValue = returnStr;
125             nret = ptrlen;
126         } else {
127             nret =  DistributedAgent::OnProcessMsg(strMsg, len, strReturnValue, returnBufLen);
128         }
129     }
130     return nret;
131 }
132 
133 // The entry of handling the major test case command
OnProcessCmd(const std::string & strCommand,int cmdLen,const std::string & strArgs,int argsLen,const std::string & strExpectValue,int expectValueLen)134 int DistributeDemoAgent::OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs,
135     int argsLen, const std::string &strExpectValue, int expectValueLen)
136 {
137     if (strCommand == "query_command") {
138         if (strArgs == "query a name?") {
139             return CMD_RETURN_TWO;
140         }
141     }
142     return DistributeDemoAgent::ProcessByUseMap(strCommand, cmdLen, strArgs, argsLen, strExpectValue, expectValueLen);
143 }
144 
145 // Handling major test case command: getkvstore
GetKvValue(const std::string & strArgs,int argsLen,const std::string & strExpectValue,int expectValueLen)146 int DistributeDemoAgent::GetKvValue(const std::string &strArgs, int argsLen, const std::string &strExpectValue,
147     int expectValueLen)
148 {
149     if (!g_kvStorePtr || (int)strArgs.length() > argsLen) {
150         HiLog::Error(LABEL, "[ohos-test-fm]agent ERROR.");
151         return -1;
152     }
153     ReadOptions readOptions;
154     readOptions.local = false;
155     Key key(strArgs);
156     Value getValue;
157     Status status = g_kvStorePtr->Get(readOptions, key, getValue);
158     if (status == Status::SUCCESS) {
159         HiLog::Info(LABEL, "Get Value SUCCESS, key=%s.", key.ToString().c_str());
160     } else {
161         HiLog::Error(LABEL, "Get Value Failed.");
162     }
163     std::vector<Entry> allEntry;
164     Status entryStatus = g_kvStorePtr->GetEntries("", allEntry);
165     if (entryStatus == Status::SUCCESS) {
166         HiLog::Info(LABEL, "GetEntries SUCCESS.");
167         for (auto entry : allEntry) {
168             HiLog::Info(LABEL, "GetEntries key= %s.", entry.key.ToString().c_str());
169         }
170     }
171     if (getValue.ToString() == strExpectValue) {
172         if (expectValueLen < (int)getValue.ToString().length()) {
173             HiLog::Error(LABEL, "[ohos-test-fm]agent end xxx.");
174             return -1;
175         }
176         HiLog::Error(LABEL, "[ohos-test-fm]agent end 200.");
177         return CMD_RETURN_TWO;
178     }
179     return -1;
180 }
181 
182 // Handling major test case command: add_for_two_int
AddTwoValue(const std::string & strArgs,int argsLen,const std::string & strExpectValue,int expectValueLen)183 int DistributeDemoAgent::AddTwoValue(const std::string &strArgs, int argsLen, const std::string &strExpectValue,
184     int expectValueLen)
185 {
186     int val1 = 0;
187     int val2 = 0;
188     int val3 = 0;
189     int ret1;
190     int ret2;
191     ret1 = sscanf_s(strArgs.c_str(), "%d %d", &val1, &val2);
192     if (ret1 <=0 || ret1 == 1) {
193         HiLog::Error(LABEL, "sscanf_s failed. ret1 != 2");
194         return -1;
195     }
196     ret2 = sscanf_s(strExpectValue.c_str(), "%d", &val3);
197     if (ret2 != 1) {
198         HiLog::Error(LABEL, "sscanf_s failed. ret2 != 1");
199         return -1;
200     }
201     EXPECT_TRUE(val3 == (val1 + val2));
202     return val1 + val2;
203 }
204 
205 // Handling major test cases through already registered commands and function mappings
ProcessByUseMap(const std::string & strCommand,int cmdLen,const std::string & strArgs,int argsLen,const std::string & strExpectValue,int expectValueLen)206 int DistributeDemoAgent::ProcessByUseMap(const std::string &strCommand, int cmdLen, const std::string &strArgs,
207     int argsLen, const std::string &strExpectValue, int expectValueLen)
208 {
209     std::map<std::string, self_func>::iterator ite = cmdFunMap_.find(strCommand);
210     if (ite != cmdFunMap_.end()) {
211         self_func callProcCmd = cmdFunMap_[strCommand];
212         if (callProcCmd != nullptr) {
213             return (this->*callProcCmd)(strArgs, argsLen, strExpectValue, expectValueLen);
214         } else {
215             return DistributedAgent::OnProcessCmd(strCommand,
216                                                   cmdLen,
217                                                   strArgs,
218                                                   argsLen,
219                                                   strExpectValue,
220                                                   expectValueLen);
221         }
222     }
223     return -1;
224 }
225 
main()226 int main()
227 {
228     // Test agent main function
229     DistributeDemoAgent obj;
230     if (obj.SetUp()) {
231         obj.Start("agent.desc");
232         obj.Join();
233     } else {
234         HiLog::Error(LABEL, "Init environment failed.");
235     }
236     if (obj.TearDown()) {
237         return 0;
238     } else {
239         HiLog::Error(LABEL, "Clear environment failed.");
240         return -1;
241     }
242 }