]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-terminal-util.c
Merge pull request #20476 from jamacku/new-feature-reloaded-stamp
[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 #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 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 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 assert_se(unlink(name) >= 0);
66 }
67
68 TEST(getttyname_malloc) {
69 _cleanup_free_ char *ttyname = NULL;
70 _cleanup_close_ int master = -1;
71
72 assert_se((master = posix_openpt(O_RDWR|O_NOCTTY)) >= 0);
73 assert_se(getttyname_malloc(master, &ttyname) >= 0);
74 log_info("ttyname = %s", ttyname);
75
76 assert_se(PATH_IN_SET(ttyname, "ptmx", "pts/ptmx"));
77 }
78
79 typedef struct {
80 const char *name;
81 const char* (*func)(void);
82 } Color;
83
84 static const Color colors[] = {
85 { "normal", ansi_normal },
86 { "highlight", ansi_highlight },
87 { "black", ansi_black },
88 { "red", ansi_red },
89 { "green", ansi_green },
90 { "yellow", ansi_yellow },
91 { "blue", ansi_blue },
92 { "magenta", ansi_magenta },
93 { "cyan", ansi_cyan },
94 { "white", ansi_white },
95 { "grey", ansi_grey },
96
97 { "bright-black", ansi_bright_black },
98 { "bright-red", ansi_bright_red },
99 { "bright-green", ansi_bright_green },
100 { "bright-yellow", ansi_bright_yellow },
101 { "bright-blue", ansi_bright_blue },
102 { "bright-magenta", ansi_bright_magenta },
103 { "bright-cyan", ansi_bright_cyan },
104 { "bright-white", ansi_bright_white },
105
106 { "highlight-black", ansi_highlight_black },
107 { "highlight-red", ansi_highlight_red },
108 { "highlight-green", ansi_highlight_green },
109 { "highlight-yellow (original)", _ansi_highlight_yellow },
110 { "highlight-yellow (replacement)", ansi_highlight_yellow },
111 { "highlight-blue", ansi_highlight_blue },
112 { "highlight-magenta", ansi_highlight_magenta },
113 { "highlight-cyan", ansi_highlight_cyan },
114 { "highlight-white", ansi_highlight_white },
115 { "highlight-grey", ansi_highlight_grey },
116
117 { "underline", ansi_underline },
118 { "highlight-underline", ansi_highlight_underline },
119 { "highlight-red-underline", ansi_highlight_red_underline },
120 { "highlight-green-underline", ansi_highlight_green_underline },
121 { "highlight-yellow-underline", ansi_highlight_yellow_underline },
122 { "highlight-blue-underline", ansi_highlight_blue_underline },
123 { "highlight-magenta-underline", ansi_highlight_magenta_underline },
124 { "highlight-grey-underline", ansi_highlight_grey_underline },
125 };
126
127 TEST(colors) {
128 for (size_t i = 0; i < ELEMENTSOF(colors); i++)
129 printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal());
130 }
131
132 TEST(text) {
133 for (size_t i = 0; !streq(colors[i].name, "underline"); i++) {
134 bool blwh = strstr(colors[i].name, "black")
135 || strstr(colors[i].name, "white");
136
137 printf("\n"
138 "Testing color %s%s\n%s%s%s\n",
139 colors[i].name,
140 blwh ? "" : ", this text should be readable",
141 colors[i].func(),
142 LOREM_IPSUM,
143 ansi_normal());
144 }
145 }
146
147 TEST(get_ctty) {
148 _cleanup_free_ char *ctty = NULL;
149 struct stat st;
150 dev_t devnr;
151 int r;
152
153 r = get_ctty(0, &devnr, &ctty);
154 if (r < 0) {
155 log_notice_errno(r, "Apparently called without a controlling TTY, cutting get_ctty() test short: %m");
156 return;
157 }
158
159 /* In almost all cases STDIN will match our controlling TTY. Let's verify that and then compare paths */
160 assert_se(fstat(STDIN_FILENO, &st) >= 0);
161 if (S_ISCHR(st.st_mode) && st.st_rdev == devnr) {
162 _cleanup_free_ char *stdin_name = NULL;
163
164 assert_se(getttyname_malloc(STDIN_FILENO, &stdin_name) >= 0);
165 assert_se(path_equal(stdin_name, ctty));
166 } else
167 log_notice("Not invoked with stdin == ctty, cutting get_ctty() test short");
168 }
169
170 DEFINE_TEST_MAIN(LOG_INFO);