diff --git a/BUILD.gn b/BUILD.gn new file mode 100755 index 0000000..a608771 --- /dev/null +++ b/BUILD.gn @@ -0,0 +1,30 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +#import("//hit/build/suite.gni") +import("test_packages.gni") +ohos_test_suite("dcts") { + deps = selected_packages +} + +ohos_test_suite("dcts_ivi") { + deps = selected_packages_ivi +} + +ohos_test_suite("dcts_intellitv") { + deps = selected_packages_intellitv +} + +ohos_test_suite("dcts_wearable") { + deps = selected_packages_wearable +} diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..4a45986 --- /dev/null +++ b/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/OAT.xml b/OAT.xml new file mode 100755 index 0000000..687a86f --- /dev/null +++ b/OAT.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + diff --git a/README.en.md b/README.en.md index 2333521..3040453 100644 --- a/README.en.md +++ b/README.en.md @@ -1,36 +1,508 @@ -# xts_dcts +# XTS -#### Description -{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**} - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request +- [Introduction](#section465982318513) +- [System Types](#section125090457443) +- [Directory Structure](#section161941989596) +- [Constraints](#section119744591305) +- [Usage Guidelines](#section137768191623) +- [Test Case Development Guidelines](#section3695134065513) + - [C-based Test Case Development and Compilation \(for the Mini System\)](#section198193336544) + - [C-based Test Case Execution \(for the Mini System\)](#section13820233175418) + - [C++-based Test Case Development and Compilation \(for Standard and Small Systems\)](#section3822123311540) + - [C++-based Test Case Execution \(for Standard and Small Systems\)](#section128222336544) -#### Gitee Feature +## Introduction -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) +The X test suite \(XTS\) subsystem contains a set of OpenHarmony certification test suites, including the currently supported distributed compatibility test suite \(DCTS\). + +This subsystem contains the DCTS and **tools** software package. + +- The **dcts** directory stores the source code and configuration files of DCTS test cases. The DCTS helps device vendors detect the distributed scenario incompatibility as early as possible and ensures that the software is compatible to OpenHarmony during the entire development process. +- The **tools** software package stores the test case development framework related to **dcts**. + +## System Types + +OpenHarmony supports the following system types: + +- Mini system + + A mini system runs on the devices whose memory is greater than or equal to 128 KiB and that are equipped with MCU processors such as ARM Cortex-M and 32-bit RISC-V. This system provides multiple lightweight network protocols and graphics frameworks, and a wide range of read/write components for the IoT bus. Typical products include connection modules, sensors, and wearables for smart home. + +- Small system + + A small system runs on the devices whose memory is greater than or equal to 1 MiB and that are equipped with application processors such as ARM Cortex-A. This system provides higher security capabilities, standard graphics frameworks, and video encoding and decoding capabilities. Typical products include smart home IP cameras, electronic cat eyes, and routers, and event data recorders \(EDRs\) for smart travel. + +- Standard system + + A standard system runs on the devices whose memory is greater than or equal to 128 MiB and that are equipped with application processors such as ARM Cortex-A. This system provides a complete application framework supporting the enhanced interaction, 3D GPU, hardware composer, diverse components, and rich animations. This system applies to high-end refrigerator displays. + + +## Directory Structure + +``` +/test/xts +├── dcts # Test code +│ └── subsystem # Source code of subsystem test cases for the standard system +│ └── subsystem_lite # Source code of subsystems test cases for mini and small systems +│ └── common # Source code of Test cases rely on shared memory for mini and small systems +│ └── BUILD.gn # Build configuration of test cases for the standard system +│ └── build_lite +│ └── BUILD.gn # Build configuration of test cases for mini and small systems +└── tools # Test tool code +``` + +## Constraints + +Test cases for the mini system must be developed based on C, and those for the small system must be developed based on C++. + +## Usage Guidelines + +**Table 1** Test case levels + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Level

+

Definition

+

Scope

+

Level0

+

Smoke

+

Verifies basic functionalities of key features and basic DFX attributes with the most common input. The pass result indicates that the features are runnable.

+

Level1

+

Basic

+

Verifies basic functionalities of key features and basic DFX attributes with common input. The pass result indicates that the features are testable.

+

Level2

+

Major

+

Verifies basic functionalities of key features and basic DFX attributes with common input and errors. The pass result indicates that the features are functional and ready for beta testing.

+

Level3

+

Regular

+

Verifies functionalities of all key features, and all DFX attributes with common and uncommon input combinations or normal and abnormal preset conditions.

+

Level4

+

Rare

+

Verifies functionalities of key features under extremely abnormal presets and uncommon input combinations.

+
+ +**Table 2** Test case granularities + + + + + + + + + + + + + + + + + + + + +

Test Scale

+

Test Objects

+

Test Environment

+

LargeTest

+

Service functionalities, all-scenario features, and mechanical power environment (MPE) and scenario-level DFX

+

Devices close to real devices

+

MediumTest

+

Modules, subsystem functionalities after module integration, and DFX

+

Single device that is actually used. You can perform message simulation, but do not mock functions.

+

SmallTest

+

Modules, classes, and functions

+

Local PC. Use a large number of mocks to replace dependencies with other modules.

+
+ +**Table 3** Test types + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Type

+

Definition

+

Function

+

Tests the correctness of both service and platform functionalities provided by the tested object for end users or developers.

+

Performance

+

Tests the processing capability of the tested object under specific preset conditions and load models. The processing capability is measured by the service volume that can be processed in a unit time, for example, call per second, frame per second, or event processing volume per second.

+

Power

+

Tests the power consumption of the tested object in a certain period of time under specific preset conditions and load models.

+

Reliability

+

Tests the service performance of the tested object under common and uncommon input conditions, or specified service volume pressure and long-term continuous running pressure. The test covers stability, pressure handling, fault injection, and Monkey test times.

+

Security

+
  • Tests the capability of defending against security threats, including but not limited to unauthorized access, use, disclosure, damage, modification, and destruction, to ensure information confidentiality, integrity, and availability.
  • Tests the privacy protection capability to ensure that the collection, use, retention, disclosure, and disposal of users' private data comply with laws and regulations.
  • Tests the compliance with various security specifications, such as security design, security requirements, and security certification of the Ministry of Industry and Information Technology (MIIT).
+

Global

+

Tests the internationalized data and localization capabilities of the tested object, including multi-language display, various input/output habits, time formats, and regional features, such as currency, time, and culture taboos.

+

Compatibility

+
  • Tests backward compatibility of an application with its own data, the forward and backward compatibility with the system, and the compatibility with different user data, such as audio file content of the player and smart SMS messages.
  • Tests system backward compatibility with its own data and the compatibility of common applications in the ecosystem.
  • Tests software compatibility with related hardware.
+

User

+

Tests user experience of the object in real user scenarios. All conclusions and comments should come from the users, which are all subjective evaluation in this case.

+

Standard

+

Tests the compliance with industry and company-specific standards, protocols, and specifications. The standards here do not include any security standards that should be classified into the security test.

+

Safety

+

Tests the safety property of the tested object to avoid possible hazards to personal safety, health, and the object itself.

+

Resilience

+

Tests the resilience property of the tested object to ensure that it can withstand and maintain the defined running status (including downgrading) when being attacked, and recover from and adapt defense to the attacks to approach mission assurance.

+
+ +## Test Case Development Guidelines + +You should select the appropriate programming language and your target test framework to develop test cases. + +**Table 4** Test frameworks and test case languages for different systems + + + + + + + + + + + + + + + + + + + + +

System

+

Test Framework

+

Language

+

Mini

+

HCTest

+

C

+

Small

+

HCPPTest

+

C++

+

Standard

+

HJSUnit and HCPPTest

+

JavaScript and C++

+
+ +### C-based Test Case Development and Compilation \(for the Mini System\) + +**Developing test cases for the mini system** + +The HCTest framework is used to support test cases developed with the C language. HCTest is enhanced and adapted based on the open-source test framework Unity. + +1. Access the **test/xts/dcts** repository where the test cases will be stored. + + ``` + ├── dcts + │ └──subsystem_lite + │ │ └── module_hal + │ │ │ └── BUILD.gn + │ │ │ └── src + │ └──build_lite + │ │ └── BUILD.gn + ``` + +2. Write the test case in the **src** directory. + + 1 Import the test framework header file. + + ``` + #include "hctest.h" + ``` + + 2. Use the **LITE\_TEST\_SUIT** macro to define names of the subsystem, module, and test suite. + + ``` + /** + * @brief Registers a test suite named IntTestSuite. + * @param test Subsystem name + * @param example Module name + * @param IntTestSuite Test suite name + */ + LITE_TEST_SUIT(test, example, IntTestSuite); + ``` + + 3. Define Setup and TearDown. + + Format: Test suite name+Setup, Test suite name+TearDown. + + The Setup and TearDown functions must exist, but function bodies can be empty. + + 4. Use the **LITE\_TEST\_CASE** macro to write the test case. + + Three parameters are involved: test suite name, test case name, and test case properties \(including type, granularity, and level\). + + ``` + LITE_TEST_CASE(IntTestSuite, TestCase001, Function | MediumTest | Level1) + { + // Do something + }; + ``` + + 5. Use the **RUN\_TEST\_SUITE** macro to register the test suite. + + ``` + RUN_TEST_SUITE(IntTestSuite); + ``` + +3. Create the configuration file \(**BUILD.gn**\) of the test module. + + Create a **BUILD.gn** \(example\) build file in each test module directory. Specify the name of the built static library and its dependent header file and library in the build file. The format is as follows: + + ``` + import("//test/xts/tools/lite/build/suite_lite.gni") + hctest_suite("DctsDemoTest") { + suite_name = "dcts" + sources = [ + "src/test_demo.c", + ] + include_dirs = [ ] + cflags = [ "-Wno-error" ] + } + ``` + +4. Add build options to the **BUILD.gn** file in the **dcts** directory. + + You need to add the test module to the **test/xts/dcts/build\_lite/BUILD.gn** script in the **dcts** directory. + + ``` + lite_component("dcts") { + ... + if(board_name == "liteos_m") { + features += [ + ... + "//xts/dcts/subsystem_lite/module_hal:DctsDemoTest" + ] + } + } + ``` + +5. Run build commands. + + Test suites are built along with version build. The DCTS is built together with the debug version. + + >![](figures/icon-note.gif) **NOTE:** + >The DCTS build middleware is a static library, which will be linked to the image. + + +### C-based Test Case Execution \(for the Mini System\) + +**Executing test cases for the mini system** + +Burn the image into the development board. + +**Executing the test** + +1. Use a serial port tool to log in to the development board and save information about the serial port. +2. Restart the device and view serial port logs. + +**Analyzing the test result** + +View the serial port logs, whose format is as follows: + +The log for each test suite starts with **Start to run test suite:** and ends with **xx Tests xx Failures xx Ignored**. + +### C++-based Test Case Development and Compilation \(for Standard and Small Systems\) + +**Developing test cases for small-system devices** \(For examples of the standard system, go to the **global/i18n\_standard directory**.\) + +The HCPPTest framework is enhanced and adapted based on the open-source framework Googletest. + +1. Access the **test/xts/dcts** repository where the test cases will be stored. + + ``` + ├── dcts + │ └──subsystem_lite + │ │ └── module_posix + │ │ │ └── BUILD.gn + │ │ │ └── src + │ └──build_lite + │ │ └── BUILD.gn + ``` + +2. Write the test case in the **src** directory. + + 1. Import the test framework header file. + + The following statement includes **gtest.h**. + + ``` + #include "gtest/gtest.h" + ``` + + 2. Define Setup and TearDown. + + ``` + using namespace std; + using namespace testing::ext; + class TestSuite: public testing::Test { + protected: + // Preset action of the test suite, which is executed before the first test case + static void SetUpTestCase(void){ + } + // Test suite cleanup action, which is executed after the last test case + static void TearDownTestCase(void){ + } + // Preset action of the test case + virtual void SetUp() + { + } + // Cleanup action of the test case + virtual void TearDown() + { + } + }; + ``` + + 3. Use the **HWTEST** or **HWTEST\_F** macro to write the test case. + + **HWTEST**: definition of common test cases, including the test suite name, test case name, and case annotation. + + **HWTEST\_F**: definition of SetUp and TearDown test cases, including the test suite name, test case name, and case annotation. + + Three parameters are involved: test suite name, test case name, and test case properties \(including type, granularity, and level\). + + ``` + HWTEST_F(TestSuite, TestCase_0001, Function | MediumTest | Level1) { + // Do something + } + ``` + +3. Create a configuration file \(**BUILD.gn**\) of the test module. + + Create a **BUILD.gn** build file in each test module directory. Specify the name of the built static library and its dependent header file and library in the build file. Each test module is independently built into a **.bin** executable file, which can be directly pushed to the development board for testing. + + Example: + + ``` + import("//test/xts/tools/lite/build/suite_lite.gni") + hcpptest_suite("DctsDemoTest") { + suite_name = "dcts" + sources = [ + "src/TestDemo.cpp" + ] + + include_dirs = [ + "src", + ... + ] + deps = [ + ... + ] + cflags = [ "-Wno-error" ] + } + ``` + +4. Add build options to the **BUILD.gn** file in the **dcts** directory. + + Add the test module to the **test/xts/dcts/build\_lite/BUILD.gn** script in the **dcts** directory. + + ``` + lite_component("dcts") { + ... + else if(board_name == "liteos_a") { + features += [ + ... + "//xts/dcts/subsystem_lite/module_posix:DctsDemoTest" + ] + } + } + ``` + +5. Run build commands. + + Test suites are built along with the version build. The DCTS is built together with the debug version. + + >![](figures/icon-note.gif) **NOTE:** + >The DCTS for the small system is independently built to an executable file \(.bin\) and archived in the **suites\\dcts** directory of the build result. + + +### C++-based Test Case Execution \(for Standard and Small Systems\) + +**Executing test cases for the small system** + +Currently, test cases are shared by the NFS and mounted to the development board for execution. + +**Setting up the environment** + +1. Use a network cable or wireless network to connect the development board to your PC. +2. Configure the IP address, subnet mask, and gateway for the development board. Ensure that the development board and the PC are in the same network segment. +3. Install and register the NFS server on the PC and start the NFS service. +4. Run the **mount** command for the development board to ensure that the development board can access NFS shared files on the PC. + + Format: **mount** _NFS server IP address_**:/**_NFS shared directory_ **/**_development board directory_ **nfs** + + Example: + + ``` + mount 192.168.1.10:/nfs /nfs nfs + ``` + + +**Executing test cases** + +Execute **DctsDemoTest.bin** to trigger test case execution, and analyze serial port logs generated after the execution is complete. diff --git a/README.md b/README.md index b5e42a1..a7d0e5b 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,553 @@ -# xts_dcts +# XTS子系统 -#### 介绍 -{**以下是 Gitee 平台说明,您可以替换此简介** -Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 -无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 [https://gitee.com/enterprises](https://gitee.com/enterprises)} +- [简介](#section465982318513) +- [系统类型](#section125090457443) +- [目录](#section161941989596) +- [约束](#section119744591305) +- [使用说明](#section137768191623) +- [用例开发指导](#section3695134065513) + - [C语言用例开发编译指导(适用于轻量系统产品用例开发)](#section198193336544) + - [C语言用例执行指导(适用于轻量系统产品用例开发)](#section13820233175418) + - [C++语言用例开发编译指导(适用于小型系统、标准系统用例开发)](#section3822123311540) + - [C++语言用例执行指导(适用于小型系统、标准系统用例开发)](#section128222336544) -#### 软件架构 -软件架构说明 +- [相关仓](#section1371113476307) + +## 简介 + +XTS子系统是OpenHarmony生态认证测试套件的集合,当前包括dcts(distributed compatibility test suite)分布式兼容性测试套件。 + +XTS子系统当前包括dcts与tools软件包: + +- dcts,存放dcts相关测试用例源码与配置文件,其目的是帮助终端设备厂商尽早发现在分布式场景下与OpenHarmony的不兼容性,确保软件在整个开发过程中满足OpenHarmony的兼容性要求。 +- tools,存放dcts相关测试用例开发框架。 + +## 系统类型 + +OpenHarmony支持如下几种系统类型: + +- 轻量系统(mini system) + + 面向MCU类处理器例如Arm Cortex-M、RISC-V 32位的设备,硬件资源极其有限,支持的设备最小内存为128KiB,可以提供多种轻量级网络协议,轻量级的图形框架,以及丰富的IOT总线读写部件等。可支撑的产品如智能家居领域的连接类模组、传感器设备、穿戴类设备等。 + +- 小型系统(small system) + + 面向应用处理器例如Arm Cortex-A的设备,支持的设备最小内存为1MiB,可以提供更高的安全能力、标准的图形框架、视频编解码的多媒体能力。可支撑的产品如智能家居领域的IP Camera、电子猫眼、路由器以及智慧出行领域的行车记录仪等。 + +- 标准系统(standard system) + + 面向应用处理器例如Arm Cortex-A的设备,支持的设备最小内存为128MiB,可以提供增强的交互能力、3D GPU以及硬件合成能力、更多控件以及动画效果更丰富的图形能力、完整的应用框架。可支撑的产品如高端的冰箱显示屏。 -#### 安装教程 +## 目录 -1. xxxx -2. xxxx -3. xxxx +``` +/test/xts +├── dcts # 测试代码存放目录 +│ └── subsystem # 标准系统子系统测试用例源码存放目录 +│ └── subsystem_lite # 轻量系统、小型系统子系统测试用例源码存放目录 +│ └── common # 测试用例依赖共享内存源码存放目录 +│ └── BUILD.gn # 标准系统测试用例编译配置 +│ └── build_lite # 轻量系统、小型系统测试用例编译配置存放目录 +│ └── BUILD.gn # 轻量系统、小型系统测试用例编译配置 +└── tools # 测试工具代码存放目录 +``` -#### 使用说明 +## 约束 -1. xxxx -2. xxxx -3. xxxx +轻量系统用例开发语言是C,小型系统用例开发语言是C++。 -#### 参与贡献 +## 使用说明 -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request +**表 1** 用例级别说明 + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

