embedded-framework/test/middleware/IpcConfig/tool/src/IpcConfigTestTool.cpp
2024-05-24 13:54:35 +08:00

68 lines
2.4 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 "IpcConfigTestTool.h"
#include "ILog.h"
#include "LinuxApi.h"
#include <stdio.h>
#include <stdlib.h>
const char *MOUNTED_FILE = "./mount_info.txt";
extern const char *SYSTEM_MOUNTED_FILE;
void IpcConfigTestTool::Init(std::shared_ptr<LinuxTest> &mock)
{
MockMountedFile();
auto api_fopen = [=](const char *pathname, const char *mode) {
LogInfo("Open mock mounted file.\n");
mFile = OpenMockMountedFile(mode);
};
EXPECT_CALL(*mock.get(), fx_fopen(SYSTEM_MOUNTED_FILE, _))
.WillRepeatedly(DoAll(WithArgs<0, 1>(Invoke(api_fopen)), (ReturnPointee(&mFile))));
}
void IpcConfigTestTool::UnInit(void)
{
constexpr int BUF_LENGTH = 256;
char cmd[BUF_LENGTH] = {0};
sprintf(cmd, "rm -rf %s", MOUNTED_FILE);
fx_system(cmd);
}
void IpcConfigTestTool::MockMountedFile(void)
{
FILE *file;
file = fopen(MOUNTED_FILE, "w");
if (file == NULL) {
perror("Error opening file");
return;
}
fprintf(file, "devtmpfs /dev devtmpfs rw,relatime,size=10176k,nr_inodes=2544,mode=755 0 0\n");
fprintf(file, "proc /proc proc rw,relatime 0 0\n");
fprintf(file, "tmpfs /tmp tmpfs rw,relatime,size=14464k,nr_inodes=3616 0 0\n");
fprintf(file, "tmpfs /run tmpfs rw,nosuid,nodev,relatime,size=14464k,nr_inodes=3616,mode=755 0 0\n");
fprintf(file, "sysfs /sys sysfs rw,relatime 0 0\n");
fprintf(file, "/dev/block/by-name" USERDATA_MOUNT_PATH " " USERDATA_MOUNT_PATH " jffs2 rw,relatime 0 0\n");
fprintf(
file,
"/dev/mmcblk1p1 /mnt/sdcard vfat "
"rw,relatime,fmask=0022,dmask=0022,codepage=936,iocharset=cp936,shortname=mixed,utf8,errors=remount-ro 0 0\n");
fclose(file);
fx_system("sync");
LogInfo("File written successfully.\n");
}
FILE *IpcConfigTestTool::OpenMockMountedFile(const char *mode)
{
FILE *fp = nullptr;
fp = fopen(MOUNTED_FILE, mode);
return fp;
}