From b7ed44c66ea38bcdb6b73abba57d7c20dfc44486 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Sat, 24 Sep 2011 01:04:15 -0400 Subject: [PATCH] Add test to check that passphrase_read reads from the controlling tty --- cunit/test_utils.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/cunit/test_utils.c b/cunit/test_utils.c index 634d9e4..6818860 100644 --- a/cunit/test_utils.c +++ b/cunit/test_utils.c @@ -222,7 +222,66 @@ void passphrase_read_prompts_to_tty() return; } +void passphrase_read_reads_from_tty() +{ + static const int read_nbyte = 11; + int masterfd; + char* slavedevice; + char read_buf[read_nbyte]; + fd_set fd_set_write; + + masterfd = posix_openpt(O_RDWR|O_NOCTTY); + + if (masterfd == -1 + || grantpt (masterfd) == -1 + || unlockpt (masterfd) == -1 + || (slavedevice = ptsname (masterfd)) == NULL) + CU_FAIL_FATAL("Could not create pty"); + + switch (fork()) + { + case -1: + CU_FAIL_FATAL("Could not fork"); + case 0: + { + static const int password_size = 512; + char buffer[password_size]; + int slavefd; + if (setsid() == (pid_t) -1) + CU_FAIL_FATAL("Could not create new session"); + + if ((slavefd = open(slavedevice, O_RDWR)) == 0) + CU_FAIL_FATAL("Could not open slave end of pty"); + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + close(masterfd); + freerdp_passphrase_read("Password: ", buffer, password_size); + write(slavefd, buffer, password_size); + close(slavefd); + exit(EXIT_SUCCESS); + } + } + + read_buf[read_nbyte - 1] = '\0'; + + FD_ZERO(&fd_set_write); + FD_SET(masterfd, &fd_set_write); + if (select(masterfd + 1, NULL, &fd_set_write, NULL, NULL) == -1) + CU_FAIL_FATAL("Master end of pty not writeable"); + if (read(masterfd, read_buf, read_nbyte) == (ssize_t) -1) + CU_FAIL_FATAL("Nothing written to slave end of pty"); + + write(masterfd, "passw0rd\n", (size_t) 2); + if (read(masterfd, read_buf, read_nbyte) == (ssize_t) -1) + CU_FAIL_FATAL("Nothing written to slave end of pty"); + CU_ASSERT_STRING_EQUAL(read_buf, "passw0rd"); + close(masterfd); + return; +} + void test_passphrase_read(void) { passphrase_read_prompts_to_tty(); + passphrase_read_reads_from_tty(); } -- 2.7.4