Backup:AppManager test code.

This commit is contained in:
Fancy code 2024-03-01 02:25:24 -08:00
parent 08031ed4fa
commit 67c79c2c6e
17 changed files with 318 additions and 62 deletions

View File

@ -22,31 +22,26 @@ using std::placeholders::_3;
// using std::placeholders::_4;
SixFrameHandle::SixFrameHandle()
{
// mResquesHandleFunc["set"] = std::bind(&SixFrameHandle::test, this, _1, _2, _3);
mResquesHandleFunc["app/set"] = std::bind(&SixFrameHandle::test, this, _1, _2, _3);
mResquesHandleFunc["favicon.ico"] = std::bind(&SixFrameHandle::DoNothing, this, _1, _2, _3);
mResquesHandleFunc["/app/getproductinfo"] = std::bind(&SixFrameHandle::RequestGetProductInfo, this, _1, _2, _3);
// mResquesHandleFunc["favicon.ico"] = std::bind(&SixFrameHandle::DoNothing, this, _1, _2, _3);
}
void SixFrameHandle::RequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle,
void *context)
{
std::multimap<std::string, std::string> paramsMap;
const std::string urlStr = url;
size_t queryStartPos = urlStr.find("HTTP");
if (queryStartPos != std::string::npos && queryStartPos + 1 < urlStr.length()) {
const std::string urlStr2 = urlStr.substr(0, queryStartPos - 1);
const std::string urlStr2 = url;
LogInfo("URL = %s\n", urlStr2.c_str());
queryStartPos = urlStr2.find('?');
size_t queryStartPos = urlStr2.find('?');
std::string command = "";
if (queryStartPos != std::string::npos && queryStartPos + 1 < urlStr2.length()) {
command = urlStr2.substr(1, queryStartPos - 1);
command = urlStr2.substr(0, queryStartPos);
}
else {
command = urlStr2.substr(1, urlStr2.length());
command = urlStr2.substr(0, urlStr2.length());
}
LogInfo("command = %s\n", command.c_str());
ExtractParamsFromUrl(urlStr2, paramsMap);
RequestHandle2(command, paramsMap, responseHandle, context);
}
}
void SixFrameHandle::ExtractParamsFromUrl(const std::string &url, std::multimap<std::string, std::string> &paramsMap)
{
@ -67,12 +62,6 @@ void SixFrameHandle::ExtractParamsFromUrl(const std::string &url, std::multimap<
}
}
}
void SixFrameHandle::test(std::multimap<std::string, std::string> &paramsMap, ResponseHandle responseHandle,
void *context)
{
//
LogInfo("sssssssssssssssssssssssssssssssss\n");
}
void SixFrameHandle::RequestHandle2(const std::string command, std::multimap<std::string, std::string> &paramsMap,
ResponseHandle responseHandle, void *context)
{
@ -82,10 +71,19 @@ void SixFrameHandle::RequestHandle2(const std::string command, std::multimap<std
}
else {
LogError("Unknown command.\n");
DoNothing(paramsMap, responseHandle, context);
}
}
void SixFrameHandle::DoNothing(std::multimap<std::string, std::string> &paramsMap, ResponseHandle responseHandle,
void *context)
{
//
responseHandle("Unknown command.", context);
}
void SixFrameHandle::RequestGetProductInfo(std::multimap<std::string, std::string> &paramsMap,
ResponseHandle responseHandle, void *context)
{
//
LogInfo("RequestGetProductInfo.\n");
responseHandle("hello world.", context);
}

View File

@ -34,10 +34,11 @@ public:
private:
void ExtractParamsFromUrl(const std::string &url, std::multimap<std::string, std::string> &paramsMap);
void test(std::multimap<std::string, std::string> &paramsMap, ResponseHandle responseHandle, void *context);
void RequestHandle2(const std::string command, std::multimap<std::string, std::string> &paramsMap,
ResponseHandle responseHandle, void *context);
void DoNothing(std::multimap<std::string, std::string> &paramsMap, ResponseHandle responseHandle, void *context);
void RequestGetProductInfo(std::multimap<std::string, std::string> &paramsMap, ResponseHandle responseHandle,
void *context);
private:
std::map<std::string, ResquesHandleFunc> mResquesHandleFunc;

