embedded-framework/test/utils/TcpModule/src/TcpModule_Test.cpp
2024-04-11 18:49:05 +08:00

74 lines
2.7 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* 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 "ILog.h"
#include "TcpModule.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace TcpModuleTest
{
// ../output_files/test/bin/TcpModuleTest --gtest_filter=TcpModuleTest.UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject
/**
* TcpModule module api will not crash when object is illegal.
*/
TEST(TcpModuleTest, UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject)
{
TcpServerParam tcpServerparam = {
.mIp = "127.0.0.1",
.mPort = 9876,
.mAcceptClientFunc = [](void *object, const char *ip) -> bool {
LogInfo("accept client, peer ip: %s\n", ip);
if (nullptr != object) {
AcceptClientWrite(object, "client accept send data.", strlen("client accept send data."));
}
return true;
},
.mClientAcceptParam = {
.mReadFunc = [](const void *data, const ssize_t len, const void *object) -> void {
LogInfo("client accept read data: %s\n", (char *)data);
},
.mClosedFunc = [](const void *object) -> void {
LogInfo("client accept closed.\n");
},
},
};
TcpClientParam param = {
.mIp = "127.0.0.1",
.mPort = 9876,
.mReadFunc = [](const void *data, const ssize_t len, const void *context) -> void {
LogInfo("read data: %s", (char *)data);
return;
},
.mClosedFunc = [](const void *object) -> void {
LogInfo("tcp client closed.\n");
},
};
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
void *tcpServer = CreateTcpServer(tcpServerparam);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
void *tcpClient = CreateTcpClient(param);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
TcpClientWrite(tcpClient, "client send data.", strlen("client send data."));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (nullptr != tcpClient) {
FreeTcpClient(tcpClient);
}
if (nullptr != tcpServer) {
FreeTcpServer(tcpServer);
}
ILogUnInit();
}
} // namespace TcpModuleTest