1 /* $NetBSD: t_pr.c,v 1.7 2011/04/26 20:42:01 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Antti Kantee <pooka@NetBSD.org> and Martin Husemann <martin@NetBSD.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/tty.h>
35
36 #include <atf-c.h>
37 #include <fcntl.h>
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <rump/rump.h>
43 #include <rump/rump_syscalls.h>
44
45 static int
sendsome(int from,int to)46 sendsome(int from, int to)
47 {
48 size_t i;
49 ssize_t cnt;
50 static const char msg[] = "hello world\n";
51 char buf[sizeof(msg)+10];
52
53 memset(buf, 0, sizeof(buf));
54 rump_sys_write(from, msg, strlen(msg));
55 cnt = rump_sys_read(to, buf, sizeof(buf));
56 if (cnt < (ssize_t)strlen(msg)) {
57 printf("short message read: %zd chars: \"%s\"\n", cnt, buf);
58 return 1;
59 }
60 for (i = 0; i < sizeof(buf); i++) {
61 if (buf[i] == '\r' || buf[i] == '\n') {
62 buf[i] = '\n';
63 buf[i+1] = '\0';
64 break;
65 }
66 }
67
68 return strcmp(buf, msg) != 0;
69 }
70
71 static int
exercise_ptytty(int master,int slave)72 exercise_ptytty(int master, int slave)
73 {
74 int error, flags;
75
76 /*
77 * send a few bytes from master to slave and read them back
78 */
79 error = sendsome(master, slave);
80 if (error)
81 return error;
82
83 flags = FREAD|FWRITE;
84 rump_sys_ioctl(master, TIOCFLUSH, &flags);
85
86 /*
87 * and the same in the other direction
88 */
89 error = sendsome(slave, master);
90 if (error)
91 return error;
92
93 flags = FREAD|FWRITE;
94 rump_sys_ioctl(master, TIOCFLUSH, &flags);
95 return 0;
96 }
97
98 ATF_TC(client_first);
ATF_TC_HEAD(client_first,tc)99 ATF_TC_HEAD(client_first, tc)
100 {
101
102 atf_tc_set_md_var(tc, "descr",
103 "test basic tty/pty operation when opening client side first");
104 }
105
ATF_TC_BODY(client_first,tc)106 ATF_TC_BODY(client_first, tc)
107 {
108 int master, slave, error, v;
109
110 rump_init();
111 slave = rump_sys_open("/dev/ttyp1", O_RDWR|O_NONBLOCK);
112 ATF_CHECK(slave != -1);
113
114 master = rump_sys_open("/dev/ptyp1", O_RDWR);
115 ATF_CHECK(master != -1);
116
117 v = 0;
118 rump_sys_ioctl(slave, FIOASYNC, &v);
119 error = exercise_ptytty(master, slave);
120 ATF_CHECK(error == 0);
121
122 rump_sys_close(master);
123 rump_sys_close(slave);
124 }
125
126 ATF_TC(master_first);
ATF_TC_HEAD(master_first,tc)127 ATF_TC_HEAD(master_first, tc)
128 {
129
130 atf_tc_set_md_var(tc, "descr",
131 "test basic tty/pty operation when opening master side first");
132 }
133
ATF_TC_BODY(master_first,tc)134 ATF_TC_BODY(master_first, tc)
135 {
136 int master, slave, error;
137
138 rump_init();
139 master = rump_sys_open("/dev/ptyp1", O_RDWR);
140 ATF_CHECK(master != -1);
141
142 slave = rump_sys_open("/dev/ttyp1", O_RDWR);
143 ATF_CHECK(slave != -1);
144
145 error = exercise_ptytty(master, slave);
146 ATF_CHECK(error == 0);
147
148 rump_sys_close(master);
149 rump_sys_close(slave);
150 }
151
152 ATF_TC(ptyioctl);
ATF_TC_HEAD(ptyioctl,tc)153 ATF_TC_HEAD(ptyioctl, tc)
154 {
155
156 atf_tc_set_md_var(tc, "descr",
157 "ioctl on pty with client side not open");
158 }
159
ATF_TC_BODY(ptyioctl,tc)160 ATF_TC_BODY(ptyioctl, tc)
161 {
162 struct termios tio;
163 int fd;
164
165 rump_init();
166 fd = rump_sys_open("/dev/ptyp1", O_RDWR);
167 ATF_CHECK(fd != -1);
168
169 /*
170 * This used to die with null deref under ptcwakeup()
171 * atf_tc_expect_signal(-1, "PR kern/40688");
172 */
173 rump_sys_ioctl(fd, TIOCGETA, &tio);
174
175 rump_sys_close(fd);
176 }
177
ATF_TP_ADD_TCS(tp)178 ATF_TP_ADD_TCS(tp)
179 {
180
181 ATF_TP_ADD_TC(tp, ptyioctl);
182 ATF_TP_ADD_TC(tp, client_first);
183 ATF_TP_ADD_TC(tp, master_first);
184
185 return atf_no_error();
186 }
187