• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <termios.h>
21 #include <unistd.h>
22 #include "test.h"
23 
24 const char *text = "Hello";
25 
26 /**
27  * @tc.name      : tcflow_0100
28  * @tc.desc      : Resume blocked terminal and read data
29  * @tc.level     : Level 0
30  */
tcflow_0100(void)31 void tcflow_0100(void)
32 {
33     char data[BUFSIZ];
34     int result;
35 
36     int fdm = open("/dev/pts/ptmx", O_RDWR | O_NOCTTY);
37     if (fdm < 0) {
38         t_error("%s open master failed", __func__);
39         return;
40     }
41     result = grantpt(fdm);
42     if (result < 0) {
43         t_error("%s grantpt failed", __func__);
44         return;
45     }
46 
47     result = unlockpt(fdm);
48     if (result < 0) {
49         t_error("%s unlockpt failed", __func__);
50         return;
51     }
52 
53     char *slave = ptsname(fdm);
54     int fds = open(slave, O_RDWR | O_NOCTTY);
55     if (fds < 0) {
56         t_error("%s open slave failed", __func__);
57         return;
58     }
59 
60     if (write(fds, text, strlen(text) + 1) == -1) {
61         t_error("%s write failed", __func__);
62         return;
63     }
64 
65     if (tcflow(fds, TCOON) != 0) {
66         t_error("%s tcflow failed", __func__);
67     }
68 
69     if (read(fdm, data, sizeof(data)) == -1) {
70         t_error("%s read resume failed", __func__);
71     }
72     if (strcmp(data, text)) {
73         t_error("%s read data is %s", __func__, data);
74     }
75 
76     close(fdm);
77     close(fds);
78 }
79 
80 /**
81  * @tc.name      : tcflow_0200
82  * @tc.desc      : Fildes is not a valid open file descriptor
83  * @tc.level     : Level 2
84  */
tcflow_0200(void)85 void tcflow_0200(void)
86 {
87     int result = tcflow(-1, TCOON);
88     if (result != -1) {
89         t_error("%s result is %d", __func__, result);
90     }
91 }
92 
main(int argc,char * argv[])93 int main(int argc, char *argv[])
94 {
95     tcflow_0100();
96     tcflow_0200();
97     return t_status;
98 }