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 <stdlib.h>
17 #include <spawn.h>
18 #include <sys/wait.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include "functionalext.h"
23 #include "filepath_util.h"
24
25 /*
26 * @tc.name : exit_0100
27 * @tc.desc : Verify that the file content of the auxiliary application output information is viewed
28 * by calling the exit(0) function.
29 * @tc.level : Level 0
30 */
exit_0100(void)31 void exit_0100(void)
32 {
33 char str[100] = {0};
34 const char *ptr = "/data/Exittest01.txt";
35 pid_t pid;
36 pid = fork();
37 if (pid < 0) {
38 fprintf(stderr, "Fork Failed");
39 exit(1);
40 }
41 else if (pid == 0) {
42 FILE *fptr = fopen(ptr, "w+");
43 fprintf(fptr, "%s", "exit before");
44 exit(0);
45 fprintf(fptr, "%s", "exit after");
46 }
47 else {
48 wait(NULL);
49 }
50
51 FILE *fp = fopen(ptr, "r");
52 if (!fp) {
53 t_error("%s fopen failed\n", __func__);
54 }
55 int ret = fseek(fp, 0, SEEK_SET);
56 if (ret != 0) {
57 t_error("%s fseek failed\n", __func__);
58 }
59
60 size_t rsize = fread(str, sizeof(str), 1, fp);
61 if (strcmp(str, "exit before")) {
62 t_error("%s exit failed\n", __func__);
63 }
64
65 fclose(fp);
66 remove(ptr);
67 }
68
69 /*
70 * @tc.name : exit_0200
71 * @tc.desc : Verify that the file content of the auxiliary application output information
72 * is viewed by calling the exit(1) function.
73 * @tc.level : Level 0
74 */
exit_0200(void)75 void exit_0200(void)
76 {
77 char abc[100] = {0};
78 const char *ptr = "/data/Exittest02.txt";
79 pid_t pid;
80 pid = fork();
81
82 if (pid < 0) {
83 fprintf(stderr, "Fork Failed");
84 exit(1);
85 }
86 else if (pid == 0) {
87 FILE *fptr = fopen(ptr, "w+");
88 fprintf(fptr, "%s", "exit before");
89 exit(1);
90 fprintf(fptr, "%s", "exit after");
91 }
92 else {
93 wait(NULL);
94 }
95
96
97 FILE *fp = fopen(ptr, "r");
98 if (!fp) {
99 t_error("%s fopen failed\n", __func__);
100 }
101 int ret = fseek(fp, 0, SEEK_SET);
102 if (ret != 0) {
103 t_error("%s fseek failed\n", __func__);
104 }
105 char *content = fgets(abc, 27, fp);
106 if (strcmp(content, "exit before")) {
107 t_error("%s exit failed\n", __func__);
108 }
109
110 fclose(fp);
111 remove(ptr);
112 }
113
main(int argc,char * argv[])114 int main(int argc, char *argv[])
115 {
116 exit_0100();
117 exit_0200();
118 return t_status;
119 }