]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-terminal-util.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/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 "fs-util.h"
12 #include "macro.h"
13 #include "path-util.h"
14 #include "strv.h"
15 #include "terminal-util.h"
16 #include "tests.h"
17 #include "tmpfile-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 TEST(default_term_for_tty) {
26 puts(default_term_for_tty("/dev/tty23"));
27 puts(default_term_for_tty("/dev/ttyS23"));
28 puts(default_term_for_tty("/dev/tty0"));
29 puts(default_term_for_tty("/dev/pty0"));
30 puts(default_term_for_tty("/dev/pts/0"));
31 puts(default_term_for_tty("/dev/console"));
32 puts(default_term_for_tty("tty23"));
33 puts(default_term_for_tty("ttyS23"));
34 puts(default_term_for_tty("tty0"));
35 puts(default_term_for_tty("pty0"));
36 puts(default_term_for_tty("pts/0"));
37 puts(default_term_for_tty("console"));
38 }
39
40 TEST(read_one_char) {
41 _cleanup_fclose_ FILE *file = NULL;
42 char r;
43 bool need_nl;
44 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-read_one_char.XXXXXX";
45
46 assert_se(fmkostemp_safe(name, "r+", &file) == 0);
47
48 assert_se(fputs("c\n", file) >= 0);
49 rewind(file);
50 assert_se(read_one_char(file, &r, 1000000, &need_nl) >= 0);
51 assert_se(!need_nl);
52 assert_se(r == 'c');
53 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
54
55 rewind(file);
56 assert_se(fputs("foobar\n", file) >= 0);
57 rewind(file);
58 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
59
60 rewind(file);
61 assert_se(fputs("\n", file) >= 0);
62 rewind(file);
63 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
64 }
65
66 TEST(getttyname_malloc) {
67 _cleanup_free_ char *ttyname = NULL;
68 _cleanup_close_ int master = -EBADF;
69
70 assert_se((master = posix_openpt(O_RDWR|O_NOCTTY)) >= 0);
71 assert_se(getttyname_malloc(master, &ttyname) >= 0);
72 log_info("ttyname = %s", ttyname);
73
74 assert_se(PATH_IN_SET(ttyname, "ptmx", "pts/ptmx"));
75 }
76
77 typedef struct {
78 const char *name;
79 const char* (*func)(void);
80 } Color;
81
82 static const Color colors[] = {
83 { "normal", ansi_normal },
84 { "highlight", ansi_highlight },
85 { "black", ansi_black },
86 { "red", ansi_red },
87 { "green", ansi_green },
88 { "yellow", ansi_yellow },
89 { "blue", ansi_blue },
90 { "magenta", ansi_magenta },
91 { "cyan", ansi_cyan },
92 { "white", ansi_white },
93 { "grey", ansi_grey },
94
95 { "bright-black", ansi_bright_black },
96 { "bright-red", ansi_bright_red },
97 { "bright-green", ansi_bright_green },
98 { "bright-yellow", ansi_bright_yellow },
99 { "bright-blue", ansi_bright_blue },
100 { "bright-magenta", ansi_bright_magenta },
101 { "bright-cyan", ansi_bright_cyan },
102 { "bright-white", ansi_bright_white },
103
104 { "highlight-black", ansi_highlight_black },
105 { "highlight-red", ansi_highlight_red },
106 { "highlight-green", ansi_highlight_green },
107 { "highlight-yellow (original)", _ansi_highlight_yellow },
108 { "highlight-yellow (replacement)", ansi_highlight_yellow },
109 { "highlight-blue", ansi_highlight_blue },
110 { "highlight-magenta", ansi_highlight_magenta },
111 { "highlight-cyan", ansi_highlight_cyan },
112 { "highlight-white", ansi_highlight_white },
113 { "highlight-grey", ansi_highlight_grey },
114
115 { "underline", ansi_underline },
116 { "highlight-underline", ansi_highlight_underline },
117 { "highlight-red-underline", ansi_highlight_red_underline },
118 { "highlight-green-underline", ansi_highlight_green_underline },
119 { "highlight-yellow-underline", ansi_highlight_yellow_underline },
120 { "highlight-blue-underline", ansi_highlight_blue_underline },
121 { "highlight-magenta-underline", ansi_highlight_magenta_underline },
122 { "highlight-grey-underline", ansi_highlight_grey_underline },
123 };
124
125 TEST(colors) {
126 for (size_t i = 0; i < ELEMENTSOF(colors); i++)
127 printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal());
128 }
129
130 TEST(text) {
131 for (size_t i = 0; !streq(colors[i].name, "underline"); i++) {
132 bool blwh = strstr(colors[i].name, "black")
133 || strstr(colors[i].name, "white");
134
135 printf("\n"
136 "Testing color %s%s\n%s%s%s\n",
137 colors[i].name,
138 blwh ? "" : ", this text should be readable",
139 colors[i].func(),
140 LOREM_IPSUM,
141 ansi_normal());
142 }
143 }
144
145 TEST(get_ctty) {
146 _cleanup_free_ char *ctty = NULL;
147 struct stat st;
148 dev_t devnr;
149 int r;
150
151 r = get_ctty(0, &devnr, &ctty);
152 if (r < 0) {
153 log_notice_errno(r, "Apparently called without a controlling TTY, cutting get_ctty() test short: %m");
154 return;
155 }
156
157 /* In almost all cases STDIN will match our controlling TTY. Let's verify that and then compare paths */
158 assert_se(fstat(STDIN_FILENO, &st) >= 0);
159 if (S_ISCHR(st.st_mode) && st.st_rdev == devnr) {
160 _cleanup_free_ char *stdin_name = NULL;
161
162 assert_se(getttyname_malloc(STDIN_FILENO, &stdin_name) >= 0);
163 assert_se(path_equal(stdin_name, ctty));
164 } else
165 log_notice("Not invoked with stdin == ctty, cutting get_ctty() test short");
166 }
167
168 TEST(get_default_background_color) {
169 double red, green, blue;
170 int r;
171
172 r = get_default_background_color(&red, &green, &blue);
173 if (r < 0)
174 log_notice_errno(r, "Can't get terminal default background color: %m");
175 else
176 log_notice("R=%g G=%g B=%g", red, green, blue);
177 }
178
179 DEFINE_TEST_MAIN(LOG_INFO);