48
test/GtestUsing.h Normal file
View File

@ -0,0 +1,48 @@
/*
* 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.
*/
#ifndef GTESTUSING_H
#define GTESTUSING_H
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::_;
using ::testing::Action;
using ::testing::ActionInterface;
using ::testing::AnyNumber;
using ::testing::Assign;
using ::testing::AtLeast;
using ::testing::ByMove;
using ::testing::ByRef;
using ::testing::DefaultValue;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::IgnoreResult;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::MakePolymorphicAction;
using ::testing::PolymorphicAction;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::ReturnNull;
using ::testing::ReturnPointee;
using ::testing::ReturnRef;
using ::testing::ReturnRefOfCopy;
using ::testing::ReturnRoundRobin;
using ::testing::SaveArg;
using ::testing::SetArgPointee;
using ::testing::SetArgumentPointee;
using ::testing::Unused;
using ::testing::WithArgs;
using ::testing::internal::BuiltInDefaultValue;
#endif

View File

@ -8,20 +8,13 @@ include_directories(
./tool/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
# ${UTILS_SOURCE_PATH}/WebServer/include
# ${UTILS_SOURCE_PATH}/McuProtocol/include
${UTILS_SOURCE_PATH}/KeyControl/include
${UTILS_SOURCE_PATH}/LedControl/include
${HAL_SOURCE_PATH}/include
# ${HAL_SOURCE_PATH}/src
${MIDDLEWARE_SOURCE_PATH}/AppManager/include
${MIDDLEWARE_SOURCE_PATH}/AppManager/src
# ${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include
# ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/hal/tool/include
# ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include
# ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
# ${TEST_SOURCE_PATH}/middleware/McuAskBase/tool/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
@ -39,7 +32,7 @@ endif()
set(TARGET_NAME AppManagerTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} AppManager gtest gmock pthread)
target_link_libraries(${TARGET_NAME} AppManager AppManagerTestTool gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
@ -84,4 +77,4 @@ endif()
define_file_name(${TARGET_NAME})
# add_subdirectory(tool)
add_subdirectory(tool)

View File

@ -12,15 +12,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "AppManagerTestTool.h"
#include "IAppManager.h"
#include "ILog.h"
// #include "WebServer.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace AppManagerTest
{
class AppManagerTest : public testing::Test
class AppManagerTest : public testing::Test, public AppManagerTestTool
{
public:
AppManagerTest() {}
@ -51,6 +51,8 @@ public:
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_Demo)
{
IAppManager::GetInstance()->Init();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
MockGetProductInfo();
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
IAppManager::GetInstance()->UnInit();
}

View File

@ -0,0 +1,54 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
include_directories(
./src
./include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/Servers/include
${MIDDLEWARE_SOURCE_PATH}/AppManager/src
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
)
# link_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# )
aux_source_directory(./src TEST_TOOL_SRC_FILES)
set(TEST_TOOL_TARGET AppManagerTestTool)
add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES})
target_link_libraries(${TEST_TOOL_TARGET} Servers Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
AppManagerTestTool_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${TEST_TOOL_SRC_FILES}
${CLANG_TIDY_CONFIG}
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager/tool
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
AppManagerTestTool_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${TEST_TOOL_SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager/tool
)
add_custom_command(
TARGET ${TEST_TOOL_TARGET}
PRE_BUILD
COMMAND make AppManagerTestTool_code_check
COMMAND make AppManagerTestTool_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TEST_TOOL_TARGET})

View File

