]>
Commit | Line | Data |
---|---|---|
0f9793a5 | 1 | /* Test for grantpt, unlockpt error corner cases. |
6d7e8eda | 2 | Copyright (C) 2001-2023 Free Software Foundation, Inc. |
c42b7058 FW |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <https://www.gnu.org/licenses/>. */ | |
18 | ||
9cddf9de UD |
19 | #include <sys/types.h> |
20 | #include <sys/stat.h> | |
21 | #include <fcntl.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <errno.h> | |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
c42b7058 FW |
27 | #include <support/check.h> |
28 | #include <support/temp_file.h> | |
29 | #include <support/xunistd.h> | |
9cddf9de | 30 | |
0f9793a5 | 31 | /* Test grantpt, unlockpt with a closed descriptor. */ |
c42b7058 | 32 | static void |
9cddf9de UD |
33 | test_ebadf (void) |
34 | { | |
35 | int fd, ret, err; | |
36 | ||
37 | fd = posix_openpt (O_RDWR); | |
38 | if (fd == -1) | |
c42b7058 FW |
39 | FAIL_EXIT1 ("posix_openpt(O_RDWR) failed\nerrno %d (%m)\n", errno); |
40 | TEST_COMPARE (unlockpt (fd), 0); | |
9cddf9de | 41 | |
c42b7058 | 42 | xclose (fd); |
9cddf9de UD |
43 | ret = grantpt (fd); |
44 | err = errno; | |
45 | if (ret != -1 || err != EBADF) | |
46 | { | |
c42b7058 | 47 | support_record_failure (); |
9cddf9de UD |
48 | printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EBADF); |
49 | printf (" got: return = %d, errno = %d\n", ret, err); | |
9cddf9de | 50 | } |
0f9793a5 FW |
51 | |
52 | TEST_COMPARE (unlockpt (fd), -1); | |
53 | TEST_COMPARE (errno, EBADF); | |
9cddf9de UD |
54 | } |
55 | ||
0f9793a5 | 56 | /* Test grantpt, unlockpt on a regular file. */ |
c42b7058 | 57 | static void |
9cddf9de UD |
58 | test_einval (void) |
59 | { | |
60 | int fd, ret, err; | |
9cddf9de | 61 | |
c42b7058 FW |
62 | fd = create_temp_file ("tst-grantpt-", NULL); |
63 | TEST_VERIFY_EXIT (fd >= 0); | |
9cddf9de UD |
64 | |
65 | ret = grantpt (fd); | |
66 | err = errno; | |
67 | if (ret != -1 || err != EINVAL) | |
68 | { | |
c42b7058 | 69 | support_record_failure (); |
9cddf9de UD |
70 | printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EINVAL); |
71 | printf (" got: return = %d, errno = %d\n", ret, err); | |
9cddf9de | 72 | } |
9cddf9de | 73 | |
0f9793a5 FW |
74 | TEST_COMPARE (unlockpt (fd), -1); |
75 | TEST_COMPARE (errno, EINVAL); | |
76 | ||
c42b7058 FW |
77 | xclose (fd); |
78 | } | |
79 | ||
0f9793a5 | 80 | /* Test grantpt, unlockpt on a non-ptmx pseudo-terminal. */ |
c42b7058 FW |
81 | static void |
82 | test_not_ptmx (void) | |
83 | { | |
84 | int ptmx = posix_openpt (O_RDWR); | |
85 | TEST_VERIFY_EXIT (ptmx >= 0); | |
86 | TEST_COMPARE (grantpt (ptmx), 0); | |
87 | TEST_COMPARE (unlockpt (ptmx), 0); | |
88 | ||
0f9793a5 FW |
89 | /* A second unlock succeeds as well. */ |
90 | TEST_COMPARE (unlockpt (ptmx), 0); | |
91 | ||
c42b7058 FW |
92 | const char *name = ptsname (ptmx); |
93 | TEST_VERIFY_EXIT (name != NULL); | |
94 | int pts = open (name, O_RDWR | O_NOCTTY); | |
95 | TEST_VERIFY_EXIT (pts >= 0); | |
96 | ||
97 | TEST_COMPARE (grantpt (pts), -1); | |
98 | TEST_COMPARE (errno, EINVAL); | |
9cddf9de | 99 | |
0f9793a5 FW |
100 | TEST_COMPARE (unlockpt (pts), -1); |
101 | TEST_COMPARE (errno, EINVAL); | |
102 | ||
c42b7058 FW |
103 | xclose (pts); |
104 | xclose (ptmx); | |
9cddf9de UD |
105 | } |
106 | ||
29955b5d AS |
107 | static int |
108 | do_test (void) | |
9cddf9de | 109 | { |
c42b7058 FW |
110 | test_ebadf (); |
111 | test_einval (); | |
112 | test_not_ptmx (); | |
113 | return 0; | |
9cddf9de | 114 | } |
29955b5d | 115 | |
c42b7058 | 116 | #include <support/test-driver.c> |