]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-terminal-util.c
libudev: hide definition of struct udev_list from other libudev components
[thirdparty/systemd.git] / src / test / test-terminal-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 static void test_default_term_for_tty(void) {
19 log_info("/* %s */", __func__);
20
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
35 static 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";
40
41 log_info("/* %s */", __func__);
42
43 assert_se(fmkostemp_safe(name, "r+", &file) == 0);
44
45 assert_se(fputs("c\n", file) >= 0);
46 rewind(file);
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
62 assert_se(unlink(name) >= 0);
63 }
64
65 static void test_getttyname_malloc(void) {
66 _cleanup_free_ char *ttyname = NULL;
67 _cleanup_close_ int master = -1;
68
69 log_info("/* %s */", __func__);
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 static void test_one_color(const char *name, const char *color) {
79 printf("<%s%s%s>\n", color, name, ansi_normal());
80 }
81
82 static 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
110 int main(int argc, char *argv[]) {
111 test_setup_logging(LOG_INFO);
112
113 test_default_term_for_tty();
114 test_read_one_char();
115 test_getttyname_malloc();
116 test_colors();
117
118 return 0;
119 }