Add test to check that passphrase_read prompts to stderr when no TTY is available
authorShea Levy <shea@shealevy.com>
Sat, 24 Sep 2011 20:54:25 +0000 (16:54 -0400)
committerShea Levy <shea@shealevy.com>
Sat, 24 Sep 2011 20:54:25 +0000 (16:54 -0400)
cunit/test_utils.c

index d03dcf7..18a3564 100644 (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();
 }