2 * FreeRDP: A Remote Desktop Protocol Client
5 * Copyright 2011 Vic Lee, 2011 Shea Levy
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #define _XOPEN_SOURCE 700
26 #include <sys/select.h>
31 #include <freerdp/freerdp.h>
32 #include <freerdp/utils/mutex.h>
33 #include <freerdp/utils/semaphore.h>
34 #include <freerdp/utils/load_plugin.h>
35 #include <freerdp/utils/wait_obj.h>
36 #include <freerdp/utils/args.h>
37 #include <freerdp/utils/passphrase.h>
38 #include <freerdp/utils/signal.h>
40 #include "test_utils.h"
42 int init_utils_suite(void)
47 int clean_utils_suite(void)
52 int add_utils_suite(void)
54 add_test_suite(utils);
56 add_test_function(mutex);
57 add_test_function(semaphore);
58 add_test_function(load_plugin);
59 add_test_function(wait_obj);
60 add_test_function(args);
61 add_test_function(passphrase_read);
62 add_test_function(handle_signals);
71 mutex = freerdp_mutex_new();
72 freerdp_mutex_lock(mutex);
73 freerdp_mutex_unlock(mutex);
74 freerdp_mutex_free(mutex);
77 void test_semaphore(void)
81 sem = freerdp_sem_new(1);
82 freerdp_sem_wait(sem);
83 freerdp_sem_signal(sem);
84 freerdp_sem_free(sem);
87 void test_load_plugin(void)
93 entry = freerdp_load_plugin("..\\channels\\cliprdr\\cliprdr", "VirtualChannelEntry");
95 entry = freerdp_load_plugin("../channels/cliprdr/cliprdr.so", "VirtualChannelEntry");
97 CU_ASSERT(entry != NULL);
100 void test_wait_obj(void)
107 set = wait_obj_is_set(wo);
111 set = wait_obj_is_set(wo);
115 set = wait_obj_is_set(wo);
118 wait_obj_select(&wo, 1, 1000);
123 static int process_plugin_args(rdpSettings* settings, const char* name,
124 RDP_PLUGIN_DATA* plugin_data, void* user_data)
126 /*printf("load plugin: %s\n", name);*/
130 static int process_ui_args(rdpSettings* settings, const char* opt,
131 const char* val, void* user_data)
133 /*printf("ui arg: %s %s\n", opt, val);*/
141 "freerdp", "-a", "8", "-u", "testuser", "-d", "testdomain", "-g", "640x480", "address1:3389",
142 "freerdp", "-a", "16", "-u", "testuser", "-d", "testdomain", "-g", "1280x960", "address2:3390"
144 char** argv = argv_c;
145 int argc = sizeof(argv_c) / sizeof(char*);
148 rdpSettings* settings;
153 settings = settings_new(NULL);
156 c = freerdp_parse_args(settings, argc, argv, process_plugin_args, NULL, process_ui_args, NULL);
160 settings_free(settings);
163 CU_ASSERT(settings->color_depth == i * 8);
164 CU_ASSERT(settings->width == i * 640);
165 CU_ASSERT(settings->height == i * 480);
166 CU_ASSERT(settings->port == i + 3388);
168 settings_free(settings);
175 void passphrase_read_prompts_to_tty()
177 static const int read_nbyte = 11;
180 char read_buf[read_nbyte];
183 masterfd = posix_openpt(O_RDWR|O_NOCTTY);
186 || grantpt (masterfd) == -1
187 || unlockpt (masterfd) == -1
188 || (slavedevice = ptsname (masterfd)) == NULL)
189 CU_FAIL_FATAL("Could not create pty");
194 CU_FAIL_FATAL("Could not fork");
197 static const int password_size = 512;
198 char buffer[password_size];
200 if (setsid() == (pid_t) -1)
201 CU_FAIL_FATAL("Could not create new session");
203 if ((slavefd = open(slavedevice, O_RDWR)) == 0)
204 CU_FAIL_FATAL("Could not open slave end of pty");
206 close(STDOUT_FILENO);
207 close(STDERR_FILENO);
209 freerdp_passphrase_read("Password: ", buffer, password_size);
215 read_buf[read_nbyte - 1] = '\0';
217 FD_ZERO(&fd_set_write);
218 FD_SET(masterfd, &fd_set_write);
219 if (select(masterfd + 1, NULL, &fd_set_write, NULL, NULL) == -1)
220 CU_FAIL_FATAL("Master end of pty not writeable");
221 if (read(masterfd, read_buf, read_nbyte) == (ssize_t) -1)
222 CU_FAIL_FATAL("Nothing written to slave end of pty");
223 CU_ASSERT_STRING_EQUAL(read_buf, "Password: ");
225 write(masterfd, "\n", (size_t) 2);
230 void passphrase_read_reads_from_tty()
232 static const int read_nbyte = 11;
236 char read_buf[read_nbyte];
239 masterfd = posix_openpt(O_RDWR|O_NOCTTY);
242 || grantpt (masterfd) == -1
243 || unlockpt (masterfd) == -1
244 || (slavedevice = ptsname (masterfd)) == NULL)
245 CU_FAIL_FATAL("Could not create pty");
247 if (pipe(pipe_ends) != 0)
248 CU_FAIL_FATAL("Could not create pipe");
253 CU_FAIL_FATAL("Could not fork");
256 static const int password_size = 512;
257 char buffer[password_size];
259 if (setsid() == (pid_t) -1)
260 CU_FAIL_FATAL("Could not create new session");
262 if ((slavefd = open(slavedevice, O_RDWR)) == 0)
263 CU_FAIL_FATAL("Could not open slave end of pty");
265 close(STDOUT_FILENO);
266 close(STDERR_FILENO);
269 freerdp_passphrase_read("Password: ", buffer, password_size);
270 write(pipe_ends[1], buffer, password_size);
278 read_buf[read_nbyte - 1] = '\0';
280 FD_ZERO(&fd_set_write);
281 FD_SET(masterfd, &fd_set_write);
282 if (select(masterfd + 1, NULL, &fd_set_write, NULL, NULL) == -1)
283 CU_FAIL_FATAL("Master end of pty not writeable");
284 if (read(masterfd, read_buf, read_nbyte) == (ssize_t) -1)
285 CU_FAIL_FATAL("Nothing written to slave end of pty");
287 write(masterfd, "passw0rd\n", sizeof "passw0rd\n");
288 if (read(pipe_ends[0], read_buf, read_nbyte) == (ssize_t) -1)
289 CU_FAIL_FATAL("Nothing written to pipe");
290 CU_ASSERT_STRING_EQUAL(read_buf, "passw0rd");
296 void passphrase_read_turns_off_echo_during_read()
298 static const int read_nbyte = 11;
299 int masterfd, slavefd;
301 char read_buf[read_nbyte];
303 struct termios term_flags;
305 masterfd = posix_openpt(O_RDWR|O_NOCTTY);
308 || grantpt (masterfd) == -1
309 || unlockpt (masterfd) == -1
310 || (slavedevice = ptsname (masterfd)) == NULL)
311 CU_FAIL_FATAL("Could not create pty");
313 slavefd = open(slavedevice, O_RDWR|O_NOCTTY);
315 CU_FAIL_FATAL("Could not open slave end of pty");
317 if (tcgetattr(slavefd, &term_flags) != 0)
318 CU_FAIL_FATAL("Could not get slave pty attributes");
319 if (!(term_flags.c_lflag & ECHO))
321 term_flags.c_lflag |= ECHO;
322 if (tcsetattr(slavefd, TCSANOW, &term_flags) != 0)
323 CU_FAIL_FATAL("Could not turn ECHO on on slave pty");
329 CU_FAIL_FATAL("Could not fork");
332 static const int password_size = 512;
334 char buffer[password_size];
335 if (setsid() == (pid_t) -1)
336 CU_FAIL_FATAL("Could not create new session");
338 if ((child_slavefd = open(slavedevice, O_RDWR)) == 0)
339 CU_FAIL_FATAL("Could not open slave end of pty");
341 close(STDOUT_FILENO);
342 close(STDERR_FILENO);
345 freerdp_passphrase_read("Password: ", buffer, password_size);
346 close(child_slavefd);
351 read_buf[read_nbyte - 1] = '\0';
353 FD_ZERO(&fd_set_write);
354 FD_SET(masterfd, &fd_set_write);
355 if (select(masterfd + 1, NULL, &fd_set_write, NULL, NULL) == -1)
356 CU_FAIL_FATAL("Master end of pty not writeable");
357 if (read(masterfd, read_buf, read_nbyte) == (ssize_t) -1)
358 CU_FAIL_FATAL("Nothing written to slave end of pty");
360 if (tcgetattr(slavefd, &term_flags) != 0)
361 CU_FAIL_FATAL("Could not get slave pty attributes");
362 CU_ASSERT(!(term_flags.c_lflag & ECHO))
363 write(masterfd, "\n", (size_t) 2);
369 void passphrase_read_resets_terminal_after_read()
371 static const int read_nbyte = 11;
372 int masterfd, slavefd, status;
374 char read_buf[read_nbyte];
376 struct termios term_flags;
379 masterfd = posix_openpt(O_RDWR|O_NOCTTY);
382 || grantpt (masterfd) == -1
383 || unlockpt (masterfd) == -1
384 || (slavedevice = ptsname (masterfd)) == NULL)
385 CU_FAIL_FATAL("Could not create pty");
387 slavefd = open(slavedevice, O_RDWR|O_NOCTTY);
389 CU_FAIL_FATAL("Could not open slave end of pty");
391 if (tcgetattr(slavefd, &term_flags) != 0)
392 CU_FAIL_FATAL("Could not get slave pty attributes");
393 if (!(term_flags.c_lflag & ECHO))
395 term_flags.c_lflag |= ECHO;
396 if (tcsetattr(slavefd, TCSANOW, &term_flags) != 0)
397 CU_FAIL_FATAL("Could not turn ECHO on on slave pty");
400 switch (child = fork())
403 CU_FAIL_FATAL("Could not fork");
406 static const int password_size = 512;
408 char buffer[password_size];
409 if (setsid() == (pid_t) -1)
410 CU_FAIL_FATAL("Could not create new session");
412 if ((child_slavefd = open(slavedevice, O_RDWR)) == 0)
413 CU_FAIL_FATAL("Could not open slave end of pty");
415 close(STDOUT_FILENO);
416 close(STDERR_FILENO);
419 freerdp_passphrase_read("Password: ", buffer, password_size);
420 close(child_slavefd);
425 read_buf[read_nbyte - 1] = '\0';
427 FD_ZERO(&fd_set_write);
428 FD_SET(masterfd, &fd_set_write);
429 if (select(masterfd + 1, NULL, &fd_set_write, NULL, NULL) == -1)
430 CU_FAIL_FATAL("Master end of pty not writeable");
431 if (read(masterfd, read_buf, read_nbyte) == (ssize_t) -1)
432 CU_FAIL_FATAL("Nothing written to slave end of pty");
434 write(masterfd, "\n", (size_t) 2);
435 waitpid(child, &status, WUNTRACED);
436 if (tcgetattr(slavefd, &term_flags) != 0)
437 CU_FAIL_FATAL("Could not get slave pty attributes");
438 CU_ASSERT(term_flags.c_lflag & ECHO)
444 void passphrase_read_turns_on_newline_echo_during_read()
446 static const int read_nbyte = 11;
447 int masterfd, slavefd;
449 char read_buf[read_nbyte];
451 struct termios term_flags;
453 masterfd = posix_openpt(O_RDWR|O_NOCTTY);
456 || grantpt (masterfd) == -1
457 || unlockpt (masterfd) == -1
458 || (slavedevice = ptsname (masterfd)) == NULL)
459 CU_FAIL_FATAL("Could not create pty");
461 slavefd = open(slavedevice, O_RDWR|O_NOCTTY);
463 CU_FAIL_FATAL("Could not open slave end of pty");
465 if (tcgetattr(slavefd, &term_flags) != 0)
466 CU_FAIL_FATAL("Could not get slave pty attributes");
467 if (term_flags.c_lflag & ECHONL)
469 term_flags.c_lflag &= ~ECHONL;
470 if (tcsetattr(slavefd, TCSANOW, &term_flags) != 0)
471 CU_FAIL_FATAL("Could not turn ECHO on on slave pty");
477 CU_FAIL_FATAL("Could not fork");
480 static const int password_size = 512;
482 char buffer[password_size];
483 if (setsid() == (pid_t) -1)
484 CU_FAIL_FATAL("Could not create new session");
486 if ((child_slavefd = open(slavedevice, O_RDWR)) == 0)
487 CU_FAIL_FATAL("Could not open slave end of pty");
489 close(STDOUT_FILENO);
490 close(STDERR_FILENO);
493 freerdp_passphrase_read("Password: ", buffer, password_size);
494 close(child_slavefd);
499 read_buf[read_nbyte - 1] = '\0';
501 FD_ZERO(&fd_set_write);
502 FD_SET(masterfd, &fd_set_write);
503 if (select(masterfd + 1, NULL, &fd_set_write, NULL, NULL) == -1)
504 CU_FAIL_FATAL("Master end of pty not writeable");
505 if (read(masterfd, read_buf, read_nbyte) == (ssize_t) -1)
506 CU_FAIL_FATAL("Nothing written to slave end of pty");
508 if (tcgetattr(slavefd, &term_flags) != 0)
509 CU_FAIL_FATAL("Could not get slave pty attributes");
510 CU_ASSERT(term_flags.c_lflag & ECHONL)
511 write(masterfd, "\n", (size_t) 2);
517 void passphrase_read_prompts_to_stderr_when_no_tty()
519 static const int read_nbyte = 11;
520 int stdin_pipe[2], stderr_pipe[2];
521 char read_buf[read_nbyte];
522 struct sigaction ignore, orig;
524 ignore.sa_handler = SIG_IGN;
525 sigemptyset(&ignore.sa_mask);
527 if (pipe(stdin_pipe) != 0 || pipe(stderr_pipe) != 0)
528 CU_FAIL_FATAL("Could not create pipe");
533 CU_FAIL_FATAL("Could not fork");
536 static const int password_size = 512;
537 char buffer[password_size];
538 close(stderr_pipe[0]);
539 close(stdin_pipe[1]);
540 if (setsid() == (pid_t) -1)
541 CU_FAIL_FATAL("Could not create new session");
543 dup2(stdin_pipe[0], STDIN_FILENO);
544 dup2(stderr_pipe[1], STDERR_FILENO);
545 freerdp_passphrase_read("Password: ", buffer, password_size);
549 close(stderr_pipe[1]);
550 close(stdin_pipe[0]);
552 read_buf[read_nbyte - 1] = '\0';
554 if (read(stderr_pipe[0], read_buf, read_nbyte) == (ssize_t) -1)
555 CU_FAIL_FATAL("Nothing written to pipe");
556 CU_ASSERT_STRING_EQUAL(read_buf, "Password: ");
558 sigaction(SIGPIPE, &ignore, &orig);
559 write(stdin_pipe[1], "\n", (size_t) 2);
560 sigaction(SIGPIPE, &orig, NULL);
561 close(stderr_pipe[0]);
562 close(stdin_pipe[1]);
566 void passphrase_read_reads_from_stdin_when_no_tty()
568 static const int read_nbyte = 11;
569 int stdin_pipe[2], stderr_pipe[2], result_pipe[2];
570 char read_buf[read_nbyte];
571 struct sigaction ignore, orig;
573 ignore.sa_handler = SIG_IGN;
574 sigemptyset(&ignore.sa_mask);
576 if (pipe(stdin_pipe) != 0
577 || pipe(stderr_pipe) != 0
578 || pipe(result_pipe) !=0)
579 CU_FAIL_FATAL("Could not create pipe");
584 CU_FAIL_FATAL("Could not fork");
587 static const int password_size = 512;
588 char buffer[password_size];
589 close(stderr_pipe[0]);
590 close(result_pipe[0]);
591 close(stdin_pipe[1]);
592 if (setsid() == (pid_t) -1)
593 CU_FAIL_FATAL("Could not create new session");
595 dup2(stdin_pipe[0], STDIN_FILENO);
596 dup2(stderr_pipe[1], STDERR_FILENO);
597 freerdp_passphrase_read("Password: ", buffer, password_size);
598 write(result_pipe[1], buffer, strlen(buffer) + (size_t) 1);
602 close(stderr_pipe[1]);
603 close(result_pipe[1]);
604 close(stdin_pipe[0]);
606 read_buf[read_nbyte - 1] = '\0';
608 if (read(stderr_pipe[0], read_buf, read_nbyte) == (ssize_t) -1)
609 CU_FAIL_FATAL("Nothing written to pipe");
611 sigaction(SIGPIPE, &ignore, &orig);
612 write(stdin_pipe[1], "passw0rd\n", sizeof "passw0rd\n");
613 sigaction(SIGPIPE, &orig, NULL);
615 if (read(result_pipe[0], read_buf, read_nbyte) == (ssize_t) -1)
616 CU_FAIL_FATAL("Nothing written to pipe");
617 CU_ASSERT_STRING_EQUAL(read_buf, "passw0rd");
619 close(stderr_pipe[0]);
620 close(stdin_pipe[1]);
624 void test_passphrase_read(void)
626 passphrase_read_prompts_to_tty();
627 passphrase_read_reads_from_tty();
628 passphrase_read_turns_off_echo_during_read();
629 passphrase_read_resets_terminal_after_read();
630 passphrase_read_turns_on_newline_echo_during_read();
631 passphrase_read_prompts_to_stderr_when_no_tty();
632 passphrase_read_reads_from_stdin_when_no_tty();
635 void handle_signals_resets_terminal(void)
637 int status, masterfd;
639 struct termios test_flags;
642 masterfd = posix_openpt(O_RDWR|O_NOCTTY);
645 || grantpt (masterfd) == -1
646 || unlockpt (masterfd) == -1
647 || (slavedevice = ptsname (masterfd)) == NULL)
648 CU_FAIL_FATAL("Could not create pty");
650 terminal_fildes = open(slavedevice, O_RDWR|O_NOCTTY);
651 tcgetattr(terminal_fildes, &orig_flags);
652 new_flags = orig_flags;
653 new_flags.c_lflag &= ~ECHO;
654 tcsetattr(terminal_fildes, TCSANOW, &new_flags);
655 terminal_needs_reset = 1;
657 if((child_pid = fork()) == 0)
659 freerdp_handle_signals();
662 while(wait(&status) != -1);
663 tcgetattr(terminal_fildes, &test_flags);
664 CU_ASSERT_EQUAL(orig_flags.c_lflag, test_flags.c_lflag);
666 close(terminal_fildes);
669 void test_handle_signals(void)
671 handle_signals_resets_terminal();