]> git.proxmox.com Git - systemd.git/blob - src/test/test-terminal-util.c
New upstream version 249~rc1
[systemd.git] / src / test / test-terminal-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "fd-util.h"
11 #include "macro.h"
12 #include "path-util.h"
13 #include "strv.h"
14 #include "terminal-util.h"
15 #include "tests.h"
16 #include "tmpfile-util.h"
17 #include "util.h"
18
19 #define LOREM_IPSUM "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " \
20 "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation " \
21 "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit " \
22 "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " \
23 "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
24
25 static void test_default_term_for_tty(void) {
26 log_info("/* %s */", __func__);
27
28 puts(default_term_for_tty("/dev/tty23"));
29 puts(default_term_for_tty("/dev/ttyS23"));
30 puts(default_term_for_tty("/dev/tty0"));
31 puts(default_term_for_tty("/dev/pty0"));
32 puts(default_term_for_tty("/dev/pts/0"));
33 puts(default_term_for_tty("/dev/console"));
34 puts(default_term_for_tty("tty23"));
35 puts(default_term_for_tty("ttyS23"));
36 puts(default_term_for_tty("tty0"));
37 puts(default_term_for_tty("pty0"));
38 puts(default_term_for_tty("pts/0"));
39 puts(default_term_for_tty("console"));
40 }
41
42 static void test_read_one_char(void) {
43 _cleanup_fclose_ FILE *file = NULL;
44 char r;
45 bool need_nl;
46 char name[] = "/tmp/test-read_one_char.XXXXXX";
47
48 log_info("/* %s */", __func__);
49
50 assert_se(fmkostemp_safe(name, "r+", &file) == 0);
51
52 assert_se(fputs("c\n", file) >= 0);
53 rewind(file);
54 assert_se(read_one_char(file, &r, 1000000, &need_nl) >= 0);
55 assert_se(!need_nl);
56 assert_se(r == 'c');
57 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
58
59 rewind(file);
60 assert_se(fputs("foobar\n", file) >= 0);
61 rewind(file);
62 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
63
64 rewind(file);
65 assert_se(fputs("\n", file) >= 0);
66 rewind(file);
67 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
68
69 assert_se(unlink(name) >= 0);
70 }
71
72 static void test_getttyname_malloc(void) {
73 _cleanup_free_ char *ttyname = NULL;
74 _cleanup_close_ int master = -1;
75
76 log_info("/* %s */", __func__);
77
78 assert_se((master = posix_openpt(O_RDWR|O_NOCTTY)) >= 0);
79 assert_se(getttyname_malloc(master, &ttyname) >= 0);
80 log_info("ttyname = %s", ttyname);
81
82 assert_se(PATH_IN_SET(ttyname, "ptmx", "pts/ptmx"));
83 }
84
85 typedef struct {
86 const char *name;
87 const char* (*func)(void);
88 } Color;
89
90 static const Color colors[] = {
91 { "normal", ansi_normal },
92 { "highlight", ansi_highlight },
93 { "black", ansi_black },
94 { "red", ansi_red },
95 { "green", ansi_green },
96 { "yellow", ansi_yellow },
97 { "blue", ansi_blue },
98 { "magenta", ansi_magenta },
99 { "cyan", ansi_cyan },
100 { "white", ansi_white },
101 { "grey", ansi_grey },
102
103 { "bright-black", ansi_bright_black },
104 { "bright-red", ansi_bright_red },
105 { "bright-green", ansi_bright_green },
106 { "bright-yellow", ansi_bright_yellow },
107 { "bright-blue", ansi_bright_blue },
108 { "bright-magenta", ansi_bright_magenta },
109 { "bright-cyan", ansi_bright_cyan },
110 { "bright-white", ansi_bright_white },
111
112 { "highlight-black", ansi_highlight_black },
113 { "highlight-red", ansi_highlight_red },
114 { "highlight-green", ansi_highlight_green },
115 { "highlight-yellow (original)", _ansi_highlight_yellow },
116 { "highlight-yellow (replacement)", ansi_highlight_yellow },
117 { "highlight-blue", ansi_highlight_blue },
118 { "highlight-magenta", ansi_highlight_magenta },
119 { "highlight-cyan", ansi_highlight_cyan },
120 { "highlight-white", ansi_highlight_white },
121 { "highlight-grey", ansi_highlight_grey },
122
123 { "underline", ansi_underline },
124 { "highlight-underline", ansi_highlight_underline },
125 { "highlight-red-underline", ansi_highlight_red_underline },
126 { "highlight-green-underline", ansi_highlight_green_underline },
127 { "highlight-yellow-underline", ansi_highlight_yellow_underline },
128 { "highlight-blue-underline", ansi_highlight_blue_underline },
129 { "highlight-magenta-underline", ansi_highlight_magenta_underline },
130 { "highlight-grey-underline", ansi_highlight_grey_underline },
131 };
132
133 static void test_colors(void) {
134 log_info("/* %s */", __func__);
135
136 for (size_t i = 0; i < ELEMENTSOF(colors); i++)
137 printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal());
138 }
139
140 static void test_text(void) {
141 log_info("/* %s */", __func__);
142
143 for (size_t i = 0; !streq(colors[i].name, "underline"); i++) {
144 bool blwh = strstr(colors[i].name, "black")
145 || strstr(colors[i].name, "white");
146
147 printf("\n"
148 "Testing color %s%s\n%s%s%s\n",
149 colors[i].name,
150 blwh ? "" : ", this text should be readable",
151 colors[i].func(),
152 LOREM_IPSUM,
153 ansi_normal());
154 }
155 }
156
157 static void test_get_ctty(void) {
158 _cleanup_free_ char *ctty = NULL;
159 struct stat st;
160 dev_t devnr;
161 int r;
162
163 r = get_ctty(0, &devnr, &ctty);
164 if (r < 0) {
165 log_notice_errno(r, "Apparently called without a controlling TTY, cutting get_ctty() test short: %m");
166 return;
167 }
168
169 /* In almost all cases STDIN will match our controlling TTY. Let's verify that and then compare paths */
170 assert_se(fstat(STDIN_FILENO, &st) >= 0);
171 if (S_ISCHR(st.st_mode) && st.st_rdev == devnr) {
172 _cleanup_free_ char *stdin_name = NULL;
173
174 assert_se(getttyname_malloc(STDIN_FILENO, &stdin_name) >= 0);
175 assert_se(path_equal(stdin_name, ctty));
176 } else
177 log_notice("Not invoked with stdin == ctty, cutting get_ctty() test short");
178 }
179
180 int main(int argc, char *argv[]) {
181 test_setup_logging(LOG_INFO);
182
183 test_default_term_for_tty();
184 test_read_one_char();
185 test_getttyname_malloc();
186 test_colors();
187 test_text();
188 test_get_ctty();
189
190 return 0;
191 }