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 #include <iostream>
17
18 #include "distributed_major.h"
19
20 #include "app_types.h"
21 #include "refbase.h"
22 #include "app_kvstore.h"
23 #include "app_distributed_kv_data_manager.h"
24 #include "hilog/log.h"
25
26
27 using namespace std;
28 using namespace OHOS;
29 using namespace testing::ext;
30 using namespace OHOS::DistributeSystemTest;
31 using namespace OHOS::HiviewDFX;
32 using namespace OHOS::AppDistributedKv;
33
34 namespace {
35 constexpr HiLogLabel LABEL = {LOG_CORE, 0, "DistributeDemo"};
36 const int SYNC_REC_TIME = 3;
37 const int MSG_LENGTH = 100;
38 const int EXPECT_RETURN_VALUE = 111;
39 const int EXPECT_ADD_TEST_SUM = 500;
40 const int SLEEP_TIME = 100000;
41 }
42
43 class DistributeDemo : public DistributeTest {
44 public:
45 DistributeDemo() = default;
46 ~DistributeDemo() = default;
47
SetUpTestCase(void)48 static void SetUpTestCase(void) {}
TearDownTestCase(void)49 static void TearDownTestCase(void) {}
50
SetUp()51 virtual void SetUp() {}
TearDown()52 virtual void TearDown() {}
53 };
54
55 /**
56 * @tc.name: msgsendtest_001
57 * @tc.desc: Verify the distributed test framework interface SendMessage.
58 * @tc.type: FUNC
59 * @tc.require: AR000CQGMV
60 */
61 HWTEST_F(DistributeDemo, msgsendtest_001, TestSize.Level0)
62 {
63 char msgbuf[MSG_LENGTH] = "I am testcase 1.";
64 EXPECT_TRUE(SendMessage(AGENT_NO::ONE, msgbuf, MSG_LENGTH));
65 };
66
67 /**
68 * @tc.name: msgsendtest_002
69 * @tc.desc: Verify the distributed test framework interface SendMessage, Maximum data length.
70 * @tc.type: FUNC
71 * @tc.require: AR000CQGMV
72 */
73 HWTEST_F(DistributeDemo, msgsendtest_002, TestSize.Level0)
74 {
75 char msgbuf[MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN] = "I am testcase 2.";
76 EXPECT_TRUE(SendMessage(AGENT_NO::ONE, msgbuf, MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN));
77 };
78
79 /**
80 * @tc.name: msgsendtest_003
81 * @tc.desc: Verify the distributed test framework interface SendMessage,
82 * Verify the data maximum length of the sent message interface .
83 * @tc.type: FUNC
84 * @tc.require: AR000CQGMV
85 */
86 HWTEST_F(DistributeDemo, msgsendtest_003, TestSize.Level0)
87 {
88 char msgbuf[MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN + 1] = "too big message.";
89 EXPECT_FALSE(SendMessage(AGENT_NO::ONE, msgbuf, MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN + 1));
90 };
91
92 /**
93 * @tc.name: msgsendtest_004
94 * @tc.desc: Verify the distributed test framework interface SendMessage, constains callback.
95 * @tc.type: FUNC
96 * @tc.require: AR000CQGMV
97 */
98 HWTEST_F(DistributeDemo, msgsendtest_004, TestSize.Level0)
99 {
100 std::string msgBuf = "recall function message test.";
101 int ret = SendMessage(AGENT_NO::ONE, msgBuf, MSG_LENGTH,
__anon5ba20f610202(const std::string &szreturnbuf, int rlen)102 [&](const std::string &szreturnbuf, int rlen)->bool {
103 std::string szbuf = "I get recall message.";
104 EXPECT_TRUE(szbuf == szreturnbuf);
105 HiLog::Info(LABEL, "msgsendtest_004 strncmp=%s", szbuf.c_str());
106 return true;
107 });
108 EXPECT_TRUE(ret > 0);
109 };
110
111 /**
112 * @tc.name: runcmdonagent_001
113 * @tc.desc: Verify the distributed test framework interface RunCmdOnAgent.
114 * @tc.type: FUNC
115 * @tc.require: AR000CQGMV
116 */
117 HWTEST_F(DistributeDemo, runcmdonagent_001, TestSize.Level0)
118 {
119 std::string command = "query_command";
120 std::string cmdArgs = "query a name?";
121 std::string expectValue = "111";
122 RunCmdOnAgent(AGENT_NO::ONE, command, cmdArgs, expectValue);
123 EXPECT_EQ(GetReturnVal(), EXPECT_RETURN_VALUE);
124 };
125
126 /**
127 * @tc.name: runcmdonagent_002
128 * @tc.desc: Verify the distributed test framework interface RunCmdOnAgent, Using command map.
129 * @tc.type: FUNC
130 * @tc.require: AR000CQGMV
131 */
132 HWTEST_F(DistributeDemo, runcmdonagent_002, TestSize.Level0) {
133 std::string command = "add_for_two_int";
134 std::string cmdArgs = "200 300";
135 std::string expectValue = "500";
136 RunCmdOnAgent(AGENT_NO::ONE, command, cmdArgs, expectValue);
137 EXPECT_EQ(GetReturnVal(), EXPECT_ADD_TEST_SUM);
138 };
139
140 /**
141 * @tc.name: getkvstore_001
142 * @tc.desc: Verify distributed data read and write and synchronization.
143 * @tc.type: FUNC
144 * @tc.require: AR000CQGMV
145 */
146 HWTEST_F(DistributeDemo, getkvstore_001, TestSize.Level0) {
147 /**
148 * @tc.steps: step1.init appkvstore, Get the kvstore pointer for the specified appId and storeId
149 */
150 static std::shared_ptr<AppDistributedKvDataManager> manager;
151 Options options;
152 options.createIfMissing = true;
153 options.encrypt = false;
154 options.persistent = true;
155 std::string appId = "com.ohos.nb.service.user1_test";
156 std::string storeId = "student_1";
157 manager = AppDistributedKvDataManager::GetInstance(appId, "/data/test");
158 std::unique_ptr<AppKvStore> kvStorePtr;
__anon5ba20f610302(std::unique_ptr<AppKvStore> kvStore) 159 Status status = manager->GetKvStore(options, storeId, [&](std::unique_ptr<AppKvStore> kvStore) {
160 kvStorePtr = std::move(kvStore);
161 });
162 if (!kvStorePtr) {
163 HiLog::Error(LABEL, "client get kvStorePtr error.");
164 }
165
166 ASSERT_TRUE(kvStorePtr != nullptr);
167
168 /**
169 * @tc.steps: step2.Write specific K and V values to the database
170 */
171 if (status == Status::SUCCESS) {
172 WriteOptions writeOptions;
173 writeOptions.local = false;
174 Key key("student_name_user1");
175 Value value = "good boy.";
176 kvStorePtr->Put(writeOptions, key, value);
177 }
178
179 /**
180 * @tc.steps: step3.Synchronize data to a specific remote device
181 */
182 std::vector<std::string> deviceIdList;
183 deviceIdList.push_back("192.168.43.200");
184 std::map<std::string, Status> sync_map;
185 Status syncinfo = kvStorePtr->Sync(deviceIdList, SyncMode::PUSH,
__anon5ba20f610402(const std::map<std::string, Status> &syncMapinfo)186 [&](const std::map<std::string, Status> &syncMapinfo){
187 sync_map = std::move(syncMapinfo);
188 });
189 if (syncinfo == Status::SUCCESS) {
190 HiLog::Info(LABEL, "agent sync success");
191 }
192
193 /**
194 * @tc.steps: step4.Judge if the data is successfully synchronized to the remote device
195 */
196 int n = 0;
197 while (n < SYNC_REC_TIME) {
198 std::string id = "";
199 for (auto mapstatus: sync_map) {
200 HiLog::Info(LABEL, "client map_id==%s", mapstatus.first.c_str());
201 if (mapstatus.second == Status::SUCCESS) {
202 id = mapstatus.first;
203 HiLog::Info(LABEL, "real client Sync success.");
204 } else {
205 HiLog::Info(LABEL, "mapstatus.second=%d.", (int)mapstatus.second);
206 }
207 n++;
208 }
209 if (id != "") {
210 break;
211 }
212 usleep(SLEEP_TIME);
213 }
214 if (syncinfo == Status::SUCCESS) {
215 HiLog::Info(LABEL, "client sync success.");
216 }
217
218 /**
219 * @tc.steps: step5.Query whether the value inserted in the local database is correct
220 */
221 std::string command = "getkvstore";
222 std::string cmdArgs = "student_name_user1";
223 std::string expectValue = "good boy.";
224 ReadOptions readOptions;
225 readOptions.local = false;
226 Key key(cmdArgs);
227 Value getValue;
228 Status getTstatus = kvStorePtr->Get(readOptions, key, getValue);
229 if (getTstatus == Status::SUCCESS) {
230 HiLog::Info(LABEL, "Get Value SUCCESS, key=%s.", key.ToString().c_str());
231 } else {
232 HiLog::Error(LABEL, "Get Value Failed.");
233 }
234
235 /**
236 * @tc.steps: step6.Send a command to the remote device to query the database with parameters and
237 * expected return values
238 */
239 RunCmdOnAgent(AGENT_NO::ONE, command, cmdArgs, expectValue);
240
241 /**
242 * @tc.steps: step7.Judge the return value
243 * @tc.expected: step7. The remote device queried the data successfully.
244 */
245 EXPECT_TRUE(GetReturnVal() == EXPECT_RETURN_VALUE);
246 };
247
main(int argc,char * argv[])248 int main(int argc, char *argv[])
249 {
250 g_pDistributetestEnv = new DistributeTestEnvironment("major.desc");
251 testing::AddGlobalTestEnvironment(g_pDistributetestEnv);
252 testing::GTEST_FLAG(output) = "xml:./";
253 testing::InitGoogleTest(&argc, argv);
254 return RUN_ALL_TESTS();
255 }
256