]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-terminal-util.c
core: add ExecStartXYZEx= with dbus support for executable prefixes
[thirdparty/systemd.git] / src / test / test-terminal-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
288a74cc 2
ca78ad1d 3#include <fcntl.h>
288a74cc 4#include <stdbool.h>
cf0fbc49 5#include <stdio.h>
ca78ad1d 6#include <unistd.h>
288a74cc 7
23b27b39 8#include "alloc-util.h"
0d39fa9c 9#include "fd-util.h"
288a74cc 10#include "macro.h"
0d53f53b 11#include "path-util.h"
81f5e513 12#include "strv.h"
0d39fa9c 13#include "terminal-util.h"
e4de7287
LP
14#include "tests.h"
15#include "tmpfile-util.h"
288a74cc 16#include "util.h"
288a74cc
RC
17
18static void test_default_term_for_tty(void) {
4e494d17
ZJS
19 log_info("/* %s */", __func__);
20
288a74cc
RC
21 puts(default_term_for_tty("/dev/tty23"));
22 puts(default_term_for_tty("/dev/ttyS23"));
23 puts(default_term_for_tty("/dev/tty0"));
24 puts(default_term_for_tty("/dev/pty0"));
25 puts(default_term_for_tty("/dev/pts/0"));
26 puts(default_term_for_tty("/dev/console"));
27 puts(default_term_for_tty("tty23"));
28 puts(default_term_for_tty("ttyS23"));
29 puts(default_term_for_tty("tty0"));
30 puts(default_term_for_tty("pty0"));
31 puts(default_term_for_tty("pts/0"));
32 puts(default_term_for_tty("console"));
33}
34
35static void test_read_one_char(void) {
36 _cleanup_fclose_ FILE *file = NULL;
37 char r;
38 bool need_nl;
39 char name[] = "/tmp/test-read_one_char.XXXXXX";
288a74cc 40
4e494d17
ZJS
41 log_info("/* %s */", __func__);
42
3b1e405f 43 assert_se(fmkostemp_safe(name, "r+", &file) == 0);
d8351049 44
288a74cc
RC
45 assert_se(fputs("c\n", file) >= 0);
46 rewind(file);
288a74cc
RC
47 assert_se(read_one_char(file, &r, 1000000, &need_nl) >= 0);
48 assert_se(!need_nl);
49 assert_se(r == 'c');
50 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
51
52 rewind(file);
53 assert_se(fputs("foobar\n", file) >= 0);
54 rewind(file);
55 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
56
57 rewind(file);
58 assert_se(fputs("\n", file) >= 0);
59 rewind(file);
60 assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
61
0d53f53b
LP
62 assert_se(unlink(name) >= 0);
63}
64
65static void test_getttyname_malloc(void) {
66 _cleanup_free_ char *ttyname = NULL;
67 _cleanup_close_ int master = -1;
68
4e494d17
ZJS
69 log_info("/* %s */", __func__);
70
0d53f53b
LP
71 assert_se((master = posix_openpt(O_RDWR|O_NOCTTY)) >= 0);
72 assert_se(getttyname_malloc(master, &ttyname) >= 0);
4e494d17
ZJS
73 log_info("ttyname = %s", ttyname);
74
0d53f53b 75 assert_se(PATH_IN_SET(ttyname, "ptmx", "pts/ptmx"));
288a74cc
RC
76}
77
042f472e
LP
78static void test_one_color(const char *name, const char *color) {
79 printf("<%s%s%s>\n", color, name, ansi_normal());
80}
81
82static void test_colors(void) {
83 log_info("/* %s */", __func__);
84
85 test_one_color("normal", ansi_normal());
86 test_one_color("highlight", ansi_highlight());
87 test_one_color("red", ansi_red());
88 test_one_color("green", ansi_green());
89 test_one_color("yellow", ansi_yellow());
90 test_one_color("blue", ansi_blue());
91 test_one_color("megenta", ansi_magenta());
92 test_one_color("grey", ansi_grey());
93 test_one_color("highlight-red", ansi_highlight_red());
94 test_one_color("highlight-green", ansi_highlight_green());
95 test_one_color("highlight-yellow", ansi_highlight_yellow());
96 test_one_color("highlight-blue", ansi_highlight_blue());
97 test_one_color("highlight-magenta", ansi_highlight_magenta());
98 test_one_color("highlight-grey", ansi_highlight_grey());
99
100 test_one_color("underline", ansi_underline());
101 test_one_color("highlight-underline", ansi_highlight_underline());
102 test_one_color("highlight-red-underline", ansi_highlight_red_underline());
103 test_one_color("highlight-green-underline", ansi_highlight_green_underline());
104 test_one_color("highlight-yellow-underline", ansi_highlight_yellow_underline());
105 test_one_color("highlight-blue-underline", ansi_highlight_blue_underline());
106 test_one_color("highlight-magenta-underline", ansi_highlight_magenta_underline());
107 test_one_color("highlight-grey-underline", ansi_highlight_grey_underline());
108}
109
288a74cc 110int main(int argc, char *argv[]) {
294bf0c3 111 test_setup_logging(LOG_INFO);
288a74cc
RC
112
113 test_default_term_for_tty();
114 test_read_one_char();
0d53f53b 115 test_getttyname_malloc();
042f472e 116 test_colors();
cb91deaf 117
288a74cc
RC
118 return 0;
119}