Add test to check that passphrase_read prompts to stderr when no TTY is available

This commit is contained in:
Shea Levy 2011-09-24 16:54:25 -04:00
parent 3abc1c0fe5
commit 6f9f0e8a17

View File

@ -22,6 +22,7 @@
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/wait.h>
@ -510,6 +511,56 @@ void passphrase_read_turns_on_newline_echo_during_read()
close(slavefd);
return;
}
void passphrase_read_prompts_to_stderr_when_no_tty()
{
static const int read_nbyte = 11;
int stdin_pipe[2], stderr_pipe[2];
char read_buf[read_nbyte];
struct sigaction ignore, orig;
ignore.sa_handler = SIG_IGN;
sigemptyset(&ignore.sa_mask);
if (pipe(stdin_pipe) != 0 || pipe(stderr_pipe) != 0)
CU_FAIL_FATAL("Could not create pipe");
switch (fork())
{
case -1:
CU_FAIL_FATAL("Could not fork");
case 0:
{
static const int password_size = 512;
char buffer[password_size];
close(stderr_pipe[0]);
close(stdin_pipe[1]);
if (setsid() == (pid_t) -1)
CU_FAIL_FATAL("Could not create new session");
dup2(stdin_pipe[0], STDIN_FILENO);
dup2(stderr_pipe[1], STDERR_FILENO);
freerdp_passphrase_read("Password: ", buffer, password_size);
exit(EXIT_SUCCESS);
}
}
close(stderr_pipe[1]);
close(stdin_pipe[0]);
read_buf[read_nbyte - 1] = '\0';
if (read(stderr_pipe[0], read_buf, read_nbyte) == (ssize_t) -1)
CU_FAIL_FATAL("Nothing written to pipe");
CU_ASSERT_STRING_EQUAL(read_buf, "Password: ");
sigaction(SIGPIPE, &ignore, &orig);
write(stdin_pipe[1], "\n", (size_t) 2);
sigaction(SIGPIPE, &orig, NULL);
close(stderr_pipe[0]);
close(stdin_pipe[1]);
return;
}
void test_passphrase_read(void)
{
passphrase_read_prompts_to_tty();
@ -517,4 +568,5 @@ void test_passphrase_read(void)
passphrase_read_turns_off_echo_during_read();
passphrase_read_resets_terminal_after_read();
passphrase_read_turns_on_newline_echo_during_read();
passphrase_read_prompts_to_stderr_when_no_tty();
}