hunting/test/middleware/AppManager/tool/src/ServersMock.cpp
2024-06-17 23:43:39 +08:00

204 lines
6.8 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 "ServersMock.h"
#include "ILog.h"
#include "servers.h"
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string.h>
#include <string>
extern const char *APP_GET_PRODUCT_INFO;
extern const char *APP_GET_DEVICE_ATTR;
extern const char *APP_GET_MEDIA_INFO;
extern const char *APP_GET_SD_CARD_INFO;
extern const char *APP_GET_BATTERY_INFO;
extern const char *APP_GET_PARAM_VALUE;
extern const char *APP_GET_PARAM_ITEMS;
extern const char *APP_GET_CAPABILITY;
extern const char *APP_GET_LOCK_VIDEO_STATUS;
extern const char *APP_GET_STORAGE_INFO;
extern const char *APP_GET_FILE_LIST;
extern const char *APP_SET_DATE_TIME;
extern const char *APP_SET_TIME_ZONE;
extern const char *APP_SET_PARAM_VALUE;
extern const char *APP_ENTER_RECORDER;
extern const char *APP_PLAYBACK;
extern const char *APP_UPLOAD_FILE;
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;
}
ServersMock::ServersMock()
{
mServerUrl = APP_MANAGER_DEVICE_IP ":" + std::to_string(APP_MANAGER_HTTP_SERVER_PORT);
}
void ServersMock::Init(void)
{
ServerParam init = {
.logFlag = LOG_FLAG_ENABLE,
.sslVerifyFlag = SSL_VERIFY_DISABLE,
};
ServersInit(init);
}
void ServersMock::UnInit(void)
{
}
void ServersMock::MockGetProductInfo(void)
{
LogInfo("APP_GET_PRODUCT_INFO test start.\n");
std::string mockRequest = mServerUrl + APP_GET_PRODUCT_INFO;
MockHttpGet(mockRequest);
}
void ServersMock::MockGetDeviceAttr(void)
{
LogInfo("APP_GET_DEVICE_ATTR test start.\n");
std::string mockRequest = mServerUrl + APP_GET_DEVICE_ATTR;
MockHttpGet(mockRequest);
}
void ServersMock::MockGetMediaInfo(void)
{
LogInfo("APP_GET_MEDIA_INFO test start.\n");
std::string mockRequest = mServerUrl + APP_GET_MEDIA_INFO;
MockHttpGet(mockRequest);
}
void ServersMock::MockGetSdCardInfo(void)
{
LogInfo("APP_GET_SD_CARD_INFO test start.\n");
std::string mockRequest = mServerUrl + APP_GET_SD_CARD_INFO;
MockHttpGet(mockRequest);
}
void ServersMock::MockGetBatteryInfo(void)
{
LogInfo("APP_GET_BATTERY_INFO test start.\n");
std::string mockRequest = mServerUrl + APP_GET_BATTERY_INFO;
MockHttpGet(mockRequest);
}
void ServersMock::MockGetParamValue(const std::string &paramName)
{
LogInfo("APP_GET_PARAM_VALUE test start.\n");
std::string mockRequest = mServerUrl + APP_GET_PARAM_VALUE + "?param=" + paramName;
MockHttpGet(mockRequest);
}
void ServersMock::MockGetParamItems(const std::string &paramName)
{
LogInfo("APP_GET_PARAM_ITEMS test start.\n");
std::string mockRequest = mServerUrl + APP_GET_PARAM_ITEMS + "?param=" + paramName;
MockHttpGet(mockRequest);
}
void ServersMock::MockGetCapability(void)
{
LogInfo("APP_GET_CAPABILITY test start.\n");
std::string mockRequest = mServerUrl + APP_GET_CAPABILITY;
MockHttpGet(mockRequest);
}
void ServersMock::MockGetLockVideoStatus(void)
{
LogInfo("APP_GET_LOCK_VIDEO_STATUS test start.\n");
std::string mockRequest = mServerUrl + APP_GET_LOCK_VIDEO_STATUS;
MockHttpGet(mockRequest);
}
void ServersMock::MockGetStorageInfo(void)
{
LogInfo("APP_GET_STORAGE_INFO test start.\n");
std::string mockRequest = mServerUrl + APP_GET_STORAGE_INFO;
MockHttpGet(mockRequest);
}
void ServersMock::MockGetStorageFileList(void)
{
LogInfo("APP_GET_FILE_LIST test start.\n");
std::string mockRequest = mServerUrl + APP_GET_FILE_LIST + "?folder=loop&start=0&end=99";
MockHttpGet(mockRequest);
}
void ServersMock::MockSetDateTime(void)
{
LogInfo("APP_SET_DATE_TIME test start.\n");
std::string mockRequest = mServerUrl + APP_SET_DATE_TIME + "?date=20240101010101";
MockHttpGet(mockRequest);
}
void ServersMock::MockSetTimeZone(void)
{
LogInfo("APP_SET_TIME_ZONE test start.\n");
std::string mockRequest = mServerUrl + APP_SET_TIME_ZONE + "?timezone=8";
MockHttpGet(mockRequest);
}
void ServersMock::MockSetParamValue(const std::string &item, const std::string &value)
{
LogInfo("APP_SET_PARAM_VALUE test start.\n");
std::string mockRequest = mServerUrl + APP_SET_PARAM_VALUE + "?param=" + item + "&value=" + value;
MockHttpGet(mockRequest);
}
void ServersMock::MockEnterRecorder(void)
{
LogInfo("APP_ENTER_RECORDER test start.\n");
std::string mockRequest = mServerUrl + APP_ENTER_RECORDER + "?timezone=8"; // TODO:
MockHttpGet(mockRequest);
}
void ServersMock::MockAppPlayback(void)
{
LogInfo("APP_PLAYBACK test start.\n");
std::string mockRequest = mServerUrl + APP_PLAYBACK + "?param=enter";
MockHttpGet(mockRequest);
}
#ifndef PLATFORM_PATH
#error Add the code in your linux.toolchain.cmake : add_definitions(-DPLATFORM_PATH="${PLATFORM_PATH}")
#endif
void ServersMock::MockUploadFiles(void)
{
LogInfo("servers test start.\n");
std::string mockRequest = mServerUrl + APP_UPLOAD_FILE;
ServerHttp *http = NewServersHttp(mockRequest.c_str());
if (http) {
http->filePath = (char *)PLATFORM_PATH "/output_files/test/bin/AppManagerTest";
LogInfo("http post file:%s\n", http->filePath);
HttpPostFile(http);
if (http->reply) {
char *replyStr = (char *)malloc(http->replyLength + 1);
memset(replyStr, 0, http->replyLength + 1);
memcpy(replyStr, http->reply, http->replyLength);
LogInfo("HttpPost response :\n%s\n", replyStr);
free(replyStr);
}
DeleteServersHttp(http);
}
}
void ServersMock::MockHttpGet(const std::string &mockRequest)
{
ServerHttp *http = NewServersHttp(mockRequest.c_str());
if (http) {
HttpGet(http);
if (http->reply) {
char *replyStr = (char *)malloc(http->replyLength + 1);
memset(replyStr, 0, http->replyLength + 1);
memcpy(replyStr, http->reply, http->replyLength);
LogInfo("HttpGet response :\n%s\n", replyStr);
free(replyStr);
}
DeleteServersHttp(http);
}
else {
LogWarning("http is null.\n");
}
}