chiark / gitweb /
73c5c853303bb76b9af502360c58be403da16da1
[elogind.git] / src / test / test-pty.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 David Herrmann <dh.herrmann@gmail.com>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <locale.h>
25 #include <string.h>
26 #include <sys/wait.h>
27 #include <unistd.h>
28
29 #include "def.h"
30 #include "pty.h"
31 #include "util.h"
32
33 static const char sndmsg[] = "message\n";
34 static const char rcvmsg[] = "message\r\n";
35 static char rcvbuf[128];
36 static size_t rcvsiz = 0;
37 static sd_event *event;
38
39 static void run_child(Pty *pty) {
40         int r, l;
41         char buf[512];
42
43         r = read(0, buf, sizeof(buf));
44         assert_se(r == strlen(sndmsg));
45         assert_se(!strncmp(buf, sndmsg, r));
46
47         l = write(1, buf, r);
48         assert_se(l == r);
49 }
50
51 static int pty_fn(Pty *pty, void *userdata, unsigned int ev, const void *ptr, size_t size) {
52         switch (ev) {
53         case PTY_DATA:
54                 assert_se(rcvsiz < strlen(rcvmsg) * 2);
55                 assert_se(rcvsiz + size < sizeof(rcvbuf));
56
57                 memcpy(&rcvbuf[rcvsiz], ptr, size);
58                 rcvsiz += size;
59
60                 if (rcvsiz >= strlen(rcvmsg) * 2) {
61                         assert_se(rcvsiz == strlen(rcvmsg) * 2);
62                         assert_se(!memcmp(rcvbuf, rcvmsg, strlen(rcvmsg)));
63                         assert_se(!memcmp(&rcvbuf[strlen(rcvmsg)], rcvmsg, strlen(rcvmsg)));
64                 }
65
66                 break;
67         case PTY_HUP:
68                 /* This is guaranteed to appear _after_ the input queues are
69                  * drained! */
70                 assert_se(rcvsiz == strlen(rcvmsg) * 2);
71                 break;
72         case PTY_CHILD:
73                 /* this may appear at any time */
74                 break;
75         default:
76                 assert_se(0);
77                 break;
78         }
79
80         /* if we got HUP _and_ CHILD, exit */
81         if (pty_get_fd(pty) < 0 && pty_get_child(pty) < 0)
82                 sd_event_exit(event, 0);
83
84         return 0;
85 }
86
87 static void run_parent(Pty *pty) {
88         int r;
89
90         /* write message to pty, ECHO mode guarantees that we get it back
91          * twice: once via ECHO, once from the run_child() fn */
92         assert_se(pty_write(pty, sndmsg, strlen(sndmsg)) >= 0);
93
94         r = sd_event_loop(event);
95         assert_se(r >= 0);
96 }
97
98 static void test_pty(void) {
99         pid_t pid;
100         Pty *pty;
101
102         rcvsiz = 0;
103         memset(rcvbuf, 0, sizeof(rcvbuf));
104
105         assert_se(sd_event_default(&event) >= 0);
106
107         pid = pty_fork(&pty, event, pty_fn, NULL, 80, 25);
108         assert_se(pid >= 0);
109
110         if (pid == 0) {
111                 /* child */
112                 run_child(pty);
113                 exit(0);
114         }
115
116         /* parent */
117         run_parent(pty);
118
119         /* Make sure the PTY recycled the child; yeah, this is racy if the
120          * PID was already reused; but that seems fine for a test. */
121         assert_se(waitpid(pid, NULL, WNOHANG) < 0 && errno == ECHILD);
122
123         pty_unref(pty);
124         sd_event_unref(event);
125 }
126
127 int main(int argc, char *argv[]) {
128         unsigned int i;
129
130         log_parse_environment();
131         log_open();
132
133         assert_se(sigprocmask_many(SIG_BLOCK, SIGCHLD, -1) >= 0);
134
135         /* Oh, there're ugly races in the TTY layer regarding HUP vs IN. Turns
136          * out they appear only 10% of the time. I fixed all of them and
137          * don't see them, anymore. But lets be safe and run this 1000 times
138          * so we catch any new ones, in case they appear again. */
139         for (i = 0; i < 1000; ++i)
140                 test_pty();
141
142         return 0;
143 }