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 <stdarg.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <signal.h>
21 #include <fcntl.h>
22 #include <wchar.h>
23 #include "test.h"
24 #include <sys/wait.h>
25 #include "functionalext.h"
26 
deal_aberrant(int code)27 void deal_aberrant(int code)
28 {
29     if (code != SIGSEGV) {
30         t_error("putw_0200 code is %d are not SIGSEGV", __func__, code);
31     }
32     exit(t_status);
33 }
34 
35 /**
36  * @tc.name      : putw_0100
37  * @tc.desc      : Test the putw method to write integers to the file
38  * @tc.level     : Level 0
39  */
putw_0100(void)40 void putw_0100(void)
41 {
42     FILE *fp;
43     char *file = "putw_test.txt";
44     fp = fopen(file, "w+");
45     if (fp == NULL) {
46         t_error("%s fopen failed\n", __func__);
47         return;
48     }
49     int result = putw(10, fp);
50     if (result != 0) {
51         t_error("%s putw error get result is %d are not want 0\n", __func__, result);
52     }
53     fclose(fp);
54     remove(file);
55 }
56 
57 /**
58  * @tc.name      : putw_0200
59  * @tc.desc      : Test the result of putw when the incoming file pointer is empty
60  * @tc.level     : Level 2
61  */
putw_0200(void)62 void putw_0200(void)
63 {
64     pid_t pid = fork();
65     if (pid == -1) {
66         t_error("putw_0200: Error forking process");
67     } else if (pid == 0) {
68         FILE *fp = NULL;
69         putw(10, fp);
70     } else {
71         int status;
72         waitpid(pid, &status, 0);
73         if (WIFSIGNALED(status)) {
74             int sig = WTERMSIG(status);
75             EXPECT_EQ("putw_0200", SIGABRT, sig);
76         }
77     }
78 }
79 
main(int argc, char *argv[])80 int main(int argc, char *argv[])
81 {
82     putw_0100();
83     putw_0200();
84     return t_status;
85 }