]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-terminal-util.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / test / test-terminal-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2013 Thomas H.P. Andersen
4 ***/
5
6 #include <stdbool.h>
7 #include <stdio.h>
8
9 #include "alloc-util.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "log.h"
13 #include "macro.h"
14 #include "strv.h"
15 #include "terminal-util.h"
16 #include "util.h"
17
18 static void test_default_term_for_tty(void) {
19 puts(default_term_for_tty("/dev/tty23"));
20 puts(default_term_for_tty("/dev/ttyS23"));
21 puts(default_term_for_tty("/dev/tty0"));
22 puts(default_term_for_tty("/dev/pty0"));
23 puts(default_term_for_tty("/dev/pts/0"));
24 puts(default_term_for_tty("/dev/console"));
25 puts(default_term_for_tty("tty23"));
26 puts(default_term_for_tty("ttyS23"));
27 puts(default_term_for_tty("tty0"));
28 puts(default_term_for_tty("pty0"));
29 puts(default_term_for_tty("pts/0"));
30 puts(default_term_for_tty("console"));
31 }
32
33 static void test_read_one_char(void) {
34 _cleanup_fclose_ FILE *file = NULL;
35 char r;
36 bool need_nl;
37 char name[] = "/tmp/test-read_one_char.XXXXXX";
38 int fd;
39
40 fd = mkostemp_safe(name);
41 assert_se(fd >= 0);
42 file = fdopen(fd, "r+");
43 assert_se(file);
44 assert_se(fputs("c\n", file) >= 0);
45 rewind(file);
46
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 unlink(name);
63 }
64
65 static void test_terminal_urlify(void) {
66 _cleanup_free_ char *formatted = NULL;
67
68 assert_se(terminal_urlify("https://www.freedesktop.org/wiki/Software/systemd/", "systemd homepage", &formatted) >= 0);
69 printf("Hey, considere visiting the %s right now! It is very good!\n", formatted);
70
71 formatted = mfree(formatted);
72
73 assert_se(terminal_urlify_path("/etc/fstab", "this link to your /etc/fstab", &formatted) >= 0);
74 printf("Or click on %s to have a look at it!\n", formatted);
75 }
76
77 static void test_cat_files(void) {
78 assert_se(cat_files("/no/such/file", NULL, 0) == -ENOENT);
79 assert_se(cat_files("/no/such/file", NULL, CAT_FLAGS_MAIN_FILE_OPTIONAL) == 0);
80
81 if (access("/etc/fstab", R_OK) >= 0)
82 assert_se(cat_files("/etc/fstab", STRV_MAKE("/etc/fstab", "/etc/fstab"), 0) == 0);
83 }
84
85 int main(int argc, char *argv[]) {
86 log_parse_environment();
87 log_open();
88
89 test_default_term_for_tty();
90 test_read_one_char();
91 test_terminal_urlify();
92 test_cat_files();
93
94 print_separator();
95
96 return 0;
97 }