]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-terminal-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
11 #include "path-util.h"
13 #include "terminal-util.h"
15 #include "tmpfile-util.h"
18 static void test_default_term_for_tty(void) {
19 log_info("/* %s */", __func__
);
21 puts(default_term_for_tty("/dev/tty23"));
22 puts(default_term_for_tty("/dev/ttyS23"));
23 puts(default_term_for_tty("/dev/tty0"));
24 puts(default_term_for_tty("/dev/pty0"));
25 puts(default_term_for_tty("/dev/pts/0"));
26 puts(default_term_for_tty("/dev/console"));
27 puts(default_term_for_tty("tty23"));
28 puts(default_term_for_tty("ttyS23"));
29 puts(default_term_for_tty("tty0"));
30 puts(default_term_for_tty("pty0"));
31 puts(default_term_for_tty("pts/0"));
32 puts(default_term_for_tty("console"));
35 static void test_read_one_char(void) {
36 _cleanup_fclose_
FILE *file
= NULL
;
39 char name
[] = "/tmp/test-read_one_char.XXXXXX";
41 log_info("/* %s */", __func__
);
43 assert_se(fmkostemp_safe(name
, "r+", &file
) == 0);
45 assert_se(fputs("c\n", file
) >= 0);
47 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) >= 0);
50 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) < 0);
53 assert_se(fputs("foobar\n", file
) >= 0);
55 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) < 0);
58 assert_se(fputs("\n", file
) >= 0);
60 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) < 0);
62 assert_se(unlink(name
) >= 0);
65 static void test_getttyname_malloc(void) {
66 _cleanup_free_
char *ttyname
= NULL
;
67 _cleanup_close_
int master
= -1;
69 log_info("/* %s */", __func__
);
71 assert_se((master
= posix_openpt(O_RDWR
|O_NOCTTY
)) >= 0);
72 assert_se(getttyname_malloc(master
, &ttyname
) >= 0);
73 log_info("ttyname = %s", ttyname
);
75 assert_se(PATH_IN_SET(ttyname
, "ptmx", "pts/ptmx"));
78 static void test_one_color(const char *name
, const char *color
) {
79 printf("<%s%s%s>\n", color
, name
, ansi_normal());
82 static void test_colors(void) {
83 log_info("/* %s */", __func__
);
85 test_one_color("normal", ansi_normal());
86 test_one_color("highlight", ansi_highlight());
87 test_one_color("red", ansi_red());
88 test_one_color("green", ansi_green());
89 test_one_color("yellow", ansi_yellow());
90 test_one_color("blue", ansi_blue());
91 test_one_color("magenta", ansi_magenta());
92 test_one_color("grey", ansi_grey());
93 test_one_color("highlight-red", ansi_highlight_red());
94 test_one_color("highlight-green", ansi_highlight_green());
95 test_one_color("highlight-yellow", ansi_highlight_yellow());
96 test_one_color("highlight-blue", ansi_highlight_blue());
97 test_one_color("highlight-magenta", ansi_highlight_magenta());
98 test_one_color("highlight-grey", ansi_highlight_grey());
100 test_one_color("underline", ansi_underline());
101 test_one_color("highlight-underline", ansi_highlight_underline());
102 test_one_color("highlight-red-underline", ansi_highlight_red_underline());
103 test_one_color("highlight-green-underline", ansi_highlight_green_underline());
104 test_one_color("highlight-yellow-underline", ansi_highlight_yellow_underline());
105 test_one_color("highlight-blue-underline", ansi_highlight_blue_underline());
106 test_one_color("highlight-magenta-underline", ansi_highlight_magenta_underline());
107 test_one_color("highlight-grey-underline", ansi_highlight_grey_underline());
110 int main(int argc
, char *argv
[]) {
111 test_setup_logging(LOG_INFO
);
113 test_default_term_for_tty();
114 test_read_one_char();
115 test_getttyname_malloc();