]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-terminal-util.c
Merge pull request #17478 from yuwata/split-network-internal
[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 <unistd.h>
7
8 #include "alloc-util.h"
9 #include "fd-util.h"
10 #include "macro.h"
11 #include "path-util.h"
12 #include "strv.h"
13 #include "terminal-util.h"
14 #include "tests.h"
15 #include "tmpfile-util.h"
16 #include "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 static void test_default_term_for_tty(void) {
25 log_info("/* %s */", __func__);
26
27 puts(default_term_for_tty("/dev/tty23"));
28 puts(default_term_for_tty("/dev/ttyS23"));
29 puts(default_term_for_tty("/dev/tty0"));
30 puts(default_term_for_tty("/dev/pty0"));
31 puts(default_term_for_tty("/dev/pts/0"));
32 puts(default_term_for_tty("/dev/console"));
33 puts(default_term_for_tty("tty23"));
34 puts(default_term_for_tty("ttyS23"));
35 puts(default_term_for_tty("tty0"));
36 puts(default_term_for_tty("pty0"));
37 puts(default_term_for_tty("pts/0"));
38 puts(default_term_for_tty("console"));
39 }
40
41 static void test_read_one_char(void) {
42 _cleanup_fclose_ FILE *file = NULL;
43 char r;
44 bool need_nl;
45 char name[] = "/tmp/test-read_one_char.XXXXXX";
46
47 log_info("/* %s */", __func__);
48
49 assert_se(fmkostemp_safe(name, "r+", &file) == 0);
50
51 assert_se(fputs("c\n", file) >= 0);
52 rewind(file);
53 assert_se(read_one_char(file, &r, 1000000, &need_nl) >= 0);
54 assert_se(!need_nl);
55 assert_se(r == 'c');
56 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
57
58 rewind(file);
59 assert_se(fputs("foobar\n", file) >= 0);
60 rewind(file);
61 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
62
63 rewind(file);
64 assert_se(fputs("\n", file) >= 0);
65 rewind(file);
66 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
67
68 assert_se(unlink(name) >= 0);
69 }
70
71 static void test_getttyname_malloc(void) {
72 _cleanup_free_ char *ttyname = NULL;
73 _cleanup_close_ int master = -1;
74
75 log_info("/* %s */", __func__);
76
77 assert_se((master = posix_openpt(O_RDWR|O_NOCTTY)) >= 0);
78 assert_se(getttyname_malloc(master, &ttyname) >= 0);
79 log_info("ttyname = %s", ttyname);
80
81 assert_se(PATH_IN_SET(ttyname, "ptmx", "pts/ptmx"));
82 }
83
84 typedef struct {
85 const char *name;
86 const char* (*func)(void);
87 } Color;
88
89 static const Color colors[] = {
90 { "normal", ansi_normal },
91 { "highlight", ansi_highlight },
92 { "black", ansi_black },
93 { "red", ansi_red },
94 { "green", ansi_green },
95 { "yellow", ansi_yellow },
96 { "blue", ansi_blue },
97 { "magenta", ansi_magenta },
98 { "cyan", ansi_cyan },
99 { "white", ansi_white },
100 { "grey", ansi_grey },
101
102 { "bright-black", ansi_bright_black },
103 { "bright-red", ansi_bright_red },
104 { "bright-green", ansi_bright_green },
105 { "bright-yellow", ansi_bright_yellow },
106 { "bright-blue", ansi_bright_blue },
107 { "bright-magenta", ansi_bright_magenta },
108 { "bright-cyan", ansi_bright_cyan },
109 { "bright-white", ansi_bright_white },
110
111 { "highlight-black", ansi_highlight_black },
112 { "highlight-red", ansi_highlight_red },
113 { "highlight-green", ansi_highlight_green },
114 { "highlight-yellow (original)", _ansi_highlight_yellow },
115 { "highlight-yellow (replacement)", ansi_highlight_yellow },
116 { "highlight-blue", ansi_highlight_blue },
117 { "highlight-magenta", ansi_highlight_magenta },
118 { "highlight-cyan", ansi_highlight_cyan },
119 { "highlight-white", ansi_highlight_white },
120 { "highlight-grey", ansi_highlight_grey },
121
122 { "underline", ansi_underline },
123 { "highlight-underline", ansi_highlight_underline },
124 { "highlight-red-underline", ansi_highlight_red_underline },
125 { "highlight-green-underline", ansi_highlight_green_underline },
126 { "highlight-yellow-underline", ansi_highlight_yellow_underline },
127 { "highlight-blue-underline", ansi_highlight_blue_underline },
128 { "highlight-magenta-underline", ansi_highlight_magenta_underline },
129 { "highlight-grey-underline", ansi_highlight_grey_underline },
130 };
131
132 static void test_colors(void) {
133 log_info("/* %s */", __func__);
134
135 for (size_t i = 0; i < ELEMENTSOF(colors); i++)
136 printf("<%s%s%s>\n", colors[i].func(), colors[i].name, ansi_normal());
137 }
138
139 static void test_text(void) {
140 log_info("/* %s */", __func__);
141
142 for (size_t i = 0; !streq(colors[i].name, "underline"); i++) {
143 bool blwh = strstr(colors[i].name, "black")
144 || strstr(colors[i].name, "white");
145
146 printf("\n"
147 "Testing color %s%s\n%s%s%s\n",
148 colors[i].name,
149 blwh ? "" : ", this text should be readable",
150 colors[i].func(),
151 LOREM_IPSUM,
152 ansi_normal());
153 }
154 }
155
156 int main(int argc, char *argv[]) {
157 test_setup_logging(LOG_INFO);
158
159 test_default_term_for_tty();
160 test_read_one_char();
161 test_getttyname_malloc();
162 test_colors();
163 test_text();
164
165 return 0;
166 }