mirror of
https://github.com/openharmony/communication_t2stack.git
synced 2026-07-01 18:23:40 -04:00
merge master into master
添加测试代码
Created-by: Agrant
Commit-by: Agrant
Merged-by: openharmony_ci
Description: ### 一、内容说明(相关的Issue)
### 二、建议测试周期和提测地址
建议测试完成时间:xxxx.xx.xx
投产上线时间:xxxx.xx.xx
提测地址:CI环境/压测环境
测试账号:
### 三、变更内容
* 3.1 关联PR列表
* 3.2 数据库和部署说明
1. 常规更新
2. 重启unicorn
3. 重启sidekiq
4. 迁移任务:是否有迁移任务,没有写 "无"
5. rake脚本:`bundle exec xxx RAILS_ENV = production`;没有写 "无"
* 3.4 其他技术优化内容(做了什么,变更了什么)
- 重构了 xxxx 代码
- xxxx 算法优化
* 3.5 废弃通知(什么字段、方法弃用?)
* 3.6 后向不兼容变更(是否有无法向后兼容的变更?)
### 四、研发自测点(自测哪些?冒烟用例全部自测?)
自测测试结论:
### 五、测试关注点(需要提醒QA重点关注的、可能会忽略的地方)
检查点:
| 需求名称 | 是否影响xx公共模块 | 是否需要xx功能 | 需求升级是否依赖其他子产品 |
|------|------------|----------|---------------|
| xxx | 否 | 需要 | 不需要 |
| | | | |
接口测试:
性能测试:
并发测试:
其他:
See merge request: openharmony-sig/communication_t2stack!6
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
# Copyright (c) 2025 Huawei Device Co., Ltd.
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import("//build/test.gni")
|
||||
|
||||
group("unittest") {
|
||||
testonly = true
|
||||
deps = [ "unittest:unittest" ]
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
# Copyright (c) 2025 Huawei Device Co., Ltd.
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import("//build/test.gni")
|
||||
|
||||
module_output_path = "developer_test/calculator"
|
||||
|
||||
ohos_unittest("DfileTest") {
|
||||
module_out_path = module_output_path
|
||||
|
||||
cflags = [
|
||||
"-Wall",
|
||||
"-fstack-protector-all",
|
||||
"-D_FORTIFY_SOURCE=2",
|
||||
"-DMBEDTLS_INCLUDED",
|
||||
"-DNSTACKX_DEVICE_L2",
|
||||
"-DNSTACKX_WITH_HMOS_LINUX",
|
||||
"-DENABLE_USER_LOG",
|
||||
"-DOPENSSL_ARM_PLATFORM",
|
||||
"-DBUILD_FOR_OHOS",
|
||||
]
|
||||
cflags_cc = cflags
|
||||
include_dirs = [
|
||||
"$NSTACKX_ROOT/nstackx_core/dfile/include",
|
||||
"$NSTACKX_ROOT/nstackx_core/dfile/interface",
|
||||
"$NSTACKX_ROOT/nstackx_core/platform/unix",
|
||||
"$NSTACKX_ROOT/nstackx_util/interface",
|
||||
"$NSTACKX_ROOT/nstackx_util/platform/unix",
|
||||
]
|
||||
sources = [ "test_dfile_test.cpp" ]
|
||||
deps = [
|
||||
"$NSTACKX_ROOT/nstackx_congestion:nstackx_congestion.open",
|
||||
"$NSTACKX_ROOT/nstackx_core/dfile:nstackx_dfile.open",
|
||||
"$NSTACKX_ROOT/nstackx_util:nstackx_util.open",
|
||||
]
|
||||
external_deps = [
|
||||
"bounds_checking_function:libsec_shared",
|
||||
"hilog:libhilog",
|
||||
"mbedtls:mbedtls_shared",
|
||||
"openssl:libcrypto_shared",
|
||||
]
|
||||
|
||||
deps += [ "//third_party/googletest:gtest_main" ]
|
||||
}
|
||||
|
||||
ohos_unittest("DstreamTest") {
|
||||
module_out_path = module_output_path
|
||||
|
||||
cflags = [
|
||||
"-Wall",
|
||||
"-fstack-protector-all",
|
||||
"-D_FORTIFY_SOURCE=2",
|
||||
"-DMBEDTLS_INCLUDED",
|
||||
"-DNSTACKX_DEVICE_L2",
|
||||
"-DNSTACKX_WITH_HMOS_LINUX",
|
||||
"-DENABLE_USER_LOG",
|
||||
"-DOPENSSL_ARM_PLATFORM",
|
||||
"-DBUILD_FOR_OHOS",
|
||||
]
|
||||
cflags_cc = cflags
|
||||
include_dirs = [
|
||||
"$NSTACKX_ROOT/fillp/include",
|
||||
"$NSTACKX_ROOT/fillp/src/public/include",
|
||||
"$NSTACKX_ROOT/fillp/src/app_lib/include",
|
||||
"$NSTACKX_ROOT/fillp/src/fillp_lib/include",
|
||||
"$NSTACKX_ROOT/fillp/src/fillp_lib/include/fillp",
|
||||
"$NSTACKX_ROOT/fillp/src/fillp_lib/include/wifi_info",
|
||||
]
|
||||
sources = [ "test_dstream_test.cpp" ]
|
||||
deps = [
|
||||
"$NSTACKX_ROOT/nstackx_congestion:nstackx_congestion.open",
|
||||
"$NSTACKX_ROOT/fillp:FillpSo.open",
|
||||
"$NSTACKX_ROOT/nstackx_util:nstackx_util.open",
|
||||
]
|
||||
external_deps = [
|
||||
"bounds_checking_function:libsec_shared",
|
||||
"hilog:libhilog",
|
||||
"mbedtls:mbedtls_shared",
|
||||
"openssl:libcrypto_shared",
|
||||
]
|
||||
|
||||
deps += [ "//third_party/googletest:gtest_main" ]
|
||||
}
|
||||
|
||||
ohos_unittest("DfinderTest") {
|
||||
module_out_path = module_output_path
|
||||
|
||||
cflags = [
|
||||
"-Wall",
|
||||
"-fstack-protector-all",
|
||||
"-D_FORTIFY_SOURCE=2",
|
||||
"-DMBEDTLS_INCLUDED",
|
||||
"-DNSTACKX_DEVICE_L2",
|
||||
"-DNSTACKX_WITH_HMOS_LINUX",
|
||||
"-DENABLE_USER_LOG",
|
||||
"-DOPENSSL_ARM_PLATFORM",
|
||||
"-DBUILD_FOR_OHOS",
|
||||
]
|
||||
cflags_cc = cflags
|
||||
include_dirs = [
|
||||
"$NSTACKX_ROOT/nstackx_ctrl/include",
|
||||
"$NSTACKX_ROOT/nstackx_ctrl/interface",
|
||||
]
|
||||
sources = [
|
||||
"dfinder_gtest/json_payload_ut.cpp",
|
||||
"dfinder_gtest/nstackx_common_ut.cpp",
|
||||
"dfinder_gtest/nstackx_device_local_ut.cpp",
|
||||
"dfinder_gtest/nstackx_device_remote_ut.cpp",
|
||||
"dfinder_gtest/nstackx_dfinder_hidump_ut.cpp",
|
||||
"dfinder_gtest/nstackx_dfinder_hievent_ut.cpp",
|
||||
]
|
||||
deps = [
|
||||
"$NSTACKX_ROOT/nstackx_congestion:nstackx_congestion.open",
|
||||
"$NSTACKX_ROOT/nstackx_ctrl:nstackx_ctrl",
|
||||
"$NSTACKX_ROOT/nstackx_util:nstackx_util.open",
|
||||
]
|
||||
external_deps = [
|
||||
"bounds_checking_function:libsec_shared",
|
||||
"hilog:libhilog",
|
||||
"mbedtls:mbedtls_shared",
|
||||
"openssl:libcrypto_shared",
|
||||
]
|
||||
|
||||
deps += [ "//third_party/googletest:gtest_main" ]
|
||||
}
|
||||
|
||||
group("unittest") {
|
||||
testonly = true
|
||||
deps = [
|
||||
":DfileTest",
|
||||
":DstreamTest",
|
||||
":DfinderTest",
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 "coap_app.h"
|
||||
#ifndef _WIN32
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
#include "coap_client.h"
|
||||
#include "nstackx_device_local.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "mockcpp/mokc.h"
|
||||
#include "ut_utils.h"
|
||||
|
||||
TEST(Dfinder, TestGetCoapContextList)
|
||||
{
|
||||
List *list = GetCoapContextList();
|
||||
ASSERT_NE(list, nullptr);
|
||||
}
|
||||
|
||||
void Invoke_coap_register_response_handler(coap_context_t *context, coap_response_handler_t handler)
|
||||
{
|
||||
(void)context;
|
||||
(void)handler;
|
||||
}
|
||||
TEST(Dfinder, TestCoapServerInit)
|
||||
{
|
||||
coap_context_t *ctx = CreateCoapContext();
|
||||
LocalIface *localIface = CreateLocalIface();
|
||||
union InetAddr ipAddr;
|
||||
ipAddr.in = localIface->addr.in;
|
||||
|
||||
// calloc is error
|
||||
MOCKER(calloc).stubs().will(returnValue((void *)NULL));
|
||||
CoapCtxType *coapCtxType = CoapServerInit(AF_INET, &ipAddr, localIface);
|
||||
ASSERT_EQ(coapCtxType, nullptr);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// CoapGetContext == NULL
|
||||
MOCKER(CoapGetContext).stubs().will(returnValue((coap_context_t *)NULL));
|
||||
coapCtxType = CoapServerInit(AF_INET, &ipAddr, localIface);
|
||||
ASSERT_EQ(coapCtxType, nullptr);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// CoapInitResources(ctx->ctx) != NSTACKX_EOK, {@link ctx} is free
|
||||
MOCKER(CoapGetContext).stubs().will(returnValue(ctx));
|
||||
MOCKER(CoapInitResources).stubs().will(returnValue(NSTACKX_EFAILED));
|
||||
coapCtxType = CoapServerInit(AF_INET, &ipAddr, localIface);
|
||||
ASSERT_EQ(coapCtxType, nullptr);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// all is ok
|
||||
MOCKER(CoapGetContext).stubs().will(returnValue(ctx));
|
||||
MOCKER(CoapInitResources).stubs().will(returnValue(NSTACKX_EOK));
|
||||
MOCKER(coap_register_response_handler).stubs().will(invoke(Invoke_coap_register_response_handler));
|
||||
coapCtxType = CoapServerInit(AF_INET, &ipAddr, localIface);
|
||||
ASSERT_NE(coapCtxType, nullptr);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
FreeLocalIface(localIface);
|
||||
ListRemoveNode(&coapCtxType->node);
|
||||
free(coapCtxType);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapGetCoapCtxType)
|
||||
{
|
||||
CoapCtxType *ctx = InsertDevice();
|
||||
|
||||
CoapCtxType *coapCtxType = CoapGetCoapCtxType(NULL);
|
||||
ASSERT_EQ(coapCtxType, nullptr);
|
||||
|
||||
coapCtxType = CoapGetCoapCtxType(ctx->ctx);
|
||||
ASSERT_EQ(coapCtxType, ctx);
|
||||
|
||||
RemoveDevice(ctx);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestIsCoapContextReady)
|
||||
{
|
||||
bool ret = IsCoapContextReady();
|
||||
ASSERT_EQ(ret, false);
|
||||
}
|
||||
|
||||
static coap_socket_t *g_socket;
|
||||
#define PREPARE_IO_RETURN_VALUE 1000
|
||||
unsigned int Invoke_coap_io_prepare_io(coap_context_t *ctx, coap_socket_t *sockets[], unsigned int max_sockets,
|
||||
unsigned int *numSockets, coap_tick_t now)
|
||||
{
|
||||
*numSockets = 1;
|
||||
g_socket = (coap_socket_t *)malloc(1*sizeof(coap_socket_t));
|
||||
g_socket->fd = 1;
|
||||
g_socket->flags = COAP_SOCKET_WANT_CONNECT;
|
||||
sockets[0] = g_socket;
|
||||
return PREPARE_IO_RETURN_VALUE;
|
||||
}
|
||||
TEST(Dfinder, TestRegisterCoAPEpollTask)
|
||||
{
|
||||
CoapCtxType *ctx = InsertDevice();
|
||||
// all is ok
|
||||
ctx->socketNum = 1;
|
||||
EpollDesc epollfd = CreateEpollDesc();
|
||||
MOCKER(coap_io_prepare_io).stubs().will(invoke(Invoke_coap_io_prepare_io));
|
||||
uint32_t ret = RegisterCoAPEpollTask(epollfd);
|
||||
ASSERT_EQ(ret, (uint32_t)1000);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
free(g_socket);
|
||||
RemoveDevice(ctx);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoAPEpollReadHandle)
|
||||
{
|
||||
CoapCtxType *ctx = InsertDevice();
|
||||
// all is ok
|
||||
ctx->socketNum = 1;
|
||||
EpollDesc epollfd = CreateEpollDesc();
|
||||
MOCKER(coap_io_prepare_io).stubs().will(invoke(Invoke_coap_io_prepare_io));
|
||||
uint32_t ret = RegisterCoAPEpollTask(epollfd);
|
||||
ASSERT_EQ(ret, (uint32_t)1000);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// data == null
|
||||
ctx->taskList[0].readHandle(NULL);
|
||||
|
||||
EpollTask task = {0};
|
||||
// task->taskfd < 0
|
||||
task.taskfd = -1;
|
||||
ctx->taskList[0].readHandle(&task);
|
||||
|
||||
// task->ptr == NULL
|
||||
task.taskfd = 0;
|
||||
task.ptr = (void *)NULL;
|
||||
ctx->taskList[0].readHandle(&task);
|
||||
|
||||
coap_socket_t socket = {0};
|
||||
task.ptr = &socket;
|
||||
// socket->flags & COAP_SOCKET_WANT_READ
|
||||
socket.flags = COAP_SOCKET_WANT_READ;
|
||||
ctx->taskList[0].readHandle(&task);
|
||||
|
||||
// socket->flags & COAP_SOCKET_WANT_ACCEPT
|
||||
socket.flags = COAP_SOCKET_WANT_ACCEPT;
|
||||
ctx->taskList[0].readHandle(&task);
|
||||
|
||||
free(g_socket);
|
||||
RemoveDevice(ctx);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoAPEpollWriteHandle)
|
||||
{
|
||||
CoapCtxType *ctx = InsertDevice();
|
||||
// all is ok
|
||||
ctx->socketNum = 1;
|
||||
EpollDesc epollfd = CreateEpollDesc();
|
||||
MOCKER(coap_io_prepare_io).stubs().will(invoke(Invoke_coap_io_prepare_io));
|
||||
uint32_t ret = RegisterCoAPEpollTask(epollfd);
|
||||
ASSERT_EQ(ret, (uint32_t)1000);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// data == null
|
||||
ctx->taskList[0].writeHandle(NULL);
|
||||
|
||||
EpollTask task = {0};
|
||||
// task->taskfd < 0
|
||||
task.taskfd = -1;
|
||||
ctx->taskList[0].writeHandle(&task);
|
||||
|
||||
// task->ptr == NULL
|
||||
task.taskfd = 0;
|
||||
task.ptr = (void *)NULL;
|
||||
ctx->taskList[0].writeHandle(&task);
|
||||
|
||||
coap_socket_t socket = {0};
|
||||
task.ptr = &socket;
|
||||
// socket->flags & COAP_SOCKET_WANT_WRITE
|
||||
socket.flags = COAP_SOCKET_WANT_WRITE;
|
||||
ctx->taskList[0].writeHandle(&task);
|
||||
|
||||
// socket->flags & COAP_SOCKET_WANT_CONNECT
|
||||
socket.flags = COAP_SOCKET_WANT_CONNECT;
|
||||
ctx->taskList[0].writeHandle(&task);
|
||||
|
||||
free(g_socket);
|
||||
RemoveDevice(ctx);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoAPEpollErrorHandle)
|
||||
{
|
||||
CoapCtxType *ctx = InsertDevice();
|
||||
// all is ok
|
||||
ctx->socketNum = 1;
|
||||
EpollDesc epollfd = CreateEpollDesc();
|
||||
MOCKER(coap_io_prepare_io).stubs().will(invoke(Invoke_coap_io_prepare_io));
|
||||
uint32_t ret = RegisterCoAPEpollTask(epollfd);
|
||||
ASSERT_EQ(ret, (uint32_t)1000);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
EpollTask task = {0};
|
||||
// task->taskfd < 0
|
||||
task.taskfd = -1;
|
||||
ctx->taskList[0].errorHandle(&task);
|
||||
|
||||
// task->ptr == NULL
|
||||
task.taskfd = 0;
|
||||
task.ptr = (void *)NULL;
|
||||
ctx->taskList[0].errorHandle(&task);
|
||||
|
||||
coap_socket_t socket = {0};
|
||||
task.ptr = &socket;
|
||||
|
||||
// IsCoapCtxEndpointSocket is ok
|
||||
MOCKER(IsCoapCtxEndpointSocket).stubs().will(returnValue(1));
|
||||
ctx->taskList[0].errorHandle(&task);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// IsCoapCtxEndpointSocket is error
|
||||
MOCKER(IsCoapCtxEndpointSocket).stubs().will(returnValue(0));
|
||||
ctx->taskList[0].errorHandle(&task);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
free(g_socket);
|
||||
RemoveDevice(ctx);
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 "coap_client.h"
|
||||
#ifndef _WIN32
|
||||
#include <netdb.h>
|
||||
#endif
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "mockcpp/mokc.h"
|
||||
#include "ut_utils.h"
|
||||
|
||||
TEST(Dfinder, TestCoapResolveAddress)
|
||||
{
|
||||
// all is ok
|
||||
coap_str_const_t *server = coap_make_str_const("server");
|
||||
struct sockaddr_in inetAddr = {0};
|
||||
inetAddr.sin_family = AF_INET;
|
||||
inetAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
inetAddr.sin_port = htons(0);
|
||||
struct sockaddr *dst = (struct sockaddr*)&inetAddr;
|
||||
int32_t ret = CoapResolveAddress(server, dst);
|
||||
ASSERT_NE(ret, -1);
|
||||
|
||||
// dst == NULL
|
||||
ret = CoapResolveAddress(server, NULL);
|
||||
ASSERT_EQ(ret, -2);
|
||||
|
||||
// server->length > 0 and then memcpy_s is error
|
||||
MOCKER(memcpy_s).stubs().will(returnValue(-1));
|
||||
ret = CoapResolveAddress(server, dst);
|
||||
ASSERT_EQ(ret, -1);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// server->length == 0 and then memcpy_s is error
|
||||
MOCKER(memcpy_s).stubs().will(returnValue(-1));
|
||||
int temp = server->length;
|
||||
server->length = 0;
|
||||
ret = CoapResolveAddress(server, dst);
|
||||
ASSERT_EQ(ret, -1);
|
||||
server->length = temp;
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// getaddrinfo return -2
|
||||
MOCKER(getaddrinfo).stubs().will(returnValue(-2));
|
||||
ret = CoapResolveAddress(server, dst);
|
||||
ASSERT_EQ(ret, -2);
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
namespace MessageHandlerNs {
|
||||
static coap_session_t *session = NULL;
|
||||
static coap_pdu_t sent;
|
||||
static coap_pdu_t recv;
|
||||
static coap_opt_t opt = {0};
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapMessageHandler_NullReceivedPdu)
|
||||
{
|
||||
coap_response_t ret = CoapMessageHandler(MessageHandlerNs::session, &MessageHandlerNs::sent, NULL, 1);
|
||||
ASSERT_EQ(ret, COAP_RESPONSE_FAIL);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapMessageHandler_ReceiveRstMsg)
|
||||
{
|
||||
MOCKER(coap_pdu_get_type).stubs().will(returnValue(COAP_MESSAGE_RST));
|
||||
int32_t ret = CoapMessageHandler(MessageHandlerNs::session, &MessageHandlerNs::sent, &MessageHandlerNs::recv, 1);
|
||||
GlobalMockObject::verify();
|
||||
ASSERT_EQ(ret, COAP_RESPONSE_FAIL);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapMessageHandler_OptNotNull)
|
||||
{
|
||||
MOCKER(coap_check_option).stubs().will(returnValue(&MessageHandlerNs::opt));
|
||||
MOCKER(coap_pdu_get_type).stubs().will(returnValue(COAP_MESSAGE_ACK));
|
||||
int32_t ret = CoapMessageHandler(MessageHandlerNs::session, &MessageHandlerNs::sent, &MessageHandlerNs::recv, 1);
|
||||
GlobalMockObject::verify();
|
||||
ASSERT_EQ(ret, COAP_RESPONSE_FAIL);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapMessageHandler_NullOptThenNotNullOpt)
|
||||
{
|
||||
MOCKER(coap_check_option).stubs().will(returnValue((coap_opt_t *)NULL)).then(returnValue(&MessageHandlerNs::opt));
|
||||
MOCKER(coap_pdu_get_type).stubs().will(returnValue(COAP_MESSAGE_ACK));
|
||||
int32_t ret = CoapMessageHandler(MessageHandlerNs::session, &MessageHandlerNs::sent, &MessageHandlerNs::recv, 1);
|
||||
GlobalMockObject::verify();
|
||||
ASSERT_EQ(ret, COAP_RESPONSE_FAIL);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapMessageHandler_AllOk)
|
||||
{
|
||||
// all is ok
|
||||
MOCKER(coap_check_option).stubs().will(returnValue((coap_opt_t *)NULL));
|
||||
MOCKER(coap_pdu_get_type).stubs().will(returnValue(COAP_MESSAGE_ACK));
|
||||
int32_t ret = CoapMessageHandler(MessageHandlerNs::session, &MessageHandlerNs::sent, &MessageHandlerNs::recv, 1);
|
||||
ASSERT_EQ(ret, COAP_RESPONSE_OK);
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapGetContext)
|
||||
{
|
||||
char addrStr[NI_MAXHOST] = COAP_SRV_DEFAULT_ADDR;
|
||||
char portStr[NI_MAXSERV] = COAP_SRV_DEFAULT_PORT;
|
||||
LocalIface *localIface = CreateLocalIface();
|
||||
union InetAddr ipAddr;
|
||||
ipAddr.in = localIface->addr.in;
|
||||
|
||||
// getaddrinfo != 0
|
||||
MOCKER(getaddrinfo).stubs().will(returnValue(-1));
|
||||
coap_context_t *ctx = CoapGetContext(addrStr, portStr, AF_INET, &ipAddr);
|
||||
ASSERT_EQ(ctx, nullptr);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// coap_new_endpoint == NULL
|
||||
MOCKER(coap_new_endpoint).stubs().will(returnValue((coap_endpoint_t *)NULL));
|
||||
ctx = CoapGetContext(addrStr, portStr, AF_INET, &ipAddr);
|
||||
ASSERT_EQ(ctx, nullptr);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// BindToDevice(ep->sock.fd, sockIpPtr) != NSTACKX_EOK
|
||||
ctx = CoapGetContext(addrStr, portStr, AF_INET, &ipAddr);
|
||||
ASSERT_EQ(ctx, nullptr);
|
||||
|
||||
FreeLocalIface(localIface);
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapGetSession)
|
||||
{
|
||||
coap_address_t dst = {0};
|
||||
struct sockaddr_in inetAddr;
|
||||
inetAddr.sin_family = AF_INET;
|
||||
inetAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
inetAddr.sin_port = htons(0);
|
||||
memcpy_s(&dst.addr.sa, sizeof(struct sockaddr), (struct sockaddr*)&inetAddr, 10);
|
||||
dst.size = (uint32_t)10;
|
||||
dst.addr.sin.sin_port = htons(COAP_DEFAULT_PORT);
|
||||
dst.addr.sa.sa_family = AF_INET;
|
||||
CoapServerParameter coapServerParameter = {
|
||||
.proto = COAP_PROTO_UDP,
|
||||
.dst = &dst
|
||||
};
|
||||
coap_context_t *ctx = CreateCoapContext();
|
||||
coap_session_t *session1 = (coap_session_t *)malloc(1*sizeof(coap_session_t *));
|
||||
// all is ok
|
||||
MOCKER(coap_new_client_session).stubs().will(returnValue(session1));
|
||||
coap_session_t *session = CoapGetSession(ctx, "127.0.0.1", COAP_SRV_DEFAULT_PORT, &coapServerParameter);
|
||||
ASSERT_EQ(session, session1);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// localAddr == NULL
|
||||
session = CoapGetSession(ctx, NULL, COAP_SRV_DEFAULT_PORT, &coapServerParameter);
|
||||
ASSERT_EQ(session, nullptr);
|
||||
|
||||
// getaddrinfo != 0
|
||||
MOCKER(getaddrinfo).stubs().will(returnValue(-1));
|
||||
session = CoapGetSession(ctx, "127.0.0.1", COAP_SRV_DEFAULT_PORT, &coapServerParameter);
|
||||
ASSERT_EQ(session, nullptr);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// coap_session_get_by_peer != NULL
|
||||
MOCKER(coap_session_get_by_peer).stubs().will(returnValue(session1));
|
||||
session = CoapGetSession(ctx, "127.0.0.1", COAP_SRV_DEFAULT_PORT, &coapServerParameter);
|
||||
ASSERT_EQ(session, session1);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// dst == NULL
|
||||
coapServerParameter.dst = NULL;
|
||||
session = CoapGetSession(ctx, "127.0.0.1", COAP_SRV_DEFAULT_PORT, &coapServerParameter);
|
||||
ASSERT_EQ(session, nullptr);
|
||||
|
||||
// proto != COAP_PROTO_UDP
|
||||
coapServerParameter.proto = COAP_PROTO_NONE;
|
||||
session = CoapGetSession(ctx, "127.0.0.1", COAP_SRV_DEFAULT_PORT, &coapServerParameter);
|
||||
ASSERT_EQ(session, nullptr);
|
||||
|
||||
// coapServerParameter == NULL
|
||||
session = CoapGetSession(ctx, "127.0.0.1", COAP_SRV_DEFAULT_PORT, NULL);
|
||||
ASSERT_EQ(session, nullptr);
|
||||
|
||||
FreeCoapContext(ctx);
|
||||
free(session1);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestIsCoapCtxEndpointSocket)
|
||||
{
|
||||
coap_context_t *coapContext = CreateCoapContext();
|
||||
uint8_t ret = IsCoapCtxEndpointSocket(coapContext, 1);
|
||||
ASSERT_EQ(ret, NSTACKX_FALSE);
|
||||
|
||||
// ctx == NULL
|
||||
ret = IsCoapCtxEndpointSocket(NULL, 1);
|
||||
ASSERT_EQ(ret, NSTACKX_FALSE);
|
||||
|
||||
FreeCoapContext(coapContext);
|
||||
}
|
||||
@@ -0,0 +1,410 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 "coap_discover.h"
|
||||
#include "coap_client.h"
|
||||
#include "nstackx_dfinder_log.h"
|
||||
#include "nstackx_util.h"
|
||||
#include "json_payload.h"
|
||||
#include "nstackx_device_local.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "mockcpp/mokc.h"
|
||||
#include "ut_utils.h"
|
||||
|
||||
#define BODY_LENGTH 331
|
||||
|
||||
void MockSetDefaultCoapDiscoverTargetCount()
|
||||
{
|
||||
// IsCoapContextReady is ok
|
||||
MOCKER(IsCoapContextReady).stubs().will(returnValue(true));
|
||||
// g_coapDiscoverType = COAP_BROADCAST_TYPE_DEFAULT
|
||||
SetCoapDiscoverType(COAP_BROADCAST_TYPE_DEFAULT);
|
||||
CoapServiceDiscoverInnerConfigurable(1);
|
||||
}
|
||||
|
||||
#define LOCAL_IP_LENGTH 9
|
||||
|
||||
void MockCoapGetSession(coap_context_t *ctx)
|
||||
{
|
||||
// setsockopt is ok
|
||||
MOCKER(setsockopt).stubs().will(returnValue(0));
|
||||
// coap_session_get_by_peer is ok
|
||||
coap_address_t dst = {0};
|
||||
struct sockaddr_in inetAddr;
|
||||
inetAddr.sin_family = AF_INET;
|
||||
inetAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
||||
inetAddr.sin_port = htons(COAP_DEFAULT_PORT);
|
||||
memcpy_s(&dst.addr.sa, sizeof(struct sockaddr), (struct sockaddr*)&inetAddr, LOCAL_IP_LENGTH);
|
||||
dst.size = (uint32_t)LOCAL_IP_LENGTH;
|
||||
dst.addr.sin.sin_port = htons(COAP_DEFAULT_PORT);
|
||||
dst.addr.sa.sa_family = AF_INET;
|
||||
coap_session_t *session = coap_new_client_session(ctx, NULL, &dst, COAP_PROTO_UDP);
|
||||
MOCKER(coap_session_get_by_peer).stubs().will(returnValue(session));
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestSetCoapDiscConfig)
|
||||
{
|
||||
uint32_t bcastInterval[1] = {0};
|
||||
DFinderDiscConfig config;
|
||||
config.bcastInterval = bcastInterval;
|
||||
config.intervalArrLen = 1;
|
||||
|
||||
// malloc is error, and g_coapIntervalArr == NULL
|
||||
MOCKER(malloc).stubs().will(returnValue((void *)NULL));
|
||||
int32_t ret = SetCoapDiscConfig(&config);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// all is ok
|
||||
ret = SetCoapDiscConfig(&config);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// g_coapIntervalArr != NULL
|
||||
ret = SetCoapDiscConfig(&config);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// malloc is error, and g_coapIntervalArr != NULL
|
||||
MOCKER(malloc).stubs().will(returnValue((void *)NULL));
|
||||
ret = SetCoapDiscConfig(&config);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// free g_coapIntervalArr
|
||||
CoapDiscoverDeinit();
|
||||
}
|
||||
|
||||
static Timer *InvokeTimerStart(EpollDesc epollfd, uint32_t ms, uint8_t repeated, TimeoutHandle handle, void *data)
|
||||
{
|
||||
handle(data);
|
||||
return (Timer *)malloc(1 * sizeof(Timer));
|
||||
}
|
||||
static void TimerHandle(void *argument)
|
||||
{
|
||||
(void)argument;
|
||||
}
|
||||
TEST(Dfinder, TestCoapDiscoverInit)
|
||||
{
|
||||
EpollDesc epollfd = CreateEpollDesc();
|
||||
|
||||
// g_recvRecountTimer == NULL
|
||||
MOCKER(TimerStart).stubs().will(returnValue((Timer *)NULL));
|
||||
int32_t ret = CoapDiscoverInit(epollfd);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// g_discoverTimer == NULL
|
||||
Timer *timer = TimerStart(epollfd, 1000, NSTACKX_TRUE, TimerHandle, NULL);
|
||||
MOCKER(TimerStart).stubs().will(returnValue(timer)).then(returnValue((Timer *)NULL));
|
||||
ret = CoapDiscoverInit(epollfd);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// g_notificationTimer == NULL
|
||||
Timer *tmpRecvTimer = TimerStart(epollfd, 1000, NSTACKX_TRUE, TimerHandle, NULL);
|
||||
EXPECT_NE(tmpRecvTimer, (Timer *)NULL) << "recv timer, case g_notificationTimer == NULL fail";
|
||||
Timer *tmpDiscTimer = TimerStart(epollfd, 1000, NSTACKX_TRUE, TimerHandle, NULL);
|
||||
EXPECT_NE(tmpDiscTimer, (Timer *)NULL) << "disc timer, case g_notificationTimer == NULL fail";
|
||||
MOCKER(TimerStart).stubs().will(returnValue(tmpRecvTimer)).
|
||||
then(returnValue(tmpDiscTimer)).then(returnValue((Timer *)NULL));
|
||||
ret = CoapDiscoverInit(epollfd);
|
||||
EXPECT_EQ(ret, NSTACKX_EFAILED) << "case g_notificationTimer == NULL fail";
|
||||
CoapDiscoverDeinit();
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// calloc is error
|
||||
MOCKER(TimerStart).stubs().will(invoke(InvokeTimerStart));
|
||||
MOCKER(calloc).stubs().will(returnValue((void *)NULL));
|
||||
ret = CoapDiscoverInit(epollfd);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// all is ok
|
||||
MOCKER(TimerStart).stubs().will(invoke(InvokeTimerStart));
|
||||
ret = CoapDiscoverInit(epollfd);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
GlobalMockObject::verify();
|
||||
CoapDiscoverDeinit();
|
||||
|
||||
// g_discoverCount < g_coapDiscoverTargetCount && IsCoapContextReady is true
|
||||
MOCKER(IsCoapContextReady).stubs().will(returnValue(true));
|
||||
MOCKER(TimerStart).stubs().will(invoke(InvokeTimerStart));
|
||||
SetCoapDiscoverType(COAP_BROADCAST_TYPE_DEFAULT);
|
||||
CoapServiceDiscoverInnerAn(1);
|
||||
// CoapPostServiceDiscover
|
||||
CoapCtxType *ctx = InsertDevice();
|
||||
|
||||
char serviceData[] = "service\0";
|
||||
NSTACKX_RegisterServiceData(serviceData);
|
||||
// GetBroadcastIp != NSTACKX_EOK
|
||||
ret = CoapDiscoverInit(epollfd);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
CoapDiscoverDeinit();
|
||||
|
||||
// GetBroadcastIp == NSTACKX_EOK
|
||||
MOCKER(GetBroadcastIp).stubs().will(returnValue(NSTACKX_EOK));
|
||||
// CoapGetSession != NULL
|
||||
coap_session_t *session = (coap_session_t *)malloc(1 * sizeof(coap_session_t));
|
||||
MOCKER(CoapGetSession).stubs().will(returnValue(session));
|
||||
// coap_split_uri >= 0
|
||||
MOCKER(coap_split_uri).stubs().will(returnValue(0));
|
||||
// CoapResolveAddress >= 0
|
||||
MOCKER(CoapResolveAddress).stubs().will(returnValue(0));
|
||||
// coap_new_pdu != NULL
|
||||
coap_pdu_t *pdu = (coap_pdu_t *)malloc(1 * sizeof(coap_pdu_t));
|
||||
MOCKER(coap_new_pdu).stubs().will(returnValue(pdu));
|
||||
// coap_send != COAP_INVALID_TID(-1)
|
||||
MOCKER(coap_send).stubs().will(returnValue(1));
|
||||
|
||||
ret = CoapDiscoverInit(epollfd);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
CoapDiscoverDeinit();
|
||||
GlobalMockObject::verify();
|
||||
|
||||
CoapDiscoverDeinit();
|
||||
RemoveDevice(ctx);
|
||||
free(session);
|
||||
free(pdu);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapDiscoverRequestOngoing)
|
||||
{
|
||||
// 调用 CoapSubscribeModuleInner,假设它会触发 CoapDiscoverRequestOngoing 返回 1
|
||||
CoapSubscribeModuleInner(1);
|
||||
uint8_t ret = CoapDiscoverRequestOngoing();
|
||||
ASSERT_EQ(ret, 1);
|
||||
}
|
||||
|
||||
namespace ServiceMsgNs {
|
||||
static const uint8_t data[] = {0};
|
||||
static MsgCtx ctx = {
|
||||
.deviceId = "deviceId",
|
||||
.moduleName = "moduleName",
|
||||
.data = data,
|
||||
.len = 1,
|
||||
.type = SERVER_TYPE_WLANORETH,
|
||||
.err = NSTACKX_EOK
|
||||
};
|
||||
const char addr[INET_ADDRSTRLEN] = "127.0.0.1:8080";
|
||||
struct in_addr remoteIp;
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapSendServiceMsg_NullCtx)
|
||||
{
|
||||
(void)inet_pton(AF_INET, ServiceMsgNs::addr, &ServiceMsgNs::remoteIp);
|
||||
MOCKER(LocalIfaceGetCoapCtxByRemoteIp).stubs().will(returnValue((CoapCtxType *)NULL));
|
||||
int32_t ret = CoapSendServiceMsg(&ServiceMsgNs::ctx, ServiceMsgNs::addr, &ServiceMsgNs::remoteIp);
|
||||
GlobalMockObject::verify();
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapSendServiceMsg_NullSession)
|
||||
{
|
||||
CoapCtxType *coapCtxType = CreateCoapCtxType();
|
||||
MOCKER(LocalIfaceGetCoapCtxByRemoteIp).stubs().will(returnValue(coapCtxType));
|
||||
MOCKER(CoapGetSession).stubs().will(returnValue((coap_session_t *)NULL));
|
||||
int32_t ret = CoapSendServiceMsg(&ServiceMsgNs::ctx, ServiceMsgNs::addr, &ServiceMsgNs::remoteIp);
|
||||
GlobalMockObject::verify();
|
||||
FreeCoapCtxType(coapCtxType);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapSendServiceMsg_MemcpysFail)
|
||||
{
|
||||
MOCKER(memcpy_s).stubs().will(returnValue(EINVAL));
|
||||
int32_t ret = CoapSendServiceMsg(&ServiceMsgNs::ctx, ServiceMsgNs::addr, &ServiceMsgNs::remoteIp);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapSendServiceMsg_CallocFail)
|
||||
{
|
||||
MOCKER(calloc).stubs().will(returnValue((void *)NULL));
|
||||
int32_t ret = CoapSendServiceMsg(&ServiceMsgNs::ctx, ServiceMsgNs::addr, &ServiceMsgNs::remoteIp);
|
||||
GlobalMockObject::verify();
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
}
|
||||
|
||||
static bool g_IsDiscoverMode = true;
|
||||
static void Invoke_coap_register_handler(coap_resource_t *resource, coap_request_t method,
|
||||
coap_method_handler_t handler)
|
||||
{
|
||||
coap_session_t *session = NULL;
|
||||
coap_pdu_t *request = (coap_pdu_t *)malloc(1 * sizeof(coap_pdu_t));
|
||||
coap_pdu_t *response = (coap_pdu_t *)malloc(1 * sizeof(coap_pdu_t));
|
||||
request->mid = 1; // Valid message ids are 0 to 2^16. Negative values are error codes.
|
||||
if (g_IsDiscoverMode) {
|
||||
request->body_data = (const uint8_t *)
|
||||
"{\"deviceId\":\"{\\\"UDID\\\":\\\"FE73EE0BA926F5A5B149B1567AD2D32BC5443A9FE05FE812CE623283EAE5CEDD\\\"}\","
|
||||
"\"devicename\":\"Mate 50\",\"type\":14,\"mode\":1,\"deviceHash\":\"0\","
|
||||
"\"serviceData\":\"\",\"extendServiceData\":\"\",\"wlanIp\":\"127.0.0.1\",\"capabilityBitmap\":[71],"
|
||||
"\"bType\":0,\"bData\":\"\", \"coapUri\":\"coap://127.0.0.1/device_discover\"}";
|
||||
} else {
|
||||
request->body_data = (const uint8_t *)
|
||||
"{\"deviceId\":\"{\\\"UDID\\\":\\\"FE73EE0BA926F5A5B149B1567AD2D32BC5443A9FE05FE812CE623283EAE5CEDD\\\"}\","
|
||||
"\"devicename\":\"Mate 50\",\"type\":14,\"mode\":2,\"deviceHash\":\"0\","
|
||||
"\"serviceData\":\"\",\"extendServiceData\":\"\",\"wlanIp\":\"127.0.0.1\",\"capabilityBitmap\":[71],"
|
||||
"\"bType\":0,\"bData\":\"\", \"coapUri\":\"coap://127.0.0.1/device_discover\"}";
|
||||
}
|
||||
request->body_length = (size_t)BODY_LENGTH;
|
||||
handler(resource, session, request, coap_get_query(request), response);
|
||||
|
||||
free(request);
|
||||
free(response);
|
||||
}
|
||||
static void Invoke_coap_resource_set_get_observable(coap_resource_t *resource, int mode)
|
||||
{
|
||||
return;
|
||||
}
|
||||
static void Invoke_coap_add_resource(coap_context_t *context, coap_resource_t *resource)
|
||||
{
|
||||
return;
|
||||
}
|
||||
static int Invoke_coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
static void Invoke_coap_pdu_set_code(coap_pdu_t *pdu, coap_pdu_code_t code)
|
||||
{
|
||||
return;
|
||||
}
|
||||
TEST(Dfinder, TestCoapInitResources)
|
||||
{
|
||||
// InsertDevice and CoapDiscoverInit
|
||||
CoapCtxType *ctx = InsertDevice();
|
||||
|
||||
// r == NULL
|
||||
MOCKER(coap_resource_init).stubs().will(returnValue((coap_resource_t *)NULL));
|
||||
int32_t ret = CoapInitResources(ctx->ctx);
|
||||
ASSERT_EQ(ret, NSTACKX_ENOMEM);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// msg == NULL
|
||||
MOCKER(coap_register_request_handler).stubs().will(invoke(Invoke_coap_register_handler));
|
||||
MOCKER(coap_resource_set_get_observable).stubs().will(invoke(Invoke_coap_resource_set_get_observable));
|
||||
MOCKER(coap_add_resource).stubs().will(invoke(Invoke_coap_add_resource));
|
||||
MOCKER(coap_delete_resource).stubs().will(invoke(Invoke_coap_delete_resource));
|
||||
coap_resource_t *res = (coap_resource_t *)malloc(1 * sizeof(coap_resource_t));
|
||||
MOCKER(coap_resource_init).stubs().will(returnValue(res)).then(returnValue((coap_resource_t *)NULL));
|
||||
ret = CoapInitResources(ctx->ctx);
|
||||
ASSERT_EQ(ret, NSTACKX_ENOMEM);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// notification == NULL
|
||||
MOCKER(coap_register_request_handler).stubs().will(invoke(Invoke_coap_register_handler));
|
||||
MOCKER(coap_resource_set_get_observable).stubs().will(invoke(Invoke_coap_resource_set_get_observable));
|
||||
MOCKER(coap_add_resource).stubs().will(invoke(Invoke_coap_add_resource));
|
||||
MOCKER(coap_delete_resource).stubs().will(invoke(Invoke_coap_delete_resource));
|
||||
coap_resource_t *resDisc = (coap_resource_t *)malloc(1 * sizeof(coap_resource_t));
|
||||
EXPECT_NE(resDisc, (coap_resource_t *)NULL) << "malloc for disc testcase notification == NULL fail";
|
||||
coap_resource_t *resMsg = (coap_resource_t *)malloc(1 * sizeof(coap_resource_t));
|
||||
EXPECT_NE(resMsg, (coap_resource_t *)NULL) << "malloc for msg testcase notification == NULL fail";
|
||||
MOCKER(coap_resource_init).stubs().will(returnValue(resDisc)).
|
||||
then(returnValue(resMsg)).then(returnValue((coap_resource_t *)NULL));
|
||||
ret = CoapInitResources(ctx->ctx);
|
||||
if (resDisc) free(resDisc);
|
||||
if (resMsg) free(resMsg);
|
||||
EXPECT_EQ(ret, NSTACKX_ENOMEM) << "case notification == NULL fail";
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// all is ok
|
||||
MOCKER(coap_register_request_handler).stubs().will(invoke(Invoke_coap_register_handler));
|
||||
MOCKER(coap_resource_set_get_observable).stubs().will(invoke(Invoke_coap_resource_set_get_observable));
|
||||
MOCKER(coap_add_resource).stubs().will(invoke(Invoke_coap_add_resource));
|
||||
MOCKER(coap_delete_resource).stubs().will(invoke(Invoke_coap_delete_resource));
|
||||
MOCKER(coap_resource_init).stubs().will(returnValue(res));
|
||||
coap_context_t *context = CreateCoapContext();
|
||||
MOCKER(coap_session_get_context).stubs().will(returnValue(context));
|
||||
MOCKER(CoapGetCoapCtxType).stubs().will(returnValue(ctx));
|
||||
MOCKER(GetServiceDiscoverInfo).stubs().will(returnValue(NSTACKX_EOK));
|
||||
MOCKER(ReportDiscoveredDevice).stubs().will(returnValue(NSTACKX_EOK));
|
||||
MOCKER(coap_pdu_set_code).stubs().will(invoke(Invoke_coap_pdu_set_code));
|
||||
MOCKER(ntohs).stubs().will(returnValue(BODY_LENGTH));
|
||||
|
||||
ret = CoapInitResources(ctx->ctx);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
CoapDiscoverDeinit();
|
||||
RemoveDevice(ctx);
|
||||
free(res);
|
||||
FreeCoapContext(context);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapServiceNotification)
|
||||
{
|
||||
// coap context not ready
|
||||
CoapServiceNotification();
|
||||
|
||||
// coap context ready, init, already running, run again: g_notificationRunCnt != 0
|
||||
MOCKER(IsCoapContextReady).stubs().will(returnValue(true));
|
||||
NSTACKX_Parameter params = {0};
|
||||
int32_t ret = NSTACKX_Init(¶ms);
|
||||
EXPECT_EQ(ret, NSTACKX_EOK) << "dfinder init fail";
|
||||
uint16_t tmpIntervals[3] = {0, 2000, 2000};
|
||||
std::string tmpMsg("hello");
|
||||
NSTACKX_NotificationConfig config = {
|
||||
msg : const_cast<char *>(tmpMsg.c_str()),
|
||||
msgLen : 5,
|
||||
intervalsMs : tmpIntervals,
|
||||
intervalLen : 3,
|
||||
businessType : NSTACKX_BUSINESS_TYPE_NULL
|
||||
};
|
||||
// ThreadInit
|
||||
ret = NSTACKX_ThreadInit();
|
||||
ASSERT_EQ(ret, NSTACKX_EOK) << "thread init fail";
|
||||
|
||||
ret = NSTACKX_SendNotification(&config);
|
||||
EXPECT_EQ(ret, NSTACKX_EOK) << "send notification fail";
|
||||
CoapServiceNotification();
|
||||
NSTACKX_Deinit();
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestLocalizeNotificationInterval)
|
||||
{
|
||||
const uint16_t tmpIntervals[3] = {0, 2000, 2000};
|
||||
const uint8_t intervalLen = 3;
|
||||
|
||||
MOCKER(calloc)
|
||||
.stubs()
|
||||
.will(returnValue(static_cast<void*>(NULL)));
|
||||
int32_t ret = LocalizeNotificationInterval(tmpIntervals, intervalLen);
|
||||
EXPECT_EQ(ret, NSTACKX_EFAILED) << "malloc for notification interval fail";
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// all ok, first call to make g_notificationIntervals not NULL
|
||||
ret = LocalizeNotificationInterval(tmpIntervals, intervalLen);
|
||||
EXPECT_EQ(ret, NSTACKX_EOK) << "first localize interval fail";
|
||||
|
||||
MOCKER(calloc)
|
||||
.stubs()
|
||||
.will(returnValue(static_cast<void*>(NULL)));
|
||||
ret = LocalizeNotificationInterval(tmpIntervals, intervalLen);
|
||||
EXPECT_EQ(ret, NSTACKX_EOK) << "second localize interval fail";
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestCoapServiceNotificationStop)
|
||||
{
|
||||
// g_notificationIntervals == NULL
|
||||
CoapServiceNotificationStop();
|
||||
|
||||
// g_notificationIntervals != NULL
|
||||
const uint16_t tmpIntervals[5] = {0, 1000, 2000, 3000, 4000};
|
||||
const uint8_t intervalLen = 5;
|
||||
int32_t ret = LocalizeNotificationInterval(tmpIntervals, intervalLen);
|
||||
EXPECT_EQ(ret, NSTACKX_EOK) << "first localize interval fail";
|
||||
CoapServiceNotificationStop();
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 "json_payload.h"
|
||||
#include <securec.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#ifndef DFINDER_USE_MINI_NSTACKX
|
||||
#include "coap_client.h"
|
||||
#endif /* END OF DFINDER_USE_MINI_NSTACKX */
|
||||
#include "nstackx_dfinder_log.h"
|
||||
#include "nstackx_dfinder_mgt_msg_log.h"
|
||||
#include "nstackx_error.h"
|
||||
#include "nstackx_device.h"
|
||||
#include "nstackx_statistics.h"
|
||||
#include "nstackx_device_local.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "mockcpp/mokc.h"
|
||||
|
||||
class Dfinder : public testing::Test {
|
||||
public:
|
||||
void SetUp()
|
||||
{
|
||||
Dfinderinit();
|
||||
}
|
||||
void TearDown()
|
||||
{
|
||||
DfinderDestroy();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(Dfinder, TestPrepareServiceDiscover)
|
||||
{
|
||||
DeviceInfo *deviceInfo = (DeviceInfo *)calloc(1, sizeof(DeviceInfo));
|
||||
char deviceName[11] = "deviceName";
|
||||
strcpy_s(deviceInfo->deviceName, sizeof(deviceInfo->deviceName), deviceName);
|
||||
deviceInfo->deviceType = UINT8_MAX + 1;
|
||||
uint32_t capabilityBitmap[NSTACKX_MAX_CAPABILITY_NUM] = {1};
|
||||
memcpy_s(deviceInfo->capabilityBitmap, sizeof(deviceInfo->capabilityBitmap), capabilityBitmap,
|
||||
sizeof(deviceInfo->capabilityBitmap));
|
||||
MOCKER(GetLocalDeviceInfo).stubs().will(returnValue(deviceInfo));
|
||||
char localIpStr[NSTACKX_MAX_IP_STRING_LEN] = "127.1.1.1";
|
||||
char serviceData[512] =
|
||||
"{"
|
||||
"\"deviceId\":\"{\\\"UDID\\\":\\\"FE73EE0BA926F5A5B149B1567AD2D32BC5443A9FE05FE812CE623283EAE5CEDD\\\"}\","
|
||||
"\"devicename\":\"Mate 50\",\"type\":0,\"seqNo\":1,\"mode\":1,"
|
||||
"\"deviceHash\":\"0\",\"serviceData\":\"\",\"extendServiceData\":\"\",\"wlanIp\":\"127.0.0.1\","
|
||||
"\"capabilityBitmap\":[71],"
|
||||
"\"bType\":0,\"bData\":\"\", \"coapUri\":\"coap://127.0.0.1/device_discover\""
|
||||
"}";
|
||||
char *ret = PrepareServiceDiscover(AF_INET, localIpStr, NSTACKX_TRUE, NSTACKX_BUSINESS_TYPE_SOFTBUS, serviceData);
|
||||
ASSERT_NE(ret, (char *)NULL);
|
||||
cJSON_free(ret);
|
||||
|
||||
// deviceInfo->deviceType <= UINT8_MAX
|
||||
deviceInfo->deviceType = UINT8_MAX;
|
||||
ret = PrepareServiceDiscover(AF_INET, localIpStr, NSTACKX_TRUE, NSTACKX_BUSINESS_TYPE_SOFTBUS, serviceData);
|
||||
ASSERT_NE(ret, (char *)NULL);
|
||||
cJSON_free(ret);
|
||||
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// cJSON_CreateString == NULL
|
||||
MOCKER(GetLocalDeviceInfo).stubs().will(returnValue(deviceInfo));
|
||||
MOCKER(cJSON_CreateString).stubs().will(returnValue((cJSON *)NULL));
|
||||
ret = PrepareServiceDiscover(AF_INET, localIpStr, NSTACKX_TRUE, NSTACKX_BUSINESS_TYPE_SOFTBUS, serviceData);
|
||||
ASSERT_EQ(ret, (char *)NULL);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// cJSON_CreateNumber == NULL
|
||||
MOCKER(GetLocalDeviceInfo).stubs().will(returnValue(deviceInfo));
|
||||
MOCKER(cJSON_CreateNumber).stubs().will(returnValue((cJSON *)NULL));
|
||||
ret = PrepareServiceDiscover(AF_INET, localIpStr, NSTACKX_TRUE, NSTACKX_BUSINESS_TYPE_SOFTBUS, serviceData);
|
||||
ASSERT_EQ(ret, (char *)NULL);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// cJSON_CreateObject == NULL
|
||||
MOCKER(cJSON_CreateObject).stubs().will(returnValue((cJSON *)NULL));
|
||||
ret = PrepareServiceDiscover(AF_INET, localIpStr, NSTACKX_TRUE, NSTACKX_BUSINESS_TYPE_SOFTBUS, serviceData);
|
||||
ASSERT_EQ(ret, (char *)NULL);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
free(deviceInfo);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestParseServiceDiscover)
|
||||
{
|
||||
char buf[512] =
|
||||
"{"
|
||||
"\"deviceId\":\"{\\\"UDID\\\":\\\"FE73EE0BA926F5A5B149B1567AD2D32BC5443A9FE05FE812CE623283EAE5CEDD\\\"}\","
|
||||
"\"devicename\":\"Mate 50\",\"type\":0,\"seqNo\":1,\"mode\":1,"
|
||||
"\"deviceHash\":\"0\",\"serviceData\":\"\",\"extendServiceData\":\"\",\"wlanIp\":\"127.0.0.1\","
|
||||
"\"capabilityBitmap\":[71],"
|
||||
"\"bType\":0,\"bData\":\"\", \"coapUri\":\"coap://127.0.0.1/device_discover\""
|
||||
"}";
|
||||
DeviceInfo *deviceInfo = (DeviceInfo *)calloc(1, sizeof(DeviceInfo));
|
||||
char *remoteUrl = NULL;
|
||||
int32_t ret = ParseServiceDiscover((uint8_t *)buf, deviceInfo, &remoteUrl);
|
||||
free(remoteUrl);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// strdup is error
|
||||
MOCKER(strdup).stubs().will(returnValue((char *)NULL));
|
||||
ret = ParseServiceDiscover((uint8_t *)buf, deviceInfo, &remoteUrl);
|
||||
GlobalMockObject::verify();
|
||||
ASSERT_EQ(ret, NSTACKX_ENOMEM);
|
||||
|
||||
// cJSON_IsString is false, 0 is false
|
||||
MOCKER(cJSON_IsString).stubs().will(returnValue(0));
|
||||
ret = ParseServiceDiscover((uint8_t *)buf, deviceInfo, &remoteUrl);
|
||||
GlobalMockObject::verify();
|
||||
ASSERT_EQ(ret, NSTACKX_EINVAL);
|
||||
|
||||
// strcpy_s != EOK
|
||||
MOCKER(strcpy_s).stubs().will(returnValue(-1));
|
||||
ret = ParseServiceDiscover((uint8_t *)buf, deviceInfo, &remoteUrl);
|
||||
GlobalMockObject::verify();
|
||||
ASSERT_EQ(ret, NSTACKX_EINVAL);
|
||||
|
||||
// cJSON_IsNumber is false, 0 is false
|
||||
MOCKER(cJSON_IsNumber).stubs().will(returnValue(0));
|
||||
ret = ParseServiceDiscover((uint8_t *)buf, deviceInfo, &remoteUrl);
|
||||
GlobalMockObject::verify();
|
||||
ASSERT_EQ(ret, NSTACKX_EINVAL);
|
||||
|
||||
free(deviceInfo);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestParseServiceDiscover_RecvMsgWithHicomVersion)
|
||||
{
|
||||
char recvedMsg[512] =
|
||||
"{"
|
||||
"\"deviceId\":\"{\\\"UDID\\\":\\\"FE73EE0BA926F5A5B149B1567AD2D32BC5443A9FE05FE812CE623283EAE5CEDD\\\"}\","
|
||||
"\"devicename\":\"Mate 50\",\"type\":0,\"seqNo\":1,\"hicomversion\":\"3.1.0.0\",\"mode\":1,"
|
||||
"\"deviceHash\":\"0\",\"serviceData\":\"\",\"extendServiceData\":\"\",\"wlanIp\":\"127.0.0.1\","
|
||||
"\"capabilityBitmap\":[71],"
|
||||
"\"bType\":0,\"bData\":\"\", \"coapUri\":\"coap://127.0.0.1/device_discover\""
|
||||
"}";
|
||||
|
||||
DeviceInfo *deviceInfo = (DeviceInfo *)calloc(1, sizeof(DeviceInfo));
|
||||
ASSERT_NE(deviceInfo, nullptr);
|
||||
|
||||
char *remoteUrl = NULL;
|
||||
int32_t ret = ParseServiceDiscover((uint8_t *)recvedMsg, deviceInfo, &remoteUrl);
|
||||
free(deviceInfo);
|
||||
free(remoteUrl);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestParseServiceNotification)
|
||||
{
|
||||
char buf[] =
|
||||
"{\"notify\":\"{\\\"UDID\\\":\\\"FE73EE0BA926F5A5B149B1567AD2D32BC5443A9FE05FE812CE623283EAE5CEDD\\\"}\"}";
|
||||
// buf NULL
|
||||
int32_t ret = ParseServiceNotification(NULL, NULL);
|
||||
EXPECT_EQ(ret, NSTACKX_EINVAL) << "input buf null";
|
||||
|
||||
char tmpMsg[NSTACKX_MAX_NOTIFICATION_DATA_LEN] = {0};
|
||||
NSTACKX_NotificationConfig config = {
|
||||
msg : tmpMsg,
|
||||
msgLen : 0,
|
||||
intervalsMs : NULL,
|
||||
intervalLen : 0,
|
||||
businessType : NSTACKX_BUSINESS_TYPE_NULL
|
||||
};
|
||||
|
||||
// cJSON_Parse fail
|
||||
MOCKER(cJSON_Parse).stubs().will(returnValue((cJSON *)NULL));
|
||||
ret = ParseServiceNotification((const uint8_t *)buf, &config);
|
||||
EXPECT_EQ(ret, NSTACKX_EINVAL) << "case cJSON_Parse fail";
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// cJSON_GetObjectItemCaseSensitive fail
|
||||
MOCKER(cJSON_GetObjectItemCaseSensitive).stubs().will(returnValue((cJSON *)NULL));
|
||||
ret = ParseServiceNotification((const uint8_t *)buf, &config);
|
||||
EXPECT_EQ(ret, NSTACKX_EFAILED) << "case cJSON_GetObjectItemCaseSensitive fail";
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// cJSON_IsString fail
|
||||
MOCKER(cJSON_IsString).stubs().will(returnValue((cJSON_bool)false));
|
||||
ret = ParseServiceNotification((const uint8_t *)buf, &config);
|
||||
EXPECT_EQ(ret, NSTACKX_EFAILED) << "case cJSON_IsString fail";
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// strcpy_s fail
|
||||
MOCKER(strcpy_s).stubs().will(returnValue(EINVAL));
|
||||
ret = ParseServiceNotification((const uint8_t *)buf, &config);
|
||||
EXPECT_EQ(ret, NSTACKX_EFAILED) << "case strcpy_s fail";
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestPrepareServiceNotification)
|
||||
{
|
||||
// cJSON_CreateObject fail
|
||||
MOCKER(cJSON_CreateObject).stubs().will(returnValue((cJSON *)NULL));
|
||||
char *ret = PrepareServiceNotification();
|
||||
EXPECT_EQ(ret, (char *)NULL) << "case cJSON_CreateObject fail";
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// JsonAddStr fail
|
||||
DeviceInfo tmpDeviceInfo = {0};
|
||||
MOCKER(GetLocalDeviceInfo).stubs().will(returnValue(&tmpDeviceInfo));
|
||||
MOCKER(cJSON_CreateString).stubs().will(returnValue((cJSON *)NULL));
|
||||
ret = PrepareServiceNotification();
|
||||
EXPECT_EQ(ret, (char *)NULL) << "JsonAddStr fail";
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// all ok
|
||||
tmpDeviceInfo = {0};
|
||||
MOCKER(GetLocalDeviceInfo).stubs().will(returnValue(&tmpDeviceInfo));
|
||||
ret = PrepareServiceNotification();
|
||||
EXPECT_NE(ret, (char *)NULL) << "case cJSON_PrintUnformatted fail";
|
||||
if (ret != NULL) {
|
||||
free(ret);
|
||||
}
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,349 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 "nstackx_device_local.h"
|
||||
#include "nstackx_device_remote.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "mockcpp/mokc.h"
|
||||
#include "ut_utils.h"
|
||||
|
||||
TEST(Dfinder, TestLocalDeviceInit)
|
||||
{
|
||||
EpollDesc epollDesc = CreateEpollDesc();
|
||||
|
||||
// g_localDevice.timer == NULL
|
||||
MOCKER(TimerStart).stubs().will(returnValue((Timer *)NULL));
|
||||
int ret = LocalDeviceInit(epollDesc);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// all is ok
|
||||
ret = LocalDeviceInit(epollDesc);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
LocalDeviceDeinit();
|
||||
}
|
||||
|
||||
static Timer *g_timer;
|
||||
static Timer *InvokeTimerStart(EpollDesc epollfd, uint32_t ms, uint8_t repeated, TimeoutHandle handle, void *data)
|
||||
{
|
||||
handle(data);
|
||||
g_timer = (Timer *)malloc(1*sizeof(Timer));
|
||||
return g_timer;
|
||||
}
|
||||
|
||||
class Dfinder : public testing::Test {
|
||||
public:
|
||||
void SetUp()
|
||||
{
|
||||
Dfinderinit();
|
||||
}
|
||||
void TearDown()
|
||||
{
|
||||
DfinderDestroy();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(Dfinder, TestAddLocalIface)
|
||||
{
|
||||
(void)LocalDeviceInit(CreateEpollDesc());
|
||||
|
||||
// iface->ctx == NULL
|
||||
union InetAddr addr;
|
||||
union InetAddr addr1;
|
||||
struct in_addr ipv4Addr;
|
||||
struct in_addr ipv4Addr1;
|
||||
inet_pton(AF_INET, "127.0.0.1", &ipv4Addr);
|
||||
addr.in = ipv4Addr;
|
||||
char serviceData[NSTACKX_MAX_SERVICE_DATA_LEN] = "data";
|
||||
int ret = AddLocalIface("eth0", serviceData, AF_INET, &addr);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
|
||||
CoapCtxType *ctx = (CoapCtxType *)malloc(1 * sizeof(CoapCtxType));
|
||||
ctx->ctx = CreateCoapContext();
|
||||
MOCKER(CoapServerInit).stubs().will(returnValue(ctx));
|
||||
|
||||
ret = AddLocalIface("eth", serviceData, AF_INET, &addr); // eth 127.0.0.1 添加到g_localDevice.readyList里
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
ret = AddLocalIface("wlan", serviceData, AF_INET, &addr); /** wlan 127.0.0.1 添加到g_localDevice.readyList里 */
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// iface == NULL and then iface != NULL
|
||||
RemoveLocalIface(AF_INET, "eth"); // // eth 127.0.0.1 添加到g_localDevice.destroyList里
|
||||
ret = AddLocalIface("eth", serviceData, AF_INET, &addr); // eth 127.0.0.1 添加到g_localDevice.readyList里
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// ip.s_addr == ip->s_addr
|
||||
ret = AddLocalIface("eth", serviceData, AF_INET, &addr); // 没有添加到任何list里
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// ip.s_addr != ip->s_addr
|
||||
inet_pton(AF_INET, "127.0.0.2", &ipv4Addr1);
|
||||
addr1.in = ipv4Addr1;
|
||||
ret = AddLocalIface("eth", serviceData, AF_INET, &addr1); /** eth 127.0.0.2 添加到g_localDevice.destroyList里 */
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// inet_ntop(AF_INET, ip, ipStr, sizeof(ipStr)) == NULL
|
||||
MOCKER(inet_ntop).stubs().will(returnValue((char const*)NULL));
|
||||
ret = AddLocalIface("eth", serviceData, AF_INET, &addr); /** eth 127.0.0.1 添加到g_localDevice.destroyList里 */
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// !NeedCreateSynchronously(iface->type), 即GetIfaceType >= IFACE_TYPE_P2P
|
||||
MOCKER(GetIfaceType).stubs().will(returnValue(2));
|
||||
ret = AddLocalIface("p2p", serviceData, AF_INET, &addr); // 没有添加到任何list里
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// !NeedCreateSynchronously(iface->type), 即GetIfaceType >= IFACE_TYPE_P2P
|
||||
MOCKER(TimerStart).stubs().will(invoke(InvokeTimerStart));
|
||||
ret = AddLocalIface("p2p", serviceData, AF_INET, &addr);/** p2p 127.0.0.1 添加到g_localDevice.creatingList里 */
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
free(g_timer);
|
||||
FreeCoapContext(ctx->ctx);
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestLocalIfaceDump)
|
||||
{
|
||||
char buf[2048];
|
||||
// 与TestAddLocalIface方法挂钩,遍历g_localDevice3个列表:creatingList、readyList、destroyList
|
||||
int ret = LocalIfaceDump(buf, 2048);
|
||||
ASSERT_EQ(ret, 181);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestLocalIfaceGetCoapCtx)
|
||||
{
|
||||
// 与TestAddLocalIface方法挂钩,遍历g_localDevice.readyList列表
|
||||
CoapCtxType *coapCtxType = LocalIfaceGetCoapCtx(AF_INET, "eth");
|
||||
ASSERT_EQ(coapCtxType, nullptr);
|
||||
}
|
||||
|
||||
bool mHasLocalIface = true;
|
||||
int32_t InvokeGetTargetInterface(const struct sockaddr_in *dstAddr, struct ifreq *localDev)
|
||||
{
|
||||
// 与TestAddLocalIface方法挂钩,LocalIfaceGetCoapCtxByRemoteIp会从g_localDevice.readyList里查找
|
||||
if (mHasLocalIface) {
|
||||
(void)strcpy_s(localDev->ifr_ifrn.ifrn_name, IFNAMSIZ, "wlan");
|
||||
} else {
|
||||
(void)strcpy_s(localDev->ifr_ifrn.ifrn_name, IFNAMSIZ, "eth");
|
||||
}
|
||||
return NSTACKX_EOK;
|
||||
}
|
||||
TEST(Dfinder, TestLocalIfaceGetCoapCtxByRemoteIp)
|
||||
{
|
||||
char ipStr[INET_ADDRSTRLEN] = "127.0.0.1"; // host:port
|
||||
struct in_addr remoteIp;
|
||||
inet_pton(AF_INET, ipStr, &remoteIp);
|
||||
|
||||
// GetTargetInterface != NSTACKX_EOK
|
||||
CoapCtxType *coapCtxType = LocalIfaceGetCoapCtxByRemoteIp(&remoteIp, SERVER_TYPE_WLANORETH);
|
||||
ASSERT_EQ(coapCtxType, nullptr);
|
||||
|
||||
// all is ok
|
||||
MOCKER(GetTargetInterface).stubs().will(invoke(InvokeGetTargetInterface));
|
||||
coapCtxType = LocalIfaceGetCoapCtxByRemoteIp(&remoteIp, SERVER_TYPE_WLANORETH);
|
||||
ASSERT_NE(coapCtxType, nullptr);
|
||||
|
||||
// !IfaceTypeIsMatch
|
||||
coapCtxType = LocalIfaceGetCoapCtxByRemoteIp(&remoteIp, SERVER_TYPE_USB + 1);
|
||||
ASSERT_EQ(coapCtxType, nullptr);
|
||||
|
||||
// iface == NULL
|
||||
mHasLocalIface = false;
|
||||
MOCKER(GetTargetInterface).stubs().will(invoke(InvokeGetTargetInterface));
|
||||
coapCtxType = LocalIfaceGetCoapCtxByRemoteIp(&remoteIp, SERVER_TYPE_WLANORETH);
|
||||
ASSERT_EQ(coapCtxType, nullptr);
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
uint8_t mBusinessType = 10;
|
||||
const char mDeviceId[] = "deviceId";
|
||||
char mNetworkName[NSTACKX_MAX_INTERFACE_NAME_LEN] = "eth";
|
||||
TEST(Dfinder, TestRegisterLocalDeviceV2)
|
||||
{
|
||||
NSTACKX_LocalDeviceInfoV2 devInfo = {0};
|
||||
// strcpy_s(devInfo->deviceId) != EOK
|
||||
int ret = RegisterLocalDeviceV2(&devInfo, REGISTER_TYPE_UPDATE_ALL);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
|
||||
// name[0] == '\0'
|
||||
devInfo.deviceId = mDeviceId;
|
||||
devInfo.name = "";
|
||||
ret = RegisterLocalDeviceV2(&devInfo, REGISTER_TYPE_UPDATE_ALL);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
devInfo.name = "name";
|
||||
devInfo.ifNums = 1;
|
||||
devInfo.hasDeviceHash = true;
|
||||
devInfo.deviceHash = 1;
|
||||
devInfo.businessType = mBusinessType;
|
||||
NSTACKX_InterfaceInfo info = {0};
|
||||
devInfo.localIfInfo = &info;
|
||||
|
||||
// ifInfo->networkName[0] == '\0'
|
||||
(void)strcpy_s(info.networkName, NSTACKX_MAX_INTERFACE_NAME_LEN, "");
|
||||
ret = RegisterLocalDeviceV2(&devInfo, REGISTER_TYPE_UPDATE_ALL);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// inet_pton(AF_INET, ifInfo[i].networkIpAddr, &ip) != 1
|
||||
(void)strcpy_s(info.networkName, NSTACKX_MAX_INTERFACE_NAME_LEN, mNetworkName);
|
||||
(void)strcpy_s(info.networkIpAddr, NSTACKX_MAX_IP_STRING_LEN, "127");
|
||||
ret = RegisterLocalDeviceV2(&devInfo, REGISTER_TYPE_UPDATE_ALL);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
|
||||
// ip.s_addr == 0
|
||||
(void)strcpy_s(info.networkIpAddr, NSTACKX_MAX_IP_STRING_LEN, "0.0.0.0");
|
||||
ret = RegisterLocalDeviceV2(&devInfo, REGISTER_TYPE_UPDATE_ALL);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// all is ok
|
||||
(void)strcpy_s(info.networkName, NSTACKX_MAX_INTERFACE_NAME_LEN, mNetworkName);
|
||||
(void)strcpy_s(info.networkIpAddr, NSTACKX_MAX_IP_STRING_LEN, "127.0.0.1");
|
||||
ret = RegisterLocalDeviceV2(&devInfo, REGISTER_TYPE_UPDATE_ALL);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
ret = RegisterLocalDeviceV2(&devInfo, REGISTER_TYPE_UPDATE_SPECIFIED);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestGetLocalDeviceBusinessType)
|
||||
{
|
||||
uint8_t ret = GetLocalDeviceBusinessType();
|
||||
ASSERT_EQ(ret, mBusinessType);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestGetLocalDeviceId)
|
||||
{
|
||||
const char *ret = GetLocalDeviceId();
|
||||
ASSERT_EQ(*ret, *mDeviceId);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestGetLocalDeviceInfo)
|
||||
{
|
||||
DeviceInfo *deviceInfo = GetLocalDeviceInfo();
|
||||
ASSERT_EQ(*(deviceInfo->deviceId), *mDeviceId);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestSetLocalDeviceCapability)
|
||||
{
|
||||
// all is ok
|
||||
uint32_t bitmapNum=1;
|
||||
uint32_t bitmap[]={0};
|
||||
int ret = SetLocalDeviceCapability(bitmapNum, bitmap);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// memcpy_s is error
|
||||
MOCKER(memcpy_s).stubs().will(returnValue(-1));
|
||||
ret = SetLocalDeviceCapability(bitmapNum, bitmap);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestSetLocalDeviceServiceData)
|
||||
{
|
||||
// all is ok
|
||||
int32_t ret = SetLocalDeviceServiceData("serviceData");
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// strcpy_s is error
|
||||
MOCKER(strcpy_s).stubs().will(returnValue(NSTACKX_EFAILED));
|
||||
ret = SetLocalDeviceServiceData("serviceData");
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestSetLocalDeviceBusinessData)
|
||||
{
|
||||
// all is ok
|
||||
int ret = SetLocalDeviceBusinessData("data", false);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// strcpy_s is error
|
||||
MOCKER(strcpy_s).stubs().will(returnValue(NSTACKX_EFAILED));
|
||||
ret = SetLocalDeviceBusinessData("data", true);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestLocalDeviceMode)
|
||||
{
|
||||
uint8_t mode = 1;
|
||||
SetLocalDeviceMode(mode);
|
||||
uint8_t ret = GetLocalDeviceMode();
|
||||
ASSERT_EQ(ret, mode);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestSetLocalDeviceExtendServiceData)
|
||||
{
|
||||
// all is ok
|
||||
int32_t ret = SetLocalDeviceExtendServiceData("extendServiceData");
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// strcpy_s is error
|
||||
MOCKER(strcpy_s).stubs().will(returnValue(NSTACKX_EFAILED));
|
||||
ret = SetLocalDeviceExtendServiceData("extendServiceData");
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestGetLocalIfaceIpStr)
|
||||
{
|
||||
LocalIface *localIface = CreateLocalIface();
|
||||
const char *ipStr = GetLocalIfaceIpStr(localIface);
|
||||
ASSERT_EQ(localIface->ipStr, ipStr);
|
||||
|
||||
FreeLocalIface(localIface);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestGetLocalIfaceName)
|
||||
{
|
||||
LocalIface *localIface = CreateLocalIface();
|
||||
const char *name = GetLocalIfaceName(localIface);
|
||||
ASSERT_EQ(*localIface->ifname, *name);
|
||||
|
||||
FreeLocalIface(localIface);
|
||||
}
|
||||
|
||||
void InvokeDestroyRxIfaceByIfname(const char *ifName)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void InvokeCoapServerDestroy(CoapCtxType *ctx, bool moduleDeinit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestLocalizeNotificationMsg)
|
||||
{
|
||||
const char *msg = "auv";
|
||||
|
||||
// strcpy_s fail
|
||||
MOCKER(strcpy_s).stubs().will(returnValue(EINVAL));
|
||||
int32_t ret = LocalizeNotificationMsg(msg);
|
||||
EXPECT_EQ(ret, NSTACKX_EFAILED) << "case strcpy_s fail";
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// all ok
|
||||
MOCKER(strcpy_s).stubs().will(returnValue(EOK));
|
||||
ret = LocalizeNotificationMsg(msg);
|
||||
EXPECT_EQ(ret, NSTACKX_EOK) << "case all ok fail";
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 "nstackx_device_remote.h"
|
||||
#include "nstackx_error.h"
|
||||
#include "nstackx_timer.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "mockcpp/mokc.h"
|
||||
|
||||
class Dfinder : public testing::Test {
|
||||
public:
|
||||
void SetUp()
|
||||
{
|
||||
Dfinderinit();
|
||||
}
|
||||
void TearDown()
|
||||
{
|
||||
DfinderDestroy();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(Dfinder, TestRemoteDeviceListInit)
|
||||
{
|
||||
// all is ok
|
||||
int32_t ret = RemoteDeviceListInit();
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestGetRemoteNodeCount)
|
||||
{
|
||||
uint32_t count = GetRemoteNodeCount();
|
||||
ASSERT_EQ(count, 0);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestUpdateRemoteNodeByDeviceInfo)
|
||||
{
|
||||
NSTACKX_InterfaceInfo ifaceInfo = {0};
|
||||
strcpy_s(ifaceInfo.networkName, sizeof(ifaceInfo.networkName), "eth");
|
||||
strcpy_s(ifaceInfo.networkIpAddr, sizeof(ifaceInfo.networkIpAddr), "127.0.0.1");
|
||||
|
||||
NSTACKX_InterfaceInfo ifaceInfo1 = {0};
|
||||
strcpy_s(ifaceInfo1.networkName, sizeof(ifaceInfo1.networkName), "eth");
|
||||
strcpy_s(ifaceInfo1.networkIpAddr, sizeof(ifaceInfo1.networkIpAddr), "127.0.0.2");
|
||||
|
||||
NSTACKX_InterfaceInfo ifaceInfo2 = {0};
|
||||
strcpy_s(ifaceInfo2.networkName, sizeof(ifaceInfo2.networkName), "eth");
|
||||
strcpy_s(ifaceInfo2.networkIpAddr, sizeof(ifaceInfo2.networkIpAddr), "127.0.0.3");
|
||||
|
||||
NSTACKX_InterfaceInfo ifaceInfo3 = {0};
|
||||
strcpy_s(ifaceInfo3.networkName, sizeof(ifaceInfo3.networkName), "eth");
|
||||
strcpy_s(ifaceInfo3.networkIpAddr, sizeof(ifaceInfo3.networkIpAddr), "127.0.0.4");
|
||||
|
||||
NSTACKX_InterfaceInfo ifaceInfo4 = {0};
|
||||
strcpy_s(ifaceInfo4.networkName, sizeof(ifaceInfo4.networkName), "eth");
|
||||
strcpy_s(ifaceInfo4.networkIpAddr, sizeof(ifaceInfo4.networkIpAddr), "127.0.0.5");
|
||||
|
||||
BusinessDataAll businessData = { 0 };
|
||||
businessData.isBroadcast = NSTACKX_FALSE;
|
||||
strcpy_s(businessData.businessDataBroadcast, sizeof(businessData.businessDataBroadcast), "Broadcast");
|
||||
strcpy_s(businessData.businessDataUnicast, sizeof(businessData.businessDataUnicast), "Unicast");
|
||||
DeviceInfo deviceInfo = { 0 };
|
||||
|
||||
deviceInfo.businessData = businessData;
|
||||
deviceInfo.netChannelInfo.wifiApInfo.af = AF_INET;
|
||||
deviceInfo.deviceType = NSTACKX_DISCOVERY_TYPE_PASSIVE;
|
||||
deviceInfo.capabilityBitmapNum = 1;
|
||||
uint8_t updated = NSTACKX_FALSE;
|
||||
|
||||
// CreateRemoteDevice == NULL, (1)calloc is error
|
||||
MOCKER(calloc).stubs().will(returnValue((void *)NULL));
|
||||
int32_t ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// CreateRemoteDevice == NULL, (2)strcpy_s is error
|
||||
MOCKER(strcpy_s).stubs().will(returnValue(NSTACKX_EFAILED));
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// g_remoteNodeCount == GetMaxDeviceNum() && CheckAndRemoveAgingNode() != NSTACKX_EOK
|
||||
MOCKER(GetMaxDeviceNum).stubs().will(returnValue(GetRemoteNodeCount()));
|
||||
SetDeviceListAgingTime(2);
|
||||
MOCKER(GetTimeDiffMs).stubs().will(returnValue(1001));
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// all is ok, add list node
|
||||
SetDeviceListAgingTime(1);
|
||||
SetMaxDeviceNum(10);
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
usleep(2000);
|
||||
|
||||
// UpdateRemoteNode, isBroadcast == NSTACKX_TRUE
|
||||
BusinessDataAll businessData1 = {0};
|
||||
businessData1.isBroadcast = NSTACKX_TRUE;
|
||||
strcpy_s(businessData1.businessDataBroadcast, sizeof(businessData1.businessDataBroadcast), "Broadcast1");
|
||||
strcpy_s(businessData1.businessDataUnicast, sizeof(businessData1.businessDataUnicast), "Unicast1");
|
||||
DeviceInfo deviceInfo1 = {0};
|
||||
strcpy_s(deviceInfo1.deviceId, sizeof(deviceInfo1.deviceId), "deviceId");
|
||||
deviceInfo.netChannelInfo.wifiApInfo.af = AF_INET;
|
||||
deviceInfo.netChannelInfo.wifiApInfo.addr.in.s_addr = 778;
|
||||
deviceInfo1.businessData = businessData1;
|
||||
deviceInfo1.deviceType = NSTACKX_DISCOVERY_TYPE_PASSIVE;
|
||||
deviceInfo1.netChannelInfo.wifiApInfo.af = AF_INET;
|
||||
deviceInfo1.capabilityBitmapNum = 1;
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo1, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// UpdateRemoteNode, isBroadcast != NSTACKX_TRUE
|
||||
BusinessDataAll businessData2 = { 0 };
|
||||
businessData2.isBroadcast = NSTACKX_FALSE;
|
||||
strcpy_s(businessData2.businessDataBroadcast, sizeof(businessData2.businessDataBroadcast), "Broadcast2");
|
||||
strcpy_s(businessData2.businessDataUnicast, sizeof(businessData2.businessDataUnicast), "Unicast2");
|
||||
DeviceInfo deviceInfo2 = { 0 };
|
||||
strcpy_s(deviceInfo2.deviceId, sizeof(deviceInfo2.deviceId), "deviceId");
|
||||
deviceInfo2.deviceType = NSTACKX_DISCOVERY_TYPE_PASSIVE;
|
||||
deviceInfo2.businessData = businessData2;
|
||||
deviceInfo2.netChannelInfo.wifiApInfo.af = AF_INET;
|
||||
deviceInfo2.capabilityBitmapNum = 1;
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo2, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// UpdateRemoteNode, businessType != newInfo->businessType
|
||||
deviceInfo.businessType = 1;
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// UpdateRemoteNode, serviceData != newInfo->serviceData
|
||||
strcpy_s(deviceInfo.serviceData, sizeof(deviceInfo.serviceData), "serviceData");
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// UpdateRemoteNode, mode != newInfo->mode
|
||||
deviceInfo.mode = 1;
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// UpdateRemoteNode, deviceHash != newInfo->deviceHash
|
||||
strcpy_s(deviceInfo.deviceHash, sizeof(deviceInfo.deviceHash), "deviceHash");
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// UpdateRemoteNode, capabilityBitmapNum > 0
|
||||
deviceInfo.capabilityBitmapNum = 1;
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// UpdateRemoteNode, capabilityBitmapNum > 0, memcpy_s is error
|
||||
MOCKER(memcpy_s).stubs().will(returnValue(-1));
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// UpdateRemoteNode, deviceName != newInfo->deviceName
|
||||
strcpy_s(deviceInfo.deviceName, sizeof(deviceInfo.deviceName), "deviceName");
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// CheckAndUpdateRemoteNodeChangeState, discoveryType == NSTACKX_DISCOVERY_TYPE_PASSIVE
|
||||
deviceInfo.discoveryType = NSTACKX_DISCOVERY_TYPE_PASSIVE;
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// UpdateRemoteNode, deviceType != newInfo->deviceType
|
||||
deviceInfo.deviceType = NSTACKX_DISCOVERY_TYPE_ACTIVE;
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
|
||||
// rxIface->remoteNodeCnt >= RX_IFACE_REMOTE_NODE_COUNT, 调用5次UpdateRemoteNodeByDeviceInfo,每次传的remoteIp不同
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
|
||||
deviceInfo.deviceType = NSTACKX_DISCOVERY_TYPE_PASSIVE;
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo1, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo2, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo3, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo4, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// insert node into list
|
||||
/** eth */
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo1, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo2, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
/** wlan */
|
||||
NSTACKX_InterfaceInfo ifaceInfo5 = {0};
|
||||
strcpy_s(ifaceInfo5.networkName, sizeof(ifaceInfo5.networkName), "wlan");
|
||||
strcpy_s(ifaceInfo5.networkIpAddr, sizeof(ifaceInfo5.networkIpAddr), "127.0.0.1");
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo5, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
NSTACKX_InterfaceInfo ifaceInfo6 = {0};
|
||||
strcpy_s(ifaceInfo6.networkName, sizeof(ifaceInfo6.networkName), "wlan");
|
||||
strcpy_s(ifaceInfo6.networkIpAddr, sizeof(ifaceInfo6.networkIpAddr), "127.0.0.2");
|
||||
ret = UpdateRemoteNodeByDeviceInfo("deviceId", &ifaceInfo, &deviceInfo, &updated);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestGetRemoteDeviceIp)
|
||||
{
|
||||
const struct in_addr *remoteIp = GetRemoteDeviceIp("deviceId");
|
||||
ASSERT_NE(remoteIp, nullptr);
|
||||
|
||||
remoteIp = GetRemoteDeviceIp("deviceId1");
|
||||
ASSERT_EQ(remoteIp, nullptr);
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestDumpRemoteDevice)
|
||||
{
|
||||
char buf[2048];
|
||||
(void)memset_s(buf, 2048, 0, 2048);
|
||||
int32_t ret = DumpRemoteDevice(buf, 2048);
|
||||
ASSERT_EQ(ret, 571);
|
||||
ret = DumpRemoteDevice(buf, 0);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 "nstackx_dfinder_hidump.h"
|
||||
#include "nstackx_util.h"
|
||||
#include "nstackx_getopt.h"
|
||||
#include "nstackx_event.h"
|
||||
#include "nstackx_device_local.h"
|
||||
#include "nstackx_device_remote.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "mockcpp/mokc.h"
|
||||
#include "ut_utils.h"
|
||||
|
||||
class Dfinder : public testing::Test {
|
||||
public:
|
||||
void SetUp()
|
||||
{
|
||||
Dfinderinit();
|
||||
}
|
||||
void TearDown()
|
||||
{
|
||||
DfinderDestroy();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(Dfinder, TestDumpDeviceInfo)
|
||||
{
|
||||
DeviceInfo deviceInfo = {0};
|
||||
(void)strcpy_s(deviceInfo.deviceId, NSTACKX_MAX_DEVICE_ID_LEN, "deviceId");
|
||||
char buf[2048];
|
||||
|
||||
// all is ok
|
||||
int ret = DumpDeviceInfo(&deviceInfo, buf, 2048, 1);
|
||||
ASSERT_EQ(ret, 99);
|
||||
|
||||
// memcpy_s is error
|
||||
MOCKER(memcpy_s).stubs().will(returnValue(-1));
|
||||
ret = DumpDeviceInfo(&deviceInfo, buf, 2048, 1);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestDFinderDumpIface)
|
||||
{
|
||||
char buf[2048];
|
||||
char ifname[] = "eth";
|
||||
LocalIface *localIface = CreateLocalIface();
|
||||
union InetAddr ipAddr;
|
||||
ipAddr.in = localIface->addr.in;
|
||||
struct DumpIfaceInfo info = {
|
||||
.ifname = "eth",
|
||||
.state = 1,
|
||||
.af = AF_INET,
|
||||
.addr = &ipAddr
|
||||
};
|
||||
|
||||
|
||||
// all is ok
|
||||
int ret = DFinderDumpIface(&info, buf, sizeof(buf));
|
||||
ASSERT_GT(ret, 0);
|
||||
|
||||
// IpAddrAnonymousFormat < 0
|
||||
MOCKER(IpAddrAnonymousFormat).stubs().will(returnValue(-10));
|
||||
ret = DFinderDumpIface(&info, buf, sizeof(buf));
|
||||
ASSERT_EQ(ret, -10);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// Test IPv6
|
||||
union InetAddr ipv6Addr;
|
||||
ipv6Addr.in6 = in6addr_any;
|
||||
struct DumpIfaceInfo ipv6Info = {
|
||||
.ifname = "eth6",
|
||||
.state = 1,
|
||||
.af = AF_INET6,
|
||||
.addr = &ipv6Addr
|
||||
};
|
||||
ret = DFinderDumpIface(&ipv6Info, buf, sizeof(buf));
|
||||
ASSERT_NE(ret, NSTACKX_EFAILED);
|
||||
|
||||
FreeLocalIface(localIface);
|
||||
}
|
||||
|
||||
static void DFinderDumpFuncCb(void *softObj, const char *data, uint32_t len)
|
||||
{
|
||||
(void)softObj;
|
||||
(void)len;
|
||||
}
|
||||
int32_t InvokePostEvent(const List *eventNodeChain, EpollDesc epollfd, EventHandle handle, void *arg)
|
||||
{
|
||||
handle(arg);
|
||||
return NSTACKX_EOK;
|
||||
}
|
||||
TEST(Dfinder, TestDFinderDump)
|
||||
{
|
||||
const char *argv[] = {"", "-f", "-h", "-l", "-r", "-s", "-m", "0", ""};
|
||||
char softObj[2048];
|
||||
|
||||
MOCKER(PostEvent).stubs().will(invoke(InvokePostEvent));
|
||||
MOCKER(LocalIfaceDump).stubs().will(returnValue(NSTACKX_EOK));
|
||||
MOCKER(DumpRemoteDevice).stubs().will(returnValue(NSTACKX_EOK));
|
||||
|
||||
// EnableMgtMsgLog enable == 0
|
||||
int ret = DFinderDump(argv, sizeof(argv)/sizeof(argv[0]), (void *)softObj, DFinderDumpFuncCb);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// EnableMgtMsgLog enable == 1
|
||||
const char *argv1[] = {"", "-f", "-h", "-l", "-r", "-s", "-m", "1", ""};
|
||||
ret = DFinderDump(argv1, sizeof(argv1)/sizeof(argv1[0]), (void *)softObj, DFinderDumpFuncCb);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// EnableMgtMsgLog strlen(optMsg) != 1
|
||||
const char *argv2[] = {"", "-f", "-h", "-l", "-r", "-s", "-m", ""};
|
||||
ret = DFinderDump(argv2, sizeof(argv2)/sizeof(argv2[0]), (void *)softObj, DFinderDumpFuncCb);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
|
||||
// NstackGetOpt is default
|
||||
MOCKER(NstackGetOpt).stubs().will(returnValue(0));
|
||||
ret = DFinderDump(argv, sizeof(argv)/sizeof(argv[0]), (void *)softObj, DFinderDumpFuncCb);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// CreateDumpMsg == NULL, that is SemInit error
|
||||
MOCKER(SemInit).stubs().will(returnValue(NSTACKX_EFAILED));
|
||||
ret = DFinderDump(argv, sizeof(argv)/sizeof(argv[0]), (void *)softObj, DFinderDumpFuncCb);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// PostEvent is error
|
||||
MOCKER(PostEvent).stubs().will(returnValue(NSTACKX_EFAILED));
|
||||
ret = DFinderDump(argv, sizeof(argv)/sizeof(argv[0]), (void *)softObj, DFinderDumpFuncCb);
|
||||
ASSERT_EQ(ret, NSTACKX_EFAILED);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
// argc = 1
|
||||
ret = DFinderDump(nullptr, 1, (void *)&softObj, DFinderDumpFuncCb);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
|
||||
// argc = 1 and calloc is error
|
||||
MOCKER(calloc).stubs().will(returnValue((void *)NULL));
|
||||
ret = DFinderDump(nullptr, 1, (void *)&softObj, DFinderDumpFuncCb);
|
||||
ASSERT_EQ(ret, NSTACKX_EOK);
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) 2024 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 "nstackx_dfinder_hievent.h"
|
||||
#include "nstackx_getopt.h"
|
||||
#include "nstackx_event.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "mockcpp/mokc.h"
|
||||
|
||||
static int g_eventCnt = 0;
|
||||
static void TestDFinderEventFuncCb(void *softObj, const DFinderEvent *info)
|
||||
{
|
||||
(void)softObj;
|
||||
(void)info;
|
||||
g_eventCnt++;
|
||||
}
|
||||
|
||||
static int32_t InvokePostEvent(const List *eventNodeChain,
|
||||
EpollDesc epollfd, EventHandle handle, void *arg)
|
||||
{
|
||||
handle(arg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
class Dfinder : public testing::Test {
|
||||
public:
|
||||
void SetUp()
|
||||
{
|
||||
Dfinderinit();
|
||||
}
|
||||
void TearDown()
|
||||
{
|
||||
DfinderDestroy();
|
||||
}
|
||||
};
|
||||
|
||||
TEST(Dfinder, TestSetEventFunc)
|
||||
{
|
||||
MOCKER(PostEvent).stubs().will(invoke(InvokePostEvent));
|
||||
ASSERT_EQ(SetEventFunc(NULL, TestDFinderEventFuncCb), 0);
|
||||
ASSERT_EQ(SetEventFunc(NULL, NULL), 0);
|
||||
GlobalMockObject::verify();
|
||||
|
||||
MOCKER(PostEvent).stubs().will(returnValue(-1));
|
||||
ASSERT_NE(SetEventFunc(NULL, TestDFinderEventFuncCb), 0);
|
||||
GlobalMockObject::verify();
|
||||
}
|
||||
|
||||
TEST(Dfinder, TestNotifyStatisticsEvent)
|
||||
{
|
||||
MOCKER(PostEvent).stubs().will(invoke(InvokePostEvent));
|
||||
NotifyStatisticsEvent();
|
||||
ASSERT_EQ(g_eventCnt, 0);
|
||||
ASSERT_EQ(SetEventFunc(NULL, TestDFinderEventFuncCb), 0);
|
||||
GlobalMockObject::verify();
|
||||
NotifyStatisticsEvent();
|
||||
ASSERT_EQ(g_eventCnt, 1);
|
||||
ResetEventFunc();
|
||||
NotifyStatisticsEvent();
|
||||
ASSERT_EQ(g_eventCnt, 1);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 "ut_utils.h"
|
||||
|
||||
LocalIface *CreateLocalIface()
|
||||
{
|
||||
struct LocalIface *iface = (struct LocalIface *)calloc(1, sizeof(struct LocalIface));
|
||||
strcpy_s(iface->ifname, sizeof(iface->ifname), "eth");
|
||||
strcpy_s(iface->ipStr, sizeof(iface->ipStr), "127.0.0.1");
|
||||
iface->type = IFACE_TYPE_ETH;
|
||||
return iface;
|
||||
}
|
||||
|
||||
void FreeLocalIface(LocalIface *localInterface)
|
||||
{
|
||||
free(localInterface);
|
||||
}
|
||||
|
||||
coap_context_t *CreateCoapContext()
|
||||
{
|
||||
return coap_new_context(NULL);
|
||||
}
|
||||
|
||||
void FreeCoapContext(coap_context_t *context)
|
||||
{
|
||||
coap_free_context(context);
|
||||
}
|
||||
|
||||
CoapCtxType *CreateCoapCtxType()
|
||||
{
|
||||
CoapCtxType *ctx = (CoapCtxType *)malloc(1 * sizeof(CoapCtxType));
|
||||
ctx->ctx = CreateCoapContext();
|
||||
ctx->iface = (void *)CreateLocalIface();
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void FreeCoapCtxType(CoapCtxType *coapCtxType)
|
||||
{
|
||||
if (coapCtxType != NULL) {
|
||||
FreeCoapContext(coapCtxType->ctx);
|
||||
FreeLocalIface((LocalIface *)coapCtxType->iface);
|
||||
}
|
||||
free(coapCtxType);
|
||||
}
|
||||
|
||||
CoapCtxType *InsertDevice(void)
|
||||
{
|
||||
CoapCtxType *ctx = CreateCoapCtxType();
|
||||
List *list = GetCoapContextList();
|
||||
ListInsertTail(list, &ctx->node);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void RemoveDevice(CoapCtxType *ctx)
|
||||
{
|
||||
ListRemoveNode(&ctx->node);
|
||||
FreeCoapCtxType(ctx);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 UT_UTILS_H
|
||||
#define UT_UTILS_H
|
||||
|
||||
#include "nstackx_timer.h"
|
||||
#include "nstackx_device.h"
|
||||
#include "nstackx_error.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct LocalIface {
|
||||
List node;
|
||||
|
||||
uint8_t type;
|
||||
uint8_t state;
|
||||
uint8_t createCount;
|
||||
uint8_t af;
|
||||
char ifname[NSTACKX_MAX_INTERFACE_NAME_LEN];
|
||||
char ipStr[NSTACKX_MAX_IP_STRING_LEN];
|
||||
union InetAddr addr;
|
||||
char serviceData[NSTACKX_MAX_SERVICE_DATA_LEN];
|
||||
struct timespec updateTime;
|
||||
|
||||
Timer *timer;
|
||||
CoapCtxType *ctx;
|
||||
};
|
||||
|
||||
LocalIface *CreateLocalIface();
|
||||
void FreeLocalIface(LocalIface *localInterface);
|
||||
coap_context_t *CreateCoapContext();
|
||||
void FreeCoapContext(coap_context_t *context);
|
||||
CoapCtxType *CreateCoapCtxType();
|
||||
void FreeCoapCtxType(CoapCtxType *coapCtxType);
|
||||
CoapCtxType *InsertDevice(void);
|
||||
void RemoveDevice(CoapCtxType *ctx);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
#endif // UT_UTILS_H
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,347 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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 <gtest/gtest.h>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "fillpadapt.h"
|
||||
#include "fillpinc.h"
|
||||
#include "socket_common.h"
|
||||
#ifdef FILLP_LINUX
|
||||
#include <sys/epoll.h>
|
||||
#include <netinet/tcp.h>
|
||||
#endif
|
||||
|
||||
using namespace testing::ext;
|
||||
|
||||
class NSTACK_DSTREAM_WITH_SETUP_AND : public testing::Test {
|
||||
public:
|
||||
void SetUp()
|
||||
{
|
||||
Ftinit();
|
||||
}
|
||||
void TearDown()
|
||||
{
|
||||
FtDestroy();
|
||||
}
|
||||
};
|
||||
|
||||
#ifndef DTFUZZ_TEST_TIMES
|
||||
#define DTFUZZ_TEST_TIMES 30000000
|
||||
#endif
|
||||
#ifndef DTFUZZ_TEST_TIME_OUT
|
||||
#define DTFUZZ_TEST_TIME_OUT 10800 // second, 3 hours max
|
||||
#endif
|
||||
|
||||
#define LOOPBACK_IP "127.0.0.1"
|
||||
char g_addr[32] = LOOPBACK_IP;
|
||||
bool g_isServer = false;
|
||||
bool g_isIpv6 = false;
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, create_socket_001, TestSize.Level1)
|
||||
{
|
||||
int ret = 0;
|
||||
ret = FtSocket(*(int *)DT_SetGetS32(&g_Element[0], 0x01), *(int *)DT_SetGetS32(&g_Element[1], 0x01),
|
||||
*(int *)DT_SetGetS32(&g_Element[2], 0x01));
|
||||
EXPECT_EQ(0, ret);
|
||||
ret = FtClose(ret);
|
||||
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_bind_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = sizeof(struct sockaddr_in);
|
||||
if (sizeof(struct sockaddr_in6) > len) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
}
|
||||
|
||||
int ret = FtBind(fd, g_addr, len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = FtClose(fd);
|
||||
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_listen_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = sizeof(struct sockaddr_in);
|
||||
if (sizeof(struct sockaddr_in6) > len) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
}
|
||||
|
||||
int ret = FtBind(fd, g_addr, len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
FtListen(fd, 10);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_accept_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = sizeof(struct sockaddr_in);
|
||||
if (sizeof(struct sockaddr_in6) > len) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
}
|
||||
|
||||
int ret = FtBind(fd, g_addr, len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
FtListen(fd, 10);
|
||||
|
||||
ret = FtAccept(fd, g_addr, &len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_connect_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = sizeof(struct sockaddr_in);
|
||||
if (sizeof(struct sockaddr_in6) > len) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
}
|
||||
|
||||
ret = FtConnect(fd, g_addr, len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_ftsend_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = sizeof(struct sockaddr_in);
|
||||
if (sizeof(struct sockaddr_in6) > len) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
}
|
||||
|
||||
ret = FtConnect(fd, g_addr, len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
char data[256];
|
||||
int datalen = 255;
|
||||
ret = FtSend(fd, data, datalen, 0);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_ftsendframe_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = sizeof(struct sockaddr_in);
|
||||
if (sizeof(struct sockaddr_in6) > len) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
}
|
||||
|
||||
ret = FtConnect(fd, g_addr, len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
char data[256];
|
||||
int datalen = 255;
|
||||
ret = FtSendFrame(fd, data, datalen, 0);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_ftsendframeext_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = sizeof(struct sockaddr_in);
|
||||
if (sizeof(struct sockaddr_in6) > len) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
}
|
||||
|
||||
ret = FtConnect(fd, g_addr, len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
char data[256];
|
||||
int datalen = 255;
|
||||
ret = FtSendFrameExt(fd, data, datalen, 0);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_ftsendframeext_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
FtEventCbkInfo evtInfo = {0};
|
||||
int ret = FtApiEventInfoGet(fd, &evtInfo);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_ftrecv_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = sizeof(struct sockaddr_in);
|
||||
if (sizeof(struct sockaddr_in6) > len) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
}
|
||||
|
||||
int ret = FtBind(fd, g_addr, len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
FtListen(fd, 10);
|
||||
|
||||
ret = FtAccept(fd, g_addr, &len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
char data[255] = 0;
|
||||
int datalen = 255;
|
||||
ret = FtRecv(fd, data, datalen, 0);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_ftwritev_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
int iovCount = 10;
|
||||
struct iovec *iov = (struct iovec *)malloc(iovCount * sizeof(struct iovec));
|
||||
for (int i = 0; i < iovCount; i++) {
|
||||
iov[i].iov_len = *(int *)DT_SetGetNumberRange(&g_Element[2 + i], 0, 0, 100);
|
||||
if (iov[i].iov_len == 0) {
|
||||
iov[i].iov_base = NULL;
|
||||
} else {
|
||||
iov[i].iov_base = malloc(iov[i].iov_len);
|
||||
}
|
||||
}
|
||||
int ret = FtWritev(fd, iov, iovCount);
|
||||
EXPECT_EQ(0, ret);
|
||||
free(iov);
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_ftgetsockname_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = sizeof(struct sockaddr_in);
|
||||
if (sizeof(struct sockaddr_in6) > len) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
}
|
||||
char *addr = (char *)malloc(len);
|
||||
|
||||
int ret = FtGetSockName(fd, addr, &len);
|
||||
EXPECT_EQ(0, ret);
|
||||
free(addr);
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_ftgetpeerkname_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = sizeof(struct sockaddr_in);
|
||||
if (sizeof(struct sockaddr_in6) > len) {
|
||||
len = sizeof(struct sockaddr_in6);
|
||||
}
|
||||
char *addr = (char *)malloc(len);
|
||||
int ret = FtGetPeerName(fd, addr, &len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
free(addr);
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_ftgetsockopt_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = 255;
|
||||
char *addr = (char *)malloc(len);
|
||||
int ret = FtGetSockOpt(fd, 0, 0, addr, &len);
|
||||
EXPECT_EQ(0, ret);
|
||||
free(addr);
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
HWTEST_F(NSTACK_DSTREAM_WITH_SETUP_AND, socket_ftsetsockopt_001, TestSize.Level1)
|
||||
{
|
||||
int fd = FtSocket(AF_INET, SOCK_STREAM, IPPROTO_FILLP);
|
||||
int flags = 1;
|
||||
FtIoctl(fd, FILLP_FIONBIO, &flags);
|
||||
|
||||
size_t len = 255;
|
||||
char *addr = (char *)malloc(len);
|
||||
int ret = FtSetSockOpt(fd, 0, 0, addr, &len);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = FtClose(fd);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
Reference in New Issue
Block a user