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 <pty.h>
17 #include <stdlib.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20 #include "functionalext.h"
21 
22 /**
23  * @tc.name      : forkpty_0100
24  * @tc.desc      : Each parameter is valid and can open a pair of pseudo-terminals for a new
25  *                 session and create a processing process.
26  * @tc.level     : Level 0
27  */
forkpty_0100(void)28 void forkpty_0100(void)
29 {
30     int master;
31     pid_t pid = forkpty(&master, NULL, NULL, NULL);
32     char *sign_r = "1";
33     char *sign_f = "0";
34     char list1[2];
35     char list2[2];
36     FILE *fp;
37     if (pid < 0) {
38         t_error("%s error in fork!", __func__);
39     } else if (pid == 0) {
40         fp = fopen("test1.txt", "w+");
41         fwrite(sign_r, sizeof(char), strlen(sign_r), fp);
42         fclose(fp);
43         exit(EXIT_SUCCESS);
44     } else {
45         wait(NULL);
46         fp = fopen("test2.txt", "w+");
47         fwrite(sign_r, sizeof(char), strlen(sign_r), fp);
48         fclose(fp);
49 
50         FILE *fp1 = fopen("test1.txt", "r");
51         FILE *fp2 = fopen("test2.txt", "r");
52         fread(list1, sizeof(list1), 1, fp1);
53         fread(list2, sizeof(list2), 1, fp2);
54         EXPECT_EQ("forkpty_0100", list1[0], '1');
55         EXPECT_EQ("forkpty_0100", list2[0], '1');
56         fclose(fp1);
57         fclose(fp2);
58         remove("test1.txt");
59         remove("test2.txt");
60     }
61 }
62 
main(int argc, char *argv[])63 int main(int argc, char *argv[])
64 {
65     forkpty_0100();
66     return t_status;
67 }