Mercurial > gnulib
annotate tests/test-openpty.c @ 40196:e63f5d3edab5
relocatable-prog: Update documentation.
* doc/relocatable-maint.texi (Supporting Relocation): Update to match
the recent changes.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 24 Feb 2019 01:49:15 +0100 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
13005
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
1 /* Test of pty.h and openpty function. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
2 Copyright (C) 2009-2019 Free Software Foundation, Inc. |
13005
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
3 |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
7 (at your option) any later version. |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
8 |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
12 GNU General Public License for more details. |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
13 |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
14 You should have received a copy of the GNU General Public License |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
13005
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
16 |
13033
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
17 /* Written by Simon Josefsson <simon@josefsson.org>, 2009 |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
18 and Bruno Haible <bruno@clisp.org>, 2010. */ |
13005
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
19 |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
20 #include <config.h> |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
21 |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
22 #include <pty.h> |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
23 |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
24 #include "signature.h" |
13006
b89998923de3
forkpty, openpty: prefer glibc's const-safe prototype
Eric Blake <eblake@redhat.com>
parents:
13005
diff
changeset
|
25 SIGNATURE_CHECK (openpty, int, (int *, int *, char *, struct termios const *, |
b89998923de3
forkpty, openpty: prefer glibc's const-safe prototype
Eric Blake <eblake@redhat.com>
parents:
13005
diff
changeset
|
26 struct winsize const *)); |
13005
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
27 |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
28 #include <stdio.h> |
13033
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
29 #include <string.h> |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
30 #include <termios.h> |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
31 #include <unistd.h> |
13005
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
32 |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
33 int |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
34 main () |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
35 { |
13033
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
36 { |
18035
674bd7704497
fix pty related tests issues on Windows
Pavel Fedin <p.fedin@samsung.com>
parents:
17848
diff
changeset
|
37 #ifndef _WIN32 |
13033
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
38 int master; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
39 int slave; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
40 |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
41 /* Open a pseudo-terminal, as a master-slave pair. */ |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
42 { |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
43 int res = openpty (&master, &slave, NULL, NULL, NULL); |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
44 if (res != 0) |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
45 { |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
46 fprintf (stderr, "openpty returned %d\n", res); |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
47 return 1; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
48 } |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
49 } |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
50 |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
51 /* Set the terminal characteristics. |
16935
498a2211d839
Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
52 On Linux or Mac OS X, they can be set on either the master or the slave; |
13033
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
53 the effect is the same. But on Solaris, they have to be set on the |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
54 master; tcgetattr on the slave fails. */ |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
55 { |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
56 int tcfd = slave; /* You can try tcfd = master; here. */ |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
57 struct termios attributes; |
13005
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
58 |
13033
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
59 if (tcgetattr (tcfd, &attributes) < 0) |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
60 { |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
61 fprintf (stderr, "tcgetattr failed\n"); |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
62 return 1; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
63 } |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
64 /* Enable canonical processing, including erase. */ |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
65 attributes.c_lflag |= ECHO | ICANON | ECHOE; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
66 attributes.c_cc[VERASE] = '\177'; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
67 if (tcsetattr (tcfd, TCSANOW, &attributes) < 0) |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
68 { |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
69 fprintf (stderr, "tcsetattr failed\n"); |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
70 return 1; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
71 } |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
72 } |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
73 |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
74 /* Write into the master side. */ |
13005
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
75 { |
13033
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
76 static const char input[] = "Hello worst\177\177ld!\n"; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
77 |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
78 if (write (master, input, strlen (input)) < (int) strlen (input)) |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
79 { |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
80 fprintf (stderr, "write failed\n"); |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
81 return 1; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
82 } |
13005
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
83 } |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
84 |
13033
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
85 /* Read from the slave side. */ |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
86 { |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
87 char buf[100]; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
88 int res = read (slave, buf, sizeof (buf)); |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
89 static const char expected[] = "Hello world!\n"; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
90 |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
91 if (res < 0) |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
92 { |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
93 fprintf (stderr, "read failed\n"); |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
94 return 1; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
95 } |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
96 if (!(res == strlen (expected) |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
97 && memcmp (buf, expected, strlen (expected)) == 0)) |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
98 { |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
99 fprintf (stderr, "read result unexpected\n"); |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
100 return 1; |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
101 } |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
102 } |
13262
b5ce1a0a3d6e
Work around a MacOS X 10.4 bug with openpty.
Simon Josefsson <simon@josefsson.org>
parents:
13033
diff
changeset
|
103 |
b5ce1a0a3d6e
Work around a MacOS X 10.4 bug with openpty.
Simon Josefsson <simon@josefsson.org>
parents:
13033
diff
changeset
|
104 /* Close the master side before the slave side gets closed. |
16935
498a2211d839
Write "Mac OS X" instead of "MacOS X".
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
105 This is necessary on Mac OS X 10.4.11. */ |
13262
b5ce1a0a3d6e
Work around a MacOS X 10.4 bug with openpty.
Simon Josefsson <simon@josefsson.org>
parents:
13033
diff
changeset
|
106 close (master); |
18035
674bd7704497
fix pty related tests issues on Windows
Pavel Fedin <p.fedin@samsung.com>
parents:
17848
diff
changeset
|
107 #endif |
13033
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
108 } |
939f5a4753f9
Tests for openpty: Make stricter.
Bruno Haible <bruno@clisp.org>
parents:
13006
diff
changeset
|
109 |
13005
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
110 return 0; |
9bc579132d24
forkpty, openpty: split functions into new modules
Eric Blake <eblake@redhat.com>
parents:
diff
changeset
|
111 } |