1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <stdlib.h>
17570af302Sopenharmony_ci#include <spawn.h>
18570af302Sopenharmony_ci#include <sys/wait.h>
19570af302Sopenharmony_ci#include <sys/stat.h>
20570af302Sopenharmony_ci#include <fcntl.h>
21570af302Sopenharmony_ci#include <unistd.h>
22570af302Sopenharmony_ci#include "functionalext.h"
23570af302Sopenharmony_ci#include "filepath_util.h"
24570af302Sopenharmony_ci
25570af302Sopenharmony_ci/*
26570af302Sopenharmony_ci * @tc.name      : exit_0100
27570af302Sopenharmony_ci * @tc.desc      : Verify that the file content of the auxiliary application output information is viewed
28570af302Sopenharmony_ci *                 by calling the exit(0) function.
29570af302Sopenharmony_ci * @tc.level     : Level 0
30570af302Sopenharmony_ci */
31570af302Sopenharmony_civoid exit_0100(void)
32570af302Sopenharmony_ci{
33570af302Sopenharmony_ci    char str[100] = {0};
34570af302Sopenharmony_ci    const char *ptr = "/data/Exittest01.txt";
35570af302Sopenharmony_ci    pid_t pid;
36570af302Sopenharmony_ci    pid = fork();
37570af302Sopenharmony_ci    if (pid < 0) {
38570af302Sopenharmony_ci        fprintf(stderr, "Fork Failed");
39570af302Sopenharmony_ci        exit(1);
40570af302Sopenharmony_ci    } else if (pid == 0) {
41570af302Sopenharmony_ci        FILE *fptr = fopen(ptr, "w+");
42570af302Sopenharmony_ci        fprintf(fptr, "%s", "exit before");
43570af302Sopenharmony_ci        exit(0);
44570af302Sopenharmony_ci        fprintf(fptr, "%s", "exit after");
45570af302Sopenharmony_ci    } else {
46570af302Sopenharmony_ci        wait(NULL);
47570af302Sopenharmony_ci    }
48570af302Sopenharmony_ci    FILE *fp = fopen(ptr, "r");
49570af302Sopenharmony_ci    if (!fp) {
50570af302Sopenharmony_ci        t_error("%s fopen failed\n", __func__);
51570af302Sopenharmony_ci    }
52570af302Sopenharmony_ci    int ret = fseek(fp, 0, SEEK_SET);
53570af302Sopenharmony_ci    if (ret != 0) {
54570af302Sopenharmony_ci        t_error("%s fseek failed\n", __func__);
55570af302Sopenharmony_ci    }
56570af302Sopenharmony_ci    size_t rsize = fread(str, sizeof(str), 1, fp);
57570af302Sopenharmony_ci    if (strcmp(str, "exit before")) {
58570af302Sopenharmony_ci        t_error("%s exit failed\n", __func__);
59570af302Sopenharmony_ci    }
60570af302Sopenharmony_ci    fclose(fp);
61570af302Sopenharmony_ci    remove(ptr);
62570af302Sopenharmony_ci}
63570af302Sopenharmony_ci
64570af302Sopenharmony_ci/*
65570af302Sopenharmony_ci * @tc.name      : exit_0200
66570af302Sopenharmony_ci * @tc.desc      : Verify that the file content of the auxiliary application output information
67570af302Sopenharmony_ci *                 is viewed by calling the exit(1) function.
68570af302Sopenharmony_ci * @tc.level     : Level 0
69570af302Sopenharmony_ci */
70570af302Sopenharmony_civoid exit_0200(void)
71570af302Sopenharmony_ci{
72570af302Sopenharmony_ci    char abc[100] = {0};
73570af302Sopenharmony_ci    const char *ptr = "/data/Exittest02.txt";
74570af302Sopenharmony_ci    pid_t pid;
75570af302Sopenharmony_ci    pid = fork();
76570af302Sopenharmony_ci    if (pid < 0) {
77570af302Sopenharmony_ci        fprintf(stderr, "Fork Failed");
78570af302Sopenharmony_ci        exit(1);
79570af302Sopenharmony_ci    } else if (pid == 0) {
80570af302Sopenharmony_ci        FILE *fptr = fopen(ptr, "w+");
81570af302Sopenharmony_ci        fprintf(fptr, "%s", "exit before");
82570af302Sopenharmony_ci        exit(1);
83570af302Sopenharmony_ci        fprintf(fptr, "%s", "exit after");
84570af302Sopenharmony_ci    } else {
85570af302Sopenharmony_ci        wait(NULL);
86570af302Sopenharmony_ci    }
87570af302Sopenharmony_ci    FILE *fp = fopen(ptr, "r");
88570af302Sopenharmony_ci    if (!fp) {
89570af302Sopenharmony_ci        t_error("%s fopen failed\n", __func__);
90570af302Sopenharmony_ci    }
91570af302Sopenharmony_ci    int ret = fseek(fp, 0, SEEK_SET);
92570af302Sopenharmony_ci    if (ret != 0) {
93570af302Sopenharmony_ci        t_error("%s fseek failed\n", __func__);
94570af302Sopenharmony_ci    }
95570af302Sopenharmony_ci    char *content = fgets(abc, 27, fp);
96570af302Sopenharmony_ci    if (strcmp(content, "exit before")) {
97570af302Sopenharmony_ci        t_error("%s exit failed\n", __func__);
98570af302Sopenharmony_ci    }
99570af302Sopenharmony_ci    fclose(fp);
100570af302Sopenharmony_ci    remove(ptr);
101570af302Sopenharmony_ci}
102570af302Sopenharmony_ci
103570af302Sopenharmony_ciint main(int argc, char *argv[])
104570af302Sopenharmony_ci{
105570af302Sopenharmony_ci    exit_0100();
106570af302Sopenharmony_ci    exit_0200();
107570af302Sopenharmony_ci    return t_status;
108570af302Sopenharmony_ci}