@ -0,0 +1,29 @@
/*
* 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.
*/
#ifndef APP_MANAGER_TEST_TOOL_H
#define APP_MANAGER_TEST_TOOL_H
#include "GtestUsing.h"
class AppManagerTestTool
{
public:
AppManagerTestTool() = default;
virtual ~AppManagerTestTool() = default;
void Init(void);
void UnInit(void);
protected:
void MockGetProductInfo(void);
};
#endif

View File

@ -0,0 +1,29 @@
/*
* 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 "AppManagerTestTool.h"
#include "ServersMock.h"
void AppManagerTestTool::Init(void)
{
//
}
void AppManagerTestTool::UnInit(void)
{
//
}
void AppManagerTestTool::MockGetProductInfo(void)
{
//
ServersMock::GetInstance()->MockGetProductInfo();
}

View File

@ -0,0 +1,75 @@
/*
* 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 "ServersMock.h"
#include "ILog.h"
#include "servers.h"
std::shared_ptr<ServersMock> &ServersMock::GetInstance(std::shared_ptr<ServersMock> *impl)
{
static auto instance = std::make_shared<ServersMock>();
if (impl) {
if (instance.use_count() == 1) {
LogInfo("Instance changed succeed.\n");
instance = *impl;
}
else {
LogError("Can't changing the instance becase of using by some one.\n");
}
}
return instance;
}
#include <arpa/inet.h>
#include <cstring>
#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
static int GetIp(void)
{
char hostname[256];
if (gethostname(hostname, sizeof(hostname)) == -1) {
std::cout << "Failed to get hostname." << std::endl;
return 1;
}
struct hostent *hostent = gethostbyname(hostname);
if (hostent == nullptr || hostent->h_addr_list[0] == nullptr) {
std::cout << "Failed to get IP address." << std::endl;
return 1;
}
struct in_addr **addr_list = reinterpret_cast<struct in_addr **>(hostent->h_addr_list);
for (int i = 0; addr_list[i] != nullptr; ++i) {
std::cout << inet_ntoa(*addr_list[i]) << std::endl;
}
return 1;
}
void ServersMock::MockGetProductInfo(void)
{
GetIp();
ServerParam init = {
.logFlag = LOG_FLAG_ENABLE,
.sslVerifyFlag = SSL_VERIFY_DISABLE,
};
ServersInit(init);
LogInfo("servers test start.\n");
ServerHttp *http = NewServersHttp("http:192.168.1.29:80/app/test");
if (http) {
HttpGet(http);
if (http->reply) {
LogInfo("HttpGet ========\n %s\n", http->reply);
}
DeleteServersHttp(http);
}
}

View File

@ -0,0 +1,26 @@
/*
* 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.
*/
#ifndef SERVERS_MOCK_H
#define SERVERS_MOCK_H
#include <memory>
class ServersMock
{
public:
ServersMock() = default;
virtual ~ServersMock() = default;
static std::shared_ptr<ServersMock> &GetInstance(std::shared_ptr<ServersMock> *impl = nullptr);
virtual void MockGetProductInfo(void);
};
#endif

View File

@ -16,8 +16,6 @@ include_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# )
aux_source_directory(./src TEST_TOOL_SRC_FILES)
set(TEST_TOOL_TARGET McuManagerTestTool)
add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES})

View File

@ -21,9 +21,13 @@ set(TARGET_NAME Servers)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} Log)
if(${CURL_OPENSSL_LIB_SHARED_ENABLE} MATCHES "false")
target_link_libraries(${TARGET_NAME} ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/lib/.libs/libcurl.a)
target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libcurl.a)
target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libssl.a)
target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libcrypto.a dl pthread)
else()
target_link_libraries(${TARGET_NAME} ${EXTERNAL_SOURCE_PATH}/curl/curl-8.1.2/lib/.libs/libcurl.so)
target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libcurl.so)
target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libssl.so)
target_link_libraries(${TARGET_NAME} ${LIBS_OUTPUT_PATH}/libcrypto.so dl pthread)
endif()
# ------------------ openssl ------------------ start

