diff --git a/tests/core/bus_center/lnn/BUILD.gn b/tests/core/bus_center/lnn/BUILD.gn index c1e73ad73b6fb511b276fb90a28e8b2edccae852..a3a61e63224356d2829763ecdd5891b70b4cbaf6 100644 --- a/tests/core/bus_center/lnn/BUILD.gn +++ b/tests/core/bus_center/lnn/BUILD.gn @@ -145,6 +145,146 @@ ohos_unittest("LNNNetBuilderTest") { } } +ohos_unittest("LNNLedgerTestMock") { + module_out_path = module_output_path + sources = [ + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/src/lnn_device_info.c", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/src/lnn_net_ledger.c", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/src/lnn_node_info.c", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/distributed_ledger/src/lnn_distributed_net_ledger.c", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/local_ledger/src/lnn_local_net_ledger.c", + "$dsoftbus_root_path/core/bus_center/utils/src/lnn_map.c", + "net_ledger/lnn_local_ledger_deps_mock.cpp", + "net_ledger/lnn_local_ledger_test.cpp", + ] + + include_dirs = [ + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/lane_manager/include", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_builder/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_buscenter/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_builder/sync_info/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/distributed_ledger/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/local_ledger/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/include", + "$dsoftbus_root_path/core/bus_center/lnn/disc_mgr/include", + "$dsoftbus_root_path/core/bus_center/monitor/include", + "$dsoftbus_root_path/core/common/include", + "$dsoftbus_root_path/adapter/common/include", + "$dsoftbus_root_path/core/connection/interface", + "$dsoftbus_root_path/core/connection/p2p/interface", + "$dsoftbus_root_path/core/connection/p2p/common/include", + "$dsoftbus_root_path/core/authentication/interface", + "$dsoftbus_root_path/core/bus_center/interface", + "$dsoftbus_root_path/core/bus_center/utils/include", + "$dsoftbus_root_path/interfaces/kits/bus_center", + "$dsoftbus_root_path/interfaces/kits/common", + "$dsoftbus_root_path/core/adapter/bus_center/include", + "//commonlibrary/c_utils/base/include", + "//third_party/cJSON", + "unittest/common/", + "$dsoftbus_root_path/core/connection/manager", + "$dsoftbus_root_path/adapter/common/bus_center/include/", + "$dsoftbus_root_path/core/discovery/manager/include", + "$dsoftbus_root_path/core/discovery/interface", + "$dsoftbus_root_path/interfaces/kits/discovery", + "$dsoftbus_root_path/core/bus_center/service/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/decision_db/include", + "//base/security/huks/interfaces/innerkits/huks_standard/main/include", + ] + + deps = [ + "$dsoftbus_root_path/adapter:softbus_adapter", + "$dsoftbus_root_path/core/common:softbus_utils", + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//base/security/device_auth/services:deviceauth_sdk", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + ] + + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + } else { + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] + } +} + +ohos_unittest("LNNLedgerTest") { + module_out_path = module_output_path + sources = [ + "net_ledger/lnn_disctributed_net_ledger_test.cpp", + "net_ledger/lnn_ledger_common_test.cpp", + "net_ledger/lnn_meta_node_ledger_test.cpp", + ] + + include_dirs = [ + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/lane_manager/include", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_builder/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_buscenter/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_builder/sync_info/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/distributed_ledger/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/local_ledger/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/include", + "$dsoftbus_root_path/core/bus_center/lnn/disc_mgr/include", + "$dsoftbus_root_path/core/bus_center/monitor/include", + "$dsoftbus_root_path/core/common/include", + "$dsoftbus_root_path/adapter/common/include", + "$dsoftbus_root_path/core/connection/interface", + "$dsoftbus_root_path/core/connection/p2p/interface", + "$dsoftbus_root_path/core/connection/p2p/common/include", + "$dsoftbus_root_path/core/authentication/interface", + "$dsoftbus_root_path/core/bus_center/interface", + "$dsoftbus_root_path/core/bus_center/utils/include", + "$dsoftbus_root_path/interfaces/kits/bus_center", + "$dsoftbus_root_path/interfaces/kits/common", + "$dsoftbus_root_path/core/adapter/bus_center/include", + "//commonlibrary/c_utils/base/include", + "//third_party/cJSON", + "unittest/common/", + "$dsoftbus_root_path/core/connection/manager", + "$dsoftbus_root_path/adapter/common/bus_center/include/", + "$dsoftbus_root_path/core/discovery/manager/include", + "$dsoftbus_root_path/core/discovery/interface", + "$dsoftbus_root_path/interfaces/kits/discovery", + "$dsoftbus_root_path/core/bus_center/service/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/decision_db/include", + "//base/security/huks/interfaces/innerkits/huks_standard/main/include", + ] + + deps = [ + "$dsoftbus_root_path/adapter:softbus_adapter", + "$dsoftbus_root_path/core/common:softbus_utils", + "$dsoftbus_root_path/core/frame:softbus_server", + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//base/security/device_auth/services:deviceauth_sdk", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + ] + if (dsoftbus_feature_lnn_net) { + sources += [ "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/distributed_ledger/src/lnn_distributed_net_ledger.c" ] + } else { + sources += [ "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/distributed_ledger/src/lnn_distributed_net_ledger_virtual.c" ] + } + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + } else { + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] + } +} + native_source_path = rebase_path("$dsoftbus_root_path") dep_dir = "dsoftbus_enhance/core/bus_center/lnn/lane_hub" enhanced_test = exec_script("$dsoftbus_root_path/check_sub_module.py", @@ -154,12 +294,90 @@ enhanced_test = exec_script("$dsoftbus_root_path/check_sub_module.py", ], "value") +if (!defined(ohos_lite) && enhanced_test) { + ohos_unittest("LNNDbTestMock") { + module_out_path = module_output_path + sources = [ + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_ctrl.c", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_fsm.c", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/src/lnn_heartbeat_strategy.c", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/decision_db/src/lnn_decision_db.c", + "$dsoftbus_root_path/core/bus_center/utils/src/lnn_state_machine.c", + "net_ledger/lnn_decision_db_deps_mock.cpp", + "net_ledger/lnn_decision_db_test.cpp", + ] + + include_dirs = [ + "$dsoftbus_root_path/core/authentication/include", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/lane_manager/include", + "$dsoftbus_root_path/core/bus_center/lnn/lane_hub/heartbeat/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_builder/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_buscenter/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_builder/sync_info/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/distributed_ledger/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/local_ledger/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/include", + "$dsoftbus_root_path/core/bus_center/lnn/disc_mgr/include", + "$dsoftbus_root_path/core/bus_center/monitor/include", + "$dsoftbus_root_path/core/common/include", + "$dsoftbus_root_path/adapter/common/include", + "$dsoftbus_root_path/core/connection/interface", + "$dsoftbus_root_path/core/connection/p2p/interface", + "$dsoftbus_root_path/core/connection/p2p/common/include", + "$dsoftbus_root_path/core/authentication/interface", + "$dsoftbus_root_path/core/bus_center/interface", + "$dsoftbus_root_path/core/bus_center/utils/include", + "$dsoftbus_root_path/interfaces/kits/bus_center", + "$dsoftbus_root_path/interfaces/kits/common", + "$dsoftbus_root_path/core/adapter/bus_center/include", + "//commonlibrary/c_utils/base/include", + "//third_party/cJSON", + "unittest/common/", + "$dsoftbus_root_path/core/connection/manager", + "$dsoftbus_root_path/adapter/common/bus_center/include/", + "$dsoftbus_root_path/core/discovery/manager/include", + "$dsoftbus_root_path/core/discovery/interface", + "$dsoftbus_root_path/interfaces/kits/discovery", + "$dsoftbus_root_path/core/bus_center/service/include", + "$dsoftbus_root_path/core/bus_center/lnn/net_ledger/decision_db/include", + "//base/security/huks/interfaces/innerkits/huks_standard/main/include", + "$dsoftbus_root_path/adapter/common/net/bluetooth/include", + ] + + deps = [ + "$dsoftbus_root_path/adapter:softbus_adapter", + "$dsoftbus_root_path/core/common:softbus_utils", + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//base/security/device_auth/services:deviceauth_sdk", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest_main", + ] + + if (is_standard_system) { + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + ] + } else { + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + ] + } + } +} + group("unittest") { testonly = true deps = [ + ":LNNLedgerTest", + ":LNNLedgerTestMock", ":LNNNetBuilderTest", ":LNNTest", ] + if (!defined(ohos_lite) && enhanced_test) { + deps += [ ":LNNDbTestMock" ] + } if (enhanced_test) { deps += [ "$dsoftbus_root_path/dsoftbus_enhance/test/core/bus_center/lnn/lane_hub/lane_channel_rating:unittest", diff --git a/tests/core/bus_center/lnn/net_ledger/lnn_decision_db_deps_mock.cpp b/tests/core/bus_center/lnn/net_ledger/lnn_decision_db_deps_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05d01d1a2f771d65bcd860d53878d5cd335ab93f --- /dev/null +++ b/tests/core/bus_center/lnn/net_ledger/lnn_decision_db_deps_mock.cpp @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2022 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 "lnn_decision_db_deps_mock.h" + +#include + +#include "softbus_common.h" +#include "softbus_error_code.h" + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +void *g_decisionDbDepsInterface; +DecisionDbDepsInterfaceMock::DecisionDbDepsInterfaceMock() +{ + g_decisionDbDepsInterface = reinterpret_cast(this); +} + +DecisionDbDepsInterfaceMock::~DecisionDbDepsInterfaceMock() +{ + g_decisionDbDepsInterface = nullptr; +} + +static DecisionDbDepsInterfaceMock *GetDecisionDbDepsInterface() +{ + return reinterpret_cast(g_decisionDbDepsInterface); +} + +int32_t DecisionDbDepsInterfaceMock::DecisionDbAsyncCallbackHelper(SoftBusLooper *looper, + LnnAsyncCallbackFunc callback, void *para) +{ + if (callback != NULL) { + callback(para); + return SOFTBUS_OK; + } + return SOFTBUS_ERR; +} + +extern "C" { +int32_t LnnGenerateKeyByHuks(struct HksBlob *keyAlias) +{ + return GetDecisionDbDepsInterface()->LnnGenerateKeyByHuks(keyAlias); +} + +int32_t LnnDeleteKeyByHuks(struct HksBlob *keyAlias) +{ + return GetDecisionDbDepsInterface()->LnnDeleteKeyByHuks(keyAlias); +} + +int32_t LnnEncryptDataByHuks(const struct HksBlob *keyAlias, + const struct HksBlob *inData, struct HksBlob *outData) +{ + return GetDecisionDbDepsInterface()->LnnEncryptDataByHuks(keyAlias, inData, outData); +} + +int32_t LnnDecryptDataByHuks(const struct HksBlob *keyAlias, + const struct HksBlob *inData, struct HksBlob *outData) +{ + return GetDecisionDbDepsInterface()->LnnDecryptDataByHuks(keyAlias, inData, outData); +} + +int32_t LnnGenerateRandomByHuks(uint8_t *randomKey, uint32_t len) +{ + return GetDecisionDbDepsInterface()->LnnGenerateRandomByHuks(randomKey, len); +} + +int32_t OpenDatabase(DbContext **ctx) +{ + return GetDecisionDbDepsInterface()->OpenDatabase(ctx); +} + +int32_t CloseDatabase(DbContext *ctx) +{ + return GetDecisionDbDepsInterface()->CloseDatabase(ctx); +} + +int32_t CreateTable(DbContext *ctx, TableNameID id) +{ + return GetDecisionDbDepsInterface()->CreateTable(ctx, id); +} + +int32_t CheckTableExist(DbContext *ctx, TableNameID id, bool *isExist) +{ + return GetDecisionDbDepsInterface()->CheckTableExist(ctx, id, isExist); +} + +int32_t RemoveRecordByKey(DbContext *ctx, TableNameID id, uint8_t *data) +{ + return GetDecisionDbDepsInterface()->RemoveRecordByKey(ctx, id, data); +} + +int32_t GetRecordNumByKey(DbContext *ctx, TableNameID id, uint8_t *data) +{ + return GetDecisionDbDepsInterface()->GetRecordNumByKey(ctx, id, data); +} + +int32_t EncryptedDb(DbContext *ctx, const uint8_t *password, uint32_t len) +{ + return GetDecisionDbDepsInterface()->EncryptedDb(ctx, password, len); +} + +int32_t UpdateDbPassword(DbContext *ctx, const uint8_t *password, uint32_t len) +{ + return GetDecisionDbDepsInterface()->UpdateDbPassword(ctx, password, len); +} + +int32_t QueryRecordByKey(DbContext *ctx, TableNameID id, uint8_t *data, uint8_t **replyInfo, int infoNum) +{ + return GetDecisionDbDepsInterface()->QueryRecordByKey(ctx, id, data, replyInfo, infoNum); +} + +int32_t LnnGetFullStoragePath(LnnFileId id, char *path, uint32_t len) +{ + return GetDecisionDbDepsInterface()->LnnGetFullStoragePath(id, path, len); +} + +int32_t SoftBusReadFullFile(const char *fileName, char *readBuf, uint32_t maxLen) +{ + return GetDecisionDbDepsInterface()->SoftBusReadFullFile(fileName, readBuf, maxLen); +} + +int32_t SoftBusWriteFile(const char *fileName, const char *writeBuf, uint32_t len) +{ + return GetDecisionDbDepsInterface()->SoftBusWriteFile(fileName, writeBuf, len); +} + +int32_t SoftBusAccessFile(const char *pathName, int32_t mode) +{ + return GetDecisionDbDepsInterface()->SoftBusAccessFile(pathName, mode); +} + +int32_t LnnAsyncCallbackHelper(SoftBusLooper *looper, LnnAsyncCallbackFunc callback, void *para) +{ + return GetDecisionDbDepsInterface()->LnnAsyncCallbackHelper(looper, callback, para); +} + +int32_t LnnGetLocalByteInfo(InfoKey key, uint8_t *info, uint32_t len) +{ + return GetDecisionDbDepsInterface()->LnnGetLocalByteInfo(key, info, len); +} + +int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen, + const unsigned char *inBuf, uint32_t inLen) +{ + return GetDecisionDbDepsInterface()->ConvertBytesToHexString(outBuf, outBufLen, inBuf, inLen); +} +} // extern "C" +} // namespace OHOS diff --git a/tests/core/bus_center/lnn/net_ledger/lnn_decision_db_deps_mock.h b/tests/core/bus_center/lnn/net_ledger/lnn_decision_db_deps_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..49f8b9a61112e0a570925658f15f51dd552f03ed --- /dev/null +++ b/tests/core/bus_center/lnn/net_ledger/lnn_decision_db_deps_mock.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2022 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. + */ + +#ifndef LNN_DECISION_DB_DEPS_MOCK_H +#define LNN_DECISION_DB_DEPS_MOCK_H + +#include +#include + +#include "bus_center_manager.h" +#include "lnn_async_callback_utils.h" +#include "lnn_file_utils.h" +#include "lnn_huks_utils.h" +#include "sqlite3_utils.h" + +#include "softbus_adapter_file.h" +#include "softbus_common.h" +#include "softbus_errcode.h" +#include "softbus_log.h" +#include "softbus_utils.h" + +namespace OHOS { +class DecisionDbDepsInterface { +public: + DecisionDbDepsInterface() {}; + virtual ~DecisionDbDepsInterface() {}; + + virtual int32_t LnnGenerateKeyByHuks(struct HksBlob *keyAlias); + virtual int32_t LnnDeleteKeyByHuks(struct HksBlob *keyAlias); + virtual int32_t LnnEncryptDataByHuks(const struct HksBlob *keyAlias, + const struct HksBlob *inData, struct HksBlob *outData); + virtual int32_t LnnDecryptDataByHuks(const struct HksBlob *keyAlias, + const struct HksBlob *inData, struct HksBlob *outData); + virtual int32_t LnnGenerateRandomByHuks(uint8_t *randomKey, uint32_t len); + virtual int32_t OpenDatabase(DbContext **ctx); + virtual int32_t CloseDatabase(DbContext *ctx); + virtual int32_t CreateTable(DbContext *ctx, TableNameID id); + virtual int32_t CheckTableExist(DbContext *ctx, TableNameID id, bool *isExist); + virtual int32_t RemoveRecordByKey(DbContext *ctx, TableNameID id, uint8_t *data); + virtual int32_t GetRecordNumByKey(DbContext *ctx, TableNameID id, uint8_t *data); + virtual int32_t EncryptedDb(DbContext *ctx, const uint8_t *password, uint32_t len); + virtual int32_t UpdateDbPassword(DbContext *ctx, const uint8_t *password, uint32_t len); + virtual int32_t QueryRecordByKey(DbContext *ctx, TableNameID id, uint8_t *data, uint8_t **replyInfo, int infoNum); + virtual int32_t LnnGetFullStoragePath(LnnFileId id, char *path, uint32_t len); + virtual int32_t SoftBusReadFullFile(const char *fileName, char *readBuf, uint32_t maxLen); + virtual int32_t SoftBusWriteFile(const char *fileName, const char *writeBuf, uint32_t len); + virtual int32_t SoftBusAccessFile(const char *pathName, int32_t mode); + virtual int32_t LnnAsyncCallbackHelper(SoftBusLooper *looper, LnnAsyncCallbackFunc callback, void *para); + virtual int32_t LnnGetLocalByteInfo(InfoKey key, uint8_t *info, uint32_t len); + virtual int32_t ConvertBytesToHexString(char *outBuf, uint32_t outBufLen, + const unsigned char *inBuf, uint32_t inLen); +}; +class DecisionDbDepsInterfaceMock : public DecisionDbDepsInterface { +public: + DecisionDbDepsInterfaceMock(); + ~DecisionDbDepsInterfaceMock() override; + MOCK_METHOD1(LnnGenerateKeyByHuks, int32_t (struct HksBlob *)); + MOCK_METHOD1(LnnDeleteKeyByHuks, int32_t (struct HksBlob *)); + MOCK_METHOD3(LnnEncryptDataByHuks, int32_t (const struct HksBlob *, const struct HksBlob *, struct HksBlob *)); + MOCK_METHOD3(LnnDecryptDataByHuks, int32_t (const struct HksBlob *, const struct HksBlob *, struct HksBlob *)); + MOCK_METHOD2(LnnGenerateRandomByHuks, int32_t (uint8_t *, uint32_t)); + MOCK_METHOD1(OpenDatabase, int32_t (DbContext **)); + MOCK_METHOD1(CloseDatabase, int32_t (DbContext *)); + MOCK_METHOD2(CreateTable, int32_t (DbContext *, TableNameID)); + MOCK_METHOD3(CheckTableExist, int32_t (DbContext *, TableNameID, bool *)); + MOCK_METHOD3(RemoveRecordByKey, int32_t (DbContext *, TableNameID, uint8_t *)); + MOCK_METHOD3(GetRecordNumByKey, int32_t (DbContext *, TableNameID, uint8_t *)); + MOCK_METHOD3(EncryptedDb, int32_t (DbContext *, const uint8_t *, uint32_t)); + MOCK_METHOD3(UpdateDbPassword, int32_t (DbContext *, const uint8_t *, uint32_t)); + MOCK_METHOD5(QueryRecordByKey, int32_t (DbContext *, TableNameID, uint8_t *, uint8_t **, int)); + MOCK_METHOD3(LnnGetFullStoragePath, int32_t (LnnFileId, char *, uint32_t)); + MOCK_METHOD3(SoftBusReadFullFile, int32_t (const char *, char *, uint32_t)); + MOCK_METHOD3(SoftBusWriteFile, int32_t (const char *, const char *, uint32_t)); + MOCK_METHOD2(SoftBusAccessFile, int32_t (const char *, int32_t)); + MOCK_METHOD3(LnnAsyncCallbackHelper, int32_t (SoftBusLooper *, LnnAsyncCallbackFunc, void *)); + MOCK_METHOD3(LnnGetLocalByteInfo, int32_t (InfoKey, uint8_t *, uint32_t)); + MOCK_METHOD4(ConvertBytesToHexString, int32_t (char *, uint32_t, const unsigned char *, uint32_t)); + + static int32_t DecisionDbAsyncCallbackHelper(SoftBusLooper *looper, LnnAsyncCallbackFunc callback, void *para); +}; +} // namespace OHOS +#endif // LNN_DECISION_DB_DEPS_MOCK_H diff --git a/tests/core/bus_center/lnn/net_ledger/lnn_decision_db_test.cpp b/tests/core/bus_center/lnn/net_ledger/lnn_decision_db_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..034470447afa3abac091c154d790c052ee342be1 --- /dev/null +++ b/tests/core/bus_center/lnn/net_ledger/lnn_decision_db_test.cpp @@ -0,0 +1,796 @@ +/* + * Copyright (c) 2022 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 +#include +#include +#include +#include + +#include "lnn_decision_db_deps_mock.h" +#include "lnn_decision_db.h" +#include "softbus_adapter_mem.h" +#include "softbus_def.h" +#include "softbus_errcode.h" +#include "softbus_log.h" + +namespace OHOS { +using namespace testing::ext; +constexpr char NODE_UDID[] = "123456ABCDEF"; +using namespace testing; +class DecisionDbTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void DecisionDbTest::SetUpTestCase() +{ +} + +void DecisionDbTest::TearDownTestCase() +{ +} + +void DecisionDbTest::SetUp() +{ + LOG_INFO("DecisionDbTest start."); +} + +void DecisionDbTest::TearDown() +{ + LOG_INFO("DecisionDbTest finish."); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_001 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_001, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_002 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_002, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_003 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_003, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_004 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_004, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_005 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_005, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_006 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_006, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_007 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_007, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDeleteKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_008 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_008, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, LnnGenerateRandomByHuks(_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_009 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_009, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGenerateRandomByHuks(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, LnnEncryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_010 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_010, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGenerateRandomByHuks(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnEncryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, SoftBusWriteFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_011 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_011, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_012 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_012, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDeleteKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_013 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_013, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDeleteKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGenerateRandomByHuks(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnEncryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusWriteFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, UpdateDbPassword(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_014 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_014, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDeleteKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGenerateRandomByHuks(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnEncryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusWriteFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, UpdateDbPassword(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, CheckTableExist(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_015 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_015, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDeleteKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGenerateRandomByHuks(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnEncryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusWriteFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, UpdateDbPassword(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, CheckTableExist(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, CreateTable(_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INIT_DECISION_DB_DELAY_Test_016 +* @tc.desc: lnn init decision db delay test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INIT_DECISION_DB_DELAY_Test_016, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGenerateKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDeleteKeyByHuks(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGenerateRandomByHuks(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnEncryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusWriteFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, UpdateDbPassword(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, CheckTableExist(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, CreateTable(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInitDecisionDbDelay(); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_001 +* @tc.desc: lnn insert specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_001, TestSize.Level1) +{ + int32_t ret = LnnInsertSpecificTrustedDevInfo(nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + ret = LnnInsertSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_002 +* @tc.desc: lnn insert specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_002, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInsertSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_003 +* @tc.desc: lnn insert specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_003, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)) + .WillRepeatedly(decisionDbMock.DecisionDbAsyncCallbackHelper); + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnInsertSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_004 +* @tc.desc: lnn insert specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_004, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)) + .WillRepeatedly(decisionDbMock.DecisionDbAsyncCallbackHelper); + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnInsertSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_005 +* @tc.desc: lnn insert specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_005, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)) + .WillRepeatedly(decisionDbMock.DecisionDbAsyncCallbackHelper); + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnInsertSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_006 +* @tc.desc: lnn insert specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_006, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)) + .WillRepeatedly(decisionDbMock.DecisionDbAsyncCallbackHelper); + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnInsertSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_007 +* @tc.desc: lnn insert specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_INSERT_SPECIFIC_TRUSTED_DEVICEINFO_Test_007, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)) + .WillRepeatedly(decisionDbMock.DecisionDbAsyncCallbackHelper); + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnInsertSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_001 +* @tc.desc: lnn delete specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_001, TestSize.Level1) +{ + int32_t ret = LnnDeleteSpecificTrustedDevInfo(nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + ret = LnnDeleteSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_002 +* @tc.desc: lnn delete specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_002, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnDeleteSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_003 +* @tc.desc: lnn delete specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_003, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)) + .WillRepeatedly(decisionDbMock.DecisionDbAsyncCallbackHelper); + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnDeleteSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_004 +* @tc.desc: lnn delete specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_004, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)) + .WillRepeatedly(decisionDbMock.DecisionDbAsyncCallbackHelper); + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnDeleteSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_005 +* @tc.desc: lnn delete specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_005, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)) + .WillRepeatedly(decisionDbMock.DecisionDbAsyncCallbackHelper); + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnDeleteSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_006 +* @tc.desc: lnn delete specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_006, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)) + .WillRepeatedly(decisionDbMock.DecisionDbAsyncCallbackHelper); + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnDeleteSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_007 +* @tc.desc: lnn delete specific trusted devInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_DELETE_SPECIFIC_TRUSTED_DEVICEINFO_Test_007, TestSize.Level1) +{ + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnAsyncCallbackHelper(_,_,_)) + .WillRepeatedly(decisionDbMock.DecisionDbAsyncCallbackHelper); + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnDeleteSpecificTrustedDevInfo(NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_GET_TRUSTED_DEVICEINFO_Test_001 +* @tc.desc: lnn get trusted devInfo from db test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_GET_TRUSTED_DEVICEINFO_Test_001, TestSize.Level1) +{ + char *udid = nullptr; + uint32_t num = 0; + int32_t ret = LnnGetTrustedDevInfoFromDb(nullptr, &num); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + ret = LnnGetTrustedDevInfoFromDb(&udid, &num); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_GET_TRUSTED_DEVICEINFO_Test_002 +* @tc.desc: lnn get trusted devInfo from db test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_GET_TRUSTED_DEVICEINFO_Test_002, TestSize.Level1) +{ + char *udid = nullptr; + uint32_t num = 0; + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnGetTrustedDevInfoFromDb(&udid, &num); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_GET_TRUSTED_DEVICEINFO_Test_003 +* @tc.desc: lnn get trusted devInfo from db test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_GET_TRUSTED_DEVICEINFO_Test_003, TestSize.Level1) +{ + char *udid = nullptr; + uint32_t num = 0; + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnGetTrustedDevInfoFromDb(&udid, &num); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_GET_TRUSTED_DEVICEINFO_Test_004 +* @tc.desc: lnn get trusted devInfo from db test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_GET_TRUSTED_DEVICEINFO_Test_004, TestSize.Level1) +{ + char *udid = nullptr; + uint32_t num = 0; + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnGetTrustedDevInfoFromDb(&udid, &num); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_GET_TRUSTED_DEVICEINFO_Test_005 +* @tc.desc: lnn get trusted devInfo from db test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_GET_TRUSTED_DEVICEINFO_Test_005, TestSize.Level1) +{ + char *udid = nullptr; + uint32_t num = 0; + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, GetRecordNumByKey(_,_,_)).WillRepeatedly(Return(0)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnGetTrustedDevInfoFromDb(&udid, &num); + EXPECT_TRUE(ret == SOFTBUS_OK); + if (ret == SOFTBUS_OK) { + SoftBusFree(udid); + udid = nullptr; + } +} + +/* +* @tc.name: LNN_GET_TRUSTED_DEVICEINFO_Test_006 +* @tc.desc: lnn get trusted devInfo from db test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_GET_TRUSTED_DEVICEINFO_Test_006, TestSize.Level1) +{ + char *udid = nullptr; + uint32_t num = 0; + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, GetRecordNumByKey(_,_,_)).WillRepeatedly(Return(2)); + EXPECT_CALL(decisionDbMock, QueryRecordByKey(_,_,_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + int32_t ret = LnnGetTrustedDevInfoFromDb(&udid, &num); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_GET_TRUSTED_DEVICEINFO_Test_007 +* @tc.desc: lnn get trusted devInfo from db test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DecisionDbTest, LNN_GET_TRUSTED_DEVICEINFO_Test_007, TestSize.Level1) +{ + char *udid = nullptr; + uint32_t num = 0; + DecisionDbDepsInterfaceMock decisionDbMock; + EXPECT_CALL(decisionDbMock, LnnGetLocalByteInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, ConvertBytesToHexString(_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, OpenDatabase(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnGetFullStoragePath(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusAccessFile(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, SoftBusReadFullFile(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, LnnDecryptDataByHuks(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, EncryptedDb(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, GetRecordNumByKey(_,_,_)).WillRepeatedly(Return(2)); + EXPECT_CALL(decisionDbMock, QueryRecordByKey(_,_,_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(decisionDbMock, CloseDatabase(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + int32_t ret = LnnGetTrustedDevInfoFromDb(&udid, &num); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} +} // namespace OHOS diff --git a/tests/core/bus_center/lnn/net_ledger/lnn_disctributed_net_ledger_test.cpp b/tests/core/bus_center/lnn/net_ledger/lnn_disctributed_net_ledger_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0eaff0a68f688a2967dd0bf19896310c58634b2c --- /dev/null +++ b/tests/core/bus_center/lnn/net_ledger/lnn_disctributed_net_ledger_test.cpp @@ -0,0 +1,528 @@ +/* + * Copyright (c) 2022 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 "lnn_distributed_net_ledger.h" + +#include +#include +#include +#include +#include + +#include "lnn_connection_addr_utils.h" +#include "lnn_fast_offline.h" +#include "lnn_map.h" +#include "lnn_node_info.h" +#include "softbus_adapter_mem.h" +#include "softbus_adapter_thread.h" +#include "softbus_bus_center.h" +#include "softbus_errcode.h" +#include "softbus_log.h" +#include "softbus_utils.h" +#include "softbus_hidumper_buscenter.h" +#include "bus_center_manager.h" + +namespace OHOS { +using namespace testing::ext; +constexpr char NODE1_DEVICE_NAME[] = "node1_test"; +constexpr char NODE1_UDID[] = "123456ABCDEF"; +constexpr char NODE1_NETWORK_ID[] = "235689BNHFCF"; +constexpr char NODE1_UUID[] = "235689BNHFCC"; +constexpr char NODE1_BT_MAC[] = "56789TTU"; +constexpr char NODE2_DEVICE_NAME[] = "node2_test"; +constexpr char NODE2_UDID[] = "123456ABCDEG"; +constexpr char NODE2_NETWORK_ID[] = "235689BNHFCG"; +constexpr char NODE2_UUID[] = "235689BNHFCD"; +constexpr char NODE2_BT_MAC[] = "56789TYU"; +constexpr char P2P_MAC[] = "11:22:33:44:55"; +constexpr char GO_MAC[] = "22:33:44:55:66"; +constexpr int32_t P2P_ROLE = 1; +constexpr uint32_t DISCOVERY_TYPE = 62; +constexpr int32_t AUTH_ID = 10; +constexpr uint64_t TIME_STAMP = 5000; +constexpr uint64_t CAPABILITY = 62; +constexpr uint64_t NEW_TIME_STAMP = 6000; +constexpr int64_t AUTH_SEQ = 1; +constexpr char NODE_ADDRESS[] = "address"; +constexpr char RECV_UDID_HASH[] = "87654321"; +constexpr int32_t INVALID_LANE_ID = -1; +using namespace testing; +class DisctributedLedgerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void DisctributedLedgerTest::SetUpTestCase() +{ +} + +void DisctributedLedgerTest::TearDownTestCase() +{ +} + +void DisctributedLedgerTest::SetUp() +{ + LOG_INFO("LocalLedgerTest start."); + int32_t ret = LnnInitDistributedLedger(); + EXPECT_TRUE(ret == SOFTBUS_OK); + NodeInfo info; + (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + info.discoveryType = DISCOVERY_TYPE; + (void)strcpy_s(info.uuid, UUID_BUF_LEN, NODE1_UUID); + (void)strcpy_s(info.deviceInfo.deviceUdid, UDID_BUF_LEN, NODE1_UDID); + (void)strcpy_s(info.networkId, NETWORK_ID_BUF_LEN, NODE1_NETWORK_ID); + (void)strcpy_s(info.connectInfo.macAddr, MAC_LEN, NODE1_BT_MAC); + info.authSeq[0] = AUTH_SEQ; + info.heartbeatTimeStamp = TIME_STAMP; + EXPECT_TRUE(REPORT_ONLINE == LnnAddOnlineNode(&info)); +} + +void DisctributedLedgerTest::TearDown() +{ + LOG_INFO("LocalLedgerTest end."); + LnnDeinitDistributedLedger(); +} + +/* +* @tc.name: LNN_ADD_ONLINE_NODE_Test_001 +* @tc.desc: lnn add online node test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_ADD_ONLINE_NODE_Test_001, TestSize.Level1) +{ + NodeInfo info; + (void)memset_s(&info, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + info.discoveryType = DISCOVERY_TYPE; + (void)strcpy_s(info.uuid, UUID_BUF_LEN, NODE1_UUID); + (void)strcpy_s(info.deviceInfo.deviceUdid, UDID_BUF_LEN, NODE1_UDID); + (void)strcpy_s(info.networkId, NETWORK_ID_BUF_LEN, NODE1_NETWORK_ID); + (void)strcpy_s(info.connectInfo.macAddr, MAC_LEN, NODE1_BT_MAC); + EXPECT_TRUE(REPORT_NONE == LnnAddOnlineNode(&info)); +} + +/* +* @tc.name: LNN_GET_REMOTE_STRINFO_Test_001 +* @tc.desc: lnn get remote strInfo test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_REMOTE_STRINFO_Test_001, TestSize.Level1) +{ + static InfoKey keyStringTable[] = { + STRING_KEY_HICE_VERSION, + STRING_KEY_DEV_UDID, + STRING_KEY_UUID, + STRING_KEY_DEV_TYPE, + STRING_KEY_DEV_NAME, + STRING_KEY_BT_MAC, + STRING_KEY_WLAN_IP, + STRING_KEY_MASTER_NODE_UDID, + STRING_KEY_P2P_MAC, + STRING_KEY_P2P_GO_MAC, + STRING_KEY_NODE_ADDR, + STRING_KEY_OFFLINE_CODE + }; + char buf[UDID_BUF_LEN] = {0}; + int32_t ret = LnnGetRemoteStrInfo(nullptr, STRING_KEY_HICE_VERSION, buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnGetRemoteStrInfo(NODE1_NETWORK_ID, STRING_KEY_HICE_VERSION, nullptr, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnGetRemoteStrInfo(NODE1_NETWORK_ID, NUM_KEY_BEGIN, buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + uint32_t i; + for (i = 0; i < sizeof(keyStringTable) / sizeof(InfoKey); i++) { + (void)memset_s(buf, UDID_BUF_LEN, 0, UDID_BUF_LEN); + ret = LnnGetRemoteStrInfo(NODE1_NETWORK_ID, keyStringTable[i], buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_OK); + } + for (i = 0; i < sizeof(keyStringTable) / sizeof(InfoKey); i++) { + ret = LnnGetRemoteStrInfo(NODE2_NETWORK_ID, keyStringTable[i], buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_ERR); + } +} + +/* +* @tc.name: LNN_GET_REMOTE_NUMNFO_Test_002 +* @tc.desc: lnn get remote num info test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_REMOTE_NUMNFO_Test_002, TestSize.Level1) +{ + static InfoKey keyNumTable[] = { + NUM_KEY_META_NODE, + NUM_KEY_SESSION_PORT, + NUM_KEY_AUTH_PORT, + NUM_KEY_PROXY_PORT, + NUM_KEY_NET_CAP, + NUM_KEY_DISCOVERY_TYPE, + NUM_KEY_MASTER_NODE_WEIGHT, + NUM_KEY_P2P_ROLE + }; + int32_t ret; + uint32_t i; + int32_t len = LNN_COMMON_LEN; + for (i = 0; i < sizeof(keyNumTable) / sizeof(InfoKey); i++) { + ret = LnnGetRemoteNumInfo(NODE1_NETWORK_ID, keyNumTable[i], &len); + EXPECT_TRUE(ret == SOFTBUS_OK); + } + for (i = 0; i < sizeof(keyNumTable) / sizeof(InfoKey); i++) { + ret = LnnGetRemoteNumInfo(NODE2_NETWORK_ID, keyNumTable[i], &len); + EXPECT_TRUE(ret == SOFTBUS_ERR); + } +} + +/* +* @tc.name: LNN_GET_CNN_CODE_Test_001 +* @tc.desc: lnn get cnn code test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_CNN_CODE_Test_001, TestSize.Level1) +{ + DiscoveryType type = DISCOVERY_TYPE_WIFI; + short ret = LnnGetCnnCode(nullptr, type); + EXPECT_TRUE(ret == INVALID_CONNECTION_CODE_VALUE); + ret = LnnGetCnnCode(NODE1_UUID, type); + EXPECT_TRUE(ret == INVALID_CONNECTION_CODE_VALUE); + ret = LnnGetCnnCode(NODE2_UUID, type); + EXPECT_TRUE(ret == INVALID_CONNECTION_CODE_VALUE); +} + +/* +* @tc.name: LNN_UPDATE_NODE_INFO_Test_001 +* @tc.desc: lnn update node info test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_UPDATE_NODE_INFO_Test_001, TestSize.Level1) +{ + NodeInfo newInfo; + (void)memset_s(&newInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + (void)strcpy_s(newInfo.deviceInfo.deviceUdid, UDID_BUF_LEN, NODE1_UDID); + int32_t ret = LnnUpdateNodeInfo(&newInfo); + EXPECT_TRUE(ret == SOFTBUS_OK); + (void)memset_s(&newInfo, sizeof(NodeInfo), 0, sizeof(NodeInfo)); + (void)strcpy_s(newInfo.deviceInfo.deviceUdid, UDID_BUF_LEN, NODE2_UDID); + ret = LnnUpdateNodeInfo(&newInfo); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_SET_NODE_OFFLINE_Test_001 +* @tc.desc: lnn set node offline test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_SET_NODE_OFFLINE_Test_001, TestSize.Level1) +{ + EXPECT_TRUE(REPORT_NONE == LnnSetNodeOffline(NODE1_UUID, CONNECTION_ADDR_WLAN, AUTH_ID)); + EXPECT_TRUE(REPORT_NONE == LnnSetNodeOffline(NODE2_UUID, CONNECTION_ADDR_WLAN, AUTH_ID)); +} + +/* +* @tc.name: LNN_GET_BASIC_INFO_BY_UDID_Test_001 +* @tc.desc: lnn get basic info by udid test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_BASIC_INFO_BY_UDID_Test_001, TestSize.Level1) +{ + NodeBasicInfo basicInfo; + (void)memset_s(&basicInfo, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo)); + int32_t ret = LnnGetBasicInfoByUdid(NODE1_UDID, nullptr); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnGetBasicInfoByUdid(NODE1_UDID, &basicInfo); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_CONVERT_DLID_Test_001 +* @tc.desc: lnn convert dl id test +* @tc.type: FUNC +* @tc.require: +*/ + +HWTEST_F(DisctributedLedgerTest, LNN_CONVERT_DLID_Test_001, TestSize.Level1) +{ + char buf[UDID_BUF_LEN] = {0}; + int32_t ret = LnnConvertDlId(nullptr, CATEGORY_UDID, CATEGORY_UDID, buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnConvertDlId(NODE1_UDID, CATEGORY_UDID, CATEGORY_UDID, buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnConvertDlId(NODE2_UDID, CATEGORY_UDID, CATEGORY_UDID, buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_NOT_FIND); + ret = LnnConvertDlId(NODE2_UUID, CATEGORY_UUID, CATEGORY_UUID, buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_NOT_FIND); + ret = LnnConvertDlId(NODE2_NETWORK_ID, CATEGORY_NETWORK_ID, CATEGORY_NETWORK_ID, buf, NETWORK_ID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_NOT_FIND); +} + +/* +* @tc.name: LNN_SET_DLP2P_INFO_Test_001 +* @tc.desc: lnn get lnn relation test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_SET_DLP2P_INFO_Test_001, TestSize.Level1) +{ + P2pInfo info; + (void)memset_s(&info, sizeof(P2pInfo), 0, sizeof(P2pInfo)); + (void)strcpy_s(info.p2pMac, MAC_LEN, P2P_MAC); + (void)strcpy_s(info.goMac, MAC_LEN, GO_MAC); + info.p2pRole = P2P_ROLE; + bool ret = LnnSetDLP2pInfo(nullptr, &info); + EXPECT_TRUE(ret == false); + ret = LnnSetDLP2pInfo(NODE1_NETWORK_ID, &info); + EXPECT_TRUE(ret == true); + ret = LnnSetDLP2pInfo(NODE2_NETWORK_ID, &info); + EXPECT_TRUE(ret == false); +} + +/* +* @tc.name: LNN_GET_NETWORKID_BYBTMAC_Test_001 +* @tc.desc: lnn get neteorkId by bt mac test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_NETWORKID_BYBTMAC_Test_001, TestSize.Level1) +{ + char buf[NETWORK_ID_BUF_LEN] = {0}; + int32_t ret = LnnGetNetworkIdByBtMac(nullptr, buf, NETWORK_ID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnGetNetworkIdByBtMac(NODE1_BT_MAC, buf, NETWORK_ID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnGetNetworkIdByBtMac(NODE2_BT_MAC, buf, NETWORK_ID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_GET_NETWORKID_BY_UUID_Test_001 +* @tc.desc: lnn get neteorkId by uuid test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_NETWORKID_BY_UUID_Test_001, TestSize.Level1) +{ + char buf[UUID_BUF_LEN] = {0}; + int32_t ret = LnnGetNetworkIdByUuid(nullptr, buf, UUID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnGetNetworkIdByUuid(NODE1_UUID, buf, NETWORK_ID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnGetNetworkIdByUuid(NODE2_UUID, buf, NETWORK_ID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_GET_NETWORKID_BY_UDID_Test_001 +* @tc.desc: lnn get neteorkId by udid test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_NETWORKID_BY_UDID_Test_001, TestSize.Level1) +{ + char buf[UDID_BUF_LEN] = {0}; + int32_t ret = LnnGetNetworkIdByUdid(nullptr, buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnGetNetworkIdByUdid(NODE1_UDID, buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnGetNetworkIdByUdid(NODE2_UDID, buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_GET_ALL_AUTH_SEQ_Test_001 +* @tc.desc: lnn get all auth seq test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_ALL_AUTH_SEQ_Test_001, TestSize.Level1) +{ + int64_t authSeq[DISCOVERY_TYPE_COUNT] = {0}; + int32_t ret = LnnGetAllAuthSeq(nullptr, authSeq, DISCOVERY_TYPE_COUNT); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnGetAllAuthSeq(NODE1_UDID, authSeq, DISCOVERY_TYPE_COUNT); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnGetAllAuthSeq(NODE2_UDID, authSeq, DISCOVERY_TYPE_COUNT); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_GET_DLHEARTBEAT_TIMER_STAMP_Test_001 +* @tc.desc: lnn get dl heartbeat time stamp test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_DLHEARTBEAT_TIMER_STAMP_Test_001, TestSize.Level1) +{ + uint64_t timeStamp; + int32_t ret = LnnGetDLHeartbeatTimestamp(NODE1_NETWORK_ID, &timeStamp); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnGetDLHeartbeatTimestamp(NODE2_NETWORK_ID, &timeStamp); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_SET_DLHEARTBEAT_TIMER_STAMP_Test_001 +* @tc.desc: lnn set dl heartbeat time stamp test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_SET_DLHEARTBEAT_TIMER_STAMP_Test_001, TestSize.Level1) +{ + uint64_t timeStamp = NEW_TIME_STAMP; + int32_t ret = LnnSetDLHeartbeatTimestamp(NODE1_NETWORK_ID, timeStamp); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetDLHeartbeatTimestamp(NODE2_NETWORK_ID, timeStamp); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_SET_DLCONN_CAPABILITY_Test_001 +* @tc.desc: lnn set dl conn capability test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_SET_DLCONN_CAPABILITY_Test_001, TestSize.Level1) +{ + uint64_t connCapability = CAPABILITY; + int32_t ret = LnnSetDLConnCapability(NODE1_NETWORK_ID, connCapability); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetDLConnCapability(NODE2_NETWORK_ID, connCapability); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_SET_DLNODE_ADDR_Test_001 +* @tc.desc: lnn set dl node addr test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_SET_DLNODE_ADDR_Test_001, TestSize.Level1) +{ + int32_t ret = LnnSetDLNodeAddr(NODE1_NETWORK_ID, CATEGORY_NETWORK_ID, NODE_ADDRESS); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetDLNodeAddr(NODE2_NETWORK_ID, CATEGORY_NETWORK_ID, NODE_ADDRESS); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_GET_ONLINE_NODE_BY_UDID_HASH_Test_001 +* @tc.desc: lnn get online node by udid hash test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_ONLINE_NODE_BY_UDID_HASH_Test_001, TestSize.Level1) +{ + EXPECT_TRUE(LnnGetOnlineNodeByUdidHash(RECV_UDID_HASH) == nullptr); +} + +/* +* @tc.name: LNN_REFRESH_DEVICE_ONLINE_STATE_AND_DEVICE_INFO_Test_001 +* @tc.desc: lnn refresh device online state and device info test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_REFRESH_DEVICE_ONLINE_STATE_AND_DEVICE_INFO_Test_001, TestSize.Level1) +{ + DeviceInfo device; + InnerDeviceInfoAddtions addtions; + (void)memset_s(&device, sizeof(DeviceInfo), 0, sizeof(DeviceInfo)); + (void)strcpy_s(device.devId, DISC_MAX_DEVICE_ID_LEN, NODE1_UDID); + addtions.medium = COAP; + LnnRefreshDeviceOnlineStateAndDevIdInfo(nullptr, &device, &addtions); + (void)memset_s(&device, sizeof(DeviceInfo), 0, sizeof(DeviceInfo)); + (void)strcpy_s(device.devId, DISC_MAX_DEVICE_ID_LEN, RECV_UDID_HASH); + addtions.medium = BLE; + LnnRefreshDeviceOnlineStateAndDevIdInfo(nullptr, &device, &addtions); +} + +/* +* @tc.name: LNN_GET_DATA_CHANGE_FLAG_Test_001 +* @tc.desc: lnn get data change flag test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_DATA_CHANGE_FLAG_Test_001, TestSize.Level1) +{ + int16_t info = 0; + int32_t ret = LnnGetRemoteNum16Info(NODE1_NETWORK_ID, NUM_KEY_DATA_CHANGE_FLAG, &info); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnGetRemoteNum16Info(NODE2_NETWORK_ID, NUM_KEY_DATA_CHANGE_FLAG, &info); + EXPECT_TRUE(ret == SOFTBUS_ERR); +} + +/* +* @tc.name: LNN_CONVERT_DLID_TO_UDID_Test_001 +* @tc.desc: lnn convert dlid to udid test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_CONVERT_DLID_TO_UDID_Test_001, TestSize.Level1) +{ + EXPECT_TRUE(LnnConvertDLidToUdid(nullptr, CATEGORY_NETWORK_ID) == nullptr); + LnnConvertDLidToUdid(NODE1_NETWORK_ID, CATEGORY_NETWORK_ID); + EXPECT_TRUE(LnnConvertDLidToUdid(NODE2_NETWORK_ID, CATEGORY_NETWORK_ID) == nullptr); +} + +/* +* @tc.name: LNN_GET_LNN_RELATION_Test_001 +* @tc.desc: lnn get lnn relation test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_LNN_RELATION_Test_001, TestSize.Level1) +{ + uint8_t relation[CONNECTION_ADDR_MAX] = {0}; + int32_t ret = LnnGetLnnRelation(nullptr, CATEGORY_UDID, relation, CONNECTION_ADDR_MAX); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnGetLnnRelation(NODE1_UDID, CATEGORY_UDID, relation, CONNECTION_ADDR_MAX); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnGetLnnRelation(NODE2_UDID, CATEGORY_UDID, relation, CONNECTION_ADDR_MAX); + EXPECT_TRUE(ret == SOFTBUS_NOT_FIND); +} + +/* +* @tc.name: LNN_SET_DL_DEVICE_INFO_NAME_Test_001 +* @tc.desc: lnn set dl device info name test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_SET_DL_DEVICE_INFO_NAME_Test_001, TestSize.Level1) +{ + bool ret = LnnSetDLDeviceInfoName(nullptr, nullptr); + EXPECT_TRUE(ret == false); + ret = LnnSetDLDeviceInfoName(NODE1_UDID, NODE1_DEVICE_NAME); + EXPECT_TRUE(ret == true); + ret = LnnSetDLDeviceInfoName(NODE2_UDID, NODE2_DEVICE_NAME); + EXPECT_TRUE(ret == false); +} + +/* +* @tc.name: LNN_GET_AND_SET_LANE_COUNT_Test_001 +* @tc.desc: lnn get and set lane count test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DisctributedLedgerTest, LNN_GET_AND_SET_LANE_COUNT_Test_001, TestSize.Level1) +{ + EXPECT_TRUE(LnnGetLaneCount(INVALID_LANE_ID) == SOFTBUS_ERR); + EXPECT_TRUE(LnnSetLaneCount(INVALID_LANE_ID, AUTH_ID) == SOFTBUS_ERR); +} +} // namespace OHOS diff --git a/tests/core/bus_center/lnn/net_ledger/lnn_ledger_common_test.cpp b/tests/core/bus_center/lnn/net_ledger/lnn_ledger_common_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16e1a962254591417651b67641ad48d0b24e7d56 --- /dev/null +++ b/tests/core/bus_center/lnn/net_ledger/lnn_ledger_common_test.cpp @@ -0,0 +1,283 @@ +/* + * Copyright (c) 2022 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 +#include +#include +#include +#include +#include + +#include "bus_center_manager.h" +#include "lnn_decision_db.h" +#include "lnn_device_info.h" +#include "lnn_distributed_net_ledger.h" +#include "lnn_huks_utils.h" +#include "lnn_local_net_ledger.h" +#include "lnn_meta_node_ledger.h" +#include "lnn_net_capability.h" +#include "lnn_net_ledger.h" +#include "lnn_node_info.h" +#include "softbus_adapter_mem.h" +#include "softbus_errcode.h" +#include "softbus_feature_config.h" +#include "softbus_log.h" +#include "softbus_utils.h" + +#define ONE_BIT_MAX_HEX 15 +#define DEVICE_TYPE_MAX_LENGTH 3 +#define LEFT_SHIFT_DEVICE_TYPE_LENGTH (DEVICE_TYPE_MAX_LENGTH * 4) + +namespace OHOS { +using namespace testing::ext; +constexpr char NODE_DEVICE_NAME[] = "node1_test"; +constexpr char INVALID_DEVICE_NAME[] = + "ASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJK\ + LPASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJ\ + KLPASDFGHJKLPASDFGHJKLPASDFGHJKLPASDFGHJKLP"; +constexpr char DEVICE_TYPE[] = "PAD"; +constexpr char INVALID_DEVICE_TYPE[] = "PADPAD"; +constexpr int32_t PORT = 1; +constexpr uint64_t PROTOCOLS = 1; +constexpr char LOCAL_NETWORKID[] = "123456LOCAL"; +constexpr char REMOTE_NETWORKID[] = "234567REMOTE"; +constexpr uint32_t BUF_LEN = 128; +constexpr int32_t KEY_MAX_INDEX = 11; +constexpr uint16_t DATA_CHANGE_FLAG = 1; +constexpr char LOCAL_UDID[] = "123456LOCALTEST"; +constexpr char LOCAL_UUID[] = "235999LOCAL"; +constexpr char LOCAL_BT_MAC[] = "56789TUT"; +constexpr char LOCAL_WLAN_IP[] = "10.146.181.134"; +constexpr int32_t DEFAULT_FD = 1; +using namespace testing; +class NetLedgerCommonTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NetLedgerCommonTest::SetUpTestCase() +{ +} + +void NetLedgerCommonTest::TearDownTestCase() +{ +} + +void NetLedgerCommonTest::SetUp() +{ + LOG_INFO("NetLedgerCommonTest start."); +} + +void NetLedgerCommonTest::TearDown() +{ +} + +/* +* @tc.name: LNN_DEVICE_INFO_Test_001 +* @tc.desc: lnn device info function test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(NetLedgerCommonTest, LNN_DEVICE_INFO_Test_001, TestSize.Level1) +{ + DeviceBasicInfo info; + uint16_t typeId = 0; + int32_t ret = memset_s(&info, sizeof(DeviceBasicInfo), 0, sizeof(DeviceBasicInfo)); + EXPECT_TRUE(ret == EOK); + EXPECT_TRUE(LnnGetDeviceName(nullptr) == NULL); + LnnGetDeviceName(&info); + EXPECT_TRUE(LnnSetDeviceName(nullptr, NODE_DEVICE_NAME) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnSetDeviceName(&info, nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnSetDeviceName(&info, INVALID_DEVICE_NAME) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnSetDeviceName(&info, NODE_DEVICE_NAME) == SOFTBUS_OK); + EXPECT_TRUE(LnnGetDeviceTypeId(nullptr, &typeId) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnGetDeviceTypeId(&info, nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnGetDeviceTypeId(&info, &typeId) == SOFTBUS_OK); + EXPECT_TRUE(LnnConvertDeviceTypeToId(nullptr, nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnConvertDeviceTypeToId(INVALID_DEVICE_TYPE, &typeId) == SOFTBUS_ERR); + EXPECT_TRUE(LnnConvertDeviceTypeToId(DEVICE_TYPE, &typeId) == SOFTBUS_OK); + typeId = 0; + LnnConvertIdToDeviceType(TYPE_WATCH_ID); + LnnConvertIdToDeviceType(typeId); + typeId = ONE_BIT_MAX_HEX << LEFT_SHIFT_DEVICE_TYPE_LENGTH; + EXPECT_TRUE(LnnConvertIdToDeviceType(typeId) != nullptr); +} + +/* +* @tc.name: LNN_HUKS_UTILS_Test_001 +* @tc.desc: lnn huks utils function test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(NetLedgerCommonTest, LNN_HUKS_UTILS_Test_001, TestSize.Level1) +{ + struct HksBlob keyAlias; + (void)memset_s(&keyAlias, sizeof(HksBlob), 0, sizeof(HksBlob)); + EXPECT_TRUE(LnnGenerateKeyByHuks(nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnGenerateKeyByHuks(&keyAlias) == SOFTBUS_ERR); + EXPECT_TRUE(LnnDeleteKeyByHuks(nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnDeleteKeyByHuks(&keyAlias) == SOFTBUS_OK); + EXPECT_TRUE(LnnEncryptDataByHuks(nullptr, nullptr, nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnDecryptDataByHuks(nullptr, nullptr, nullptr) == SOFTBUS_INVALID_PARAM); +} + +/* +* @tc.name: LNN_NET_CAPABILITY_Test_001 +* @tc.desc: lnn net capability function test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(NetLedgerCommonTest, LNN_NET_CAPABILITY_Test_001, TestSize.Level1) +{ + uint32_t capability = 0; + EXPECT_TRUE(LnnSetNetCapability(nullptr, BIT_COUNT) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnClearNetCapability(nullptr, BIT_COUNT) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnClearNetCapability(&capability, BIT_BLE) == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_NODE_INFO_Test_001 +* @tc.desc: lnn node info function test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(NetLedgerCommonTest, LNN_NODE_INFO_Test_001, TestSize.Level1) +{ + EXPECT_TRUE(LnnHasDiscoveryType(nullptr, DISCOVERY_TYPE_WIFI) == false); + EXPECT_TRUE(LnnGetDeviceUdid(nullptr) == nullptr); + EXPECT_TRUE(LnnSetDeviceUdid(nullptr, nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnSetDiscoveryType(nullptr, DISCOVERY_TYPE_WIFI) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnClearDiscoveryType(nullptr, DISCOVERY_TYPE_WIFI) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnIsNodeOnline(nullptr) == false); + LnnSetNodeConnStatus(nullptr, STATUS_ONLINE); + LnnGetBtMac(nullptr); + LnnSetBtMac(nullptr, nullptr); + LnnGetNetIfName(nullptr); + LnnSetNetIfName(nullptr, nullptr); + LnnGetWiFiIp(nullptr); + LnnSetWiFiIp(nullptr, nullptr); + EXPECT_TRUE(LnnGetMasterUdid(nullptr) == nullptr); + EXPECT_TRUE(LnnSetMasterUdid(nullptr, nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnGetAuthPort(nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnSetAuthPort(nullptr, PORT) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnGetSessionPort(nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnSetSessionPort(nullptr, PORT) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnGetProxyPort(nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnSetProxyPort(nullptr, PORT) == SOFTBUS_OK); + EXPECT_TRUE(LnnSetP2pRole(nullptr, PORT) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnGetP2pRole(nullptr) == 0); + EXPECT_TRUE(LnnSetP2pMac(nullptr, nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnGetP2pMac(nullptr) == nullptr); + EXPECT_TRUE(LnnSetP2pGoMac(nullptr, nullptr) == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnGetP2pGoMac(nullptr) == nullptr); + EXPECT_TRUE(LnnGetSupportedProtocols(nullptr) == 0); + EXPECT_TRUE(LnnSetSupportedProtocols(nullptr, PROTOCOLS) == SOFTBUS_OK); +} + +/* +* @tc.name: LNN_NET_LEDGER_Test_001 +* @tc.desc: lnn net ledger function test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(NetLedgerCommonTest, LNN_NET_LEDGER_Test_001, TestSize.Level1) +{ + int32_t i; + EXPECT_TRUE(LnnInitNetLedger() == SOFTBUS_OK); + EXPECT_TRUE(LnnSetLocalStrInfo(STRING_KEY_NETWORKID, LOCAL_NETWORKID) == SOFTBUS_OK); + uint8_t info[BUF_LEN] = {0}; + EXPECT_TRUE(LnnGetNodeKeyInfo(nullptr, 0, info, BUF_LEN) == SOFTBUS_ERR); + EXPECT_TRUE(LnnGetNodeKeyInfo(LOCAL_NETWORKID, 0, info, BUF_LEN) == SOFTBUS_ERR); + EXPECT_TRUE(LnnGetNodeKeyInfo(LOCAL_NETWORKID, KEY_MAX_INDEX - 1, info, BUF_LEN) == SOFTBUS_ERR); + for (i = 1; i < KEY_MAX_INDEX - 1; i++) { + EXPECT_TRUE(LnnGetNodeKeyInfo(LOCAL_NETWORKID, i, info, BUF_LEN) == SOFTBUS_OK); + } + for (i = 0; i < KEY_MAX_INDEX; i++) { + LnnGetNodeKeyInfo(REMOTE_NETWORKID, i, info, BUF_LEN); + } + LnnDeinitNetLedger(); +} + +/* +* @tc.name: LNN_NET_LEDGER_Test_002 +* @tc.desc: lnn net ledger function test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(NetLedgerCommonTest, LNN_NET_LEDGER_Test_002, TestSize.Level1) +{ + EXPECT_TRUE(LnnInitNetLedger() == SOFTBUS_OK); + EXPECT_TRUE(LnnSetLocalStrInfo(STRING_KEY_NETWORKID, LOCAL_NETWORKID) == SOFTBUS_OK); + EXPECT_TRUE(LnnSetNodeDataChangeFlag(nullptr, DATA_CHANGE_FLAG) == SOFTBUS_ERR); + EXPECT_TRUE(LnnSetNodeDataChangeFlag(LOCAL_NETWORKID, DATA_CHANGE_FLAG) == SOFTBUS_OK); + EXPECT_TRUE(LnnSetNodeDataChangeFlag(REMOTE_NETWORKID, DATA_CHANGE_FLAG) == SOFTBUS_ERR); + LnnDeinitNetLedger(); +} + +/* +* @tc.name: LNN_NET_LEDGER_Test_003 +* @tc.desc: lnn net ledger function test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(NetLedgerCommonTest, LNN_NET_LEDGER_Test_003, TestSize.Level1) +{ + static int32_t nodeKeyInfoLenTable[] = { + UDID_BUF_LEN, + UUID_BUF_LEN, + UDID_BUF_LEN, + MAC_LEN, + IP_LEN, + DEVICE_NAME_BUF_LEN, + LNN_COMMON_LEN, + LNN_COMMON_LEN, + SOFTBUS_ERR, + DATA_CHANGE_FLAG_BUF_LEN, + SOFTBUS_ERR + }; + EXPECT_TRUE(LnnInitNetLedger() == SOFTBUS_OK); + for (int32_t i = 0; i < KEY_MAX_INDEX; i++) { + EXPECT_TRUE(LnnGetNodeKeyInfoLen(i) == nodeKeyInfoLenTable[i]); + } + LnnDeinitNetLedger(); +} + +/* +* @tc.name: LNN_NET_LEDGER_Test_004 +* @tc.desc: lnn net ledger function test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(NetLedgerCommonTest, LNN_NET_LEDGER_Test_004, TestSize.Level1) +{ + EXPECT_TRUE(LnnInitNetLedger() == SOFTBUS_OK); + EXPECT_TRUE(LnnSetLocalStrInfo(STRING_KEY_DEV_UDID, LOCAL_UDID) == SOFTBUS_OK); + EXPECT_TRUE(LnnSetLocalStrInfo(STRING_KEY_NETWORKID, LOCAL_NETWORKID) == SOFTBUS_OK); + EXPECT_TRUE(LnnSetLocalStrInfo(STRING_KEY_UUID, LOCAL_UUID) == SOFTBUS_OK); + EXPECT_TRUE(LnnSetLocalStrInfo(STRING_KEY_BT_MAC, LOCAL_BT_MAC) == SOFTBUS_OK); + EXPECT_TRUE(LnnSetLocalStrInfo(STRING_KEY_WLAN_IP, LOCAL_WLAN_IP) == SOFTBUS_OK); + EXPECT_TRUE(LnnSetLocalNumInfo(NUM_KEY_NET_CAP, 1 << BIT_BR) == SOFTBUS_OK); + NodeBasicInfo nodeInfo; + (void)memset_s(&nodeInfo, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo)); + (void)strcpy_s(nodeInfo.deviceName, DEVICE_NAME_BUF_LEN, NODE_DEVICE_NAME); + (void)strcpy_s(nodeInfo.networkId, NETWORK_ID_BUF_LEN, LOCAL_NETWORKID); + SoftBusDumpBusCenterPrintInfo(DEFAULT_FD, nullptr); + SoftBusDumpBusCenterPrintInfo(DEFAULT_FD, &nodeInfo); + LnnDeinitNetLedger(); +} +} // namespace OHOS \ No newline at end of file diff --git a/tests/core/bus_center/lnn/net_ledger/lnn_local_ledger_deps_mock.cpp b/tests/core/bus_center/lnn/net_ledger/lnn_local_ledger_deps_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97fc6113958ae483abfed1b85ae3b29cba24f4d0 --- /dev/null +++ b/tests/core/bus_center/lnn/net_ledger/lnn_local_ledger_deps_mock.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2022 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 "lnn_local_ledger_deps_mock.h" + +#include + +#include "softbus_common.h" +#include "softbus_error_code.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +void *g_localLedgerDepsInterface; +constexpr char DEFAULT_DEVICE_NAME[] = "OpenHarmony"; +constexpr char DEFAULT_DEVICE_UDID[] = "aaabbbcccdddeeefffggghhh"; +constexpr char DEFAULT_DEVICE_TYPE[] = "default_type"; +constexpr int32_t SOFTBUS_BUSCENTER_DUMP_LOCALDEVICEINFO_FD = -1; + +LocalLedgerDepsInterfaceMock::LocalLedgerDepsInterfaceMock() +{ + g_localLedgerDepsInterface = reinterpret_cast(this); +} + +LocalLedgerDepsInterfaceMock::~LocalLedgerDepsInterfaceMock() +{ + g_localLedgerDepsInterface = nullptr; +} + +static LocalLedgerDepsInterfaceMock *GetLocalLedgerDepsInterface() +{ + return reinterpret_cast(g_localLedgerDepsInterface); +} + +int32_t LocalLedgerDepsInterfaceMock::LedgerGetCommonDevInfo(const CommonDeviceKey key, + char *value, uint32_t len) +{ + if (value == nullptr) { + return SOFTBUS_ERR; + } + switch (key) { + case COMM_DEVICE_KEY_DEVNAME: + if (strcpy_s(value, len, DEFAULT_DEVICE_NAME) != EOK) { + return SOFTBUS_ERR; + } + break; + case COMM_DEVICE_KEY_UDID: + if (strcpy_s(value, len, DEFAULT_DEVICE_UDID) != EOK) { + return SOFTBUS_ERR; + } + break; + case COMM_DEVICE_KEY_DEVTYPE: + if (strcpy_s(value, len, DEFAULT_DEVICE_TYPE) != EOK) { + return SOFTBUS_ERR; + } + break; + default: + break; + } + return SOFTBUS_OK; +} + +int32_t LocalLedgerDepsInterfaceMock::LedgerSoftBusRegBusCenterVarDump(char *dumpVar, SoftBusVarDumpCb cb) +{ + int32_t ret = SOFTBUS_ERR; + if (cb != nullptr) { + ret = cb(SOFTBUS_BUSCENTER_DUMP_LOCALDEVICEINFO_FD); + } + return ret; +} + +extern "C" { +uint32_t LnnGetNetCapabilty(void) +{ + return GetLocalLedgerDepsInterface()->LnnGetNetCapabilty(); +} + +int32_t SoftBusGenerateRandomArray(unsigned char *randStr, uint32_t len) +{ + return GetLocalLedgerDepsInterface()->SoftBusGenerateRandomArray(randStr, len); +} + +int32_t GetCommonDevInfo(const CommonDeviceKey key, char *value, uint32_t len) +{ + return GetLocalLedgerDepsInterface()->GetCommonDevInfo(key, value, len); +} + +int32_t LnnInitLocalP2pInfo(NodeInfo *info) +{ + return GetLocalLedgerDepsInterface()->LnnInitLocalP2pInfo(info); +} + +int32_t SoftBusRegBusCenterVarDump(char *dumpVar, SoftBusVarDumpCb cb) +{ + return GetLocalLedgerDepsInterface()->SoftBusRegBusCenterVarDump(dumpVar, cb); +} + +int32_t LnnInitOhosAccount(void) +{ + return GetLocalLedgerDepsInterface()->LnnInitOhosAccount(); +} +} // extern "C" +} // namespace OHOS diff --git a/tests/core/bus_center/lnn/net_ledger/lnn_local_ledger_deps_mock.h b/tests/core/bus_center/lnn/net_ledger/lnn_local_ledger_deps_mock.h new file mode 100644 index 0000000000000000000000000000000000000000..be38bfbead80addd2929f5c45ce5c3fa5b0819bd --- /dev/null +++ b/tests/core/bus_center/lnn/net_ledger/lnn_local_ledger_deps_mock.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 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. + */ + +#ifndef LNN_LOCAL_LEDGER_DEPS_MOCK_H +#define LNN_LOCAL_LEDGER_DEPS_MOCK_H + +#include +#include + +#include "lnn_device_info.h" +#include "lnn_net_capability.h" +#include "softbus_adapter_crypto.h" +#include "bus_center_adapter.h" +#include "lnn_p2p_info.h" +#include "softbus_adapter_thread.h" +#include "softbus_hidumper_buscenter.h" +#include "lnn_ohos_account.h" + +namespace OHOS { +class LocalLedgerDepsInterface { +public: + LocalLedgerDepsInterface() {}; + virtual ~LocalLedgerDepsInterface() {}; + + virtual uint32_t LnnGetNetCapabilty(void); + virtual int32_t SoftBusGenerateRandomArray(unsigned char *randStr, uint32_t len); + virtual int32_t GetCommonDevInfo(const CommonDeviceKey key, char *value, uint32_t len); + virtual int32_t LnnInitLocalP2pInfo(NodeInfo *info); + virtual int32_t SoftBusRegBusCenterVarDump(char *dumpVar, SoftBusVarDumpCb cb); + virtual int32_t LnnInitOhosAccount(void); +}; +class LocalLedgerDepsInterfaceMock : public LocalLedgerDepsInterface { +public: + LocalLedgerDepsInterfaceMock(); + ~LocalLedgerDepsInterfaceMock() override; + MOCK_METHOD0(LnnGetNetCapabilty, uint32_t ()); + MOCK_METHOD2(SoftBusGenerateRandomArray, int32_t (unsigned char *, uint32_t)); + MOCK_METHOD3(GetCommonDevInfo, int32_t (const CommonDeviceKey, char *, uint32_t)); + MOCK_METHOD1(LnnInitLocalP2pInfo, int32_t (NodeInfo *info)); + MOCK_METHOD2(SoftBusRegBusCenterVarDump, int32_t (char *, SoftBusVarDumpCb)); + MOCK_METHOD0(LnnInitOhosAccount, int32_t ()); + + static int32_t LedgerGetCommonDevInfo(const CommonDeviceKey key, char *value, uint32_t len); + static int32_t LedgerSoftBusRegBusCenterVarDump(char *dumpVar, SoftBusVarDumpCb cb); +}; +} // namespace OHOS +#endif // LNN_LOCAL_LEDGER_DEPS_MOCK_H diff --git a/tests/core/bus_center/lnn/net_ledger/lnn_local_ledger_test.cpp b/tests/core/bus_center/lnn/net_ledger/lnn_local_ledger_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..876a15e23ffcf2de3a09827df6603237c6a581fe --- /dev/null +++ b/tests/core/bus_center/lnn/net_ledger/lnn_local_ledger_test.cpp @@ -0,0 +1,319 @@ +/* + * Copyright (c) 2022 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 +#include +#include +#include +#include + +#include "bus_center_info_key.h" +#include "bus_center_manager.h" +#include "lnn_local_net_ledger.h" +#include "lnn_node_info.h" +#include "lnn_local_ledger_deps_mock.h" +#include "softbus_adapter_mem.h" +#include "softbus_error_code.h" +#include "softbus_common.h" +#include "softbus_log.h" + +namespace OHOS { +using namespace testing::ext; +constexpr char LOCAL_UDID[] = "123456LOCALTEST"; +constexpr char LOCAL_DEVTYPE[] = "TYPE_WATCH"; +constexpr char LOCAL_BT_MAC[] = "56789TUT"; +constexpr char LOCAL_WLAN_IP[] = "10.146.181.134"; +constexpr char LOCAL_NET_IF_NAME[] = "LOCAL"; +constexpr char MASTER_NODE_UDID[] = "234567LOCALTEST"; +constexpr char LOCAL_NODE_ADDR[] = "ADDR"; +constexpr char LOCAL_P2P_MAC[] = "11:22:33:44:55"; +constexpr char LOCAL_GO_MAC[] = "22:33:44:55:66"; +constexpr uint32_t LOCAL_SESSION_PORT = 5000; +constexpr uint32_t LOCAL_AUTH_PORT = 6000; +constexpr uint32_t LOCAL_PROXY_PORT = 7000; +constexpr uint32_t LOCAL_CAPACITY = 3; +constexpr int32_t MASTER_WEIGHT = 10; +constexpr int32_t P2P_ROLE = 1; +constexpr uint32_t CAPABILTY = 17; +using namespace testing; +class LocalLedgerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void LocalLedgerTest::SetUpTestCase() +{ +} + +void LocalLedgerTest::TearDownTestCase() +{ +} + +void LocalLedgerTest::SetUp() +{ + LOG_INFO("LocalLedgerTest start."); +} + +void LocalLedgerTest::TearDown() +{ +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_001 +* @tc.desc: local ledger init test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_001, TestSize.Level1) +{ + LocalLedgerDepsInterfaceMock localLedgerMock; + EXPECT_CALL(localLedgerMock, LnnGetNetCapabilty()).WillRepeatedly(Return(CAPABILTY)); + EXPECT_CALL(localLedgerMock, SoftBusGenerateRandomArray(_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_TRUE(LnnInitLocalLedger() == SOFTBUS_ERR); +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_002 +* @tc.desc: local ledger init and deinit test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_002, TestSize.Level1) +{ + LocalLedgerDepsInterfaceMock localLedgerMock; + EXPECT_CALL(localLedgerMock, LnnGetNetCapabilty()).WillRepeatedly(Return(CAPABILTY)); + EXPECT_CALL(localLedgerMock, SoftBusGenerateRandomArray(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, + GetCommonDevInfo(_,NotNull(),_)).WillRepeatedly(localLedgerMock.LedgerGetCommonDevInfo); + EXPECT_CALL(localLedgerMock, LnnInitLocalP2pInfo(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, + SoftBusRegBusCenterVarDump(_,_)).WillRepeatedly(localLedgerMock.LedgerSoftBusRegBusCenterVarDump); + int32_t ret = LnnInitLocalLedger(); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnInitLocalLedger(); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_003 +* @tc.desc: local ledger delay init test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_003, TestSize.Level1) +{ + LocalLedgerDepsInterfaceMock localLedgerMock; + EXPECT_CALL(localLedgerMock, GetCommonDevInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(localLedgerMock, LnnInitOhosAccount()).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_TRUE(LnnInitLocalLedgerDelay() == SOFTBUS_ERR); +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_004 +* @tc.desc: lnn local key table test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_004, TestSize.Level1) +{ + static InfoKey getLocalStringInfoKeyTable[] = { + STRING_KEY_HICE_VERSION, + STRING_KEY_DEV_UDID, + STRING_KEY_NETWORKID, + STRING_KEY_UUID, + STRING_KEY_DEV_TYPE, + STRING_KEY_DEV_NAME, + STRING_KEY_BT_MAC, + STRING_KEY_WLAN_IP, + STRING_KEY_NET_IF_NAME, + STRING_KEY_MASTER_NODE_UDID, + STRING_KEY_NODE_ADDR, + STRING_KEY_P2P_MAC, + STRING_KEY_P2P_GO_MAC, + STRING_KEY_OFFLINE_CODE + }; + char buf[UDID_BUF_LEN] = {0}; + int32_t ret; + uint32_t i; + LnnSetLocalStrInfo(STRING_KEY_DEV_UDID, LOCAL_UDID); + for (i = 0; i < sizeof(getLocalStringInfoKeyTable) / sizeof(InfoKey); i++) { + ret = LnnGetLocalStrInfo(getLocalStringInfoKeyTable[i], nullptr, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + } + for (i = 0; i < sizeof(getLocalStringInfoKeyTable) / sizeof(InfoKey); i++) { + (void)memset_s(buf, UDID_BUF_LEN, 0, UDID_BUF_LEN); + ret = LnnGetLocalStrInfo(getLocalStringInfoKeyTable[i], buf, UDID_BUF_LEN); + EXPECT_TRUE(ret == SOFTBUS_OK); + } +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_005 +* @tc.desc: lnn local key table test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_005, TestSize.Level1) +{ + int32_t ret = LnnSetLocalStrInfo(STRING_KEY_DEV_UDID, LOCAL_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalStrInfo(STRING_KEY_DEV_TYPE, LOCAL_DEVTYPE); + EXPECT_TRUE(ret == SOFTBUS_ERR); + ret = LnnSetLocalStrInfo(STRING_KEY_BT_MAC, LOCAL_BT_MAC); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalStrInfo(STRING_KEY_WLAN_IP, LOCAL_WLAN_IP); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalStrInfo(STRING_KEY_NET_IF_NAME, LOCAL_NET_IF_NAME); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalStrInfo(STRING_KEY_MASTER_NODE_UDID, MASTER_NODE_UDID); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalStrInfo(STRING_KEY_NODE_ADDR, LOCAL_NODE_ADDR); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalStrInfo(STRING_KEY_P2P_MAC, LOCAL_P2P_MAC); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalStrInfo(STRING_KEY_P2P_GO_MAC, LOCAL_GO_MAC); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_006 +* @tc.desc: lnn local key table test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_006, TestSize.Level1) +{ + static InfoKey getLocalNumInfoKeyTable[] = { + NUM_KEY_SESSION_PORT, + NUM_KEY_AUTH_PORT, + NUM_KEY_PROXY_PORT, + NUM_KEY_NET_CAP, + NUM_KEY_DISCOVERY_TYPE, + NUM_KEY_DEV_TYPE_ID, + NUM_KEY_MASTER_NODE_WEIGHT, + NUM_KEY_P2P_ROLE + }; + int32_t ret, info; + LnnSetLocalNumInfo(NUM_KEY_AUTH_PORT, LOCAL_AUTH_PORT); + for (uint32_t i = 0; i < sizeof(getLocalNumInfoKeyTable) / sizeof(InfoKey); i++) { + info = 0; + ret = LnnGetLocalNumInfo(getLocalNumInfoKeyTable[i], &info); + EXPECT_TRUE(ret == SOFTBUS_OK); + } +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_007 +* @tc.desc: lnn local key table test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_007, TestSize.Level1) +{ + int32_t ret = LnnSetLocalNumInfo(NUM_KEY_SESSION_PORT, LOCAL_SESSION_PORT); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalNumInfo(NUM_KEY_AUTH_PORT, LOCAL_AUTH_PORT); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalNumInfo(NUM_KEY_PROXY_PORT, LOCAL_PROXY_PORT); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalNumInfo(NUM_KEY_NET_CAP, LOCAL_CAPACITY); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalNumInfo(NUM_KEY_MASTER_NODE_WEIGHT, MASTER_WEIGHT); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnSetLocalNumInfo(NUM_KEY_P2P_ROLE, P2P_ROLE); + EXPECT_TRUE(ret == SOFTBUS_OK); +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_008 +* @tc.desc: local ledger init test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_008, TestSize.Level1) +{ + LocalLedgerDepsInterfaceMock localLedgerMock; + EXPECT_CALL(localLedgerMock, LnnGetNetCapabilty()).WillRepeatedly(Return(CAPABILTY)); + EXPECT_CALL(localLedgerMock, SoftBusGenerateRandomArray(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, GetCommonDevInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_CALL(localLedgerMock, LnnInitLocalP2pInfo(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, SoftBusRegBusCenterVarDump(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_TRUE(LnnInitLocalLedger() == SOFTBUS_OK); +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_009 +* @tc.desc: local ledger init test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_009, TestSize.Level1) +{ + LocalLedgerDepsInterfaceMock localLedgerMock; + EXPECT_CALL(localLedgerMock, LnnGetNetCapabilty()).WillRepeatedly(Return(CAPABILTY)); + EXPECT_CALL(localLedgerMock, SoftBusGenerateRandomArray(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, GetCommonDevInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, LnnInitLocalP2pInfo(_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_TRUE(LnnInitLocalLedger() == SOFTBUS_OK); +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_010 +* @tc.desc: local ledger init test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_010, TestSize.Level1) +{ + LocalLedgerDepsInterfaceMock localLedgerMock; + EXPECT_CALL(localLedgerMock, LnnGetNetCapabilty()).WillRepeatedly(Return(CAPABILTY)); + EXPECT_CALL(localLedgerMock, SoftBusGenerateRandomArray(_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, GetCommonDevInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, LnnInitLocalP2pInfo(_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, SoftBusRegBusCenterVarDump(_,_)).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_TRUE(LnnInitLocalLedger() == SOFTBUS_OK); +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_011 +* @tc.desc: local ledger init test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_011, TestSize.Level1) +{ + LocalLedgerDepsInterfaceMock localLedgerMock; + EXPECT_CALL(localLedgerMock, GetCommonDevInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, LnnInitOhosAccount()).WillRepeatedly(Return(SOFTBUS_ERR)); + EXPECT_TRUE(LnnInitLocalLedgerDelay() == SOFTBUS_ERR); +} + +/* +* @tc.name: LOCAL_LEDGER_MOCK_Test_012 +* @tc.desc: local ledger init test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(LocalLedgerTest, LOCAL_LEDGER_MOCK_Test_012, TestSize.Level1) +{ + LocalLedgerDepsInterfaceMock localLedgerMock; + EXPECT_CALL(localLedgerMock, GetCommonDevInfo(_,_,_)).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_CALL(localLedgerMock, LnnInitOhosAccount()).WillRepeatedly(Return(SOFTBUS_OK)); + EXPECT_TRUE(LnnInitLocalLedgerDelay() == SOFTBUS_OK); +} +} // namespace OHOS diff --git a/tests/core/bus_center/lnn/net_ledger/lnn_meta_node_ledger_test.cpp b/tests/core/bus_center/lnn/net_ledger/lnn_meta_node_ledger_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..568620fa220c508e506abe71c625f0e9607835af --- /dev/null +++ b/tests/core/bus_center/lnn/net_ledger/lnn_meta_node_ledger_test.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2022 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 +#include +#include +#include +#include + +#include "lnn_meta_node_ledger.h" +#include "lnn_network_id.h" +#include "softbus_adapter_mem.h" +#include "softbus_def.h" +#include "softbus_errcode.h" +#include "softbus_log.h" +#include "softbus_utils.h" + +typedef struct { + ListNode node; + MetaNodeInfo info; +} MetaNodeStorageInfo; + +namespace OHOS { +using namespace testing::ext; +constexpr char NODE_DEVICE_NAME[] = "node1_test"; +constexpr char NODE_UDID[] = "123456ABCDEF"; +constexpr char META_NODE_ID[] = "235689BNHFCC"; +constexpr uint32_t ADDR_NUM = 6; +constexpr int32_t INFO_NUM = 0; +constexpr int32_t INVALID_INFO_NUM = -1; +using namespace testing; +class MetaNodeLedgerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void MetaNodeLedgerTest::SetUpTestCase() +{ +} + +void MetaNodeLedgerTest::TearDownTestCase() +{ +} + +void MetaNodeLedgerTest::SetUp() +{ + LOG_INFO("MetaNodeLedgerTest start."); + LnnInitMetaNodeLedger(); +} + +void MetaNodeLedgerTest::TearDown() +{ + LOG_INFO("MetaNodeLedgerTest finish."); + LnnDeinitMetaNodeLedger(); +} + +/* +* @tc.name: LNN_ACTIVE_META_NODE_Test_001 +* @tc.desc: lnn active meta node test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(MetaNodeLedgerTest, LNN_ACTIVE_META_NODE_Test_001, TestSize.Level1) +{ + MetaNodeConfigInfo info; + (void)memset_s(&info, sizeof(MetaNodeConfigInfo), 0, sizeof(MetaNodeConfigInfo)); + info.addrNum = CONNECTION_ADDR_WLAN; + (void)strcpy_s(info.udid, UUID_BUF_LEN, NODE_UDID); + (void)strcpy_s(info.deviceName, DEVICE_NAME_BUF_LEN, NODE_DEVICE_NAME); + char metaNodeId[NETWORK_ID_BUF_LEN] = {0}; + int32_t ret = LnnActiveMetaNode(&info, metaNodeId); + EXPECT_TRUE(ret == SOFTBUS_OK); + info.addrNum = ADDR_NUM; + ret = LnnActiveMetaNode(&info, metaNodeId); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + ret = LnnActiveMetaNode(nullptr, metaNodeId); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); +} + +/* +* @tc.name: LNN_GET_ALL_META_NODE_INFO_Test_001 +* @tc.desc: lnn get all meta node info test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(MetaNodeLedgerTest, LNN_GET_ALL_META_NODE_INFO_Test_001, TestSize.Level1) +{ + MetaNodeInfo infos[MAX_META_NODE_NUM]; + int32_t infoNum1 = INFO_NUM; + int32_t infoNum2 = INVALID_INFO_NUM; + int32_t ret = LnnGetAllMetaNodeInfo(infos, &infoNum1); + EXPECT_TRUE(ret == SOFTBUS_OK); + ret = LnnGetAllMetaNodeInfo(infos, &infoNum2); + EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM); + EXPECT_TRUE(LnnGetAllMetaNodeInfo(nullptr, nullptr) == SOFTBUS_INVALID_PARAM); +} + +/* +* @tc.name: LNN_DEACTIVE_META_NODE_Test_001 +* @tc.desc: lnn deactive meta node test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(MetaNodeLedgerTest, LNN_DEACTIVE_META_NODE_Test_001, TestSize.Level1) +{ + int32_t ret = LnnDeactiveMetaNode(META_NODE_ID); + EXPECT_TRUE(ret == SOFTBUS_ERR); + EXPECT_TRUE(LnnDeactiveMetaNode(nullptr) == SOFTBUS_INVALID_PARAM); + LnnDeinitMetaNodeLedger(); +} +} // namespace OHOS