]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
288a74cc | 2 | /*** |
288a74cc RC |
3 | Copyright 2010 Lennart Poettering |
4 | Copyright 2013 Thomas H.P. Andersen | |
288a74cc RC |
5 | ***/ |
6 | ||
288a74cc | 7 | #include <stdbool.h> |
cf0fbc49 | 8 | #include <stdio.h> |
288a74cc | 9 | |
23b27b39 | 10 | #include "alloc-util.h" |
0d39fa9c LP |
11 | #include "fd-util.h" |
12 | #include "fileio.h" | |
13 | #include "log.h" | |
288a74cc | 14 | #include "macro.h" |
81f5e513 | 15 | #include "strv.h" |
0d39fa9c | 16 | #include "terminal-util.h" |
288a74cc | 17 | #include "util.h" |
288a74cc RC |
18 | |
19 | static void test_default_term_for_tty(void) { | |
20 | puts(default_term_for_tty("/dev/tty23")); | |
21 | puts(default_term_for_tty("/dev/ttyS23")); | |
22 | puts(default_term_for_tty("/dev/tty0")); | |
23 | puts(default_term_for_tty("/dev/pty0")); | |
24 | puts(default_term_for_tty("/dev/pts/0")); | |
25 | puts(default_term_for_tty("/dev/console")); | |
26 | puts(default_term_for_tty("tty23")); | |
27 | puts(default_term_for_tty("ttyS23")); | |
28 | puts(default_term_for_tty("tty0")); | |
29 | puts(default_term_for_tty("pty0")); | |
30 | puts(default_term_for_tty("pts/0")); | |
31 | puts(default_term_for_tty("console")); | |
32 | } | |
33 | ||
34 | static void test_read_one_char(void) { | |
35 | _cleanup_fclose_ FILE *file = NULL; | |
36 | char r; | |
37 | bool need_nl; | |
38 | char name[] = "/tmp/test-read_one_char.XXXXXX"; | |
39 | int fd; | |
40 | ||
646853bd | 41 | fd = mkostemp_safe(name); |
288a74cc RC |
42 | assert_se(fd >= 0); |
43 | file = fdopen(fd, "r+"); | |
44 | assert_se(file); | |
45 | assert_se(fputs("c\n", file) >= 0); | |
46 | rewind(file); | |
47 | ||
48 | assert_se(read_one_char(file, &r, 1000000, &need_nl) >= 0); | |
49 | assert_se(!need_nl); | |
50 | assert_se(r == 'c'); | |
51 | assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0); | |
52 | ||
53 | rewind(file); | |
54 | assert_se(fputs("foobar\n", file) >= 0); | |
55 | rewind(file); | |
56 | assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0); | |
57 | ||
58 | rewind(file); | |
59 | assert_se(fputs("\n", file) >= 0); | |
60 | rewind(file); | |
61 | assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0); | |
62 | ||
63 | unlink(name); | |
64 | } | |
65 | ||
23b27b39 LP |
66 | static void test_terminal_urlify(void) { |
67 | _cleanup_free_ char *formatted = NULL; | |
68 | ||
69 | assert_se(terminal_urlify("https://www.freedesktop.org/wiki/Software/systemd/", "systemd homepage", &formatted) >= 0); | |
70 | printf("Hey, considere visiting the %s right now! It is very good!\n", formatted); | |
71 | ||
72 | formatted = mfree(formatted); | |
73 | ||
74 | assert_se(terminal_urlify_path("/etc/fstab", "this link to your /etc/fstab", &formatted) >= 0); | |
75 | printf("Or click on %s to have a look at it!\n", formatted); | |
76 | } | |
77 | ||
81f5e513 | 78 | static void test_cat_files(void) { |
854a42fb ZJS |
79 | assert_se(cat_files("/no/such/file", NULL, 0) == -ENOENT); |
80 | assert_se(cat_files("/no/such/file", NULL, CAT_FLAGS_MAIN_FILE_OPTIONAL) == 0); | |
81f5e513 ZJS |
81 | |
82 | if (access("/etc/fstab", R_OK) >= 0) | |
854a42fb | 83 | assert_se(cat_files("/etc/fstab", STRV_MAKE("/etc/fstab", "/etc/fstab"), 0) == 0); |
81f5e513 ZJS |
84 | } |
85 | ||
288a74cc RC |
86 | int main(int argc, char *argv[]) { |
87 | log_parse_environment(); | |
88 | log_open(); | |
89 | ||
90 | test_default_term_for_tty(); | |
91 | test_read_one_char(); | |
23b27b39 | 92 | test_terminal_urlify(); |
81f5e513 | 93 | test_cat_files(); |
288a74cc | 94 | |
cb91deaf LP |
95 | print_separator(); |
96 | ||
288a74cc RC |
97 | return 0; |
98 | } |