diff --git a/examples/distributedb/test/BUILD.gn b/examples/distributedb/test/BUILD.gn index 998fc5f0296f47b7c6089bcd6e30d6ae6abd002b..24421d6353dc2dcfead105ea34b9f3aeff0d38bc 100755 --- a/examples/distributedb/test/BUILD.gn +++ b/examples/distributedb/test/BUILD.gn @@ -20,26 +20,15 @@ config("module_private_config") { visibility = [ ":*" ] include_dirs = [ - "//commonlibrary/c_utils/base/include", - "//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/include", - "//foundation/distributeddatamgr/kv_store/frameworks/innerkitsimpl/distributeddatafwk/src", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/app_distributeddata/include", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/app/include", - "//test/testfwk/developer_test/aw/distributed", + "../../../../../../commonlibrary/c_utils/base/include", + "../../../aw/distributed", ] - - # link library - #ldflags = [ "-llog" ] - - # library path - lib_dirs = [ "//foundation/appexecfwk/libs" ] } ohos_distributedtest("DistributeDemo") { module_out_path = module_output_path - sources = [ "//test/testfwk/developer_test/examples/distributedb/test/distributedtest/common/distribute_demo.cpp" ] + sources = [ "./distributedtest/common/distribute_demo.cpp" ] configs = [ ":module_private_config" ] @@ -51,17 +40,13 @@ ohos_distributedtest("DistributeDemo") { "samgr:samgr_proxy", ] - deps = [ - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/:distributeddata_inner", - "//third_party/googletest:gtest", - ] + deps = [ "//third_party/googletest:gtest" ] } ohos_distributedtest("DistributeDemoAgent") { module_out_path = module_output_path - sources = [ "//test/testfwk/developer_test/examples/distributedb/test/distributedtest/common/distribute_demo_agent.cpp" ] + sources = [ "./distributedtest/common/distribute_demo_agent.cpp" ] configs = [ ":module_private_config" ] @@ -73,11 +58,7 @@ ohos_distributedtest("DistributeDemoAgent") { "samgr:samgr_proxy", ] - deps = [ - "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", - "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/:distributeddata_inner", - "//third_party/googletest:gtest", - ] + deps = [ "//third_party/googletest:gtest" ] } ############################################################################### diff --git a/examples/distributedb/test/distributedtest/common/distribute_demo.cpp b/examples/distributedb/test/distributedtest/common/distribute_demo.cpp index f485b7f58cfea055cf510285886d03f18d5406e6..bf70f9ce25241273b82750360af2ccfb006e51a9 100644 --- a/examples/distributedb/test/distributedtest/common/distribute_demo.cpp +++ b/examples/distributedb/test/distributedtest/common/distribute_demo.cpp @@ -17,239 +17,94 @@ #include "distributed_major.h" -#include "app_types.h" #include "refbase.h" -#include "app_kvstore.h" -#include "app_distributed_kv_data_manager.h" #include "hilog/log.h" - using namespace std; using namespace OHOS; using namespace testing::ext; using namespace OHOS::DistributeSystemTest; using namespace OHOS::HiviewDFX; -using namespace OHOS::AppDistributedKv; namespace { constexpr HiLogLabel LABEL = {LOG_CORE, 0, "DistributeDemo"}; - const int SYNC_REC_TIME = 3; + const int MSG_LENGTH = 100; const int EXPECT_RETURN_VALUE = 111; - const int EXPECT_ADD_TEST_SUM = 500; - const int SLEEP_TIME = 100000; -} -class DistributeDemo : public DistributeTest { -public: - DistributeDemo() = default; - ~DistributeDemo() = default; - static void SetUpTestCase(void) {} - static void TearDownTestCase(void) {} + class DistributeDemo : public DistributeTest { + DistributeDemo() = default; + ~DistributeDemo() = default; - virtual void SetUp() {} - virtual void TearDown() {} -}; + static void SetUpTestCase() {}; + static void TearDownTestCase() {}; + virtual void SetUp() {}; + virtual void TearDown() {}; + }; -/** - * @tc.name: msgsendtest_001 - * @tc.desc: Verify the distributed test framework interface SendMessage. - * @tc.type: FUNC - * @tc.require: AR000CQGMV - */ -HWTEST_F(DistributeDemo, msgsendtest_001, TestSize.Level0) -{ - char msgbuf[MSG_LENGTH] = "I am testcase 1."; - EXPECT_TRUE(SendMessage(AGENT_NO::ONE, msgbuf, MSG_LENGTH)); -}; - -/** - * @tc.name: msgsendtest_002 - * @tc.desc: Verify the distributed test framework interface SendMessage, Maximum data length. - * @tc.type: FUNC - * @tc.require: AR000CQGMV - */ -HWTEST_F(DistributeDemo, msgsendtest_002, TestSize.Level0) -{ - char msgbuf[MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN] = "I am testcase 2."; - EXPECT_TRUE(SendMessage(AGENT_NO::ONE, msgbuf, MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN)); -}; - -/** - * @tc.name: msgsendtest_003 - * @tc.desc: Verify the distributed test framework interface SendMessage, - * Verify the data maximum length of the sent message interface . - * @tc.type: FUNC - * @tc.require: AR000CQGMV - */ -HWTEST_F(DistributeDemo, msgsendtest_003, TestSize.Level0) -{ - char msgbuf[MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN + 1] = "too big message."; - EXPECT_FALSE(SendMessage(AGENT_NO::ONE, msgbuf, MAX_BUFF_LEN - DST_COMMAND_HEAD_LEN + 1)); -}; - -/** - * @tc.name: msgsendtest_004 - * @tc.desc: Verify the distributed test framework interface SendMessage, constains callback. - * @tc.type: FUNC - * @tc.require: AR000CQGMV - */ -HWTEST_F(DistributeDemo, msgsendtest_004, TestSize.Level0) -{ - std::string msgBuf = "recall function message test."; - int ret = SendMessage(AGENT_NO::ONE, msgBuf, MSG_LENGTH, - [&](const std::string &szreturnbuf, int rlen)->bool { - std::string szbuf = "I get recall message."; - EXPECT_TRUE(szbuf == szreturnbuf); - HiLog::Info(LABEL, "msgsendtest_004 strncmp=%s", szbuf.c_str()); - return true; - }); - EXPECT_TRUE(ret > 0); -}; - -/** - * @tc.name: runcmdonagent_001 - * @tc.desc: Verify the distributed test framework interface RunCmdOnAgent. - * @tc.type: FUNC - * @tc.require: AR000CQGMV - */ -HWTEST_F(DistributeDemo, runcmdonagent_001, TestSize.Level0) -{ - std::string command = "query_command"; - std::string cmdArgs = "query a name?"; - std::string expectValue = "111"; - RunCmdOnAgent(AGENT_NO::ONE, command, cmdArgs, expectValue); - EXPECT_EQ(GetReturnVal(), EXPECT_RETURN_VALUE); -}; - -/** - * @tc.name: runcmdonagent_002 - * @tc.desc: Verify the distributed test framework interface RunCmdOnAgent, Using command map. - * @tc.type: FUNC - * @tc.require: AR000CQGMV - */ -HWTEST_F(DistributeDemo, runcmdonagent_002, TestSize.Level0) { - std::string command = "add_for_two_int"; - std::string cmdArgs = "200 300"; - std::string expectValue = "500"; - RunCmdOnAgent(AGENT_NO::ONE, command, cmdArgs, expectValue); - EXPECT_EQ(GetReturnVal(), EXPECT_ADD_TEST_SUM); -}; - -/** - * @tc.name: getkvstore_001 - * @tc.desc: Verify distributed data read and write and synchronization. - * @tc.type: FUNC - * @tc.require: AR000CQGMV - */ -HWTEST_F(DistributeDemo, getkvstore_001, TestSize.Level0) { - /** - * @tc.steps: step1.init appkvstore, Get the kvstore pointer for the specified appId and storeId - */ - static std::shared_ptr manager; - Options options; - options.createIfMissing = true; - options.encrypt = false; - options.persistent = true; - std::string appId = "com.ohos.nb.service.user1_test"; - std::string storeId = "student_1"; - manager = AppDistributedKvDataManager::GetInstance(appId, "/data/test"); - std::unique_ptr kvStorePtr; - Status status = manager->GetKvStore(options, storeId, [&](std::unique_ptr kvStore) { - kvStorePtr = std::move(kvStore); - }); - if (!kvStorePtr) { - HiLog::Error(LABEL, "client get kvStorePtr error."); + void DistributeDemo::SetUpTestCase() + { } - - ASSERT_TRUE(kvStorePtr != nullptr); - - /** - * @tc.steps: step2.Write specific K and V values to the database - */ - if (status == Status::SUCCESS) { - WriteOptions writeOptions; - writeOptions.local = false; - Key key("student_name_user1"); - Value value = "good boy."; - kvStorePtr->Put(writeOptions, key, value); + void DistributeDemo::TearDownTestCase() + { } - /** - * @tc.steps: step3.Synchronize data to a specific remote device - */ - std::vector deviceIdList; - deviceIdList.push_back("192.168.43.200"); - std::map sync_map; - Status syncinfo = kvStorePtr->Sync(deviceIdList, SyncMode::PUSH, - [&](const std::map &syncMapinfo){ - sync_map = std::move(syncMapinfo); - }); - if (syncinfo == Status::SUCCESS) { - HiLog::Info(LABEL, "agent sync success"); + void DistributeDemo::SetUp() + { } - - /** - * @tc.steps: step4.Judge if the data is successfully synchronized to the remote device - */ - int n = 0; - while (n < SYNC_REC_TIME) { - std::string id = ""; - for (auto mapstatus: sync_map) { - HiLog::Info(LABEL, "client map_id==%s", mapstatus.first.c_str()); - if (mapstatus.second == Status::SUCCESS) { - id = mapstatus.first; - HiLog::Info(LABEL, "real client Sync success."); - } else { - HiLog::Info(LABEL, "mapstatus.second=%d.", (int)mapstatus.second); - } - n++; - } - if (id != "") { - break; - } - usleep(SLEEP_TIME); + void DistributeDemo::TearDown() + { } - if (syncinfo == Status::SUCCESS) { - HiLog::Info(LABEL, "client sync success."); + + /* + * @tc.name:SendMessageTest001 + * @tc.desc:Verify the distributed test framework interface SendMessage. + * @tc.type:FUNC + * @tc.require:ArOOOCQGMV + */ + HWTEST_F(DistributeDemo, SendMessageTest001, TestSize.Level1) + { + char msgbuf[MSG_LENGTH] = "I am testcase 1"; + int ret = SendMessage(AGENT_NO::ONE, msgbuf, MSG_LENGTH); + + EXPECT_FALSE(ret)<<"ret = 2"; } - /** - * @tc.steps: step5.Query whether the value inserted in the local database is correct - */ - std::string command = "getkvstore"; - std::string cmdArgs = "student_name_user1"; - std::string expectValue = "good boy."; - ReadOptions readOptions; - readOptions.local = false; - Key key(cmdArgs); - Value getValue; - Status getTstatus = kvStorePtr->Get(readOptions, key, getValue); - if (getTstatus == Status::SUCCESS) { - HiLog::Info(LABEL, "Get Value SUCCESS, key=%s.", key.ToString().c_str()); - } else { - HiLog::Error(LABEL, "Get Value Failed."); + HWTEST_F(DistributeDemo, SendMessageTest002, TestSize.Level1) + { + char msgbuf[MSG_LENGTH] = "I am recall"; + int ret = SendMessage(AGENT_NO::ONE, msgbuf, MSG_LENGTH, + [&](const std::string &szreturnbuf, int rlen)->bool { + std::string szbuf = "ok"; + HiLog::Info(LABEL, "SendMessageTest002 = %s", szbuf.c_str()); + return true; + }); + EXPECT_TRUE(ret)<<"ret = 0"; } - /** - * @tc.steps: step6.Send a command to the remote device to query the database with parameters and - * expected return values - */ - RunCmdOnAgent(AGENT_NO::ONE, command, cmdArgs, expectValue); + HWTEST_F(DistributeDemo, SendMessageTest003, TestSize.Level1) + { + char msgbuf[MSG_LENGTH] = "I am testcase 2"; + int ret = SendMessage(AGENT_NO::ONE, msgbuf, MSG_LENGTH); + EXPECT_FALSE(ret)<<"ret = 0"; + } - /** - * @tc.steps: step7.Judge the return value - * @tc.expected: step7. The remote device queried the data successfully. - */ - EXPECT_TRUE(GetReturnVal() == EXPECT_RETURN_VALUE); -}; + HWTEST_F(DistributeDemo, RunCmdOnAgent001, TestSize.Level1) + { + std::string command = "query_command"; + std::string cmdArgs = "query a name?"; + std::string expectValue = "111"; + RunCmdOnAgent(AGENT_NO::ONE, command, cmdArgs, expectValue); + EXPECT_EQ(GetReturnVal(), EXPECT_RETURN_VALUE); + } +} -int main(int argc, char *argv[]) +int main(int argc, char*argv[]) { g_pDistributetestEnv = new DistributeTestEnvironment("major.desc"); testing::AddGlobalTestEnvironment(g_pDistributetestEnv); testing::GTEST_FLAG(output) = "xml:./"; testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -} +} \ No newline at end of file diff --git a/examples/distributedb/test/distributedtest/common/distribute_demo_agent.cpp b/examples/distributedb/test/distributedtest/common/distribute_demo_agent.cpp index 50886b9ed0dadecff347e1f7a6553568a4bfcef0..702be5cc946998799c0dda1d99ee318f260c90b3 100644 --- a/examples/distributedb/test/distributedtest/common/distribute_demo_agent.cpp +++ b/examples/distributedb/test/distributedtest/common/distribute_demo_agent.cpp @@ -18,27 +18,20 @@ #include #include -#include "app_types.h" #include "refbase.h" -#include "app_kvstore.h" -#include "app_distributed_kv_data_manager.h" #include "hilog/log.h" using namespace testing; using namespace OHOS; using namespace OHOS::DistributeSystemTest; using namespace OHOS::HiviewDFX; -using namespace OHOS::AppDistributedKv; namespace { - constexpr HiLogLabel LABEL = {LOG_CORE, 0, "DistributeDemoAgent"}; - std::shared_ptr g_manager; + constexpr HiLogLabel LABEL = {LOG_CORE, 0, "DistributedDemoAgent"}; + std::string g_appId = "com.ohos.nb.service.user1_test"; std::string g_storeId = "student_1"; - std::unique_ptr g_kvStorePtr; - const int CMD_LEN = 4; - const int RETURN_HALF = 2; - const int MSG_CALL_LEN = 6; + const int CMD_RETURN_TWO = 111; } @@ -49,17 +42,18 @@ public: virtual bool SetUp(); virtual bool TearDown(); - virtual int OnProcessMsg(const std::string &strMsg, int len, std::string &strReturnValue, int returnBufLen); + + virtual int OnProcessMsg(const std::string &dtrMsg, int len, std::string &strReturnValue, int returnBufLen); virtual int OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen, const std::string &strExpectValue, int expectValueLen); int GetKvValue(const std::string &strArgs, int argsLen, const std::string &strExpectValue, - int expectValueLen); + int expectValueLen); + int AddTwoValue(const std::string &strArgs, int argsLen, const std::string &strExpectValue, - int expectValueLen); + int expectValueLen); int ProcessByUseMap(const std::string &strCommand, int cmdLen, const std::string &strArgs, int argsLen, - const std::string &strExpectValue, int expectValueLen); - + const std::string &strExpectValue, int expectValueLen); private: typedef int (DistributeDemoAgent::*self_func)(const std::string &, int, const std::string &, int); std::map cmdFunMap_; @@ -71,161 +65,51 @@ DistributeDemoAgent::DistributeDemoAgent() DistributeDemoAgent::~DistributeDemoAgent() { } - bool DistributeDemoAgent::SetUp() { - /** - * @tc.setup: Initialize test agent, start data management service - */ - cmdFunMap_["getkvstore"] = &DistributeDemoAgent::GetKvValue; - cmdFunMap_["add_for_two_int"] = &DistributeDemoAgent::AddTwoValue; - g_manager = AppDistributedKvDataManager::GetInstance(g_appId, "/data/test"); - Options options; - options.createIfMissing = true; - options.encrypt = false; - options.persistent = true; - std::string storeId = g_storeId; - Status status = g_manager->GetKvStore(options, storeId, [&](std::unique_ptr kvStore) { - g_kvStorePtr = std::move(kvStore); - }); - if (status == Status::SUCCESS) { - return true; - } - return false; + return true; } bool DistributeDemoAgent::TearDown() { - /** - * @tc.teardown: Recovery test agent device environment - */ + /* + * @tc.teardown:Recovery test agent device environment + */ return true; } // The entry of handlingthe major test case message -int DistributeDemoAgent::OnProcessMsg(const std::string &strMsg, int len, - std::string &strReturnValue, int returnBufLen) +void DistributeDemoAgent::OnProcessMsg(const std::string &strMsg, + int len, std::string &strReturnValue, int returnValueLen) { - int nret = 0; std::string returnStr = "agent return message."; - if ((len > CMD_LEN) && (strMsg.find("\0\1\0\1") == 1)) { - for (int i = 0; i < returnBufLen; i++) { - strReturnValue += std::to_string((i + 1) % RETURN_HALF); - } - nret = returnBufLen; + std::string strrq = "I am recall"; + if (strstr(strMsg.c_str(), strrq.c_str())) { + strReturnValue = "ok"; + return strReturnValue; + } + std::string strrq = "I am testcase2"; + if (strstr(strMsg.c_str(), strrq.c_str())) { + HiLog::Info(LABEL, "I am testcase2"); } else { - HiLog::Info(LABEL, "receive message=%s.", strMsg.c_str()); - if (!strncmp(strMsg.c_str(), "recall", MSG_CALL_LEN)) { - returnStr = "I get recall message."; - int ptrlen = returnStr.size(); - if (ptrlen > returnBufLen) { - ptrlen = returnBufLen - 1; - } - strReturnValue = returnStr; - nret = ptrlen; - } else { - nret = DistributedAgent::OnProcessMsg(strMsg, len, strReturnValue, returnBufLen); - } + return DistributedAgent::OnProcessMsg(strMsg, len, strReturnValue, returnValueLen); } - return nret; } -// The entry of handling the major test case command -int DistributeDemoAgent::OnProcessCmd(const std::string &strCommand, int cmdLen, const std::string &strArgs, - int argsLen, const std::string &strExpectValue, int expectValueLen) +void DistributeDemoAgent::OnProcessCmd(const std::string &strCommand, + int cmdLen, const std::string &strExpectValue, int expectValueLen) { if (strCommand == "query_command") { if (strArgs == "query a name?") { return CMD_RETURN_TWO; } } - return DistributeDemoAgent::ProcessByUseMap(strCommand, cmdLen, strArgs, argsLen, strExpectValue, expectValueLen); -} - -// Handling major test case command: getkvstore -int DistributeDemoAgent::GetKvValue(const std::string &strArgs, int argsLen, const std::string &strExpectValue, - int expectValueLen) -{ - if (!g_kvStorePtr || (int)strArgs.length() > argsLen) { - HiLog::Error(LABEL, "[ohos-test-fm]agent ERROR."); - return -1; - } - ReadOptions readOptions; - readOptions.local = false; - Key key(strArgs); - Value getValue; - Status status = g_kvStorePtr->Get(readOptions, key, getValue); - if (status == Status::SUCCESS) { - HiLog::Info(LABEL, "Get Value SUCCESS, key=%s.", key.ToString().c_str()); - } else { - HiLog::Error(LABEL, "Get Value Failed."); - } - std::vector allEntry; - Status entryStatus = g_kvStorePtr->GetEntries("", allEntry); - if (entryStatus == Status::SUCCESS) { - HiLog::Info(LABEL, "GetEntries SUCCESS."); - for (auto entry : allEntry) { - HiLog::Info(LABEL, "GetEntries key= %s.", entry.key.ToString().c_str()); - } - } - if (getValue.ToString() == strExpectValue) { - if (expectValueLen < (int)getValue.ToString().length()) { - HiLog::Error(LABEL, "[ohos-test-fm]agent end xxx."); - return -1; - } - HiLog::Error(LABEL, "[ohos-test-fm]agent end 200."); - return CMD_RETURN_TWO; - } - return -1; -} - -// Handling major test case command: add_for_two_int -int DistributeDemoAgent::AddTwoValue(const std::string &strArgs, int argsLen, const std::string &strExpectValue, - int expectValueLen) -{ - int val1 = 0; - int val2 = 0; - int val3 = 0; - int ret1; - int ret2; - ret1 = sscanf_s(strArgs.c_str(), "%d %d", &val1, &val2); - if (ret1 <=0 || ret1 == 1) { - HiLog::Error(LABEL, "sscanf_s failed. ret1 != 2"); - return -1; - } - ret2 = sscanf_s(strExpectValue.c_str(), "%d", &val3); - if (ret2 != 1) { - HiLog::Error(LABEL, "sscanf_s failed. ret2 != 1"); - return -1; - } - EXPECT_TRUE(val3 == (val1 + val2)); - return val1 + val2; -} - -// Handling major test cases through already registered commands and function mappings -int DistributeDemoAgent::ProcessByUseMap(const std::string &strCommand, int cmdLen, const std::string &strArgs, - int argsLen, const std::string &strExpectValue, int expectValueLen) -{ - std::map::iterator ite = cmdFunMap_.find(strCommand); - if (ite != cmdFunMap_.end()) { - self_func callProcCmd = cmdFunMap_[strCommand]; - if (callProcCmd != nullptr) { - return (this->*callProcCmd)(strArgs, argsLen, strExpectValue, expectValueLen); - } else { - return DistributedAgent::OnProcessCmd(strCommand, - cmdLen, - strArgs, - argsLen, - strExpectValue, - expectValueLen); - } - } - return -1; + return DistributedAgent::OnProcessCmd(strCommand, cmdLen, strExpectValue, expectValueLen); } int main() { - // Test agent main function + // Test agent main function DistributeDemoAgent obj; if (obj.SetUp()) { obj.Start("agent.desc"); @@ -233,10 +117,12 @@ int main() } else { HiLog::Error(LABEL, "Init environment failed."); } + if (obj.TearDown()) { return 0; } else { HiLog::Error(LABEL, "Clear environment failed."); return -1; } -} \ No newline at end of file +} +