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 <gtest/gtest.h>
17
18 #include "ability_sync.h"
19 #include "distributeddb_tools_unit_test.h"
20 #include "single_ver_relational_sync_task_context.h"
21 #include "single_ver_kv_sync_task_context.h"
22 #include "sync_types.h"
23 #include "version.h"
24 #include "virtual_communicator_aggregator.h"
25 #include "virtual_single_ver_sync_db_Interface.h"
26 #include "virtual_relational_ver_sync_db_interface.h"
27
28 using namespace std;
29 using namespace testing::ext;
30 using namespace DistributedDB;
31
32 namespace {
33 const std::string DEVICE_A = "deviceA";
34 const std::string DEVICE_B = "deviceB";
35 const std::string TEST_SCHEMA = "{\"SCHEMA_DEFINE\":{\"value\":\"LONG\"},\"SCHEMA_MODE\":\"COMPATIBLE\","
36 "\"SCHEMA_VERSION\":\"1.0\"}";
37
38 VirtualSingleVerSyncDBInterface *g_syncInterface = nullptr;
39 VirtualCommunicatorAggregator *g_communicatorAggregator = nullptr;
40
41 ICommunicator *g_communicatorA = nullptr;
42 ICommunicator *g_communicatorB = nullptr;
43 std::shared_ptr<Metadata> g_meta = nullptr;
44 }
45
46 class DistributedDBAbilitySyncTest : public testing::Test {
47 public:
48 static void SetUpTestCase(void);
49 static void TearDownTestCase(void);
50 void SetUp();
51 void TearDown();
52 };
53
SetUpTestCase(void)54 void DistributedDBAbilitySyncTest::SetUpTestCase(void)
55 {
56 /**
57 * @tc.setup: NA
58 */
59 }
60
TearDownTestCase(void)61 void DistributedDBAbilitySyncTest::TearDownTestCase(void)
62 {
63 /**
64 * @tc.teardown: NA
65 */
66 }
67
SetUp(void)68 void DistributedDBAbilitySyncTest::SetUp(void)
69 {
70 DistributedDBUnitTest::DistributedDBToolsUnitTest::PrintTestCaseInfo();
71 /**
72 * @tc.setup: create the instance for virtual communicator, virtual storage
73 */
74 g_syncInterface = new (std::nothrow) VirtualSingleVerSyncDBInterface();
75 ASSERT_TRUE(g_syncInterface != nullptr);
76 g_syncInterface->SetSchemaInfo(TEST_SCHEMA);
77 g_communicatorAggregator = new (std::nothrow) VirtualCommunicatorAggregator;
78 ASSERT_TRUE(g_communicatorAggregator != nullptr);
79 int errCode = E_OK;
80 g_communicatorA = g_communicatorAggregator->AllocCommunicator(DEVICE_A, errCode);
81 ASSERT_TRUE(g_communicatorA != nullptr);
82 g_communicatorB = g_communicatorAggregator->AllocCommunicator(DEVICE_B, errCode);
83 ASSERT_TRUE(g_communicatorB != nullptr);
84 g_meta = std::make_shared<Metadata>();
85 g_meta->Initialize(g_syncInterface);
86 }
87
TearDown(void)88 void DistributedDBAbilitySyncTest::TearDown(void)
89 {
90 /**
91 * @tc.teardown: delete the ptr for testing
92 */
93 if (g_communicatorA != nullptr && g_communicatorAggregator != nullptr) {
94 g_communicatorAggregator->ReleaseCommunicator(g_communicatorA);
95 g_communicatorA = nullptr;
96 }
97 if (g_communicatorB != nullptr && g_communicatorAggregator != nullptr) {
98 g_communicatorAggregator->ReleaseCommunicator(g_communicatorB);
99 g_communicatorB = nullptr;
100 }
101 if (g_communicatorAggregator != nullptr) {
102 RefObject::KillAndDecObjRef(g_communicatorAggregator);
103 g_communicatorAggregator = nullptr;
104 }
105 if (g_syncInterface != nullptr) {
106 delete g_syncInterface;
107 g_syncInterface = nullptr;
108 }
109 }
110
111 /**
112 * @tc.name: RequestPacketTest001
113 * @tc.desc: Verify RequestPacketSerialization and RequestPacketDeSerialization function.
114 * @tc.type: FUNC
115 * @tc.require:
116 * @tc.author: xushaohua
117 */
118 HWTEST_F(DistributedDBAbilitySyncTest, RequestPacketTest001, TestSize.Level0)
119 {
120 /**
121 * @tc.steps: step1. create a AbilityRequestPacket packet1
122 * @tc.steps: step2. set version = ABILITY_SYNC_VERSION_V1. schema = TEST_SCHEMA.
123 */
124 AbilitySyncRequestPacket packet1;
125 DbAbility ability1;
126 #ifndef OMIT_ZLIB
127 ability1.SetAbilityItem(SyncConfig::DATABASE_COMPRESSION_ZLIB, SUPPORT_MARK);
128 #endif
129 packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1);
130 packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT);
131 packet1.SetSchema(TEST_SCHEMA);
132 packet1.SetSendCode(E_OK);
133 packet1.SetDbAbility(ability1);
134 Message msg1(ABILITY_SYNC_MESSAGE);
135 msg1.SetMessageType(TYPE_REQUEST);
136 msg1.SetCopiedObject(packet1);
137
138 /**
139 * @tc.steps: step3. call Serialization to Serialization the msg
140 * @tc.expected: step3. Serialization return E_OK
141 */
142 uint32_t bufflen = packet1.CalculateLen();
143 ASSERT_TRUE(bufflen != 0);
144 std::vector<uint8_t> buff(bufflen, 0);
145 ASSERT_TRUE(AbilitySync::Serialization(buff.data(), bufflen, &msg1) == E_OK);
146
147 /**
148 * @tc.steps: step4. call DeSerialization to DeSerialization the buff
149 * @tc.expected: step4. DeSerialization return E_OK
150 */
151 Message msg2(ABILITY_SYNC_MESSAGE);
152 msg2.SetMessageType(TYPE_REQUEST);
153 ASSERT_TRUE(AbilitySync::DeSerialization(buff.data(), bufflen, &msg2) == E_OK);
154 const AbilitySyncRequestPacket *packet2 = msg2.GetObject<AbilitySyncRequestPacket>();
155 ASSERT_TRUE(packet2 != nullptr);
156
157 /**
158 * @tc.expected: step5. packet1 == packet2
159 */
160 EXPECT_TRUE(packet2->GetProtocolVersion() == ABILITY_SYNC_VERSION_V1);
161 EXPECT_TRUE(packet2->GetSoftwareVersion() == SOFTWARE_VERSION_CURRENT);
162 EXPECT_TRUE(packet2->GetSendCode() == E_OK);
163 EXPECT_TRUE(packet2->GetDbAbility() == ability1);
164 std::string schema = packet2->GetSchema();
165 EXPECT_EQ(schema, TEST_SCHEMA);
166 }
167
168 /**
169 * @tc.name: RequestPacketTest002
170 * @tc.desc: Verify RequestPacketSerialization and RequestPacketDeSerialization function when version not support.
171 * @tc.type: FUNC
172 * @tc.require:
173 * @tc.author: xushaohua
174 */
175 HWTEST_F(DistributedDBAbilitySyncTest, RequestPacketTest002, TestSize.Level0)
176 {
177 /**
178 * @tc.steps: step1. create a AbilityRequestPacket packet1
179 * @tc.steps: step2. set version = ABILITY_SYNC_VERSION_V1 + 1. schema = TEST_SCHEMA.
180 */
181 AbilitySyncRequestPacket packet1;
182 packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1 + 1);
183 packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT);
184 packet1.SetSchema("");
185 Message msg1(ABILITY_SYNC_MESSAGE);
186 msg1.SetMessageType(TYPE_REQUEST);
187 msg1.SetCopiedObject(packet1);
188
189 /**
190 * @tc.steps: step3. call Serialization to Serialization the msg
191 * @tc.expected: step3. Serialization return E_OK
192 */
193 uint32_t bufflen = packet1.CalculateLen();
194 ASSERT_TRUE(bufflen != 0);
195 std::vector<uint8_t> buff(bufflen, 0);
196 ASSERT_TRUE(AbilitySync::Serialization(buff.data(), bufflen, &msg1) == E_OK);
197
198 /**
199 * @tc.steps: step4. call DeSerialization to DeSerialization the buff
200 * @tc.expected: step4. DeSerialization return E_OK
201 */
202 Message msg2(ABILITY_SYNC_MESSAGE);
203 msg2.SetMessageType(TYPE_REQUEST);
204 ASSERT_TRUE(AbilitySync::DeSerialization(buff.data(), bufflen, &msg2) == E_OK);
205 const AbilitySyncRequestPacket *packet2 = msg2.GetObject<AbilitySyncRequestPacket>();
206 ASSERT_TRUE(packet2 != nullptr);
207
208 /**
209 * @tc.expected: step5. packet2->GetSendCode() == -E_VERSION_NOT_SUPPORT
210 */
211 EXPECT_TRUE(packet2->GetSendCode() == -E_VERSION_NOT_SUPPORT);
212 }
213
214 /**
215 * @tc.name: RequestPacketTest003
216 * @tc.desc: Verify RequestPacketSerialization and RequestPacketDeSerialization function.
217 * @tc.type: FUNC
218 * @tc.require:
219 * @tc.author: xushaohua
220 */
221 HWTEST_F(DistributedDBAbilitySyncTest, RequestPacketTest003, TestSize.Level0)
222 {
223 /**
224 * @tc.steps: step1. create a AbilityRequestPacket packet1
225 * @tc.steps: step2. set version = ABILITY_SYNC_VERSION_V1. schema = TEST_SCHEMA.
226 */
227 AbilitySyncRequestPacket packet1;
228 packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1);
229 packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT);
230 packet1.SetSchema(TEST_SCHEMA);
231 packet1.SetSendCode(E_OK);
232 int secLabel = 3; // label 3
233 int secFlag = 1; // flag 1
234 packet1.SetSecLabel(secLabel);
235 packet1.SetSecFlag(secFlag);
236 Message msg1(ABILITY_SYNC_MESSAGE);
237 msg1.SetMessageType(TYPE_REQUEST);
238 msg1.SetCopiedObject(packet1);
239
240 /**
241 * @tc.steps: step3. call Serialization to Serialization the msg
242 * @tc.expected: step3. Serialization return E_OK
243 */
244 uint32_t bufflen = packet1.CalculateLen();
245 ASSERT_TRUE(bufflen != 0);
246 std::vector<uint8_t> buff(bufflen, 0);
247 ASSERT_TRUE(AbilitySync::Serialization(buff.data(), bufflen, &msg1) == E_OK);
248
249 /**
250 * @tc.steps: step4. call DeSerialization to DeSerialization the buff
251 * @tc.expected: step4. DeSerialization return E_OK
252 */
253 Message msg2(ABILITY_SYNC_MESSAGE);
254 msg2.SetMessageType(TYPE_REQUEST);
255 ASSERT_TRUE(AbilitySync::DeSerialization(buff.data(), bufflen, &msg2) == E_OK);
256 const AbilitySyncRequestPacket *packet2 = msg2.GetObject<AbilitySyncRequestPacket>();
257 ASSERT_TRUE(packet2 != nullptr);
258
259 /**
260 * @tc.expected: step5. packet1 == packet2
261 */
262 EXPECT_TRUE(packet2->GetProtocolVersion() == ABILITY_SYNC_VERSION_V1);
263 EXPECT_TRUE(packet2->GetSoftwareVersion() == SOFTWARE_VERSION_CURRENT);
264 EXPECT_TRUE(packet2->GetSendCode() == E_OK);
265 std::string schema = packet2->GetSchema();
266 EXPECT_EQ(schema, TEST_SCHEMA);
267 EXPECT_TRUE(packet2->GetSecFlag() == secFlag);
268 EXPECT_TRUE(packet2->GetSecLabel() == secLabel);
269 }
270
271 /**
272 * @tc.name: RequestPacketTest004
273 * @tc.desc: Verify RequestPacketSerialization and RequestPacketDeSerialization function.
274 * @tc.type: FUNC
275 * @tc.require:
276 * @tc.author: xushaohua
277 */
278 HWTEST_F(DistributedDBAbilitySyncTest, RequestPacketTest004, TestSize.Level0)
279 {
280 /**
281 * @tc.steps: step1. create a AbilityRequestPacket packet1
282 * @tc.steps: step2. set version = ABILITY_SYNC_VERSION_V1. schema = TEST_SCHEMA.
283 */
284 AbilitySyncRequestPacket packet1;
285 packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1);
286 packet1.SetSoftwareVersion(SOFTWARE_VERSION_RELEASE_2_0);
287 packet1.SetSchema(TEST_SCHEMA);
288 packet1.SetSendCode(E_OK);
289 int secLabel = 3; // label 3
290 int secFlag = 1; // flag 1
291 packet1.SetSecLabel(secLabel);
292 packet1.SetSecFlag(secFlag);
293 Message msg1(ABILITY_SYNC_MESSAGE);
294 msg1.SetMessageType(TYPE_REQUEST);
295 msg1.SetCopiedObject(packet1);
296
297 /**
298 * @tc.steps: step3. call Serialization to Serialization the msg
299 * @tc.expected: step3. Serialization return E_OK
300 */
301 uint32_t bufflen = packet1.CalculateLen();
302 ASSERT_TRUE(bufflen != 0);
303 std::vector<uint8_t> buff(bufflen, 0);
304 ASSERT_TRUE(AbilitySync::Serialization(buff.data(), bufflen, &msg1) == E_OK);
305
306 /**
307 * @tc.steps: step4. call DeSerialization to DeSerialization the buff
308 * @tc.expected: step4. DeSerialization return E_OK
309 */
310 Message msg2(ABILITY_SYNC_MESSAGE);
311 msg2.SetMessageType(TYPE_REQUEST);
312 ASSERT_TRUE(AbilitySync::DeSerialization(buff.data(), bufflen, &msg2) == E_OK);
313 const AbilitySyncRequestPacket *packet2 = msg2.GetObject<AbilitySyncRequestPacket>();
314 ASSERT_TRUE(packet2 != nullptr);
315
316 /**
317 * @tc.expected: step5. packet1 == packet2
318 */
319 EXPECT_TRUE(packet2->GetProtocolVersion() == ABILITY_SYNC_VERSION_V1);
320 EXPECT_TRUE(packet2->GetSoftwareVersion() == SOFTWARE_VERSION_RELEASE_2_0);
321 EXPECT_TRUE(packet2->GetSendCode() == E_OK);
322 std::string schema = packet2->GetSchema();
323 EXPECT_EQ(schema, TEST_SCHEMA);
324 EXPECT_TRUE(packet2->GetSecFlag() == 0);
325 EXPECT_TRUE(packet2->GetSecLabel() == 0);
326 }
327
328 /**
329 * @tc.name: AckPacketTest001
330 * @tc.desc: Verify AckPacketSerialization and AckPacketDeSerialization function.
331 * @tc.type: FUNC
332 * @tc.require:
333 * @tc.author: xushaohua
334 */
335 HWTEST_F(DistributedDBAbilitySyncTest, AckPacketTest001, TestSize.Level0)
336 {
337 /**
338 * @tc.steps: step1. create a AbilityAckPacket packet1
339 * @tc.steps: step2. set version = ABILITY_SYNC_VERSION_V1. schema = TEST_SCHEMA.
340 */
341 AbilitySyncAckPacket packet1;
342 DbAbility ability1;
343 #ifndef OMIT_ZLIB
344 ability1.SetAbilityItem(SyncConfig::DATABASE_COMPRESSION_ZLIB, SUPPORT_MARK);
345 #endif
346 packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1);
347 packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT);
348 packet1.SetSchema(TEST_SCHEMA);
349 packet1.SetAckCode(E_VERSION_NOT_SUPPORT);
350 packet1.SetDbAbility(ability1);
351 Message msg1(ABILITY_SYNC_MESSAGE);
352 msg1.SetMessageType(TYPE_RESPONSE);
353 msg1.SetCopiedObject(packet1);
354
355 /**
356 * @tc.steps: step3. call Serialization to Serialization the msg
357 * @tc.expected: step3. Serialization return E_OK
358 */
359 uint32_t bufflen = packet1.CalculateLen();
360 ASSERT_TRUE(bufflen != 0);
361 std::vector<uint8_t> buff(bufflen, 0);
362 ASSERT_EQ(AbilitySync::Serialization(buff.data(), bufflen, &msg1), E_OK);
363
364 /**
365 * @tc.steps: step4. call DeSerialization to DeSerialization the buff
366 * @tc.expected: step4. DeSerialization return E_OK
367 */
368 Message msg2(ABILITY_SYNC_MESSAGE);
369 msg2.SetMessageType(TYPE_RESPONSE);
370 ASSERT_TRUE(AbilitySync::DeSerialization(buff.data(), bufflen, &msg2) == E_OK);
371 const AbilitySyncAckPacket *packet2 = msg2.GetObject<AbilitySyncAckPacket>();
372 ASSERT_TRUE(packet2 != nullptr);
373
374 /**
375 * @tc.expected: step5. packet1 == packet2
376 */
377 EXPECT_TRUE(packet2->GetProtocolVersion() == ABILITY_SYNC_VERSION_V1);
378 EXPECT_TRUE(packet2->GetSoftwareVersion() == SOFTWARE_VERSION_CURRENT);
379 EXPECT_TRUE(packet2->GetAckCode() == E_VERSION_NOT_SUPPORT);
380 EXPECT_TRUE(packet2->GetDbAbility() == ability1);
381 std::string schema = packet2->GetSchema();
382 ASSERT_TRUE(schema == TEST_SCHEMA);
383 }
384
385 /**
386 * @tc.name: SyncStartTest001
387 * @tc.desc: Verify Ability sync SyncStart function.
388 * @tc.type: FUNC
389 * @tc.require:
390 * @tc.author: xushaohua
391 */
392 HWTEST_F(DistributedDBAbilitySyncTest, SyncStart001, TestSize.Level0)
393 {
394 /**
395 * @tc.steps: step1. create a AbilitySync
396 */
397 AbilitySync async;
398 async.Initialize(g_communicatorB, g_syncInterface, g_meta, DEVICE_A);
399
400 /**
401 * @tc.steps: step2. call SyncStart
402 * @tc.expected: step2. SyncStart return E_OK
403 */
404 EXPECT_EQ(async.SyncStart(1, 1, 1), E_OK);
405
406 /**
407 * @tc.steps: step3. disable the communicator
408 */
409 static_cast<VirtualCommunicator *>(g_communicatorB)->Disable();
410
411 /**
412 * @tc.steps: step4. call SyncStart
413 * @tc.expected: step4. SyncStart return -E_PERIPHERAL_INTERFACE_FAIL
414 */
415 EXPECT_TRUE(async.SyncStart(1, 1, 1) == -E_PERIPHERAL_INTERFACE_FAIL);
416 }
417 #ifndef OMIT_JSON
418 /**
419 * @tc.name: RequestReceiveTest001
420 * @tc.desc: Verify Ability RequestReceive callback.
421 * @tc.type: FUNC
422 * @tc.require:
423 * @tc.author: xushaohua
424 */
425 HWTEST_F(DistributedDBAbilitySyncTest, RequestReceiveTest001, TestSize.Level0)
426 {
427 /**
428 * @tc.steps: step1. create a AbilitySync
429 */
430 AbilitySync async;
431 async.Initialize(g_communicatorB, g_syncInterface, g_meta, DEVICE_A);
432
433 /**
434 * @tc.steps: step2. call RequestRecv, set inMsg nullptr or set context nullptr
435 * @tc.expected: step2. RequestRecv return -E_INVALID_ARGS
436 */
437 Message msg1(ABILITY_SYNC_MESSAGE);
438 msg1.SetMessageType(TYPE_REQUEST);
439 SingleVerSyncTaskContext *context = new (std::nothrow) SingleVerKvSyncTaskContext();
440 ASSERT_TRUE(context != nullptr);
441 EXPECT_EQ(async.RequestRecv(nullptr, context), -E_INVALID_ARGS);
442 EXPECT_EQ(async.RequestRecv(&msg1, nullptr), -E_INVALID_ARGS);
443
444 /**
445 * @tc.steps: step3. call RequestRecv, set inMsg with no packet
446 * @tc.expected: step3. RequestRecv return -E_INVALID_ARGS
447 */
448 EXPECT_EQ(async.RequestRecv(&msg1, context), -E_INVALID_ARGS);
449
450 /**
451 * @tc.steps: step4. create a AbilityRequestkPacket packet1
452 */
453 AbilitySyncRequestPacket packet1;
454 packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1);
455 packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT);
456 packet1.SetSchema(TEST_SCHEMA);
457 msg1.SetCopiedObject(packet1);
458
459 /**
460 * @tc.steps: step5. call RequestRecv, set inMsg with packet
461 * @tc.expected: step5. RequestRecv return ok, GetRemoteSoftwareVersion is SOFTWARE_VERSION_CURRENT
462 * IsSchemaCompatible true
463 *
464 */
465 EXPECT_EQ(async.RequestRecv(&msg1, context), -E_SECURITY_OPTION_CHECK_ERROR);
466 EXPECT_TRUE(context->GetRemoteSoftwareVersion() == SOFTWARE_VERSION_CURRENT);
467 EXPECT_TRUE(context->GetTaskErrCode() != -E_SCHEMA_MISMATCH);
468
469 /**
470 * @tc.steps: step6. call RequestRecv, set inMsg sendCode -E_VERSION_NOT_SUPPORT
471 * @tc.expected: step6. RequestRecv return E_VERSION_NOT_SUPPORT
472 */
473 packet1.SetSendCode(-E_VERSION_NOT_SUPPORT);
474 msg1.SetCopiedObject(packet1);
475 EXPECT_EQ(async.RequestRecv(&msg1, context), -E_VERSION_NOT_SUPPORT);
476
477 /**
478 * @tc.steps: step7. call RequestRecv, SetSchema ""
479 * @tc.expected: step7. IsSchemaCompatible false
480 */
481 packet1.SetSchema("");
482 packet1.SetSendCode(E_OK);
483 msg1.SetCopiedObject(packet1);
484 EXPECT_EQ(async.RequestRecv(&msg1, context), -E_SECURITY_OPTION_CHECK_ERROR);
485 EXPECT_FALSE(context->GetTaskErrCode() != -E_SCHEMA_MISMATCH);
486 RefObject::KillAndDecObjRef(context);
487 }
488 #endif
489 /**
490 * @tc.name: AckReceiveTest001
491 * @tc.desc: Verify Ability AckReceive callback.
492 * @tc.type: FUNC
493 * @tc.require:
494 * @tc.author: xushaohua
495 */
496 HWTEST_F(DistributedDBAbilitySyncTest, AckReceiveTest001, TestSize.Level0)
497 {
498 /**
499 * @tc.steps: step1. create a AbilitySync
500 */
501 AbilitySync async;
502 async.Initialize(g_communicatorB, g_syncInterface, g_meta, DEVICE_A);
503
504 /**
505 * @tc.steps: step2. call AckRecv, set inMsg nullptr or set context nullptr
506 * @tc.expected: step2. AckRecv return -E_INVALID_ARGS
507 */
508 SingleVerSyncTaskContext *context = new (std::nothrow) SingleVerKvSyncTaskContext();
509 ASSERT_TRUE(context != nullptr);
510 Message msg1(ABILITY_SYNC_MESSAGE);
511 msg1.SetMessageType(TYPE_RESPONSE);
512 EXPECT_EQ(async.AckRecv(nullptr, context), -E_INVALID_ARGS);
513 EXPECT_EQ(async.AckRecv(&msg1, nullptr), -E_INVALID_ARGS);
514
515 /**
516 * @tc.steps: step3. call AckRecv, set inMsg with no packet
517 * @tc.expected: step3. AckRecv return -E_INVALID_ARGS
518 */
519 EXPECT_EQ(async.AckRecv(&msg1, context), -E_INVALID_ARGS);
520 ASSERT_TRUE(context != nullptr);
521
522 /**
523 * @tc.steps: step4. create a AbilityAckPacket packet1
524 */
525 AbilitySyncAckPacket packet1;
526 packet1.SetProtocolVersion(ABILITY_SYNC_VERSION_V1);
527 packet1.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT);
528 packet1.SetAckCode(E_OK);
529 packet1.SetSchema(TEST_SCHEMA);
530 msg1.SetCopiedObject(packet1);
531
532 /**
533 * @tc.steps: step5. call AckRecv, set inMsg with packet
534 * @tc.expected: step5. AckRecv return ok GetRemoteSoftwareVersion is SOFTWARE_VERSION_CURRENT
535 * IsSchemaCompatible true;
536 */
537 EXPECT_EQ(async.AckRecv(&msg1, context), -E_SCHEMA_MISMATCH);
538 EXPECT_TRUE(context->GetRemoteSoftwareVersion() == SOFTWARE_VERSION_CURRENT);
539 EXPECT_TRUE(context->GetTaskErrCode() == -E_SCHEMA_MISMATCH);
540
541 /**
542 * @tc.steps: step6. call RequestRecv, SetSchema ""
543 * @tc.expected: step6. IsSchemaCompatible false
544 */
545 packet1.SetSchema("");
546 msg1.SetCopiedObject(packet1);
547 EXPECT_EQ(async.AckRecv(&msg1, context), -E_SCHEMA_MISMATCH);
548
549 /**
550 * @tc.steps: step7. call AckRecv, set inMsg sendCode -E_VERSION_NOT_SUPPORT
551 * @tc.expected: step7. return -E_VERSION_NOT_SUPPORT
552 */
553 packet1.SetSchema(TEST_SCHEMA);
554 packet1.SetAckCode(-E_VERSION_NOT_SUPPORT);
555 msg1.SetCopiedObject(packet1);
556 EXPECT_EQ(async.AckRecv(&msg1, context), -E_VERSION_NOT_SUPPORT);
557 RefObject::KillAndDecObjRef(context);
558 }
559
560 /**
561 * @tc.name: AckReceiveTest002
562 * @tc.desc: Verify Ability RDB AckReceive callback.
563 * @tc.type: FUNC
564 * @tc.require:
565 * @tc.author: xushaohua
566 */
567 HWTEST_F(DistributedDBAbilitySyncTest, AckReceiveTest002, TestSize.Level1)
568 {
569 /**
570 * @tc.steps: step1. create a AbilitySync
571 */
572 AbilitySync async;
573 VirtualRelationalVerSyncDBInterface *interface = new(std::nothrow) VirtualRelationalVerSyncDBInterface();
574 ASSERT_NE(interface, nullptr);
575 async.Initialize(g_communicatorB, interface, g_meta, DEVICE_A);
576 interface->SetPermitCreateDistributedTable(false);
577
578 /**
579 * @tc.steps: step2. call AckRecv, set inMsg nullptr or set context nullptr
580 * @tc.expected: step2. AckRecv return -E_INVALID_ARGS
581 */
582 auto *context = new (std::nothrow) SingleVerRelationalSyncTaskContext();
583 const std::string RDB_SCHEMA = "{\"TABLE_MODE\":\"SPLIT_BY_DEVICE\","
584 "\"SCHEMA_TYPE\":\"RELATIVE\","
585 "\"SCHEMA_VERSION\":\"2.1\"}";
586 ASSERT_TRUE(context != nullptr);
587 Message msg1(ABILITY_SYNC_MESSAGE);
588 msg1.SetMessageType(TYPE_RESPONSE);
589 AbilitySyncAckPacket packet;
590 packet.SetProtocolVersion(ABILITY_SYNC_VERSION_V1);
591 packet.SetSoftwareVersion(SOFTWARE_VERSION_CURRENT);
592 packet.SetAckCode(E_OK);
593 packet.SetSchema(RDB_SCHEMA);
594 packet.SetSchemaType(static_cast<uint32_t>(SchemaType::RELATIVE));
595 msg1.SetCopiedObject(packet);
596 EXPECT_EQ(async.AckRecv(&msg1, context), -E_NOT_SUPPORT);
597 EXPECT_EQ(context->GetTaskErrCode(), -E_NOT_SUPPORT);
598
599 RefObject::KillAndDecObjRef(context);
600 delete interface;
601 }
602