我正在开发一个与子进程通信的系统,使用管道连接stdin和stdout。 子进程使用API库来促进这种通信,我需要为该库编写单元测试。 我找到的唯一正确测试这些函数的方法是替换stdin/stdout为管道,以便测试时可以在调用函数时假装成父系统。
我已经尝试将stdin和stdout保存在temps中,并对它们使用fdopen()(应该可以工作,因为它们是FILE*),但是这并没有导致内容正确地写入管道。当直接从主机的shell上运行此代码时,它可以完美地工作。但是在通过ssh运行时,问题就出现了。单元测试执行得非常完美,但是在此测试套件之后,当我尝试向stdout写入任何内容时,会收到一个broken pipe错误。
有没有什么办法可以避免使用dup2,使得stdin和stdout永远不会关闭,或者如何重新打开stdin和stdout,以便它们在shell和ssh上能够正常工作?
/* replace stdin and stdout with pipes */
void setup(void) {
pipe(in_sub);
pipe(out_sub);
dup2(out_sub[1], fileno(stdout));
dup2( in_sub[0], fileno(stdin));
read_from = fdopen(out_sub[0], "rb");
write_to = fdopen( in_sub[1], "wb");
stdout_t = fopen("/dev/tty", "wb");
stdin_t = fopen("/dev/tty", "rb");
}
/* reopen stdin and stdout for future test suites */
void teardown(void) {
fclose(read_from);
fclose(write_to);
stdout = stdout_t;
stdin = stdin_t;
close(in_sub[0]);
close(in_sub[1]);
close(out_sub[0]);
close(out_sub[1]);
}
我已经尝试将stdin和stdout保存在temps中,并对它们使用fdopen()(应该可以工作,因为它们是FILE*),但是这并没有导致内容正确地写入管道。当直接从主机的shell上运行此代码时,它可以完美地工作。但是在通过ssh运行时,问题就出现了。单元测试执行得非常完美,但是在此测试套件之后,当我尝试向stdout写入任何内容时,会收到一个broken pipe错误。
有没有什么办法可以避免使用dup2,使得stdin和stdout永远不会关闭,或者如何重新打开stdin和stdout,以便它们在shell和ssh上能够正常工作?