]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-terminal-util.c
Merge pull request #30284 from YHNdnzj/fstab-wantedby-defaultdeps
[thirdparty/systemd.git] / src / test / test-terminal-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
288a74cc 2
ca78ad1d 3#include <fcntl.h>
288a74cc 4#include <stdbool.h>
cf0fbc49 5#include <stdio.h>
11f3c130 6#include <sys/stat.h>
ca78ad1d 7#include <unistd.h>
288a74cc 8
23b27b39 9#include "alloc-util.h"
0d39fa9c 10#include "fd-util.h"
596b44b1 11#include "fs-util.h"
288a74cc 12#include "macro.h"
0d53f53b 13#include "path-util.h"
81f5e513 14#include "strv.h"
0d39fa9c 15#include "terminal-util.h"
e4de7287
LP
16#include "tests.h"
17#include "tmpfile-util.h"
288a74cc 18
37873ebf
ZJS
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
4f7452a8 25TEST(default_term_for_tty) {
288a74cc
RC
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
4f7452a8 40TEST(read_one_char) {
288a74cc
RC
41 _cleanup_fclose_ FILE *file = NULL;
42 char r;
43 bool need_nl;
596b44b1 44 _cleanup_(unlink_tempfilep) char name[] = "/tmp/test-read_one_char.XXXXXX";
288a74cc 45
3b1e405f 46 assert_se(fmkostemp_safe(name, "r+", &file) == 0);
d8351049 47
288a74cc
RC
48 assert_se(fputs("c\n", file) >= 0);
49 rewind(file);
288a74cc
RC
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);
0d53f53b
LP
64}
65
4f7452a8 66TEST(getttyname_malloc) {
0d53f53b 67 _cleanup_free_ char *ttyname = NULL;
5bb1d7fb 68 _cleanup_close_ int master = -EBADF;
0d53f53b
LP
69
70 assert_se((master = posix_openpt(O_RDWR|O_NOCTTY)) >= 0);
71 assert_se(getttyname_malloc(master, &ttyname) >= 0);
4e494d17
ZJS
72 log_info("ttyname = %s", ttyname);
73
0d53f53b 74 assert_se(PATH_IN_SET(ttyname, "ptmx", "pts/ptmx"));
288a74cc
RC
75}
76
66bb0059
ZJS
77typedef struct {
78 const char *name;
79 const char* (*func)(void);
80} Color;
81
82static 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 },
5ee27251
ZJS
107 { "highlight-yellow (original)", _ansi_highlight_yellow },
108 { "highlight-yellow (replacement)", ansi_highlight_yellow },
66bb0059
ZJS
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};
042f472e 124
4f7452a8 125TEST(colors) {
66bb0059
ZJS
126 for (size_t i = 0; i < ELEMENTSOF(colors); i++)
127 printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal());
042f472e
LP
128}
129
4f7452a8 130TEST(text) {
37873ebf
ZJS
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
4f7452a8 145TEST(get_ctty) {
11f3c130
LP
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
63e9c383
LP
168TEST(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
4f7452a8 179DEFINE_TEST_MAIN(LOG_INFO);