级别名称

+

基本定义

+

测试范围

+

Level0

+

冒烟

+

验证关键功能点基本功能/最基本DFX属性在最常见输入下的表现,通过表示功能基本可运行。

+

Level1

+

基本

+

验证各功能点基本功能/基本DFX属性在常见输入下的表现,通过表示功能基本可测试。

+

Level2

+

重要

+

验证各功能点的基本功能/基本DFX属性在常规输入/常见异常情况下的表现,通过表示功能基本正常可用,可开展Beta。

+

Level3

+

一般

+

验证各功能点的全部功能/全部DFX属性在各种常规/非常规输入组合下,或各种正常/异常预置条件组合下的表现。

+

Level4

+

生僻

+

验证关键功能点在极端异常预置条件下、用户难以触及的异常输入组合下的表现。

+
+ +**表 2** 用例粒度说明 + + + + + + + + + + + + + + + + + + + + +

用例规模

+

被测试对象

+

测试环境

+

LargeTest

+

业务功能/全场景特性/整机及场景级DFX

+

尽量使用贴近真实的环境设备

+

MediumTest

+

模块/子系统集成至设备后的功能/DFX

+

使用真实的单设备进行验证,可进行消息模拟,尽量不对函数进行MOCK

+

SmallTest

+

模块/类/函数

+

在开发者个人环境进行测试,尽量不依赖其他模块,存在大量的MOCK

+
+ +**表 3** 测试类型说明 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

测试类型名称

+

测试类型定义

+

Function

+

验证被测对象提供给用户的业务功能实现正确性的测试项,这里的“用户”可以是终端用户或开发者,功能包括业务功能及平台功能

+

Performance

+

验证被测对象在特定预置条件/负载模型下的处理能力的测试项,“处理能力”一般以单位时间内可处理的业务量来衡量,如呼叫/秒,帧率/秒,事件处理量/秒等

+

Power

+

验证被测对象在特定预置条件/负载模型下在一定时间内能源消耗量的测试项

+

Reliability

+

验证被测对象在正常/异常输入情况下,或业务量压力和长时间连续运行压力情况下业务表现的测试项,含稳定性、压力、故障注入、Monkey测试项

+

Security

+

验证系统对恶意威胁的防护能力,威胁包括但不限于未授权访问、使用、泄露、破坏、修改、毁灭,以保障信息的机密性、完整性和可用性; 验证系统对用户隐私的保护能力,保障用户的隐私数据被收集、使用、保有、披露和处置符合法律规范,保障用户的隐私权; 验证对各类安全规范的遵从情况,如安全设计规范、安全红线、工信部安全认证规范等,保障安全相关法律法规的合规。

+

Global

+

验证被测对象在是否具有国际化数据支持和本地化能力的测试项,包括语言显示、输入/输出习惯、时间显示、区域特性如货币时间禁忌等等

+

Compatibility

+

当被测对象为应用时,包括被测对象对于自身数据的后向兼容性、对于系统的前后向兼容性、对于不同用户数据(如播放器之音频文件格式/智能短信之用户短信内容)的兼容性测试项; 当被测对象为系统时,包括被测系统对于系统自身数据的后向兼容性、以及对于生态中常用应用的兼容性测试项;当被测对象为软件时,包括被测系统对于相关的硬件的兼容性;

+

User

+

验证被测对象在真实用户场景下的用户体验感受的测试项,注意此种情况下没有客观的“正确”与“失败”,所有的结论及评价都应该来自于用户

+

Standard

+

验证被测对象对于行业及公司内标准/协议/规范的遵从情况的测试项,注意此处的“标准”不包含任何安全标准,针对安全标准的测试项划归为“安全测试”类型

+

Safety

+

验证被测对象的Safety属性,避免产品可能对人身安全、健康以及产品本身带来的危害。

+

Resilience

+

验证被测对象的韧性属性,确保系统受攻击时承受并保持在有定义的运行状态(包括降级)、恢复并适应攻击以保障Mission达成。

+
+ +## 用例开发指导 + +根据测试系统选择测试框架和对应测试用例语言。 + +**表 4** 系统和测试框架、开发语言对应关系 + + + + + + + + + + + + + + + + + + + + +

系统

+

测试框架

+

语言

+

轻量系统

+

hctest

+

c

+

小型系统

+

hcpptest

+

c++

+

标准系统

+

HJSUnit、hcpptest

+

js、c++

