chiark / gitweb /
remove unused includes
[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 <locale.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27
28 #include "pty.h"
29 #include "util.h"
30
31 static const char sndmsg[] = "message\n";
32 static const char rcvmsg[] = "message\r\n";
33 static char rcvbuf[128];
34 static size_t rcvsiz = 0;
35 static sd_event *event;
36
37 static void run_child(Pty *pty) {
38         ssize_t r, l;
39         char buf[512];
40
41         r = read(0, buf, sizeof(buf));
42         assert_se((size_t)r == strlen(sndmsg));
43         assert_se(!strncmp(buf, sndmsg, r));
44
45         l = write(1, buf, r);
46         assert_se(l == r);
47 }
48
49 static int pty_fn(Pty *pty, void *userdata, unsigned int ev, const void *ptr, size_t size) {
50         switch (ev) {
51         case PTY_DATA:
52                 assert_se(rcvsiz < strlen(rcvmsg) * 2);
53                 assert_se(rcvsiz + size < sizeof(rcvbuf));
54
55                 memcpy(&rcvbuf[rcvsiz], ptr, size);
56                 rcvsiz += size;
57
58                 if (rcvsiz >= strlen(rcvmsg) * 2) {
59                         assert_se(rcvsiz == strlen(rcvmsg) * 2);
60                         assert_se(!memcmp(rcvbuf, rcvmsg, strlen(rcvmsg)));
61                         assert_se(!memcmp(&rcvbuf[strlen(rcvmsg)], rcvmsg, strlen(rcvmsg)));
62                 }
63
64                 break;
65         case PTY_HUP:
66                 /* This is guaranteed to appear _after_ the input queues are
67                  * drained! */
68                 assert_se(rcvsiz == strlen(rcvmsg) * 2);
69                 break;
70         case PTY_CHILD:
71                 /* this may appear at any time */
72                 break;
73         default:
74                 assert_se(0);
75                 break;
76         }
77
78         /* if we got HUP _and_ CHILD, exit */
79         if (pty_get_fd(pty) < 0 && pty_get_child(pty) < 0)
80                 sd_event_exit(event, 0);
81
82         return 0;
83 }
84
85 static void run_parent(Pty *pty) {
86         int r;
87
88         /* write message to pty, ECHO mode guarantees that we get it back
89          * twice: once via ECHO, once from the run_child() fn */
90         assert_se(pty_write(pty, sndmsg, strlen(sndmsg)) >= 0);
91
92         r = sd_event_loop(event);
93         assert_se(r >= 0);
94 }
95
96 static void test_pty(void) {
97         pid_t pid;
98         Pty *pty;
99
100         rcvsiz = 0;
101         zero(rcvbuf);
102
103         assert_se(sd_event_default(&event) >= 0);
104
105         pid = pty_fork(&pty, event, pty_fn, NULL, 80, 25);
106         assert_se(pid >= 0);
107
108         if (pid == 0) {
109                 /* child */
110                 run_child(pty);
111                 exit(0);
112         }
113
114         /* parent */
115         run_parent(pty);
116
117         /* Make sure the PTY recycled the child; yeah, this is racy if the
118          * PID was already reused; but that seems fine for a test. */
119         assert_se(waitpid(pid, NULL, WNOHANG) < 0 && errno == ECHILD);
120
121         pty_unref(pty);
122         sd_event_unref(event);
123 }
124
125 int main(int argc, char *argv[]) {
126         unsigned int i;
127
128         log_parse_environment();
129         log_open();
130
131         assert_se(sigprocmask_many(SIG_BLOCK, SIGCHLD, -1) >= 0);
132
133         /* Oh, there're ugly races in the TTY layer regarding HUP vs IN. Turns
134          * out they appear only 10% of the time. I fixed all of them and
135          * don't see them, anymore. But lets be safe and run this 1000 times
136          * so we catch any new ones, in case they appear again. */
137         for (i = 0; i < 1000; ++i)
138                 test_pty();
139
140         return 0;
141 }