From 6f9f0e8a17b4571ec9a9de25cba099fd5761adf2 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sat, 24 Sep 2011 16:54:25 -0400 Subject: [PATCH] Add test to check that passphrase_read prompts to stderr when no TTY is available --- cunit/test_utils.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/cunit/test_utils.c b/cunit/test_utils.c index d03dcf763..18a356430 100644 --- a/cunit/test_utils.c +++ b/cunit/test_utils.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -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(); }