小编Ale*_*ton的帖子

替换然后在ssh上打开stdin/stdout

我正在使用管道与stdin和stdout进行通信的系统.子进程使用api库来促进这种通信,我需要为库编写单元测试.我能弄清楚如何正确测试这些函数的唯一方法是用管道替换stdin/stdout,以便测试在调用函数时可以伪装成父系统.

/* 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]);
}
Run Code Online (Sandbox Code Playgroud)

我试过在temps中保存stdin和stdout并在它们上使用fdopen()(应该工作,因为它们是FILE*)但这不会导致正确写入管道.直接从主机shell运行时,此代码可以正常工作.在ssh上运行时会出现问题.单元测试执行完美,但是当我在这个测试套件之后向stdout写任何东西时,我收到一个破坏的管道错误.

我能做些什么来避免使用dup2以便stdin和stdout永远不会被关闭,或者我如何重新打开stdin和stdout以便它们能够正确地在shell和ssh中工作?

c linux ssh unit-testing pipe

5
推荐指数
1
解决办法
1119
查看次数

标签 统计

c ×1

linux ×1

pipe ×1

ssh ×1

unit-testing ×1