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 <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/wait.h>
21 #include "test.h"
22
23 const int WAIT_TIME = 2;
24
25 /**
26 * @tc.name : wait_0100
27 * @tc.desc : Test the wait function to wait for the child process
28 * @tc.level : Level 0
29 */
wait_0100(void)30 void wait_0100(void)
31 {
32 pid_t pid = fork();
33 if (pid > 0) {
34 int flag = 0;
35 pid_t wait_for_pind = wait(&flag);
36 if (wait_for_pind != pid) {
37 t_error("%s wait get pid is %d are not want %d\n", __func__, wait_for_pind, pid);
38 }
39 if (flag != 0) {
40 t_error("%s wait get status is not 0\n", __func__);
41 }
42 } else if (pid == 0) {
43 sleep(1);
44 exit(0);
45 } else {
46 t_error("%s wait fork error\n");
47 }
48 }
49
50 /**
51 * @tc.name : wait_0200
52 * @tc.desc : Test the result of the wait function when the exit code of the child process is not 0
53 * @tc.level : Level 1
54 */
wait_0200(void)55 void wait_0200(void)
56 {
57 pid_t pid = fork();
58 if (pid > 0) {
59 int flag = 0;
60 pid_t wait_for_pind = wait(&flag);
61 if (wait_for_pind != pid) {
62 t_error("%s wait get pid is %d are not want %d\n", __func__, wait_for_pind, pid);
63 }
64 if (flag == 0) {
65 t_error("%s wait get status is not 0\n", __func__);
66 }
67 } else if (pid == 0) {
68 sleep(1);
69 exit(66);
70 } else {
71 t_error("%s wait fork error\n");
72 }
73 }
74
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77 wait_0100();
78 sleep(WAIT_TIME);
79 wait_0200();
80 return t_status;
81 }