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