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