1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "alloc-util.h"
12 #include "path-util.h"
14 #include "terminal-util.h"
16 #include "tmpfile-util.h"
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."
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"));
40 _cleanup_fclose_
FILE *file
= NULL
;
43 char name
[] = "/tmp/test-read_one_char.XXXXXX";
45 assert_se(fmkostemp_safe(name
, "r+", &file
) == 0);
47 assert_se(fputs("c\n", file
) >= 0);
49 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) >= 0);
52 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) < 0);
55 assert_se(fputs("foobar\n", file
) >= 0);
57 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) < 0);
60 assert_se(fputs("\n", file
) >= 0);
62 assert_se(read_one_char(file
, &r
, 1000000, &need_nl
) < 0);
64 assert_se(unlink(name
) >= 0);
67 TEST(getttyname_malloc
) {
68 _cleanup_free_
char *ttyname
= NULL
;
69 _cleanup_close_
int master
= -1;
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
);
75 assert_se(PATH_IN_SET(ttyname
, "ptmx", "pts/ptmx"));
80 const char* (*func
)(void);
83 static const Color colors
[] = {
84 { "normal", ansi_normal
},
85 { "highlight", ansi_highlight
},
86 { "black", ansi_black
},
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
},
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
},
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
},
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
},
127 for (size_t i
= 0; i
< ELEMENTSOF(colors
); i
++)
128 printf("<%s%s%s>\n", colors
[i
].func(), colors
[i
].name
, ansi_normal());
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");
137 "Testing color %s%s\n%s%s%s\n",
139 blwh
? "" : ", this text should be readable",
147 _cleanup_free_
char *ctty
= NULL
;
152 r
= get_ctty(0, &devnr
, &ctty
);
154 log_notice_errno(r
, "Apparently called without a controlling TTY, cutting get_ctty() test short: %m");
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
;
163 assert_se(getttyname_malloc(STDIN_FILENO
, &stdin_name
) >= 0);
164 assert_se(path_equal(stdin_name
, ctty
));
166 log_notice("Not invoked with stdin == ctty, cutting get_ctty() test short");
169 DEFINE_TEST_MAIN(LOG_INFO
);