+
+ +### C语言用例开发编译指导(适用于轻量系统产品用例开发) + +**示例:轻量系统测试用例开发** + +当前使用的测试框架是hctest,hctest测试框架支持使用C语言编写测试用例,是在开源测试框架unity的基础上进行增强和适配。 + +1. 用例目录规范:测试用例存储到test/xts/dcts仓中 + + ``` + ├── dcts + │ └──subsystem_lite + │ │ └── module_hal + │ │ │ └── BUILD.gn + │ │ │ └── src + │ └──build_lite + │ │ └── BUILD.gn + ``` + +2. src目录下用例编写样例。 + + 1.引用测试框架 + + ``` + #include "hctest.h" + ``` + + 2. 使用宏定义LITE\_TEST\_SUIT定义子系统、模块、测试套件名称 + + ``` + /** + * @brief register a test suit named "IntTestSuite" + * @param test subsystem name + * @param example module name + * @param IntTestSuite test suit name + */ + LITE_TEST_SUIT(test, example, IntTestSuite); + ``` + + 3. 定义Setup与TearDown + + 命名方式:测试套件名称+Setup,测试套件名称+TearDown。 + + Setup与TearDown必须存在,可以为空函数。 + + 4. 使用宏定义LITE\_TEST\_CASE写测试用例 + + 包括三个参数:测试套件名称,测试用例名称,用例属性(测试类型、用例粒度、用例级别)。 + + ``` + LITE_TEST_CASE(IntTestSuite, TestCase001, Function | MediumTest | Level1) + { + //do something + }; + ``` + + 5. 使用宏定义 RUN\_TEST\_SUITE注册测试套件 + + ``` + RUN_TEST_SUITE(IntTestSuite); + ``` + +3. 测试模块的配置文件(BUILD.gn)样例: + + 在每个测试模块目录下新建BUILD.gn编译文件,用于指定编译后静态库的名称、依赖的头文件、依赖的库等;具体写法如下: + + ``` + import("//test/xts/tools/lite/build/suite_lite.gni") + hctest_suite("DctsDemoTest") { + suite_name = "dcts" + sources = [ + "src/test_demo.c", + ] + include_dirs = [ ] + cflags = [ "-Wno-error" ] + } + ``` + +4. dcts下BUILD.gn增加编译选项。 + + 需要将测试模块加入到dcts目录下的编译脚本中,编译脚本路径:test/xts/dcts/build\_lite/BUILD.gn。 + + ``` + lite_component("dcts") { + ... + if(board_name == "liteos_m") { + features += [ + ... + "//xts/dcts/subsystem_lite/module_hal:DctsDemoTest" + ] + } + } + ``` + +5. 测试套件编译命令。 + + 随版本编译,debug版本编译时会同步编译dcts测试套件 + + >![](figures/icon-note.gif) **说明:** + >dcts测试套件编译中间件为静态库,最终链接到版本镜像中 。 -#### 特技 +### C语言用例执行指导(适用于轻量系统产品用例开发) + +**示例:轻量系统测试用例执行** + +将版本镜像烧录进开发板。 + +**测试步骤** + +1. 使用串口工具登录开发板,并保存串口打印信息。 +2. 重启设备,查看串口日志。 + +**测试结果分析指导** + +基于串口打印日志进行分析; + +每个测试套件执行以Start to run test suite开始,以xx Tests xx Failures xx Ignored结束。 + +### C++语言用例开发编译指导(适用于小型系统、标准系统用例开发) + +**示例:小型系统测试用例开发**(标准系统参考具体样例目录:global/i18n\_standard) + +当前使用的测试框架是hcpptest,hcpptest测试框架是在开源的googletest测试框架的基础上进行的增强和适配。 + +1. 规范用例目录:测试用例存储到test/xts/dcts仓中。 + + ``` + ├── dcts + │ └──subsystem_lite + │ │ └── module_posix + │ │ │ └── BUILD.gn + │ │ │ └── src + │ └──build_lite + │ │ └── BUILD.gn + ``` + +2. 测试模块src下用例编写样例: + + 1. 引用测试框架: + + 需要引用gtest.h 如:\#include "gtest/gtest.h" + + ``` + #include "gtest/gtest.h" + ``` + + 2. 定义Setup与TearDown + + ``` + using namespace std; + using namespace testing::ext; + class TestSuite: public testing::Test { + protected: + // Preset action of the test suite, which is executed before the first test case + static void SetUpTestCase(void){ + } + // Test suite cleanup action, which is executed after the last test case + static void TearDownTestCase(void){ + } + // Preset action of the test case + virtual void SetUp() + { + } + // Cleanup action of the test case + virtual void TearDown() + { + } + }; + ``` + + 3. 使用宏定义HWTEST或HWTEST\_F写测试用例 + + 普通测试用例的定义:HWTEST(测试套名称, 测试用例名称, 用例标注)。 + + 包含SetUp和TearDown的测试用例的定义 :HWTEST\_F(测试套名称, 测试用例名称,用例标注)。 + + 宏定义包括三个参数:测试套件名称,测试用例名称,用例属性(测试类型、用例粒度、用例级别)。 + + ``` + HWTEST_F(TestSuite, TestCase_0001, Function | MediumTest | Level1) { + // do something + } + ``` + +3. 测试模块下用例配置文件(BUILD.gn)样例: + + 每个测试模块目录下新建BUILD.gn编译文件,用于指定编译后可执行文件的名称、依赖的头文件、依赖的库等;具体写法如下。每个测试模块将独立编译成.bin可执行文件, 该文件可直接push到单板上进行测试。 + + 举例: + + ``` + import("//test/xts/tools/lite/build/suite_lite.gni") + hcpptest_suite("DctsDemoTest") { + suite_name = "dcts" + sources = [ + "src/TestDemo.cpp" + ] + + include_dirs = [ + "src", + ... + ] + deps = [ + ... + ] + cflags = [ "-Wno-error" ] + } + + ``` + +4. dcts目录下增加编译选项(BUILD.gn)样例: + + 将测试模块加入到dcts目录下的编译脚本中,编译脚本为:test/xts/dcts/build\_lite/BUILD.gn。 + + ``` + lite_component("dcts") { + ... + else if(board_name == "liteos_a") { + features += [ + ... + "//xts/dcts/subsystem_lite/module_posix:DctsDemoTest" + ] + } + } + ``` + +5. 测试套件编译命令。 + + 随版本编译,debug版本编译时会同步编译dcts测试套件 + + >![](figures/icon-note.gif) **说明:** + >小型系统dcts独立编译成可执行文件(bin格式), 在编译产物的suites\\dcts目录下归档。 + + +### C++语言用例执行指导(适用于小型系统、标准系统用例开发) + +**示例:小型系统测试用例执行** + +目前的用例执行采用nfs共享的方式,mount到单板去执行。 + +**环境搭建** + +1. 使用网线或无线网络将开发板与PC进行连接。 +2. 开发板配置IP、子网掩码、网关,确保开发板与PC处于同一个网段。 +3. PC安装nfs服务器并完成注册,启动nfs服务。 +4. 开发板配置mount命令,确保开发板可以访问PC端的nfs共享文件。 + + 格式:mount \[nfs服务器IP\]:\[/nfs共享目录\] \[/开发板目录\] nfs + + 举例: + + ``` + mount 192.168.1.10:/nfs /nfs nfs + ``` + + +**用例执行** + +测试套件执行 DctsDemoTest.bin 触发用例执行,基于串口打印日志进行分析。 + +### 全量编译指导(适用于标准系统) + +全量编译test/xts/dcts目录下执行编译命令: ./build.sh suite=dcts system_size=standard + +测试用例输出目录:out/release/suites/dcts/testcases + +测试框架&用例整体输出目录:out/release/suites/dcts(编译用例时会同步编译测试套执行框架) + +### 全量用例执行指导(适用于小型系统、标准系统) + +搭建测试环境 Windows工作台下安装python3.7及以上版本,确保工作台和测试设备正常连接。 + +测试执行目录(对应编译生成的out/release/suites/dcts目录) + + ``` +├── testcase # 测试套文件存放目录 +│ └──xxx.hap # 测试套可执行hap文件 +│ └──xxx.json # 测试套对应执行配置文件 +├── tools # 测试框架工具目录 +├── run.bat # window平台测试套启动执行文件 +├── report # 测试报告生成目录 + + ``` +用例执行 + +在Windows工作台上,找到从Linux服务器上拷贝下来的测试套件用例目录,在Windows命令窗口进入对应目录,直接执行dcts\run.bat。 + +界面启动后,输入用例执行指令。 + +全量执行:run dcts + +模块执行(具体模块可以查看\dcts\testcases):run –l DctsSamgrTest + +查看测试报告。 进入dcts\reports\,获取当前的执行记录,打开“summary_report.html”可以获取到测试报告。 + + +## 相关仓 + +xts\_dcts + +xts\_tools -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..4920c36 --- /dev/null +++ b/build.sh @@ -0,0 +1,119 @@ +#!/bin/bash + +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e + +usage() +{ + echo + echo "USAGE" + echo " ./build.sh [suite=BUILD_TARGET] [target_os=TARGET_OS] [target_arch=TARGET_ARCH] [variant=BUILD_VARIANT] [target_subsystem=TARGET_SUBSYSTEM]" + echo " target_platform : TARGET_PLATFORM the target platform, such as phone or ivi; Default to phone" + echo " suite : BUILD_TARGET cts/hit/vts and so on, default value is hit" + echo " target_arch : TARGET_ARCH arm64 or arm32, default value is arm64" + echo " variant : BUILD_VARIANT release or debug, default value is debug" + echo " target_subsystem : TARGET_SUBSYSTEM the target subsystem to build" + echo " system_size : SYSTEM_SIZE standard, large and son on, large is for L3-L5, standard is for L2 default value is large" + echo " product_name : PRODUCT_NAME the name of product. such as hikey960, Hi3516DV300, and so on." + echo + exit 1 +} + + +parse_cmdline() +{ + BASE_HOME=$(dirname $(cd $(dirname $0); pwd)) + BASE_HOME=${BASE_HOME}/../.. + BUILD_TOOLS_DIR=${BASE_HOME}/prebuilts/build-tools/linux-x86/bin + OUT_DIR=${BASE_HOME}/out + BUILD_SHELL=${BASE_HOME}/build.sh + # build all parts for all products by default + BUILD_TARGET="" + TARGET_PLATFORM=all + GN_ARGS="is_dbt_test=true include_all=false" + TARGET_ARCH=arm64 + BUILD_VARIANT=release + UPLOAD_API_INFO=False + SYSTEM_SIZE=large + PRODUCT_NAME="" + export PATH=${BASE_HOME}/prebuilts/python/linux-x86/3.8.3/bin:$PATH + + while [ -n "$1" ] + do + var="$1" + OPTIONS=${var%%=*} + PARAM=${var#*=} + echo "OPTIONS=$OPTIONS" + echo "PARAM=$PARAM" + echo "-------------------" + case "$OPTIONS" in + suite) BUILD_TARGET="$PARAM" + ;; + target_arch) TARGET_ARCH="$PARAM" + ;; + variant) BUILD_VARIANT="$PARAM" + ;; + target_platform) TARGET_PLATFORM="$PARAM" + ;; + target_subsystem) export target_subsystem=${PARAM} + ;; + system_size) SYSTEM_SIZE="$PARAM" + ;; + product_name) PRODUCT_NAME="$PARAM" + ;; + upload_api_info) UPLOAD_API_INFO=$(echo $PARAM |tr [a-z] [A-Z]) + ;; + *) usage + break;; + esac + shift + done + if [ "$SYSTEM_SIZE" = "standard" ]; then + BUILD_TARGET=${BUILD_TARGET:-"dcts"} + PRODUCT_NAME=${PRODUCT_NAME:-"Hi3516DV300"} + else + BUILD_TARGET=${BUILD_TARGET:-"dcts dcts_ivi dcts_intellitv dcts_wearable"} + PRODUCT_NAME=${PRODUCT_NAME:-"arm64"} + fi +} + + +do_make() +{ + cd $BASE_HOME + HATS_ROOT="$BASE_HOME/test/xts/dcts" + + rm -rf "$BASE_HOME/test/xts/autogen_apiobjs" + export XTS_SUITENAME=dcts + if [ "$SYSTEM_SIZE" = "standard" ]; then + ./build.sh --product-name $PRODUCT_NAME --gn-args build_xts=true --build-target $BUILD_TARGET --build-target "deploy_testtools" --gn-args is_standard_system=true + else + if [ "$BUILD_TARGET" = "dcts dcts_ivi dcts_intellitv dcts_wearable" ]; then + ./build.sh --product-name $PRODUCT_NAME --gn-args build_xts=true --build-target "dcts" --build-target "dcts_ivi" --build-target "dcts_intellitv" --build-target "dcts_wearable" --build-target "deploy_testtools" + else + ./build.sh --product-name $PRODUCT_NAME --gn-args build_xts=true --build-target $BUILD_TARGET --build-target "deploy_testtools" + fi + fi + ret=$? + + rm -rf "$BASE_HOME/test/xts/autogen_apiobjs" + if [ "$ret" != 0 ]; then + echo "build error" + exit 1 + fi +} +parse_cmdline $@ +do_make +exit 0 diff --git a/common/shm_utils.cpp b/common/shm_utils.cpp new file mode 100644 index 0000000..1b1735d --- /dev/null +++ b/common/shm_utils.cpp @@ -0,0 +1,177 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "shm_utils.h" +#include +#include +using namespace std; + +static void *shm = NULL; //分配的共享内存的原始首地址 +static struct shared_use_st *shared; //指向shm +static int shmid; //共享内存标识符 + +int createShm(int key) +{ + //创建共享内存 + shmid = shmget((key_t)key,sizeof(struct shared_use_st), 0666|IPC_CREAT); + if(shmid == -1) + { + return -1; + } + //将共享内存连接到当前进程的地址空间 + shm = shmat(shmid,NULL,0); + if(shm == (void*)-1) + { + return -1; + } + + shared = (struct shared_use_st*)shm; + initShm(); + return 0; +} + +void initShm(void) +{ + memset_s(shm, sizeof(struct shared_use_st), 0, sizeof(struct shared_use_st)); + shared->written = 0; + memset_s(shared->data, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH); + return; +} + +int readDataFromShm(char* buf) +{ + //LOG("readDataFormShm, begin"); + if(shared->written !=0) + { + strcpy_s(buf , strlen(shared->data)+1, shared->data); + shared->written = 0; + LOG("readDataFromShm %s",buf); + memset_s(shared->data, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH); + } + else + { + return -1; + } + return 0; +} + +int waitDataWithCode(char* code, char* data) +{ + int i = 0; + int timeout = 10; + char str[1024] = {0}; + memset_s(str, MAX_DATA_LENGTH, 0, MAX_DATA_LENGTH); + while(iwritten == 1) + { + sleep(1); + } + + LOG("writeDataToShm %s", buf); + memset_s(shared->data, 5, 0, 5); + strcpy_s(shared->data, strlen(buf)+1, buf); + shared->written = 1; + sleep(2); + return 0; +} + +int disconnectShm(void) +{ + if(shmdt(shm) ==-1) + { + return -1; + } + return 0; +} + +int deleteShm(void) +{ + if(shmctl(shmid, IPC_RMID, 0) == -1) + { + return -1; + } + return 0; +} + + +char* Int2String(int num,char *str)//10进制 +{ + int i = 0;//指示填充str + if (num<0)//如果num为负数,将num变正 + { + num = -num; + str[i++] = '-'; + } + //转换 + do + { + str[i++] = num%10+48;//取num最低位 字符0~9的ASCII码是48~57:简单来说数字0+48=48,ASCII码对应字符'0' + num /=10;//去掉最低位 + }while(num);//num不为0继续循环 + + str[i] = '\0'; + + //确定开始调整的位置 + int j = 0; + if(str[0]=='-')//如果有负号,负号不用调整 + { + j = 1;//从第二位开始调整 + ++i;//由于有负号,所以交换的对称轴也要后移一位 + } + //对称交换 + for(;j +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_DATA_LENGTH 1024 +#define MAX_WAIT_TIMEOUT 10 +#define SHM_SEND_KEY 123456 +#define SHM_RECV_KEY 123466 + +struct shared_use_st +{ + int written;//作为一个标志,非0:表示可读,0表示可写 + char data[MAX_DATA_LENGTH];//记录写入和读取的文本 +}; + +#define LOG(format, ...) \ +do \ +{ \ + time_t timeSec; \ + time(&timeSec); \ + struct tm tmRst; \ + localtime_r(&timeSec, &tmRst); \ + char strTime[10]; \ + strftime(strTime, sizeof(strTime), "%H:%M:%S", &tmRst); \ + fprintf(stdout, "[shm-utils] %s " format "\n",strTime, ##__VA_ARGS__); \ + FILE* fp; \ + fp = fopen("/data/disTestSuiteLog" , "a+"); \ + fprintf(fp, "[shm-utils] %s " format "\n",strTime, ##__VA_ARGS__); \ + fclose(fp); \ +} while(0) + +int createShm(int key); +int disconnectShm(void); +int writeCodeDataToShm(int code, char* buf); +int waitDataWithCode(char* code, char* data); +int writeDataToShm(char* buf); +int deleteShm(void); +char* Int2String(int num,char *str); +void initShm(void); +int readDataFromShm(char* buf); + + +#endif + diff --git a/distributeddatamgr/BUILD.gn b/distributeddatamgr/BUILD.gn new file mode 100644 index 0000000..5703a28 --- /dev/null +++ b/distributeddatamgr/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import("//build/ohos_var.gni") +group("distributeddatatest") { + testonly = true + if (is_standard_system) { + deps = [ + "distributed_kv_store:disDataTest", + ":disDataProcess", + ] + } +} + +shared_library("disDataProcess") { + #module_out_path = module_output_path + # + sources = [ + "./process_common/process_msg.cpp", + + ] + + visibility = [ ":*" ] + + include_dirs = [ + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/src", + "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/app_distributeddata/include", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/permission", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/account", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/broadcaster", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/utils", + "//utils/native/base/include", + "//utils/system/safwk/native/ include", + "../include", + "../../../../interfaces/innerkits/distributeddata", + "//third_party/json/single_include", + "//base/powermgr/power_manager/interfaces/innerkits/native/include", + "../../common", + "../common", + ] + + deps = [ + "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/libs/distributeddb:distributeddb", + "//utils/native/base:utils", + ] + + # external_deps = [ + # "hiviewdfx_hilog_native:libhilog", + # "ipc:ipc_core", + # "permission_standard:libpermissionsdk_standard", + # ] + + # subsystem_name = "distributeddatamgr" +} + diff --git a/distributeddatamgr/distributed_kv_store/BUILD.gn b/distributeddatamgr/distributed_kv_store/BUILD.gn new file mode 100644 index 0000000..66f532d --- /dev/null +++ b/distributeddatamgr/distributed_kv_store/BUILD.gn @@ -0,0 +1,63 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import("//test/xts/tools/build/suite.gni") + +module_output_path = "dcts/distributeddatamgr" + +############################################################################################### +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/src", + "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/app_distributeddata/include", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/permission", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/account", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/broadcaster", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/utils", + "//utils/native/base/include", + "//utils/system/safwk/native/ include", + "../include", + "../../../../interfaces/innerkits/distributeddata", + "//third_party/json/single_include", + "//base/powermgr/power_manager/interfaces/innerkits/native/include", + "../../common", + ] +} + +ohos_moduletest_suite("disDataTest") { + module_out_path = module_output_path + + sources = [ + "./disdata_func_test.cpp", + "../../common/shm_utils.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", + "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter:distributeddata_adapter", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "permission_standard:libpermissionsdk_standard", + ] +} + +########################################################################################### diff --git a/distributeddatamgr/distributed_kv_store/Test.json b/distributeddatamgr/distributed_kv_store/Test.json new file mode 100644 index 0000000..40d6469 --- /dev/null +++ b/distributeddatamgr/distributed_kv_store/Test.json @@ -0,0 +1,18 @@ +{ + "kits":[ + { + "push":[ + "disDataTest->/data/local/tmp/disDataTest" + ], + "type":"PushKit" + } + ], + "driver":{ + "native-test-timeout":"120000", + "type":"CppTest", + "module-name":"disDataTest", + "runtime-hint":"1s", + "native-test-device-path":"/data/local/tmp" + }, + "description":"Configuration for disDataTest Tests" +} diff --git a/distributeddatamgr/distributed_kv_store/disdata_func_test.cpp b/distributeddatamgr/distributed_kv_store/disdata_func_test.cpp new file mode 100644 index 0000000..229d15e --- /dev/null +++ b/distributeddatamgr/distributed_kv_store/disdata_func_test.cpp @@ -0,0 +1,3159 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "disDataTest" + +#include +#include +#include +#include "log_print.h" +#include "distributed_kv_data_manager.h" +#include "types.h" +#include "shm_utils.h" +#include +#include +#include // 使用stringstream +#include +#include +#include +#include +#include +typedef enum{ + CTRL_CODE_SOFTBUS_TYPE = 1000, + CTRL_CODE_SOFTBUS_OPEN_SESSION, + CTRL_CODE_DATAMGR_TYPE = 2000, + CTRL_CODE_DATAMGR_GET_DATA, + CTRL_CODE_DATAMGR_GET_DATA_REPLY, + CTRL_CODE_DATAMGR_CREATE_KV, + CTRL_CODE_DATAMGR_DELETE_KV, + CTRL_CODE_DATAMGR_PUT_DATA, + CTRL_CODE_DATAMGR_DELETE_DATA, + CTRL_CODE_DM_TYPE = 3000, + CTRL_CODE_FILEMGR_TYPE = 4000, + CTRL_CODE_DP_TYPE = 5000, + CTRL_CODE_SEC_TYPE = 6000, + CTRL_CODE_MEDIA_TYPE = 7000, + CTRL_CODE_RESULT_TYPE = 9999, +} CtrlCodeType; + +#define MAX_DATA_LENGTH 1024 + +using namespace testing::ext; +using namespace OHOS::DistributedKv; +namespace { +const int USLEEP_TIME = 2000000; +} + + +class DeathRecipient : public KvStoreDeathRecipient { + public: DeathRecipient() {} + virtual ~DeathRecipient() {} + virtual void OnRemoteDied() override { + std::cout << "server is dead" << std::endl; } +}; + + +class DistributedKvDataManagerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + + static const uint32_t CHECK_WAITING_TIME = 50000; //50ms + static void RemoveAllStore(DistributedKvDataManager manager); + + DistributedKvDataManagerTest(); +}; + +class DisKvTest { +public: + static DistributedKvDataManager manager; + static std::shared_ptr KvStorePtr; // declare kvstore instance. + static Status statusGetKvStore; + static Status statusCloseKvStore; + static Status statusDeleteKvStore; + static Options create; + static UserId userId; + static AppId appId; + static StoreId storeIdTest; + +}; + +DistributedKvDataManager DisKvTest::manager; +std::shared_ptr DisKvTest::KvStorePtr = nullptr; // declare kvstore instance. +Status DisKvTest::statusGetKvStore = Status::ERROR; +Status DisKvTest::statusCloseKvStore = Status::ERROR; +Status DisKvTest::statusDeleteKvStore = Status::ERROR; +UserId DisKvTest::userId; +AppId DisKvTest::appId; +StoreId DisKvTest::storeIdTest; + +void DistributedKvDataManagerTest::SetUpTestCase(void) +{ + //连接共享内存 + createShm(SHM_SEND_KEY); +} + +void DistributedKvDataManagerTest::TearDownTestCase(void) +{ + //解关联共享内存 + disconnectShm(); +} + +void DistributedKvDataManagerTest::SetUp(void) +{ + DisKvTest::userId.userId = "account0"; + DisKvTest::appId.appId = "com.ohos.kvdatamanager3.test"; + DisKvTest::storeIdTest.storeId = "test3"; + + Options options { + .createIfMissing = true, + .encrypt = false, // .persistant = true, + .autoSync = false, + .backup = false, + .kvStoreType = KvStoreType::SINGLE_VERSION + }; + + + //1.本地删除数据库 + RemoveAllStore(DisKvTest::manager); + + //2.远端删除数据库 + char* str1 =(char *)malloc(MAX_DATA_LENGTH); + memset_s(str1,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(str1, strlen("")+1,""); + writeCodeDataToShm(CTRL_CODE_DATAMGR_DELETE_KV, str1); + + char code[10]={"9999"}; + memset_s(str1,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE,str1))+1, Int2String(CTRL_CODE_RESULT_TYPE,str1)); + memset_s(str1,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + waitDataWithCode(code, str1); + + free(str1); + + //1.本地创建数据库 + auto deathRecipient = std::make_shared(); + DisKvTest::manager.RegisterKvStoreServiceDeathRecipient(deathRecipient); + + DisKvTest::statusGetKvStore = DisKvTest::manager.GetSingleKvStore(options, { DisKvTest::appId }, { DisKvTest::storeIdTest }, DisKvTest::KvStorePtr); + + + //2. 远端创建数据库 + str1 =(char *)malloc(MAX_DATA_LENGTH); + memset_s(str1,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(str1, strlen("")+1,""); + writeCodeDataToShm(CTRL_CODE_DATAMGR_CREATE_KV, str1); + + memset_s(str1,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE,str1))+1, Int2String(CTRL_CODE_RESULT_TYPE,str1)); + waitDataWithCode(code, str1); + free(str1); + +} + +void DistributedKvDataManagerTest::TearDown(void) +{ + // //1.本地删除数据库 + // RemoveAllStore(DisKvTest::manager); + + // //2.远端删除数据库 + // char* str1 =(char *)malloc(MAX_DATA_LENGTH); + // memset_s(str1,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + // strcpy_s(str1,""); + // writeCodeDataToShm(CTRL_CODE_DATAMGR_DELETE_KV, str1); + + // char code[10]={"9999"}; + // memset_s(str1,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + // strcpy_s(code, Int2String(CTRL_CODE_RESULT_TYPE,str1)); + // memset_s(str1,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + // waitDataWithCode(code, str1); + + // free(str1); + +} + +void DistributedKvDataManagerTest::RemoveAllStore(DistributedKvDataManager manager) +{ + DisKvTest::statusCloseKvStore = DisKvTest::manager.CloseAllKvStore(DisKvTest::appId); + DisKvTest::statusDeleteKvStore = DisKvTest::manager.DeleteAllKvStore(DisKvTest::appId); +} + +DistributedKvDataManagerTest::DistributedKvDataManagerTest(void) +{} + + + + +class KvStoreSyncCallbackTestImpl : public KvStoreSyncCallback { +public: + void SyncCompleted(const std::map &results); +}; + +void KvStoreSyncCallbackTestImpl::SyncCompleted(const std::map &results) +{ + //std::cout<<"KvStoreSyncCallbackTestImpl timestamp:"<< getCurrentLocalTimeStamp() << std::endl; + std::cout<<"KvStoreSyncCallbackTestImpl timestamp:" << std::endl; + for (const auto& result : results) + { + std::cout<< result.first << " "<< static_cast(result.second) << std::endl; + } + +} + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Push_0100 + * @tc.name: DistribitedKvDataManagerTest sync push + * @tc.desc: sync push, put int data + * @tc.type: FUNC + */ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Push_0100, TestSize.Level1|Function|MediumTest ) + { + ZLOGI("DistribitedKvDataManager_Sync_Push_0100 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + + //本地数据库添加数据 100 + std::string stringKey = "math_score_int"; + Key keyInt = stringKey; + //int nint = 100; + Value valueInt = Value(TransferTypeToByteArray(100)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + + + char strKV[MAX_DATA_LENGTH] = {"math"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_int:100")+1, "math_score_int:100"); + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); //2001:math_score_int:10 + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "int 100 get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + // ret = 0; + EXPECT_EQ(ret, 0); +} + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Push_0200 + * @tc.name: DistribitedKvDataManagerTest sync push + * @tc.desc: sync push, put float data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Push_0200, TestSize.Level1) + { + ZLOGI("DistribitedKvDataManager_Sync_Push_0200 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + ////////////////////////////////////////////////////////////////////////////////////////////// + //本地数据库添加数据 3.14f + std::string stringKey = "math_score_float"; + Key keyInt = stringKey; + //float nfloat = 3.14f; + Value valueInt = Value(TransferTypeToByteArray(3.14f)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + + + char strKV[MAX_DATA_LENGTH] = {"math"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_float:3.14")+1, "math_score_float:3.14"); + + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); //2001:math_score_int:10 + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " float 3.14f get result=" << str<< std::endl; + + //检查远端是否返回成功 + + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + } + + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Push_0300 + * @tc.name: DistribitedKvDataManagerTest sync push + * @tc.desc: sync push, put double data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Push_0300, TestSize.Level1) + { + ZLOGI("DistribitedKvDataManager_Sync_Push_0300 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + + ////////////////////////////////////////////////////////////////////////////////////////////// + //本地数据库添加数据 28.288f + std::string stringKey = "math_score_double"; + Key keyInt = stringKey; + //double nfloat = 28.288f; + Value valueInt = Value(TransferTypeToByteArray(28.288f)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + + + //同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + char strKV[MAX_DATA_LENGTH] = {"math"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_double:28.288")+1, "math_score_double:28.288"); + + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); //2001:math_score_int:10 + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "(28.288f) get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Push_0400 + * @tc.name: DistribitedKvDataManagerTest sync push + * @tc.desc: sync push, put int64_t data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Push_0400, TestSize.Level1) + { + ZLOGI("DistribitedKvDataManager_Sync_Push_0400 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //本地数据库添加数据 12345678 + Key keyInt = "math_score_int64_t"; + Value valueInt = Value(TransferTypeToByteArray(12345678)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //数据发送到远端并等待返回结果 + char strKV[MAX_DATA_LENGTH] = {"math"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_int64_t:12345678")+1, "math_score_int64_t:12345678"); + + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); //2001:math_score_int:10 + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " (12345678) get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + + } + + + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Push_0500 + * @tc.name: DistribitedKvDataManagerTest sync push + * @tc.desc: sync push, put size_t data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Push_0500, TestSize.Level1) + { + ZLOGI("DistribitedKvDataManager_Sync_Push_0500 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + + //本地数据库添加数据 28 + Key keyInt = "math_score_size_t"; + Value valueInt = Value(TransferTypeToByteArray(28)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //数据发送到远端并等待返回结果 + char strKV[MAX_DATA_LENGTH] = {"math"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_size_t:28")+1, "math_score_size_t:28"); + + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); //2001:math_score_int:10 + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " (28) get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Push_0600 + * @tc.name: DistribitedKvDataManagerTest sync push + * @tc.desc: sync push, put string data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Push_0600, TestSize.Level1) + { + ZLOGI("DistribitedKvDataManager_Sync_Push_0600 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + + //本地数据库添加数据string + Key keyInt = "math_score_string"; + Value valueInt = Value( "{\"class\":20, \"age\":18, \"gradle\":\"good\"}" ); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //数据发送到远端并等待返回结果 + char str[MAX_DATA_LENGTH] ={"math_score_string:{\"class\":20, \"age\":18, \"gradle\":\"good\"}"}; + //strcpy_s(str, strlen("math_score_int:d")+1, "math_score_int:d"); + + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, str); //2001:math_score_int:10 + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "string get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + + } + + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Push_0700 + * @tc.name: DistribitedKvDataManagerTest sync push + * @tc.desc: sync push, put vector data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Push_0700, TestSize.Level1) + { + ZLOGI("DistribitedKvDataManager_Sync_Push_0700 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + + //1./////////////////////////////////////////////////////////////////////////////////////////////////////// + //本地数据库添加数据 vector + std::string stringKey = "math_score_vector"; + Key keyInt = stringKey; + std::vector vect = {0,1,2,3,4,5,6,7}; + Value valueInt = Value( vect ); + Status status = DisKvTest::KvStorePtr->Put(keyInt,valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + + status =DisKvTest:: KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + usleep(USLEEP_TIME); + + //数据发送到远端并等待返回结果 + //std::vector vect = {0,1,2,3,4,5,6,0}; + std::string strvc; + strvc.assign(vect.begin(), vect.end()); + + char strKV[MAX_DATA_LENGTH] = {"math_score_int"}; + strcpy_s(strKV, strlen(stringKey.c_str())+1, stringKey.c_str()); + strcat(strKV, ":"); + strcat(strKV, strvc.c_str()); + + std::cout<< "strvc = "<< strvc < get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Pull_0100 + * @tc.name: remote put data, local sync pull + * @tc.desc: sync pull, put int data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Pull_0100, TestSize.Level1) + { + ZLOGI("DistribitedKvDataManager_Sync_Pull_0100 begin."); + //1.本端创建KV + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端KV + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_int"; + Value valueInt = Value(TransferTypeToByteArray(100)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端修改数据200 + //char strPut[MAX_DATA_LENGTH] = {"math_score_int:200"}; + char strKV[MAX_DATA_LENGTH] = {"math"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_int:200")+1, "math_score_int:200"); + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " put-200 get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PULL); //PUSH_PULL + EXPECT_EQ(status, Status::SUCCESS); + + auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + + + + //7.本地get数据 与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get("math_score_int", valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + int aaa = TransferByteArrayToType(valueRetInt.Data()); + EXPECT_EQ( aaa, 200); + + + //8.数据发送到远端并等待返回结果 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " get-200 get result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Pull_0200 + * @tc.name: remote put data, local sync pull + * @tc.desc: sync pull, put float data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Pull_0200, TestSize.Level1) + { + ZLOGI("DistribitedKvDataManager_Sync_Pull_0200 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据 100 + std::string stringKey = "math_score_float"; + Key keyInt = stringKey; + //float nfloat = 3.14f; + Value valueInt = Value(TransferTypeToByteArray(3.14f)); + + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端修改数据200 + + + char strKV[MAX_DATA_LENGTH] = {"math"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_float:9.99")+1, "math_score_float:9.99"); + + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "put-9.99 get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + + //6.本地pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PULL); + EXPECT_EQ(status, Status::SUCCESS); + + auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + + //7.本地get数据 与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + float aaa = TransferByteArrayToType(valueRetInt.Data()); + float delta = aaa - 9.99f; + EXPECT_LE( std::abs(delta), 0.00001); + + //8.数据发送到远端并等待返回结果 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "get-9.99 get result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Pull_0300 + * @tc.name: remote put data, local sync pull + * @tc.desc: sync pull, put double data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Pull_0300, TestSize.Level1) + { + ZLOGI("DistribitedKvDataManager_Sync_Pull_0300 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据28.288f + std::string stringKey = "math_score_double"; + Key keyInt = stringKey; + //double nfloat = 28.288; + Value valueInt = Value(TransferTypeToByteArray(28.288)); + + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端修改数据 + + char strKV[MAX_DATA_LENGTH] = {"math_score_double:999.999"}; + // memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + //strcpy_s(strKV, strlen("math_score_double:999.999")+1, "math_score_double:999.999"); + + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "put double get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PULL); + EXPECT_EQ(status, Status::SUCCESS); + + auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据 与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + double aaa = TransferByteArrayToType(valueRetInt.Data()); + double delta = aaa - 999.999; + std::cout<<"aaa = "<< aaa <UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + } + + + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Pull_0400 + * @tc.name: remote put data, local sync pull + * @tc.desc: sync pull, put int64_t data + * @tc.type: FUNC +*/ +HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Pull_0400, TestSize.Level1) +{ + ZLOGI("DistribitedKvDataManager_Sync_Pull_0400 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_int64_t"; + Value valueInt = Value(TransferTypeToByteArray(12345678)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端修改数据200 + //char strPut[MAX_DATA_LENGTH] = {"math_score_int:99889988"}; + char strKV[MAX_DATA_LENGTH] = {"math"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_int64_t:99889988")+1, "math_score_int64_t:99889988"); + + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "put-200 get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PULL); + EXPECT_EQ(status, Status::SUCCESS); + auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据 与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + int64_t aaa = TransferByteArrayToType(valueRetInt.Data()); + EXPECT_EQ( aaa, 99889988u); + + + //8.数据发送到远端并等待返回结果 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "put int64_t get result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + + } + + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Pull_0500 + * @tc.name: remote put data, local sync pull + * @tc.desc: sync pull, put size_t data + * @tc.type: FUNC +*/ +HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Pull_0500, TestSize.Level1) +{ + ZLOGI("DistribitedKvDataManager_Sync_Pull_0100 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_size_t"; + Value valueInt = Value(TransferTypeToByteArray(28)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端修改数据200 + //char strPut[MAX_DATA_LENGTH] = {"math_score_int:88"}; + char strKV[MAX_DATA_LENGTH] = {"math"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_size_t:88")+1, "math_score_size_t:88"); + + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str));//9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "size_t get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地pull远端数据 + //std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + //std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PULL); + EXPECT_EQ(status, Status::SUCCESS); + auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据 与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + size_t aaa = TransferByteArrayToType(valueRetInt.Data()); + EXPECT_EQ( aaa, 88u); + + + //8.数据发送到远端并等待返回结果 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + // char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "get size_t result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Pull_0600 + * @tc.name: remote put data, local sync pull + * @tc.desc: sync pull, put string data + * @tc.type: FUNC +*/ +HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Pull_0600, TestSize.Level1) +{ + ZLOGI("DistribitedKvDataManager_Sync_Pull_0600 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_string"; + Value valueInt = Value( "{\"class\":20, \"age\":18, \"gradle\":\"good\"}" ); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端修改数据200 + char strPut[MAX_DATA_LENGTH] = {"math_score_string:{\"class\":88, \"age\":99, \"gradle\":\"QQWWQQ\"}"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strPut); + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "put string result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PULL); + EXPECT_EQ(status, Status::SUCCESS); + auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据 与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + std::string aaa = valueRetInt.ToString(); + std::string sstring = "{\"class\":88, \"age\":99, \"gradle\":\"QQWWQQ\"}"; + EXPECT_EQ( aaa, sstring); + + + //8.数据发送到远端并等待返回结果 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strPut); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + //char code[10]={0}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "get string result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_Pull_0700 + * @tc.name: remote put data, local sync pull + * @tc.desc: sync pull, put vector data + * @tc.type: FUNC +*/ +HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_Pull_0700, TestSize.Level1) +{ + ZLOGI("DistribitedKvDataManager_Sync_Pull_0700 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3.本地数据库添加数据 vector + std::string stringKey = "math_score_vector"; + Key keyInt = stringKey; + std::vector vect = {0,1,2,3,4,5,6,7}; + Value valueInt = Value( vect ); + Status status = DisKvTest::KvStorePtr->Put(keyInt,valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端修改数据200 + + std::vector vect2 = {9,9,8,8,7,7,6,6}; + std::string strvc; + strvc.assign(vect2.begin(), vect2.end()); + + char strKV[MAX_DATA_LENGTH] = {"math_score_int"}; + strcpy_s(strKV, strlen(stringKey.c_str())+1, stringKey.c_str()); + strcat(strKV, ":"); + strcat(strKV, strvc.c_str()); + + std::cout<< "strvc = "<< strvc < get result=" << str<< std::endl; + + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PULL); + EXPECT_EQ(status, Status::SUCCESS); + auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据 与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + std::string strValueRet = valueRetInt.ToString(); + EXPECT_EQ( strvc, strValueRet.c_str()); + + + //8.数据发送到远端并等待返回结果 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "put vector get result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + + } + +/** + * @tc.number: DistribitedKvDataManager_Sync_PUSH_PULL_0100 + * @tc.name: remote delete data, local sync push_pull + * @tc.desc: sync push_pull ,delete int data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_PUSH_PULL_0100, TestSize.Level1) + { + ZLOGI("DistribitedKvDataManager_Sync_PUSH_PULL_0100 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_int"; + Value valueInt = Value(TransferTypeToByteArray(100)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端delete 数据 + char strKV[MAX_DATA_LENGTH] = {"math_score_int:100"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_DELETE_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " delete int result=" << str<< std::endl; + + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地push_pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH_PULL); + EXPECT_EQ(status, Status::SUCCESS); + auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据失败 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::KEY_NOT_FOUND); + + //8.远端get数据失败 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " get int result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"1"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + + } + + + +/** + * @tc.number: DistribitedKvDataManager_Sync_PUSH_PULL_0200 + * @tc.name: remote delete data, local sync push_pull + * @tc.desc: sync push_pull ,delete float data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_PUSH_PULL_0200, TestSize.Level1) + { + + ZLOGI("DistribitedKvDataManager_Sync_PUSH_PULL_0200 begin."); + + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_int"; + Value valueInt = Value(TransferTypeToByteArray(3.14f)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端delete 数据 + char strKV[MAX_DATA_LENGTH] = {"math_score_int:100"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_DELETE_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " delete float get result=" << str<< std::endl; + + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地push_pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH_PULL); + EXPECT_EQ(status, Status::SUCCESS); + auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据失败 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::KEY_NOT_FOUND); + + + //8.远端get数据失败 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " get result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"1"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_PUSH_PULL_0300 + * @tc.name: remote delete data, local sync push_pull + * @tc.desc: sync push_pull ,delete double data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_PUSH_PULL_0300, TestSize.Level1) + { + + ZLOGI("DistribitedKvDataManager_Sync_PUSH_PULL_0300 begin."); + + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_int"; + Value valueInt = Value(TransferTypeToByteArray(28.288f)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端delete 数据 + char strKV[MAX_DATA_LENGTH] = {"math_score_int:100"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_DELETE_DATA, strKV); + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "delete double result=" << str<< std::endl; + + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地push_pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH_PULL); + EXPECT_EQ(status, Status::SUCCESS); +auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据失败 + usleep(USLEEP_TIME); + + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::KEY_NOT_FOUND); + + + //8.远端get数据失败 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "get result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"1"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_PUSH_PULL_0400 + * @tc.name: remote delete data, local sync push_pull + * @tc.desc: sync push_pull ,delete int64_t data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_PUSH_PULL_0400, TestSize.Level1) + { + + ZLOGI("DistribitedKvDataManager_Sync_PUSH_PULL_0400 begin."); + + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_int"; + Value valueInt = Value(TransferTypeToByteArray(12345678)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端delete 数据 + char strKV[MAX_DATA_LENGTH] = {"math_score_int:100"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_DELETE_DATA, strKV); + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " delete int64_t result=" << str<< std::endl; + + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地push_pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH_PULL); + EXPECT_EQ(status, Status::SUCCESS); + auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据失败 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::KEY_NOT_FOUND); + + //8.远端get数据失败 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "get result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"1"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_PUSH_PULL_0500 + * @tc.name: remote delete data, local sync push_pull + * @tc.desc: sync push_pull ,delete size_t data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_PUSH_PULL_0500, TestSize.Level1) + { + + ZLOGI("DistribitedKvDataManager_Sync_PUSH_PULL_0500 begin."); + + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_int"; + Value valueInt = Value(TransferTypeToByteArray(28)); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端delete 数据 + char strKV[MAX_DATA_LENGTH] = {"math_score_int:100"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_DELETE_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " delete size_t result=" << str<< std::endl; + + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + + //6.本地push_pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH_PULL); + EXPECT_EQ(status, Status::SUCCESS); +auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据失败 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::KEY_NOT_FOUND); + + //8.远端get数据失败 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " get result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"1"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_PUSH_PULL_0600 + * @tc.name: remote delete data, local sync push_pull + * @tc.desc: sync push_pull ,delete string data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_PUSH_PULL_0600, TestSize.Level1) + { + + ZLOGI("DistribitedKvDataManager_Sync_PUSH_PULL_0600 begin."); + + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + + //2.创建远端Lv + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_int"; + Value valueInt = Value( "{\"class\":20, \"age\":18, \"gradle\":\"good\"}" ); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端delete 数据 + char strKV[MAX_DATA_LENGTH] = {"math_score_int:100"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_DELETE_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={0}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "delete string result=" << str<< std::endl; + + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + + //6.本地push_pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH_PULL); + EXPECT_EQ(status, Status::SUCCESS); +auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据失败 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::KEY_NOT_FOUND); + + //8.远端get数据失败 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " get result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"1"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + } + + +/** + * @tc.number: DistribitedKvDataManager_Sync_PUSH_PULL_0700 + * @tc.name: remote delete data, local sync push_pull + * @tc.desc: sync push_pull ,delete vector data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, DistribitedKvDataManager_Sync_PUSH_PULL_0700, TestSize.Level1) + { + + ZLOGI("DistribitedKvDataManager_Sync_PUSH_PULL_0700 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //2.创建远端Lv + //3. 本地数据库添加数据 100 + Key keyInt = "math_score_int"; + std::vector vect = {0,1,2,3,4,5,6,7}; + Value valueInt = Value( vect ); + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + EXPECT_EQ(Status::SUCCESS, status); + + //4. sync push 同步数据到远端 + std::vector remoteDevice; + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH); + ASSERT_EQ(status, Status::SUCCESS); + + //5.远端delete 数据 + char strKV[MAX_DATA_LENGTH] = {"math_score_int:100"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_DELETE_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " delete vector result=" << str<< std::endl; + + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //6.本地push_pull远端数据 + // std::vector remoteDevice; + remoteDevice.clear(); + status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + // std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync" << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + status = DisKvTest::KvStorePtr->Sync(deviceList, SyncMode::PUSH_PULL); + EXPECT_EQ(status, Status::SUCCESS); +auto syncCallback = std::make_shared(); + status = DisKvTest::KvStorePtr->RegisterSyncCallback(syncCallback); + EXPECT_EQ(status, Status::SUCCESS) << "register sync callback failed."; + if(status == Status::SUCCESS) + { + std::cout<<"LOGdisDataTest--SUCCESS: register sync callback"<< std::endl; + } + else + { + std::cout<<"LOGdisDataTest--FAIL: register sync callback"<< static_cast(status) << std::endl; + } + + //7.本地get数据失败 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::KEY_NOT_FOUND); + + //8.远端get数据失败 + writeCodeDataToShm(CTRL_CODE_DATAMGR_GET_DATA, strKV); + + //char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << " get result=" << str<< std::endl; + + //检查远端是否返回成功 + ret = strcmp(str,"1"); + EXPECT_EQ(ret, 0); + + //解注册回调 + status = DisKvTest::KvStorePtr->UnRegisterSyncCallback(); + EXPECT_EQ(status, Status::SUCCESS) << "un register sync callback failed."; + + } + + + + + +/** + * @tc.number: SubscribeWithQuery_0100 + * @tc.name: SubscribeWithQuery + * @tc.desc: SubscribeWithQuery, int data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SubscribeWithQuery_0100, TestSize.Level1) + { + + ZLOGI("SubscribeWithQuery_0100 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + //char strPut[MAX_DATA_LENGTH] = {"math_score_int:200"}; + char strKV[MAX_DATA_LENGTH] = {"math_score_int"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_int:200")+1, "math_score_int:200"); + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "yput-200 get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //4. 本段sync远端数据 SubscribeWithQuery + std::vector remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SubscribeWithQuery"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + Key keyInt = "math_score_int"; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + int aaa = TransferByteArrayToType(valueRetInt.Data()); + std::cout<< "LOGdisDataTest--本地get数据与200比较: aaa = " <UnsubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(unSubscribeStatus, Status::SUCCESS) <<"LOGdisDataTest--ERR:SubscribeWithQuery"; + + } + + + + +/** + * @tc.number: SubscribeWithQuery_0200 + * @tc.name: SubscribeWithQuery + * @tc.desc: SubscribeWithQuery, float data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SubscribeWithQuery_0200, TestSize.Level1) + { + + ZLOGI("SubscribeWithQuery_0200 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + char strKV[MAX_DATA_LENGTH] = {"math_score_float:9.99"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "yput-200 get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //4. 本段sync远端数据 SubscribeWithQuery + std::vector remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SubscribeWithQuery"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + std::string stringKey = "math_score_float"; + Key keyInt = stringKey; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS) << "LOGdisDataTest--ERR:Get(keyInt, valueRetInt)"; + float aaa = TransferByteArrayToType(valueRetInt.Data()); + float delta = aaa - 9.99f; + EXPECT_LE( std::abs(delta), 0.00001); + + //////取消订阅 + auto unSubscribeStatus = DisKvTest::KvStorePtr->UnsubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(unSubscribeStatus, Status::SUCCESS) <<"LOGdisDataTest--ERR:SubscribeWithQuery"; + + } + + +/** + * @tc.number: SubscribeWithQuery_0300 + * @tc.name: SubscribeWithQuery + * @tc.desc: SubscribeWithQuery, double data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SubscribeWithQuery_0300, TestSize.Level1) + { + + ZLOGI("SubscribeWithQuery_0300 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + char strKV[MAX_DATA_LENGTH] = {"math_score_double:999.999"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "yput-200 get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //4. 本段sync远端数据 SubscribeWithQuery + std::vector remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SubscribeWithQuery"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + std::string stringKey = "math_score_double"; + Key keyInt = stringKey; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + double aaa = TransferByteArrayToType(valueRetInt.Data()); + double delta = aaa - 999.999; + std::cout<<"aaa = "<< aaa < remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SubscribeWithQuery"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Key keyInt = "math_score_int64_t"; + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + int64_t aaa = TransferByteArrayToType(valueRetInt.Data()); + EXPECT_EQ( aaa, 99889988u); + + + //////取消订阅 + auto unSubscribeStatus = DisKvTest::KvStorePtr->UnsubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(unSubscribeStatus, Status::SUCCESS) <<"LOGdisDataTest--ERR:SubscribeWithQuery"; + + } + + + +/** + * @tc.number: SubscribeWithQuery_0500 + * @tc.name: SubscribeWithQuery + * @tc.desc: SubscribeWithQuery, size_t data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SubscribeWithQuery_0500, TestSize.Level1) + { + + ZLOGI("SubscribeWithQuery_0500 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + char strKV[MAX_DATA_LENGTH] = {"math_score_size_t:88"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "yput-200 get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //4. 本段sync远端数据 SubscribeWithQuery + std::vector remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SubscribeWithQuery"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Key keyInt = "math_score_size_t"; + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + size_t aaa = TransferByteArrayToType(valueRetInt.Data()); + EXPECT_EQ( aaa, 88u); + + + //////取消订阅 + auto unSubscribeStatus = DisKvTest::KvStorePtr->UnsubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(unSubscribeStatus, Status::SUCCESS) <<"LOGdisDataTest--ERR:SubscribeWithQuery"; + + } + + +/** + * @tc.number: SubscribeWithQuery_0600 + * @tc.name: SubscribeWithQuery + * @tc.desc: SubscribeWithQuery, string data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SubscribeWithQuery_0600, TestSize.Level1) + { + + ZLOGI("SubscribeWithQuery_0600 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + char strKV[MAX_DATA_LENGTH] = {"math_score_string:{\"class\":88, \"age\":99, \"gradle\":\"QQWWQQ\"}"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "yput-200 get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //4. 本段sync远端数据 SubscribeWithQuery + std::vector remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SubscribeWithQuery"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Key keyInt = "math_score_string"; + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + std::string aaa = valueRetInt.ToString(); + std::string sstring = "{\"class\":88, \"age\":99, \"gradle\":\"QQWWQQ\"}"; + EXPECT_EQ( aaa, sstring); + + + //////取消订阅 + auto unSubscribeStatus = DisKvTest::KvStorePtr->UnsubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(unSubscribeStatus, Status::SUCCESS) <<"LOGdisDataTest--ERR:SubscribeWithQuery"; + + } + + +/** + * @tc.number: SubscribeWithQuery_0700 + * @tc.name: SubscribeWithQuery + * @tc.desc: SubscribeWithQuery, vector data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SubscribeWithQuery_0700, TestSize.Level1) + { + ZLOGI("SubscribeWithQuery_0700 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + + std::string stringKey = "math_score_vector"; + Key keyInt = stringKey; + + std::vector vect2 = {9,9,8,8,7,7,6,6}; + std::string strvc; + strvc.assign(vect2.begin(), vect2.end()); + + char strKV[MAX_DATA_LENGTH] = {"math_score_int"}; + strcpy_s(strKV, strlen(stringKey.c_str())+1, stringKey.c_str()); + strcat(strKV, ":"); + strcat(strKV, strvc.c_str()); + + std::cout<< "strvc = "<< strvc < get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //4. 本段sync远端数据 SubscribeWithQuery + std::vector remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SubscribeWithQuery"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + std::string strValueRet = valueRetInt.ToString(); + EXPECT_EQ( strvc, strValueRet.c_str()); + + //////取消订阅 + auto unSubscribeStatus = DisKvTest::KvStorePtr->UnsubscribeWithQuery(deviceList, dataQuery); + EXPECT_EQ(unSubscribeStatus, Status::SUCCESS) <<"LOGdisDataTest--ERR:SubscribeWithQuery"; + } + + +/** + * @tc.number: SyncWithCondition_0100 + * @tc.name: SyncWithCondition + * @tc.desc: SyncWithCondition, int data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SyncWithCondition_0100, TestSize.Level1) + { + + ZLOGI("SyncWithCondition_0100 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + //char strPut[MAX_DATA_LENGTH] = {"math_score_int:200"}; + char strKV[MAX_DATA_LENGTH] = {"math_score_int"}; + memset_s(strKV,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + strcpy_s(strKV, strlen("math_score_int:200")+1, "math_score_int:200"); + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "yput-200 get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //4. 本段sync远端数据 SyncWithCondition + std::vector remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SyncWithCondition(deviceList, SyncMode::PULL, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SyncWithCondition"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + Key keyInt = "math_score_int"; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + int aaa = TransferByteArrayToType(valueRetInt.Data()); + std::cout<< "LOGdisDataTest--本地get数据与200比较: aaa = " < remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SyncWithCondition(deviceList, SyncMode::PULL, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SyncWithCondition"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + std::string stringKey = "math_score_float"; + Key keyInt = stringKey; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS) << "LOGdisDataTest--ERR:Get(keyInt, valueRetInt)"; + float aaa = TransferByteArrayToType(valueRetInt.Data()); + float delta = aaa - 9.99f; + EXPECT_LE( std::abs(delta), 0.00001); + } + + +/** + * @tc.number: SyncWithCondition_0300 + * @tc.name: SyncWithCondition + * @tc.desc: SyncWithCondition, double data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SyncWithCondition_0300, TestSize.Level1) + { + + ZLOGI("SyncWithCondition_0300 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + char strKV[MAX_DATA_LENGTH] = {"math_score_double:999.999"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "yput-200 get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //4. 本段sync远端数据 SyncWithCondition + std::vector remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SyncWithCondition(deviceList, SyncMode::PULL, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SyncWithCondition"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + std::string stringKey = "math_score_double"; + Key keyInt = stringKey; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + double aaa = TransferByteArrayToType(valueRetInt.Data()); + double delta = aaa - 999.999; + std::cout<<"aaa = "<< aaa < remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SyncWithCondition(deviceList, SyncMode::PULL, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SyncWithCondition"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Key keyInt = "math_score_int64_t"; + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + int64_t aaa = TransferByteArrayToType(valueRetInt.Data()); + EXPECT_EQ( aaa, 99889988u); + + } + + + +/** + * @tc.number: SyncWithCondition_0500 + * @tc.name: SyncWithCondition + * @tc.desc: SyncWithCondition, size_t data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SyncWithCondition_0500, TestSize.Level1) + { + + ZLOGI("SyncWithCondition_0500 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + char strKV[MAX_DATA_LENGTH] = {"math_score_size_t:88"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "yput-200 get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //4. 本段sync远端数据 SyncWithCondition + std::vector remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SyncWithCondition(deviceList, SyncMode::PULL, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SyncWithCondition"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Key keyInt = "math_score_size_t"; + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + size_t aaa = TransferByteArrayToType(valueRetInt.Data()); + EXPECT_EQ( aaa, 88u); + + } + + +/** + * @tc.number: SyncWithCondition_0600 + * @tc.name: SyncWithCondition + * @tc.desc: SyncWithCondition, string data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SyncWithCondition_0600, TestSize.Level1) + { + + ZLOGI("SyncWithCondition_0600 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + char strKV[MAX_DATA_LENGTH] = {"math_score_string:{\"class\":88, \"age\":99, \"gradle\":\"QQWWQQ\"}"}; + writeCodeDataToShm(CTRL_CODE_DATAMGR_PUT_DATA, strKV); + + char str[MAX_DATA_LENGTH] = {0}; + memset_s(str,MAX_DATA_LENGTH,0,MAX_DATA_LENGTH); + char code[10]={"9999"}; + memset_s(code,10,0,10); + strcpy_s(code, strlen(Int2String(CTRL_CODE_RESULT_TYPE, str))+1, Int2String(CTRL_CODE_RESULT_TYPE, str)); //9999 + memset_s(str,MAX_DATA_LENGTH, 0,MAX_DATA_LENGTH); + waitDataWithCode(code, str); + std::cout << "yput-200 get result=" << str<< std::endl; + //检查远端是否返回成功 + int ret = strcmp(str,"0"); + EXPECT_EQ(ret, 0); + + //4. 本段sync远端数据 SyncWithCondition + std::vector remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SyncWithCondition(deviceList, SyncMode::PULL, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SyncWithCondition"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Key keyInt = "math_score_string"; + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + std::string aaa = valueRetInt.ToString(); + std::string sstring = "{\"class\":88, \"age\":99, \"gradle\":\"QQWWQQ\"}"; + EXPECT_EQ( aaa, sstring); + + } + + +/** + * @tc.number: SyncWithCondition_0700 + * @tc.name: SyncWithCondition + * @tc.desc: SyncWithCondition, vector data + * @tc.type: FUNC +*/ + HWTEST_F(DistributedKvDataManagerTest, SyncWithCondition_0700, TestSize.Level1) + { + ZLOGI("SyncWithCondition_0700 begin."); + EXPECT_EQ(Status::SUCCESS, DisKvTest::statusGetKvStore) << "statusGetKvStore return wrong status"; + EXPECT_NE(nullptr, DisKvTest::KvStorePtr) << "KvStorePtr is nullptr"; + //1.本端创建Lv + //2.远端创建kv + + //3. 远端put int + std::string stringKey = "math_score_vector"; + Key keyInt = stringKey; + + std::vector vect2 = {9,9,8,8,7,7,6,6}; + std::string strvc; + strvc.assign(vect2.begin(), vect2.end()); + + char strKV[MAX_DATA_LENGTH] = {"math_score_int"}; + strcpy_s(strKV, strlen(stringKey.c_str())+1, stringKey.c_str()); + strcat(strKV, ":"); + strcat(strKV, strvc.c_str()); + + std::cout<< "strvc = "<< strvc < remoteDevice; + remoteDevice.clear(); + Status status = DisKvTest::manager.GetDeviceList(remoteDevice, DeviceFilterStrategy::NO_FILTER); + EXPECT_EQ(status, Status::SUCCESS); + + std::vector deviceList; + deviceList.clear(); + for (const auto& device : remoteDevice) { + std::cout << "start sync deviceId = " << device.deviceId << std::endl; + deviceList.push_back(device.deviceId); + } + + DataQuery dataQuery; + dataQuery.KeyPrefix("math"); + auto syncStatus = DisKvTest::KvStorePtr->SyncWithCondition(deviceList, SyncMode::PULL, dataQuery); + EXPECT_EQ(syncStatus, Status::SUCCESS) << "LOGdisDataTest--SUCCESS:SyncWithCondition"; + + + //5.本地get数据与200比较 + usleep(USLEEP_TIME); + + Value valueRetInt; + status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + EXPECT_EQ(status, Status::SUCCESS); + std::string strValueRet = valueRetInt.ToString(); + EXPECT_EQ( strvc, strValueRet.c_str()); + } + + + + + diff --git a/distributeddatamgr/process_common/process_msg.cpp b/distributeddatamgr/process_common/process_msg.cpp new file mode 100644 index 0000000..2cb1ba8 --- /dev/null +++ b/distributeddatamgr/process_common/process_msg.cpp @@ -0,0 +1,506 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "process_msg.h" +#include +#include +#include +#include +#include "types.h" +#include "distributed_kv_data_manager.h" +#include "string.h" +#include // 使用stringstream +#include +#include +#include +using namespace OHOS::DistributedKv; +using namespace std; + +class DisKvTest { +public: + static DistributedKvDataManager manager; + static std::shared_ptr KvStorePtr; // declare kvstore instance. + static Status statusGetKvStore; + static Status statusCloseKvStore; + static Status statusDeleteKvStore; + static Options create; + static UserId userId; + static AppId appId; + static StoreId storeIdTest; +}; + +DistributedKvDataManager DisKvTest::manager; +std::shared_ptr DisKvTest::KvStorePtr = nullptr; // declare kvstore instance. +Status DisKvTest::statusGetKvStore = Status::ERROR; +Status DisKvTest::statusCloseKvStore = Status::ERROR; +Status DisKvTest::statusDeleteKvStore = Status::ERROR; +UserId DisKvTest::userId; +AppId DisKvTest::appId; +StoreId DisKvTest::storeIdTest; + +void initKvstoreId() +{ + DisKvTest::userId.userId = "account0"; + DisKvTest::appId.appId = "com.ohos.kvdatamanager3.test"; + DisKvTest::storeIdTest.storeId = "test3"; + + //1.创建数据库 + Options options { + .createIfMissing = true, + .encrypt = false, // .persistant = true, + .autoSync = false, + .backup = false, + .kvStoreType = KvStoreType::SINGLE_VERSION + }; + + DisKvTest::statusGetKvStore = DisKvTest::manager.GetSingleKvStore(options, { DisKvTest::appId }, { DisKvTest::storeIdTest }, DisKvTest::KvStorePtr); + + +} + +char* getRealData(char* str, char* delims) +{ + char *result = strtok( str, delims ); + char *second = NULL; + // result = strtok( str, delims ); + while( result != NULL ) { + //printf( "result is \"%s\"\n", result ); + second = result; + result = strtok( NULL,delims ); + } + return second; +} + +void getParam(char* putData, char ret[] [MAX_DATA_LENGTH]) +{ + char str[1024] = {":"}; + memset_s(str,1024,0,1024); + strcpy_s(str, strlen(putData)+1, putData); + char delims[2] = {":"}; + //memset_s(delims,0,2); + //strcpy_s(delims,strlen()+1, ":"); + + char *result = strtok( str, delims ); + //char *second = NULL; + // result = strtok( str, delims ); + int i = 0; + while( result != NULL ) { + printf( "result is \"%s\"\n", result); + strcpy_s(ret[i], strlen(result)+1, result); + //second = result; + result = strtok( NULL, delims ); + i++; + // printf("i= %d", i); + if(i==3) + { + return; + } + + } + return; +} + +int str2int(char *str, int base) { + char sign; + int rv = 0; + int newbase = -1; + + if ((base < 2 && base !=0) || base > 36) + return 0; + + //跳过str开头的空白字符 + while(*str && isspace(*str)) + str++; + + //取得符号位 + sign = (*str == '-' || *str == '+') ? *str++ : '+'; + + //猜测需要转换的类型 + if (*str == '0' && ++str) { + if ((*str == 'x' || *str == 'X') && ++str) { + newbase = 16; + } else if ((*str == 'b' || *str == 'B') &&++str) { + newbase = 2; + } else { + newbase = 8; + } + } + + //默认10进制 + if (base == 0) { + base = newbase == -1 ? 10 : newbase; + } + + for (;*str; str++) { + //大写转小写 + char c = *str | 0x20; + if (c >= 'a' && c<= 'z') { + rv = rv * base + c - 'a' + 10; + } else if (c >= '0' && c <= '9') { + rv = rv * base + c - '0'; + } else { + break; + } + //printf("rv = %d\n", rv); + } + + return sign == '-' ? -rv : rv; +} + +int ProcessSoftBus(int code, char* recvData) +{ + return RESULT_OK; +} + +int ProcessDataMgr(int code, char* recvData) +{ + LOG("ProcessDataMgr, begin"); + + initKvstoreId(); + std::cout << "create status=" << static_cast(DisKvTest::statusGetKvStore) << std::endl; + if ( Status::SUCCESS != DisKvTest::statusGetKvStore) + { + std::cout << "ERR:statusGetKvStore"<< std::endl; + return RESULT_ERR; + } + + switch(code) + { + case CTRL_CODE_DATAMGR_GET_DATA: + { + return processGetData(recvData); + } + break; + case CTRL_CODE_DATAMGR_GET_DATA_REPLY: + { + return RESULT_OK; + } + break; + case CTRL_CODE_DATAMGR_CREATE_KV: + { + return processCreateKv(recvData); + } + break; + case CTRL_CODE_DATAMGR_DELETE_KV: + { + return processDeleteKv(recvData); + } + break; + case CTRL_CODE_DATAMGR_PUT_DATA: + { + return processPutData(recvData); + } + case CTRL_CODE_DATAMGR_DELETE_DATA: + { + return processDeletetData(recvData); + } + break; + default: + break; + } + return -1; +} + + +int processDeletetData(char* putData) +{ + LOG("LOGdisDataTest---processDeletetData, begin"); + //解析远端发来的数据 result[0]=code result[1]=key result[2]=value + char result[5][MAX_DATA_LENGTH]; + memset_s(result,5*MAX_DATA_LENGTH,0,5*MAX_DATA_LENGTH); + getParam(putData, result); + for(int i=0;i<3;i++) + { + LOG("LOGdisDataTest---processGetData %s", result[i]); + } + + + //put data删除数据 + Value valueInt; + Key keyInt=result[1]; + Status status = DisKvTest::KvStorePtr->Delete(keyInt); + if (Status::SUCCESS == status ) + { + LOG("LOGdisDataTest---Delete:RESULT_OK"); + return RESULT_OK; + } + LOG("LOGdisDataTest---Delete:RESULT_ERR"); + return RESULT_ERR; +} + + +int processPutData(char* putData) +{ + LOG("LOGdisDataTest-processPutData, begin"); + //解析远端发来的数据 result[0]=code result[1]=key result[2]=value + char result[3][MAX_DATA_LENGTH]; + memset_s(result,3*MAX_DATA_LENGTH,0,3*MAX_DATA_LENGTH); + getParam(putData, result); + for(int i=0;i<3;i++) + { + LOG("LOGdisDataTest-processPutData %s", result[i]); + } + + + //put data修改数据 + Value valueInt; + Key keyInt=result[1]; + if(strcmp(result[1], "math_score_int") == 0) + { + int numInt = atoi(result[2]); + valueInt = Value(TransferTypeToByteArray(numInt)); + } + else if(strcmp(result[1], "math_score_float") == 0) + { + float f2 = atof(result[2]); + valueInt = Value(TransferTypeToByteArray(f2)); + } + else if(strcmp(result[1], "math_score_double") == 0) + { + double f2 = atof(result[2]); + valueInt = Value(TransferTypeToByteArray(f2)); + } + else if(strcmp(result[1], "math_score_int64_t") == 0) + { + int64_t numInt = atoi(result[2]); + valueInt = Value(TransferTypeToByteArray(numInt)); + } + else if(strcmp(result[1], "math_score_size_t") == 0) + { + size_t numInt = atoi(result[2]); + valueInt = Value(TransferTypeToByteArray(numInt)); + } + + else if(strcmp(result[1], "math_score_string") == 0) + { + + // result[2]解析的不完整 + string ss ; + string ss2 ; + //strcpy_s(ss,str); + ss=putData; + ss2 = ss.substr(23); + cout<<"LOGdisDataTest2--ss = "<< ss< vect; + vect.assign(str.begin(), str.end()); + valueInt = Value(vect); + } + Status status = DisKvTest::KvStorePtr->Put(keyInt, valueInt); + if (Status::SUCCESS == status ) + { + LOG("LOGdisDataTest2--putData:RESULT_OK"); + return RESULT_OK; + } + LOG("LOGdisDataTest2--putData:RESULT_ERR"); + return RESULT_ERR; +} + +int processGetData(char* putData) +{ + LOG("LOGdisDataTest-processGetData, begin"); + + //解析远端发来的数据 result[0]=code result[1]=key result[2]=value + char result[3][MAX_DATA_LENGTH]; + memset_s(result,3*MAX_DATA_LENGTH,0,3*MAX_DATA_LENGTH); + getParam(putData, result); + // LOG("LOGdisDataTest--putData= %s", putData); + for(int i=0;i<3;i++) + { + LOG("for result[i] %s", result[i]); + } + + + //获取到本端数据 + Value valueRetInt; + Key keyInt=result[1]; + Status status = DisKvTest::KvStorePtr->Get(keyInt, valueRetInt); + if (Status::SUCCESS != status ) + { + LOG("LOGdisDataTest--ERROR: Get(keyInt, valueRetInt)"); + return RESULT_ERR; + } + + std::string ret = valueRetInt.ToString(); + LOG("LOGdisDataTest--result[2]= %s", result[2]); + LOG("LOGdisDataTest--ret.c_str() = %s", ret.c_str()); + + if(strcmp(result[1], "math_score_int") == 0) + { + LOG("LOGdisDataTest--math_score_int "); + int aaa = TransferByteArrayToType(valueRetInt.Data()); + int i2 = atoi(result[2]); + LOG("LOGdisDataTest--aaa= %d", aaa); + LOG("LOGdisDataTest--i2 = %d", i2); + if ( aaa == i2) + { + return RESULT_OK; + } + + } + else if(strcmp(result[1], "math_score_float") == 0) + { + LOG("LOGdisDataTest--math_score_float "); + float aaa = TransferByteArrayToType(valueRetInt.Data()); + + // float fret = atof(ret.c_str()); + float f2 = atof(result[2]); + float delta = f2 - aaa; + LOG("LOGdisDataTest--aaa= %f", aaa); + LOG("LOGdisDataTest--f2 = %f", f2); + LOG("LOGdisDataTest--delta = %f", delta); + if ( std::abs(delta) <= 0.00001) + { + return RESULT_OK; + } + } + else if(strcmp(result[1], "math_score_double") == 0) + { + LOG("LOGdisDataTest--math_score_double "); + double aaa = TransferByteArrayToType(valueRetInt.Data()); + double d2 = atof(result[2]); + double delta = d2 - aaa; + LOG("LOGdisDataTest--aaa= %f", aaa); + LOG("LOGdisDataTest--d2 = %f", d2); + LOG("LOGdisDataTest--delta = %f", delta); + if ( std::abs(delta) <= 0.00001) + { + return RESULT_OK; + } + } + else if(strcmp(result[1], "math_score_int64_t") == 0) + { + LOG("LOGdisDataTest--math_score_int64_t "); + int64_t aaa = TransferByteArrayToType(valueRetInt.Data()); + int64_t i2 = atoi(result[2]); + //LOG("LOGdisDataTest--aaa= %ld", aaa); + // LOG("LOGdisDataTest--i2 = %ld", i2); + if ( aaa == i2) + { + return RESULT_OK; + } + } + else if(strcmp(result[1], "math_score_size_t") == 0) + { + LOG("LOGdisDataTest--math_score_size_t "); + size_t aaa = TransferByteArrayToType(valueRetInt.Data()); + size_t i2 = atoi(result[2]); + // LOG("LOGdisDataTest--aaa= %lu", aaa); + // LOG("LOGdisDataTest--i2 = %lu", i2); + if ( aaa == i2) + { + return RESULT_OK; + } + } + else if(strcmp(result[1], "math_score_string") == 0) + { + // result[2]解析的不完整 + string ss ; + string ss2 ; + //strcpy_s(ss,str); + ss=putData; + ss2 = ss.substr(23); + cout<<"LOGdisDataTest--ss = "<< ss< +#include +#include +#include +#include +#include +#include +#include +#include "distributed_kv_data_manager.h" +#include + +#define MAX_DATA_LENGTH 1024 +#define RESULT_OK 0 +#define RESULT_ERR 1 + +typedef enum{ + CTRL_CODE_SOFTBUS_TYPE = 1000, + CTRL_CODE_SOFTBUS_OPEN_SESSION, + CTRL_CODE_DATAMGR_TYPE = 2000, + CTRL_CODE_DATAMGR_GET_DATA, + CTRL_CODE_DATAMGR_GET_DATA_REPLY, + CTRL_CODE_DATAMGR_CREATE_KV, + CTRL_CODE_DATAMGR_DELETE_KV, + CTRL_CODE_DATAMGR_PUT_DATA, + CTRL_CODE_DATAMGR_DELETE_DATA, + CTRL_CODE_DM_TYPE = 3000, + CTRL_CODE_FILEMGR_TYPE = 4000, + CTRL_CODE_DP_TYPE = 5000, + CTRL_CODE_SEC_TYPE = 6000, + CTRL_CODE_MEDIA_TYPE = 7000, + CTRL_CODE_RESULT_TYPE = 9999, +} CtrlCodeType; + + +#define LOG(format, ...) \ +do \ +{ \ + time_t timeSec; \ + time(&timeSec); \ + struct tm tmRst; \ + localtime_r(&timeSec, &tmRst); \ + char strTime[10]; \ + strftime(strTime, sizeof(strTime), "%H:%M:%S", &tmRst); \ + fprintf(stdout, "[process-dataMsg] %s " format "\n", strTime, ##__VA_ARGS__); \ +} while(0) + + +int ProcessSoftBus(int code, char* recvData); +int ProcessDataMgr(int code, char* recvData); +int ProcessDP(int code, char* recvData); +int ProcessDM(int code, char* recvData); +int ProcessFileMgr(int code, char* recvData); +int ProcessSecMgr(int code, char* recvData); +int ProcessMediaMgr(int code, char* recvData); + +int processDeletetData(char* putData); +int processGetData(char* putData); +int processPutData(char* putData); +int processCreateKv(char* putData); +int processDeleteKv(char* putData); +char* getRealData(char* str, char* delims); +int str2int(char *str, int base); +void getParam(char* str, char ret[] [MAX_DATA_LENGTH]); +void initKvstoreId(); +#endif + + diff --git a/runtest.sh b/runtest.sh new file mode 100755 index 0000000..15ca1d0 --- /dev/null +++ b/runtest.sh @@ -0,0 +1,187 @@ +#!/bin/bash + +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e + +usage() +{ + echo + echo "USAGE" + echo " ./runtest.sh device=IP:PORT serial=[SERIAL_NUMBER] [module=MODULE_NAME] [test=TEST] [runonly=RUN_ONLY]" + echo " First, you must be execute stf/start.bat in the windows which connected a device." + echo " target_platform : TARGET_PLATFORM the target platform, such as phone or ivi; Default to phone" + echo " device : IP:PORT the host ip:port of the connected device." + echo " module : MODULE_NAME the module name to run. Run all modules by default." + echo " test : TEST_NAME the test name to run. Run all tests by default. This should be a FULL-QUALIFIED className or methodName." + echo " runonly : RUN_ONLY TRUE=not build, only runtest; FALSE=build and runtest. FALSE by default." + echo + exit 1 +} + +parse_cmdline() +{ + echo parse_cmdline:$@ + SCRIPT_DIR=$(cd $(dirname $0); pwd) + BUILD_SCRIPT=${SCRIPT_DIR}/build.sh + BASE_HOME=${SCRIPT_DIR}/../../.. + OUT_DIR=${SCRIPT_DIR}/../../../out + TARGET_ARCH=arm64 + TARGET_PLATFORM=all + BUILD_VARIANT=release + MODULE_NAME="" + DEVICE_SN="" + TEST_NAME="" + RUN_ONLY=FALSE + BUILD_PARAM="" + HATS_HOME=$SCRIPT_DIR/../../../out/$BUILD_VARIANT/suites/dcts + MODULE_INFO_FILE=$SCRIPT_DIR/../../../out/$BUILD_VARIANT/suites/dcts/testcases/module_info.list + export PATH=${BASE_HOME}/prebuilts/python/linux-x86/3.8.5/bin:$PATH + BUILD_PARAM="$BUILD_PARAM target_arch=$TARGET_ARCH variant=$BUILD_VARIANT" + + while [ -n "$1" ] + do + var="$1" + OPTIONS=$(echo ${var%%=*}) + PARAM=$(echo ${var#*=}) + case "$OPTIONS" in + target_platform) TARGET_PLATFORM="$PARAM" + BUILD_PARAM="$BUILD_PARAM target_platform=$TARGET_PLATFORM" + ;; + module) MODULE_NAME="$PARAM" + BUILD_PARAM="$BUILD_PARAM suite=$MODULE_NAME" + ;; + test) TEST_NAME="$PARAM" + ;; + device) DEVICE_SN="$DEVICE_SN;$PARAM" + ;; + runonly) RUN_ONLY=$(echo $PARAM |tr [a-z] [A-Z]) + ;; + *) usage + break;; + esac + shift + done + if [ "$DEVICE_SN" == "" ];then + usage + fi +} + +do_make() +{ + cd $BASE_HOME + echo "================================" + echo "start to build" + echo "$BUILD_SCRIPT $BUILD_PARAM" + echo "================================" + + echo BUILD_PARAM= $BUILD_PARAM + $BUILD_SCRIPT $BUILD_PARAM + if [ "$?" != 0 ]; then + echo -e "\033[31mbuild error!\033[0m" + exit 1 + fi +} + +get_testmodule_name() +{ + BUILD_TARGET_NAME=$1 + # map from build_target name to actual testmodule name + test_module_name="" + OLD_IFS=$IFS + IFS=$'\n' + for line in $(cat $MODULE_INFO_FILE); do + VAR1=$(echo $line |awk -F ' ' '{print $1}') + VAR2=$(echo $line |awk -F ' ' '{print $2}') + if [ "$BUILD_TARGET_NAME" == "$VAR1" ] ; then + test_module_name=$VAR2 + break + fi + done + IFS=$OLD_IFS + echo $test_module_name +} + +run_test_with_xdevice() +{ + TC_PATH=$HATS_HOME/testcases + RES_PATH=$HATS_HOME/resource/ + REPORT_PATH=$HATS_HOME/xdevice_reports/$(date "+%Y.%m.%d-%H.%M.%S") + python -m easy_install --quiet $HATS_HOME/tools/xdevice-0.0.0.tar.gz + python -m easy_install --quiet $HATS_HOME/tools/xdevice-extension-0.0.0.tar.gz + + run_arguments="run dcts -tcpath $TC_PATH -respath $RES_PATH -rp $REPORT_PATH" + if [ ! -z "$MODULE_NAME" ]; then + run_arguments="$run_arguments -l $1" + fi + if [ ! -z "$TEST_NAME" ]; then + TEST_LIST_FILE=$HATS_HOME/include_tests + echo "$TEST_NAME" > $TEST_LIST_FILE + run_arguments="$run_arguments -ta test-file-include-filter:$TEST_LIST_FILE" + fi + if [ ! -z "$DEVICE_SN" ]; then + run_arguments="$run_arguments $(echo $DEVICE_SN | sed 's/;/-sn /')" + fi + + echo $run_arguments + cd $HATS_HOME + python -m xdevice "$run_arguments" + + return $? +} + +run_test() +{ + cd $BASE_HOME + + MAPPED_MODULE_NAME=$MODULE_NAME + # map from build_target name to actual testmodule name + if [ ! -z "$MODULE_NAME" ]; then + MAPPED_MODULE_NAME=$(get_testmodule_name $MODULE_NAME $HATS_HOME) + if [ ! -z "$MAPPED_MODULE_NAME" ]; then + echo -e "\033[32mTest module name of build_target "$MODULE_NAME is" "$MAPPED_MODULE_NAME"\033[0m" + else + echo -e "\033[31mTest module "$MODULE_NAME" dose not exist!\033[0m" + exit 1 + fi + fi + + JSON_CONFIG_FILE=$HATS_HOME/testcases/$MAPPED_MODULE_NAME.json + ret_code=0 + if [ -f "$JSON_CONFIG_FILE" ]; then + echo -e "\033[32mRun $MAPPED_MODULE_NAME with xdevice ...\033[0m" + run_test_with_xdevice $MAPPED_MODULE_NAME + ret_code=$? + elif [ -z "$MODULE_NAME" ]; then + echo -e "\033[32mRun all modules with xdevice ...\033[0m" + run_test_with_xdevice + ret_code=$? + else + echo -e "\033[31mNon-xdevice module in dcts!\033[0m" + exit 1 + fi + + if [ "$ret_code" != "0" ]; then + echo -e "\033[31mFailed to run test, ret=$ret_code\033[0m" + exit 1 + fi +} + +parse_cmdline $@ +if [ "$RUN_ONLY" == FALSE ];then + do_make +fi +mkdir -p $OUT_DIR/$BUILD_VARIANT/suites/dcts/resource/tools +run_test +exit 0 diff --git a/test_packages.gni b/test_packages.gni new file mode 100755 index 0000000..8d39c25 --- /dev/null +++ b/test_packages.gni @@ -0,0 +1,81 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//test/xts/tools/build/suite.gni") + +_all_test_packages = [ "${DCTS_ROOT}/distributeddatamgr:distributeddatatest", + #"${DCTS_ROOT}/distributedschedule:distributedschetest", + ] + +_all_test_packages_ivi = [ + # add dcts for ivi here + # "${DCTS_ROOT}/multimedia:multimedia_ivi", +] + +_all_test_packages_intellitv = [ + # add dcts for intellitv here + #"${DCTS_ROOT}/multimedia:multimedia_intellitv", +] + +_all_test_packages_wearable = [ + # add dcts for wearable here + #"${DCTS_ROOT}/multimedia:multimedia_wearable", +] + +selected_packages_wearable = [] +selected_packages_intellitv = [] +selected_packages_ivi = [] +selected_packages = [] +assert(XTS_SUITENAME != "") +if (build_xts == true && XTS_SUITENAME == "dcts") { + _select_script = "//test/xts/tools/build/test_package_select.py" + _subsystem_config_path = rebase_path( + "$root_build_dir/build_configs/subsystem_info/src_subsystem_info.json") + _select_args = [ _subsystem_config_path ] + _all_test_packages + _select_args_ivi = [ _subsystem_config_path ] + _all_test_packages_ivi + _select_args_intellitv = + [ _subsystem_config_path ] + _all_test_packages_intellitv + _select_args_wearable = + [ _subsystem_config_path ] + _all_test_packages_wearable + _target_subsystem = getenv("target_subsystem") + + if (_target_subsystem != "" && _target_subsystem != "all") { + print("Build dcts, target_subsystem=" + _target_subsystem) + _select_args += [ "selected_subsystem=" + _target_subsystem ] + _select_args_ivi += [ "selected_subsystem=" + _target_subsystem ] + _select_args_intellitv += [ "selected_subsystem=" + _target_subsystem ] + _select_args_wearable += [ "selected_subsystem=" + _target_subsystem ] + } + + selected_packages = exec_script(_select_script, _select_args, "list lines") + + selected_packages_ivi = + exec_script(_select_script, _select_args_ivi, "list lines") + + selected_packages_intellitv = + exec_script(_select_script, _select_args_intellitv, "list lines") + + selected_packages_wearable = + exec_script(_select_script, _select_args_wearable, "list lines") + + print("Selected packages:") + print(selected_packages) + + # always include testtools + selected_packages += [ "testtools:deploy_testtools" ] + selected_packages_ivi += [ "testtools:deploy_testtools" ] + selected_packages_intellitv += [ "testtools:deploy_testtools" ] + selected_packages_wearable += [ "testtools:deploy_testtools" ] +} else { + print("DCTS build is not enabled") +} diff --git a/testtools/BUILD.gn b/testtools/BUILD.gn new file mode 100755 index 0000000..3756387 --- /dev/null +++ b/testtools/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//test/xts/tools/build/suite.gni") + +ohos_deploy_xdevice("deploy_testtools") { + configs_dir = "./config" +} diff --git a/testtools/config/dcts.json b/testtools/config/dcts.json new file mode 100755 index 0000000..9854751 --- /dev/null +++ b/testtools/config/dcts.json @@ -0,0 +1,33 @@ +{ + "description": "Common configurations for running dcts modules", + "kits": [ + { + "type": "PropertyCheckKit", + "property-name": "ro.build.type", + "expected-value": "user", + "throw-error": "false" + }, + { + "type": "PropertyCheckKit", + "property-name": "ro.product.locale", + "expected-value": "en-US", + "throw-error": "false" + }, + { + "type": "ShellKit", + "run-command": [ + "remount", + "mkdir /data/data/resource", + "chmod -R 777 /data/data/resource", + "settings put global verifier_verify_hdc_installs 0", + "settings put secure hdc_install_need_confirm 0", + "setprop persist.sys.platformautotest 1", + "settings put secure smart_suggestion_enable 1" + ], + "teardown-command": [ + "remount", + "rm -rf /data/data/resource" + ] + } + ] +} diff --git a/testtools/config/user_config.xml b/testtools/config/user_config.xml new file mode 100755 index 0000000..bab0f05 --- /dev/null +++ b/testtools/config/user_config.xml @@ -0,0 +1,33 @@ + + + + + + true + + + + + + + + + + + + + +