Line data Source code
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 1 : static void test_default_term_for_tty(void) {
19 1 : log_info("/* %s */", __func__);
20 :
21 1 : puts(default_term_for_tty("/dev/tty23"));
22 1 : puts(default_term_for_tty("/dev/ttyS23"));
23 1 : puts(default_term_for_tty("/dev/tty0"));
24 1 : puts(default_term_for_tty("/dev/pty0"));
25 1 : puts(default_term_for_tty("/dev/pts/0"));
26 1 : puts(default_term_for_tty("/dev/console"));
27 1 : puts(default_term_for_tty("tty23"));
28 1 : puts(default_term_for_tty("ttyS23"));
29 1 : puts(default_term_for_tty("tty0"));
30 1 : puts(default_term_for_tty("pty0"));
31 1 : puts(default_term_for_tty("pts/0"));
32 1 : puts(default_term_for_tty("console"));
33 1 : }
34 :
35 1 : static void test_read_one_char(void) {
36 1 : _cleanup_fclose_ FILE *file = NULL;
37 : char r;
38 : bool need_nl;
39 1 : char name[] = "/tmp/test-read_one_char.XXXXXX";
40 :
41 1 : log_info("/* %s */", __func__);
42 :
43 1 : assert_se(fmkostemp_safe(name, "r+", &file) == 0);
44 :
45 1 : assert_se(fputs("c\n", file) >= 0);
46 1 : rewind(file);
47 1 : assert_se(read_one_char(file, &r, 1000000, &need_nl) >= 0);
48 1 : assert_se(!need_nl);
49 1 : assert_se(r == 'c');
50 1 : assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
51 :
52 1 : rewind(file);
53 1 : assert_se(fputs("foobar\n", file) >= 0);
54 1 : rewind(file);
55 1 : assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
56 :
57 1 : rewind(file);
58 1 : assert_se(fputs("\n", file) >= 0);
59 1 : rewind(file);
60 1 : assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
61 :
62 1 : assert_se(unlink(name) >= 0);
63 1 : }
64 :
65 1 : static void test_getttyname_malloc(void) {
66 1 : _cleanup_free_ char *ttyname = NULL;
67 1 : _cleanup_close_ int master = -1;
68 :
69 1 : log_info("/* %s */", __func__);
70 :
71 1 : assert_se((master = posix_openpt(O_RDWR|O_NOCTTY)) >= 0);
72 1 : assert_se(getttyname_malloc(master, &ttyname) >= 0);
73 1 : log_info("ttyname = %s", ttyname);
74 :
75 1 : assert_se(PATH_IN_SET(ttyname, "ptmx", "pts/ptmx"));
76 1 : }
77 :
78 22 : static void test_one_color(const char *name, const char *color) {
79 22 : printf("<%s%s%s>\n", color, name, ansi_normal());
80 22 : }
81 :
82 1 : static void test_colors(void) {
83 1 : log_info("/* %s */", __func__);
84 :
85 1 : test_one_color("normal", ansi_normal());
86 1 : test_one_color("highlight", ansi_highlight());
87 1 : test_one_color("red", ansi_red());
88 1 : test_one_color("green", ansi_green());
89 1 : test_one_color("yellow", ansi_yellow());
90 1 : test_one_color("blue", ansi_blue());
91 1 : test_one_color("megenta", ansi_magenta());
92 1 : test_one_color("grey", ansi_grey());
93 1 : test_one_color("highlight-red", ansi_highlight_red());
94 1 : test_one_color("highlight-green", ansi_highlight_green());
95 1 : test_one_color("highlight-yellow", ansi_highlight_yellow());
96 1 : test_one_color("highlight-blue", ansi_highlight_blue());
97 1 : test_one_color("highlight-magenta", ansi_highlight_magenta());
98 1 : test_one_color("highlight-grey", ansi_highlight_grey());
99 :
100 1 : test_one_color("underline", ansi_underline());
101 1 : test_one_color("highlight-underline", ansi_highlight_underline());
102 1 : test_one_color("highlight-red-underline", ansi_highlight_red_underline());
103 1 : test_one_color("highlight-green-underline", ansi_highlight_green_underline());
104 1 : test_one_color("highlight-yellow-underline", ansi_highlight_yellow_underline());
105 1 : test_one_color("highlight-blue-underline", ansi_highlight_blue_underline());
106 1 : test_one_color("highlight-magenta-underline", ansi_highlight_magenta_underline());
107 1 : test_one_color("highlight-grey-underline", ansi_highlight_grey_underline());
108 1 : }
109 :
110 1 : int main(int argc, char *argv[]) {
111 1 : test_setup_logging(LOG_INFO);
112 :
113 1 : test_default_term_for_tty();
114 1 : test_read_one_char();
115 1 : test_getttyname_malloc();
116 1 : test_colors();
117 :
118 1 : return 0;
119 : }
|