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 } else if (pid == 0) {
41 FILE *fptr = fopen(ptr, "w+");
42 fprintf(fptr, "%s", "exit before");
43 exit(0);
44 fprintf(fptr, "%s", "exit after");
45 } else {
46 wait(NULL);
47 }
48 FILE *fp = fopen(ptr, "r");
49 if (!fp) {
50 t_error("%s fopen failed\n", __func__);
51 }
52 int ret = fseek(fp, 0, SEEK_SET);
53 if (ret != 0) {
54 t_error("%s fseek failed\n", __func__);
55 }
56 size_t rsize = fread(str, sizeof(str), 1, fp);
57 if (strcmp(str, "exit before")) {
58 t_error("%s exit failed\n", __func__);
59 }
60 fclose(fp);
61 remove(ptr);
62 }
63
64 /*
65 * @tc.name : exit_0200
66 * @tc.desc : Verify that the file content of the auxiliary application output information
67 * is viewed by calling the exit(1) function.
68 * @tc.level : Level 0
69 */
exit_0200(void)70 void exit_0200(void)
71 {
72 char abc[100] = {0};
73 const char *ptr = "/data/Exittest02.txt";
74 pid_t pid;
75 pid = fork();
76 if (pid < 0) {
77 fprintf(stderr, "Fork Failed");
78 exit(1);
79 } else if (pid == 0) {
80 FILE *fptr = fopen(ptr, "w+");
81 fprintf(fptr, "%s", "exit before");
82 exit(1);
83 fprintf(fptr, "%s", "exit after");
84 } else {
85 wait(NULL);
86 }
87 FILE *fp = fopen(ptr, "r");
88 if (!fp) {
89 t_error("%s fopen failed\n", __func__);
90 }
91 int ret = fseek(fp, 0, SEEK_SET);
92 if (ret != 0) {
93 t_error("%s fseek failed\n", __func__);
94 }
95 char *content = fgets(abc, 27, fp);
96 if (strcmp(content, "exit before")) {
97 t_error("%s exit failed\n", __func__);
98 }
99 fclose(fp);
100 remove(ptr);
101 }
102
main(int argc, char *argv[])103 int main(int argc, char *argv[])
104 {
105 exit_0100();
106 exit_0200();
107 return t_status;
108 }