execve_hook/tests/test_client.c

70 lines
1.7 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "../src/client.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* 测试客户端 - 用于测试 send_exec_params 函数
*
* 用法:
* ./test_client <log_file_path>
*
* 示例:
* ./test_client /tmp/test_error.log
*/
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "用法: %s <log_file_path>\n", argv[0]);
fprintf(stderr, "示例: %s /tmp/test_error.log\n", argv[0]);
return 1;
}
const char *log_path = argv[1];
// 模拟要执行的命令
const char *filename = "/usr/bin/python3";
// 模拟命令行参数
char *test_argv[] = {
"python3",
"test_script.py",
"--arg1",
"value1",
NULL
};
// 模拟环境变量
char *test_envp[] = {
"PATH=/usr/bin:/bin",
"HOME=/home/test",
"USER=test",
"SHELL=/bin/bash",
NULL
};
printf("===== 客户端测试开始 =====\n");
printf("socket地址%s\n", SOCKET_PATH);
printf("文件名: %s\n", filename);
printf("日志路径: %s\n", log_path);
printf("参数:\n");
for (int i = 0; test_argv[i] != NULL; i++) {
printf(" argv[%d]: %s\n", i, test_argv[i]);
}
printf("\n正在发送请求到服务端...\n\n");
printf("--- 服务端响应 ---\n");
// 调用客户端函数
int result = seeking_solutions(filename, test_argv, test_envp, log_path, &STDOUT_FILENO);
printf("\n--- 响应结束 ---\n");
if (result == 0) {
printf("\n✓ 测试成功\n");
return 0;
} else {
fprintf(stderr, "\n✗ 测试失败: send_exec_params 返回 %d\n", result);
return 1;
}
}