1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
11 #include "path-util.h"
13 #include "terminal-util.h"
15 #include "tmpfile-util.h"
18 #define LOREM_IPSUM "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " \
19 "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation " \
20 "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " \
21 "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " \
22 "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
24 static void test_default_term_for_tty(void) {
25 log_info("/* %s */", __func__
);
27 puts(default_term_for_tty("/dev/tty23"));
28 puts(default_term_for_tty("/dev/ttyS23"));
29 puts(default_term_for_tty("/dev/tty0"));
30 puts(default_term_for_tty("/dev/pty0"));
31 puts(default_term_for_tty("/dev/pts/0"));
32 puts(default_term_for_tty("/dev/console"));
33 puts(default_term_for_tty("tty23"));
34 puts(default_term_for_tty("ttyS23"));
35 puts(default_term_for_tty("tty0"));
36 puts(default_term_for_tty("pty0"));
37 puts(default_term_for_tty("pts/0"));
38 puts(default_term_for_tty("console"));
41 static void test_read_one_char(void) {
42 _cleanup_fclose_
FILE *file
= NULL
;
45 char name
[] = "/tmp/test-read_one_char.XXXXXX";
47 log_info("/* %s */", __func__
);
49 assert_se(fmkostemp_safe(name
, "r+", &file
) == 0);
51 assert_se(fputs("c\n", file
) >= 0);
53 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) >= 0);
56 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) < 0);
59 assert_se(fputs("foobar\n", file
) >= 0);
61 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) < 0);
64 assert_se(fputs("\n", file
) >= 0);
66 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) < 0);
68 assert_se(unlink(name
) >= 0);
71 static void test_getttyname_malloc(void) {
72 _cleanup_free_
char *ttyname
= NULL
;
73 _cleanup_close_
int master
= -1;
75 log_info("/* %s */", __func__
);
77 assert_se((master
= posix_openpt(O_RDWR
|O_NOCTTY
)) >= 0);
78 assert_se(getttyname_malloc(master
, &ttyname
) >= 0);
79 log_info("ttyname = %s", ttyname
);
81 assert_se(PATH_IN_SET(ttyname
, "ptmx", "pts/ptmx"));
86 const char* (*func
)(void);
89 static const Color colors
[] = {
90 { "normal", ansi_normal
},
91 { "highlight", ansi_highlight
},
92 { "black", ansi_black
},
94 { "green", ansi_green
},
95 { "yellow", ansi_yellow
},
96 { "blue", ansi_blue
},
97 { "magenta", ansi_magenta
},
98 { "cyan", ansi_cyan
},
99 { "white", ansi_white
},
100 { "grey", ansi_grey
},
102 { "bright-black", ansi_bright_black
},
103 { "bright-red", ansi_bright_red
},
104 { "bright-green", ansi_bright_green
},
105 { "bright-yellow", ansi_bright_yellow
},
106 { "bright-blue", ansi_bright_blue
},
107 { "bright-magenta", ansi_bright_magenta
},
108 { "bright-cyan", ansi_bright_cyan
},
109 { "bright-white", ansi_bright_white
},
111 { "highlight-black", ansi_highlight_black
},
112 { "highlight-red", ansi_highlight_red
},
113 { "highlight-green", ansi_highlight_green
},
114 { "highlight-yellow (original)", _ansi_highlight_yellow
},
115 { "highlight-yellow (replacement)", ansi_highlight_yellow
},
116 { "highlight-blue", ansi_highlight_blue
},
117 { "highlight-magenta", ansi_highlight_magenta
},
118 { "highlight-cyan", ansi_highlight_cyan
},
119 { "highlight-white", ansi_highlight_white
},
120 { "highlight-grey", ansi_highlight_grey
},
122 { "underline", ansi_underline
},
123 { "highlight-underline", ansi_highlight_underline
},
124 { "highlight-red-underline", ansi_highlight_red_underline
},
125 { "highlight-green-underline", ansi_highlight_green_underline
},
126 { "highlight-yellow-underline", ansi_highlight_yellow_underline
},
127 { "highlight-blue-underline", ansi_highlight_blue_underline
},
128 { "highlight-magenta-underline", ansi_highlight_magenta_underline
},
129 { "highlight-grey-underline", ansi_highlight_grey_underline
},
132 static void test_colors(void) {
133 log_info("/* %s */", __func__
);
135 for (size_t i
= 0; i
< ELEMENTSOF(colors
); i
++)
136 printf("<%s%s%s>\n", colors
[i
].func(), colors
[i
].name
, ansi_normal());
139 static void test_text(void) {
140 log_info("/* %s */", __func__
);
142 for (size_t i
= 0; !streq(colors
[i
].name
, "underline"); i
++) {
143 bool blwh
= strstr(colors
[i
].name
, "black")
144 || strstr(colors
[i
].name
, "white");
147 "Testing color %s%s\n%s%s%s\n",
149 blwh
? "" : ", this text should be readable",
156 int main(int argc
, char *argv
[]) {
157 test_setup_logging(LOG_INFO
);
159 test_default_term_for_tty();
160 test_read_one_char();
161 test_getttyname_malloc();