1570af302Sopenharmony_ci/** 2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4570af302Sopenharmony_ci * you may not use this file except in compliance with the License. 5570af302Sopenharmony_ci * You may obtain a copy of the License at 6570af302Sopenharmony_ci * 7570af302Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8570af302Sopenharmony_ci * 9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12570af302Sopenharmony_ci * See the License for the specific language governing permissions and 13570af302Sopenharmony_ci * limitations under the License. 14570af302Sopenharmony_ci */ 15570af302Sopenharmony_ci 16570af302Sopenharmony_ci#include <fcntl.h> 17570af302Sopenharmony_ci#include <pty.h> 18570af302Sopenharmony_ci#include <stdlib.h> 19570af302Sopenharmony_ci#include <sys/ioctl.h> 20570af302Sopenharmony_ci#include <unistd.h> 21570af302Sopenharmony_ci 22570af302Sopenharmony_ci#include "functionalext.h" 23570af302Sopenharmony_ci 24570af302Sopenharmony_ci#define TEST_BUFFER_SIZE 128 25570af302Sopenharmony_ci 26570af302Sopenharmony_ci/** 27570af302Sopenharmony_ci * @tc.name : grantpt_0100 28570af302Sopenharmony_ci * @tc.desc : Grant access to the slave pseudoterminal 29570af302Sopenharmony_ci * @tc.level : Level 0 30570af302Sopenharmony_ci */ 31570af302Sopenharmony_civoid grantpt_0100(void) 32570af302Sopenharmony_ci{ 33570af302Sopenharmony_ci int rev = grantpt(0); 34570af302Sopenharmony_ci EXPECT_EQ("grantpt_0100", rev, 0); 35570af302Sopenharmony_ci} 36570af302Sopenharmony_ci 37570af302Sopenharmony_ci/** 38570af302Sopenharmony_ci * @tc.name : ptsname_r_0100 39570af302Sopenharmony_ci * @tc.desc : Provide the correct parameters to get the terminal device name 40570af302Sopenharmony_ci * @tc.level : Level 0 41570af302Sopenharmony_ci */ 42570af302Sopenharmony_civoid ptsname_r_0100(void) 43570af302Sopenharmony_ci{ 44570af302Sopenharmony_ci char buf[TEST_BUFFER_SIZE]; 45570af302Sopenharmony_ci int fd = open("/dev/ptmx", O_RDWR | O_NOCTTY); 46570af302Sopenharmony_ci EXPECT_NE("ptsname_r_0100", fd, -1); 47570af302Sopenharmony_ci int ret = grantpt(fd); 48570af302Sopenharmony_ci EXPECT_EQ("ptsname_r_0100", ret, 0); 49570af302Sopenharmony_ci 50570af302Sopenharmony_ci ret = ptsname_r(fd, buf, sizeof(buf)); 51570af302Sopenharmony_ci EXPECT_EQ("ptsname_r_0100", ret, 0); 52570af302Sopenharmony_ci close(fd); 53570af302Sopenharmony_ci} 54570af302Sopenharmony_ci 55570af302Sopenharmony_ci/** 56570af302Sopenharmony_ci * @tc.name : ptsname_r_0200 57570af302Sopenharmony_ci * @tc.desc : Provide illegal buffer parameter, get terminal device name 58570af302Sopenharmony_ci * @tc.level : Level 2 59570af302Sopenharmony_ci */ 60570af302Sopenharmony_civoid ptsname_r_0200(void) 61570af302Sopenharmony_ci{ 62570af302Sopenharmony_ci char *buf = NULL; 63570af302Sopenharmony_ci int fd = open("/dev/ptmx", O_RDWR | O_NOCTTY); 64570af302Sopenharmony_ci EXPECT_NE("ptsname_r_0200", fd, -1); 65570af302Sopenharmony_ci int ret = grantpt(fd); 66570af302Sopenharmony_ci EXPECT_EQ("ptsname_r_0200", ret, 0); 67570af302Sopenharmony_ci 68570af302Sopenharmony_ci ret = ptsname_r(fd, buf, TEST_BUFFER_SIZE); 69570af302Sopenharmony_ci EXPECT_EQ("ptsname_r_0200", ret, ERANGE); 70570af302Sopenharmony_ci close(fd); 71570af302Sopenharmony_ci} 72570af302Sopenharmony_ci 73570af302Sopenharmony_ci/** 74570af302Sopenharmony_ci * @tc.name : ptsname_r_0300 75570af302Sopenharmony_ci * @tc.desc : Provide wrong device ID number, get terminal device name 76570af302Sopenharmony_ci * @tc.level : Level 2 77570af302Sopenharmony_ci */ 78570af302Sopenharmony_civoid ptsname_r_0300(void) 79570af302Sopenharmony_ci{ 80570af302Sopenharmony_ci char buf[TEST_BUFFER_SIZE]; 81570af302Sopenharmony_ci int ret = ptsname_r(STDOUT_FILENO, buf, sizeof(buf)); 82570af302Sopenharmony_ci EXPECT_EQ("ptsname_r_0300", ret, ENOTTY); 83570af302Sopenharmony_ci} 84570af302Sopenharmony_ci 85570af302Sopenharmony_ci/** 86570af302Sopenharmony_ci * @tc.name : ptsname_r_0400 87570af302Sopenharmony_ci * @tc.desc : Provide a small buffer to get the terminal device name 88570af302Sopenharmony_ci * @tc.level : Level 2 89570af302Sopenharmony_ci */ 90570af302Sopenharmony_civoid ptsname_r_0400(void) 91570af302Sopenharmony_ci{ 92570af302Sopenharmony_ci char buf[1]; 93570af302Sopenharmony_ci int fd = open("/dev/ptmx", O_RDWR | O_NOCTTY); 94570af302Sopenharmony_ci EXPECT_NE("ptsname_r_0400", fd, -1); 95570af302Sopenharmony_ci int ret = grantpt(fd); 96570af302Sopenharmony_ci EXPECT_EQ("ptsname_r_0400", ret, 0); 97570af302Sopenharmony_ci 98570af302Sopenharmony_ci ret = ptsname_r(fd, buf, sizeof(buf)); 99570af302Sopenharmony_ci EXPECT_EQ("ptsname_r_0400", ret, ERANGE); 100570af302Sopenharmony_ci close(fd); 101570af302Sopenharmony_ci} 102570af302Sopenharmony_ci 103570af302Sopenharmony_ci/** 104570af302Sopenharmony_ci * @tc.name : posix_openpt_0100 105570af302Sopenharmony_ci * @tc.desc : Open the ptmx file using flag O_RDWR (success) 106570af302Sopenharmony_ci * @tc.level : Level 0 107570af302Sopenharmony_ci */ 108570af302Sopenharmony_civoid posix_openpt_0100(void) 109570af302Sopenharmony_ci{ 110570af302Sopenharmony_ci int amaster, aslave; 111570af302Sopenharmony_ci int ret = posix_openpt(O_RDWR); 112570af302Sopenharmony_ci EXPECT_NE("posix_openpt_0100", ret, ERREXPECT); 113570af302Sopenharmony_ci close(ret); 114570af302Sopenharmony_ci} 115570af302Sopenharmony_ci 116570af302Sopenharmony_ci/** 117570af302Sopenharmony_ci * @tc.name : posix_openpt_0200 118570af302Sopenharmony_ci * @tc.desc : Open the ptmx file using flag O_RDWR | O_NOCTTY (success) 119570af302Sopenharmony_ci * @tc.level : Level 2 120570af302Sopenharmony_ci */ 121570af302Sopenharmony_civoid posix_openpt_0200(void) 122570af302Sopenharmony_ci{ 123570af302Sopenharmony_ci errno = ENOSPC; 124570af302Sopenharmony_ci int ret = posix_openpt(O_RDWR | O_NOCTTY); 125570af302Sopenharmony_ci EXPECT_NE("posix_openpt_0200", ret, ERREXPECT); 126570af302Sopenharmony_ci close(ret); 127570af302Sopenharmony_ci} 128570af302Sopenharmony_ci 129570af302Sopenharmony_ciint main(void) 130570af302Sopenharmony_ci{ 131570af302Sopenharmony_ci grantpt_0100(); 132570af302Sopenharmony_ci ptsname_r_0100(); 133570af302Sopenharmony_ci ptsname_r_0200(); 134570af302Sopenharmony_ci ptsname_r_0300(); 135570af302Sopenharmony_ci ptsname_r_0400(); 136570af302Sopenharmony_ci posix_openpt_0100(); 137570af302Sopenharmony_ci posix_openpt_0200(); 138570af302Sopenharmony_ci 139570af302Sopenharmony_ci return t_status; 140570af302Sopenharmony_ci}