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 <stdio_ext.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <stddef.h>
21 #include "functionalext.h"
22 #include "filepath_util.h"
23 
24 const int32_t NUM_ZERO = 0;
25 
26 /**
27  * @tc.name      : __fwriting_0100
28  * @tc.desc      : Verify that the file stream is write-only
29  * @tc.level     : Level 0
30  */
__fwriting_0100(void)31 void __fwriting_0100(void)
32 {
33     char ptr[PATH_MAX] = {0};
34     FILE_ABSOLUTE_PATH(STR_FREAD_TEST_TXT, ptr);
35     FILE *fptr = fopen(ptr, "w");
36     int result = __fwriting(fptr);
37     EXPECT_NE("__fwriting_0100", result, NUM_ZERO);
38     fclose(fptr);
39     remove(ptr);
40 }
41 
42 /**
43  * @tc.name      : __fwriting_0200
44  * @tc.desc      : The last operation on the stream was a write operation
45  * @tc.level     : Level 0
46  */
__fwriting_0200(void)47 void __fwriting_0200(void)
48 {
49     const char *wrstring = "helloworld";
50     char ptr[PATH_MAX] = {0};
51     FILE_ABSOLUTE_PATH(STR_FREAD_TEST_TXT, ptr);
52     FILE *fptr = fopen(ptr, "w+");
53     fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
54     int result = __fwriting(fptr);
55     EXPECT_NE("__fwriting_0200", result, NUM_ZERO);
56     fclose(fptr);
57     remove(ptr);
58 }
59 
60 /**
61  * @tc.name      : __fwriting_0300
62  * @tc.desc      : The last operation on the stream was not a write operation
63  * @tc.level     : Level 2
64  */
__fwriting_0300(void)65 void __fwriting_0300(void)
66 {
67     char abc[100] = {0};
68     const char *wrstring = "helloworld";
69     char ptr[PATH_MAX] = {0};
70     FILE_ABSOLUTE_PATH(STR_FREAD_TEST_TXT, ptr);
71     FILE *fptr = fopen(ptr, "w+");
72     fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
73     fseek(fptr, 0, SEEK_SET);
74     int32_t rsize = fread(abc, 1, 10, fptr);
75     int result = __fwriting(fptr);
76     EXPECT_EQ("__fwriting_0300", result, NUM_ZERO);
77     fclose(fptr);
78     remove(ptr);
79 }
80 
main(int argc, char *argv[])81 int main(int argc, char *argv[])
82 {
83     __fwriting_0100();
84     __fwriting_0200();
85     __fwriting_0300();
86     return t_status;
87 }