View File

@ -44,7 +44,7 @@ TEST(ServersTest, FtpsUpload)
const char *url = "ftp://150.109.112.64/ServersTest";test/bin/ServersTest";
const char *uploadFile = "./ServersTest";
const char *user_password = "ftp_user:Sifar%123456";
ServerInit init = {
ServerParam init = {
.logFlag = LOG_FLAG_ENABLE, // 开启curl日志
.sslVerifyFlag = SSL_VERIFY_DISABLE, //关闭ssl的证书校验功能
};

View File

@ -47,7 +47,7 @@ typedef struct ServersInit
{
LogFlag logFlag;
SslFlag sslVerifyFlag;
} ServerInit;
} ServerParam;
typedef struct servers_http
{
const char *url;
@ -84,7 +84,7 @@ typedef struct servers_smtp
char **attachment;
int code;
} ServerSmtp;
void ServersInit(ServerInit init);
void ServersInit(ServerParam init);
void ServersUnInit(void);
// HTTP API
ServerHttp *NewServersHttp(const char *url);

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
#include "curl_serve.h"
static ServerInit gCurlServe;
static ServerParam gCurlServe;
void SetVerboseLog(LogFlag flag) { gCurlServe.logFlag = flag; }
void SetSslVerify(SslFlag flag) { gCurlServe.sslVerifyFlag = flag; }
CURL *CurlEasyMake(void)

View File

@ -21,7 +21,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
void ServersInit(ServerInit init)
void ServersInit(ServerParam init)
{
SetVerboseLog(init.logFlag);
SetSslVerify(init.sslVerifyFlag);

View File

@ -18,6 +18,7 @@
#include "js.h"
#include <signal.h>
static int finished = 0;
static HttpHandleCallback gHttpHandle = NULL;
static void sigHandler(int signo)
{
LogInfo("Stop goahead web server.\n");
@ -50,25 +51,20 @@ void initPlatform(void)
signal(SIGKILL, sigHandler);
signal(SIGPIPE, SIG_IGN);
}
static bool testHandler(Webs *wp)
static void response_handle(const char *responseStr, void *context)
{
if (smatch(wp->path, "/")) {
// websRewriteRequest(wp, "/home.html");
/* Fall through */
struct Webs *wp = (struct Webs *)context;
if (NULL != responseStr) {
websWrite(wp, "%s", responseStr);
}
LogInfo("sssssssssssssssssssssssssssss url = %s\n", wp->url);
// websSetStatus(wp, HTTP_CODE_OK);
// websWriteHeaders(wp, 0, 0);
// websWriteEndHeaders(wp);
// websWrite(wp, "<html><body><h2>");
// websWrite(wp, "sssssssssssssssssssssss");
// websWrite(wp, "</h2></body></html>\n");
// websDone(wp);
}
static bool AppRequestHandle(Webs *wp)
{
websSetStatus(wp, 200);
websWriteHeaders(wp, -1, 0);
websWriteHeader(wp, "Content-Type", "text/plain");
websWriteEndHeaders(wp);
websWrite(wp, "Hello Legacy World\n");
gHttpHandle(wp->url, 0, response_handle, wp);
websDone(wp);
return 1;
return 1;
@ -98,8 +94,11 @@ StatusCode WebServerInit(const WebServerParam webParam)
if (websListen(listen) < 0) {
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
websDefineHandler("test", 0, testHandler, 0, 0);
websAddRoute("/test", "test", 0);
if (nullptr != webParam.mHttpRequestHandle) {
gHttpHandle = webParam.mHttpRequestHandle;
websDefineHandler("appRequestHandle", 0, AppRequestHandle, 0, 0);
websAddRoute("/app", "appRequestHandle", 0);
}
websServiceEvents(&finished);
logmsg(1, "Instructed to exit\n");
websClose();