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