1/**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *   http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <fcntl.h>
17#include <pty.h>
18#include <stdlib.h>
19#include <sys/ioctl.h>
20#include <unistd.h>
21
22#include "functionalext.h"
23
24#define TEST_BUFFER_SIZE 128
25
26/**
27 * @tc.name      : grantpt_0100
28 * @tc.desc      : Grant access to the slave pseudoterminal
29 * @tc.level     : Level 0
30 */
31void grantpt_0100(void)
32{
33    int rev = grantpt(0);
34    EXPECT_EQ("grantpt_0100", rev, 0);
35}
36
37/**
38 * @tc.name      : ptsname_r_0100
39 * @tc.desc      : Provide the correct parameters to get the terminal device name
40 * @tc.level     : Level 0
41 */
42void ptsname_r_0100(void)
43{
44    char buf[TEST_BUFFER_SIZE];
45    int fd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
46    EXPECT_NE("ptsname_r_0100", fd, -1);
47    int ret = grantpt(fd);
48    EXPECT_EQ("ptsname_r_0100", ret, 0);
49
50    ret = ptsname_r(fd, buf, sizeof(buf));
51    EXPECT_EQ("ptsname_r_0100", ret, 0);
52    close(fd);
53}
54
55/**
56 * @tc.name      : ptsname_r_0200
57 * @tc.desc      : Provide illegal buffer parameter, get terminal device name
58 * @tc.level     : Level 2
59 */
60void ptsname_r_0200(void)
61{
62    char *buf = NULL;
63    int fd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
64    EXPECT_NE("ptsname_r_0200", fd, -1);
65    int ret = grantpt(fd);
66    EXPECT_EQ("ptsname_r_0200", ret, 0);
67
68    ret = ptsname_r(fd, buf, TEST_BUFFER_SIZE);
69    EXPECT_EQ("ptsname_r_0200", ret, ERANGE);
70    close(fd);
71}
72
73/**
74 * @tc.name      : ptsname_r_0300
75 * @tc.desc      : Provide wrong device ID number, get terminal device name
76 * @tc.level     : Level 2
77 */
78void ptsname_r_0300(void)
79{
80    char buf[TEST_BUFFER_SIZE];
81    int ret = ptsname_r(STDOUT_FILENO, buf, sizeof(buf));
82    EXPECT_EQ("ptsname_r_0300", ret, ENOTTY);
83}
84
85/**
86 * @tc.name      : ptsname_r_0400
87 * @tc.desc      : Provide a small buffer to get the terminal device name
88 * @tc.level     : Level 2
89 */
90void ptsname_r_0400(void)
91{
92    char buf[1];
93    int fd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
94    EXPECT_NE("ptsname_r_0400", fd, -1);
95    int ret = grantpt(fd);
96    EXPECT_EQ("ptsname_r_0400", ret, 0);
97
98    ret = ptsname_r(fd, buf, sizeof(buf));
99    EXPECT_EQ("ptsname_r_0400", ret, ERANGE);
100    close(fd);
101}
102
103/**
104 * @tc.name      : posix_openpt_0100
105 * @tc.desc      : Open the ptmx file using flag O_RDWR (success)
106 * @tc.level     : Level 0
107 */
108void posix_openpt_0100(void)
109{
110    int amaster, aslave;
111    int ret = posix_openpt(O_RDWR);
112    EXPECT_NE("posix_openpt_0100", ret, ERREXPECT);
113    close(ret);
114}
115
116/**
117 * @tc.name      : posix_openpt_0200
118 * @tc.desc      : Open the ptmx file using flag O_RDWR | O_NOCTTY (success)
119 * @tc.level     : Level 2
120 */
121void posix_openpt_0200(void)
122{
123    errno = ENOSPC;
124    int ret = posix_openpt(O_RDWR | O_NOCTTY);
125    EXPECT_NE("posix_openpt_0200", ret, ERREXPECT);
126    close(ret);
127}
128
129int main(void)
130{
131    grantpt_0100();
132    ptsname_r_0100();
133    ptsname_r_0200();
134    ptsname_r_0300();
135    ptsname_r_0400();
136    posix_openpt_0100();
137    posix_openpt_0200();
138
139    return t_status;
140}