363 lines
11 KiB
C
363 lines
11 KiB
C
/* AUTOGENERATED FILE. DO NOT EDIT. */
|
|
|
|
/*=======Test Runner Used To Run Each Test Below=====*/
|
|
#define RUN_TEST(TestFunc, TestLineNum) \
|
|
{ \
|
|
Unity.CurrentTestName = #TestFunc; \
|
|
Unity.CurrentTestLineNumber = TestLineNum; \
|
|
Unity.NumberOfTests++; \
|
|
if (TEST_PROTECT()) { \
|
|
setUp(); \
|
|
TestFunc(); \
|
|
} \
|
|
if (TEST_PROTECT()) { \
|
|
tearDown(); \
|
|
} \
|
|
UnityConcludeTest(); \
|
|
}
|
|
|
|
/*=======Automagically Detected Files To Include=====*/
|
|
|
|
#include "unity.h"
|
|
#include <setjmp.h>
|
|
|
|
#include "user_fs.h"
|
|
#include "mock_shell/mock_shell.h"
|
|
#include "shell.h"
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
extern int mkdir_command(int argc, char *argv[]);
|
|
extern int rm_command(int argc, char *argv[]);
|
|
extern int ls_command(int argc, char *argv[]);
|
|
extern int touch_command(int argc, char *argv[]);
|
|
extern int pwd_command(int argc, char *argv[]);
|
|
|
|
|
|
/*=======测试辅助函数=====*/
|
|
static void test_remove_path(const char *path, int is_dir)
|
|
{
|
|
if (is_dir) {
|
|
rm_command(3, (char *[]) {"rm", "-r", (char *)path});
|
|
} else {
|
|
rm_command(2, (char *[]) {"rm", (char *)path});
|
|
}
|
|
}
|
|
|
|
static void test_create_dir(const char *dir_path)
|
|
{
|
|
mkdir_command(2, (char *[]) {"mkdir", (char *)dir_path});
|
|
}
|
|
|
|
static void test_create_file(const char *file_path)
|
|
{
|
|
touch_command(2, (char *[]) {"touch", (char *)file_path});
|
|
}
|
|
|
|
static void test_run_command_get_output(const char *cmd, char **out_log)
|
|
{
|
|
mock_shell_reset();
|
|
mock_shell_run((char *)cmd);
|
|
|
|
if (out_log) {
|
|
size_t log_size = 0;
|
|
*out_log = mock_shell_get_log_buf(&log_size);
|
|
}
|
|
}
|
|
|
|
static void test_assert_path_exists(const char *path, unsigned int mode)
|
|
{
|
|
int ret = 0;
|
|
struct stat sb;
|
|
|
|
ret = stat(path, &sb);
|
|
TEST_ASSERT_EQUAL(0, ret);
|
|
|
|
if (mode == S_IFDIR) {
|
|
TEST_ASSERT_TRUE(S_ISDIR(sb.st_mode));
|
|
} else if (mode == S_IFREG) {
|
|
TEST_ASSERT_TRUE(S_ISREG(sb.st_mode));
|
|
}
|
|
}
|
|
|
|
static void test_assert_path_not_exists(const char *path)
|
|
{
|
|
int ret = 0;
|
|
struct stat sb;
|
|
|
|
ret = stat(path, &sb);
|
|
TEST_ASSERT_NOT_EQUAL_INT(0, ret);
|
|
}
|
|
|
|
static void test_setup_cp_dir_test(const char *src_dir, const char *dst_dir, const char *test_file)
|
|
{
|
|
test_remove_path(src_dir, 1);
|
|
test_remove_path(dst_dir, 1);
|
|
|
|
test_create_dir(src_dir);
|
|
|
|
if (test_file) {
|
|
char file_path[256] = {0};
|
|
snprintf(file_path, sizeof(file_path), "%s/%s", src_dir, test_file);
|
|
test_create_file(file_path);
|
|
}
|
|
}
|
|
|
|
static void test_verify_cp_dir_result(const char *ls_cmd, const char *expected_output)
|
|
{
|
|
char *log = NULL;
|
|
test_run_command_get_output(ls_cmd, &log);
|
|
TEST_ASSERT_EQUAL_STRING(expected_output, log);
|
|
}
|
|
|
|
static void test_cleanup_cp_dir_test(const char *src_dir, const char *dst_dir)
|
|
{
|
|
test_remove_path(src_dir, 1);
|
|
test_remove_path(dst_dir, 1);
|
|
}
|
|
|
|
void setUp(void)
|
|
{
|
|
mock_shell_run("cd /SD:/");
|
|
mock_shell_reset();
|
|
}
|
|
|
|
void tearDown(void)
|
|
{
|
|
}
|
|
|
|
/*=======Test Reset Option=====*/
|
|
void resetTest(void);
|
|
void resetTest(void)
|
|
{
|
|
tearDown();
|
|
setUp();
|
|
}
|
|
|
|
|
|
void test_cmd_mkdir(void)
|
|
{
|
|
test_remove_path("test_dir", 1);
|
|
test_create_dir("test_dir");
|
|
test_assert_path_exists("/SD:/test_dir", S_IFDIR);
|
|
}
|
|
|
|
void test_cmd_mkdir_with_exist_dir_name(void)
|
|
{
|
|
char *log = NULL;
|
|
|
|
test_create_dir("test_dir");
|
|
test_run_command_get_output("mkdir test_dir", &log);
|
|
TEST_ASSERT_EQUAL_STRING("\r\nError: Directory '/SD:/test_dir' already exists\r\n", log);
|
|
}
|
|
|
|
void test_cmd_ls(void)
|
|
{
|
|
test_remove_path("test_dir", 1);
|
|
test_create_dir("test_dir");
|
|
test_create_file("test_dir/test_file");
|
|
|
|
test_verify_cp_dir_result("ls test_dir/", "\r\ntest_file \r\n");
|
|
}
|
|
|
|
void test_cmd_touch(void)
|
|
{
|
|
test_remove_path("test_file", 1);
|
|
test_create_file("test_file");
|
|
test_assert_path_exists("/SD:/test_file", S_IFREG);
|
|
}
|
|
|
|
void test_cmd_touch_with_exist_file_name(void)
|
|
{
|
|
char *log = NULL;
|
|
|
|
test_create_file("test_file");
|
|
test_run_command_get_output("touch test_file", &log);
|
|
TEST_ASSERT_EQUAL_STRING("\r\nFile created: /SD:/test_file\r\n", log);
|
|
}
|
|
|
|
void test_cmd_rm_file(void)
|
|
{
|
|
test_remove_path("test_file", 0);
|
|
test_create_file("test_file");
|
|
|
|
test_run_command_get_output("rm test_file", NULL);
|
|
test_assert_path_not_exists("/SD:/test_file");
|
|
}
|
|
|
|
void test_cmd_rm_empty_dir(void)
|
|
{
|
|
test_remove_path("test_dir", 0);
|
|
test_create_dir("test_dir");
|
|
|
|
test_run_command_get_output("rm -r test_dir", NULL);
|
|
test_assert_path_not_exists("/SD:/test_dir");
|
|
}
|
|
|
|
void test_cmd_rm_dir_without_dash_r(void)
|
|
{
|
|
test_remove_path("test_dir", 0);
|
|
test_create_dir("test_dir");
|
|
|
|
test_run_command_get_output("rm test_dir", NULL);
|
|
test_assert_path_exists("/SD:/test_dir", S_IFDIR);
|
|
}
|
|
|
|
void test_cmd_rm_dir_with_file(void)
|
|
{
|
|
test_remove_path("test_dir", 0);
|
|
test_create_dir("test_dir");
|
|
test_create_file("test_dir/test_file");
|
|
|
|
test_run_command_get_output("rm -r test_dir", NULL);
|
|
test_assert_path_not_exists("/SD:/test_dir");
|
|
}
|
|
|
|
void test_cmd_rm_dir_with_subdir(void)
|
|
{
|
|
test_remove_path("test_dir", 0);
|
|
test_create_dir("test_dir");
|
|
test_create_dir("test_dir/sub_dir");
|
|
|
|
test_run_command_get_output("rm -r test_dir", NULL);
|
|
test_assert_path_not_exists("/SD:/test_dir");
|
|
test_assert_path_not_exists("/SD:/test_dir/sub_dir");
|
|
}
|
|
|
|
void test_cmd_cd(void)
|
|
{
|
|
char *log = NULL;
|
|
|
|
test_remove_path("test_dir", 0);
|
|
test_create_dir("test_dir");
|
|
test_create_file("test_dir/test_file");
|
|
|
|
test_run_command_get_output("cd test_dir", NULL);
|
|
test_run_command_get_output("ls", &log);
|
|
|
|
TEST_ASSERT_EQUAL_STRING("\r\ntest_file \r\n", log);
|
|
test_run_command_get_output("cd /SD:/", NULL);
|
|
}
|
|
|
|
void test_cmd_pwd(void)
|
|
{
|
|
char *log = NULL;
|
|
|
|
test_remove_path("test_dir", 0);
|
|
test_create_dir("test_dir");
|
|
|
|
test_run_command_get_output("cd test_dir", NULL);
|
|
test_run_command_get_output("pwd", &log);
|
|
|
|
TEST_ASSERT_EQUAL_STRING("\r\n/SD:/test_dir\r\n", log);
|
|
}
|
|
|
|
void test_cmd_cp_file(void)
|
|
{
|
|
test_remove_path("test_file", 0);
|
|
test_remove_path("test_file_copy", 0);
|
|
test_create_file("test_file");
|
|
|
|
test_run_command_get_output("cp test_file test_file_copy", NULL);
|
|
test_assert_path_exists("/SD:/test_file_copy", S_IFREG);
|
|
|
|
test_remove_path("test_file_copy", 0);
|
|
test_remove_path("test_file", 0);
|
|
}
|
|
|
|
void test_cmd_cp_dir_to_not_exist_dir(void)
|
|
{
|
|
test_setup_cp_dir_test("test_dir", "test_dir_copy", "test_file");
|
|
|
|
test_run_command_get_output("cp -r test_dir test_dir_copy", NULL);
|
|
test_assert_path_exists("/SD:/test_dir_copy", S_IFDIR);
|
|
test_verify_cp_dir_result("ls test_dir_copy/", "\r\ntest_file \r\n");
|
|
|
|
test_cleanup_cp_dir_test("test_dir", "test_dir_copy");
|
|
}
|
|
|
|
void test_cmd_cp_dir_to_exist_dir(void)
|
|
{
|
|
test_setup_cp_dir_test("test_dir", "test_dir_contain_subdir", "test_file");
|
|
test_create_dir("test_dir_contain_subdir");
|
|
|
|
test_run_command_get_output("cp -r test_dir test_dir_contain_subdir", NULL);
|
|
test_assert_path_exists("/SD:/test_dir_contain_subdir", S_IFDIR);
|
|
test_verify_cp_dir_result("ls test_dir_contain_subdir/", "\r\ntest_dir \r\n");
|
|
|
|
test_cleanup_cp_dir_test("test_dir", "test_dir_contain_subdir");
|
|
}
|
|
|
|
void test_cmd_cp_abs_dir_to_abs_dir(void)
|
|
{
|
|
test_setup_cp_dir_test("test_dir", "test_dir_contain_subdir", "test_file");
|
|
test_create_dir("test_dir_contain_subdir");
|
|
|
|
test_run_command_get_output("cp -r /SD:/test_dir /SD:/test_dir_contain_subdir", NULL);
|
|
test_assert_path_exists("/SD:/test_dir_contain_subdir", S_IFDIR);
|
|
test_verify_cp_dir_result("ls /SD:/test_dir_contain_subdir/", "\r\ntest_dir \r\n");
|
|
|
|
test_cleanup_cp_dir_test("test_dir", "test_dir_contain_subdir");
|
|
}
|
|
|
|
void test_cmd_cp_relative_dir_to_abs_dir(void)
|
|
{
|
|
test_setup_cp_dir_test("test_dir", "test_dir_contain_subdir", "test_file");
|
|
test_create_dir("test_dir_contain_subdir");
|
|
|
|
test_run_command_get_output("cp -r test_dir /SD:/test_dir_contain_subdir", NULL);
|
|
test_assert_path_exists("/SD:/test_dir_contain_subdir", S_IFDIR);
|
|
test_verify_cp_dir_result("ls /SD:/test_dir_contain_subdir/", "\r\ntest_dir \r\n");
|
|
|
|
test_cleanup_cp_dir_test("test_dir", "test_dir_contain_subdir");
|
|
}
|
|
|
|
void test_cmd_cp_abs_dir_to_relative_dir(void)
|
|
{
|
|
test_setup_cp_dir_test("test_dir", "test_dir_contain_subdir", "test_file");
|
|
test_create_dir("test_dir_contain_subdir");
|
|
|
|
test_run_command_get_output("cp -r /SD:/test_dir test_dir_contain_subdir", NULL);
|
|
test_assert_path_exists("/SD:/test_dir_contain_subdir", S_IFDIR);
|
|
test_verify_cp_dir_result("ls /SD:/test_dir_contain_subdir/", "\r\ntest_dir \r\n");
|
|
|
|
test_cleanup_cp_dir_test("test_dir", "test_dir_contain_subdir");
|
|
}
|
|
|
|
/*=======MAIN=====*/
|
|
int main(void)
|
|
{
|
|
user_fs_init();
|
|
|
|
printf("user fs init complete\n");
|
|
|
|
mock_shell_init();
|
|
|
|
UnityBegin("test/simple_box/test_main.c");
|
|
RUN_TEST(test_cmd_mkdir, __LINE__);
|
|
RUN_TEST(test_cmd_mkdir_with_exist_dir_name, __LINE__);
|
|
RUN_TEST(test_cmd_ls, __LINE__);
|
|
RUN_TEST(test_cmd_touch, __LINE__);
|
|
RUN_TEST(test_cmd_touch_with_exist_file_name, __LINE__);
|
|
RUN_TEST(test_cmd_rm_file, __LINE__);
|
|
RUN_TEST(test_cmd_rm_empty_dir, __LINE__);
|
|
RUN_TEST(test_cmd_rm_dir_without_dash_r, __LINE__);
|
|
RUN_TEST(test_cmd_rm_dir_with_file, __LINE__);
|
|
RUN_TEST(test_cmd_rm_dir_with_subdir, __LINE__);
|
|
RUN_TEST(test_cmd_cd, __LINE__);
|
|
RUN_TEST(test_cmd_pwd, __LINE__);
|
|
RUN_TEST(test_cmd_cp_file, __LINE__);
|
|
RUN_TEST(test_cmd_cp_dir_to_not_exist_dir, __LINE__);
|
|
RUN_TEST(test_cmd_cp_dir_to_exist_dir, __LINE__);
|
|
RUN_TEST(test_cmd_cp_abs_dir_to_abs_dir, __LINE__);
|
|
RUN_TEST(test_cmd_cp_relative_dir_to_abs_dir, __LINE__);
|
|
RUN_TEST(test_cmd_cp_abs_dir_to_relative_dir, __LINE__);
|
|
|
|
return (UnityEnd());
|
|
}
|