chiark / gitweb /
sd-bus: drop redundant code
[elogind.git] / src / test / test-terminal-util.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
5   Copyright 2013 Thomas H.P. Andersen
6
7   systemd is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as published by
9   the Free Software Foundation; either version 2.1 of the License, or
10   (at your option) any later version.
11
12   systemd is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <stdio.h>
22 #include <stdbool.h>
23
24 #include "terminal-util.h"
25 #include "macro.h"
26 #include "util.h"
27 #include "log.h"
28
29 static void test_default_term_for_tty(void) {
30         puts(default_term_for_tty("/dev/tty23"));
31         puts(default_term_for_tty("/dev/ttyS23"));
32         puts(default_term_for_tty("/dev/tty0"));
33         puts(default_term_for_tty("/dev/pty0"));
34         puts(default_term_for_tty("/dev/pts/0"));
35         puts(default_term_for_tty("/dev/console"));
36         puts(default_term_for_tty("tty23"));
37         puts(default_term_for_tty("ttyS23"));
38         puts(default_term_for_tty("tty0"));
39         puts(default_term_for_tty("pty0"));
40         puts(default_term_for_tty("pts/0"));
41         puts(default_term_for_tty("console"));
42 }
43
44 static void test_read_one_char(void) {
45         _cleanup_fclose_ FILE *file = NULL;
46         char r;
47         bool need_nl;
48         char name[] = "/tmp/test-read_one_char.XXXXXX";
49         int fd;
50
51         fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
52         assert_se(fd >= 0);
53         file = fdopen(fd, "r+");
54         assert_se(file);
55         assert_se(fputs("c\n", file) >= 0);
56         rewind(file);
57
58         assert_se(read_one_char(file, &r, 1000000, &need_nl) >= 0);
59         assert_se(!need_nl);
60         assert_se(r == 'c');
61         assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
62
63         rewind(file);
64         assert_se(fputs("foobar\n", file) >= 0);
65         rewind(file);
66         assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
67
68         rewind(file);
69         assert_se(fputs("\n", file) >= 0);
70         rewind(file);
71         assert_se(read_one_char(file, &r, 1000000, &need_nl) < 0);
72
73         unlink(name);
74 }
75
76 int main(int argc, char *argv[]) {
77         log_parse_environment();
78         log_open();
79
80         test_default_term_for_tty();
81         test_read_one_char();
82
83         return 0;
84 }