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 <sys/stat.h>
17 #include <sys/wait.h>
18 #include <stdlib.h>
19 #include "functionalext.h"
20
21 typedef void (*TEST_FUN)();
22 const int32_t NUM_ZERO = 0;
23 const int32_t NUM_TEN = 10;
24 const int32_t NUM_TWENTY = 20;
25
26 /**
27 * @tc.name : fread_0100
28 * @tc.desc : Verify that the number of data read is less than the number of data in the file
29 * @tc.level : Level 0
30 */
fread_0100(void)31 void fread_0100(void)
32 {
33 char abc[100] = {0};
34 const char *wrstring = "starttowritehelloworld";
35 const char *ptr = "/data/Freadtest.txt";
36 FILE *fptr = fopen(ptr, "w+");
37 EXPECT_PTRNE("fread_0100", fptr, NULL);
38
39 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
40 fseek(fptr, 0, SEEK_SET);
41
42 size_t rsize = fread(abc, 1, 10, fptr);
43 EXPECT_EQ("fread_0100", rsize, 10);
44 EXPECT_STREQ("fread_0100", abc, "starttowri");
45
46 fclose(fptr);
47 remove(ptr);
48 }
49
50 /**
51 * @tc.name : fread_0200
52 * @tc.desc : Verify that the number of data read is greater than the number of data in the file
53 * @tc.level : Level 1
54 */
fread_0200(void)55 void fread_0200(void)
56 {
57 char abc[100] = {0};
58 const char *wrstring = "startwritehelloworld";
59 const char *ptr = "/data/Freadtest.txt";
60
61 FILE *fptr = fopen(ptr, "w+");
62 EXPECT_PTRNE("fread_0200", fptr, NULL);
63
64 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
65 fseek(fptr, 0, SEEK_SET);
66
67 size_t rsize = fread(abc, 1, 25, fptr);
68 EXPECT_EQ("fread_0200", rsize, 20);
69 EXPECT_STREQ("fread_0200", abc, wrstring);
70
71 fclose(fptr);
72 remove(ptr);
73 }
74
75 /**
76 * @tc.name : fread_0300
77 * @tc.desc : Verify that the number of data read is greater than the number of data in the file
78 * (the file pointer is at the end)
79 * @tc.level : Level 2
80 */
fread_0300(void)81 void fread_0300(void)
82 {
83 char abc[100] = {0};
84 const char *wrstring = "startwritehelloworld";
85 const char *ptr = "/data/Freadtest.txt";
86 FILE *fptr = fopen(ptr, "w+");
87 EXPECT_PTRNE("fread_0200", fptr, NULL);
88
89 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
90 fseek(fptr, 0, SEEK_END);
91 size_t rsize = fread(abc, 1, 10, fptr);
92 EXPECT_EQ("fread_0300", rsize, 0);
93
94 fclose(fptr);
95 remove(ptr);
96 }
97
98 /**
99 * @tc.name : fread_0400
100 * @tc.desc : Verify that the size parameter is invalid and cannot read the specified file content
101 * @tc.level : Level 2
102 */
fread_0400(void)103 void fread_0400(void)
104 {
105 char abc[100] = {0};
106 const char *wrstring = "startwritehelloworld";
107 const char *ptr = "/data/Freadtest.txt";
108 FILE *fptr = fopen(ptr, "w+");
109 EXPECT_PTRNE("fread_0200", fptr, NULL);
110
111 fwrite(wrstring, sizeof(char), strlen(wrstring), fptr);
112 fseek(fptr, 0, SEEK_END);
113
114 size_t rsize = fread(abc, 0, 10, fptr);
115 EXPECT_EQ("fread_0400", rsize, 0);
116
117 fclose(fptr);
118 remove(ptr);
119 }
120
121 /**
122 * @tc.name : fread_0500
123 * @tc.desc : Verify that the return value of syscall have been processed correctly
124 * @tc.level : Level 2
125 */
126 #define FREAD_0500_BUFSZ (4097)
fread_0500(void)127 void fread_0500(void)
128 {
129 pid_t pid = fork();
130 if (pid == -1) {
131 perror("fread_0500 fork:");
132 exit(-1);
133 }
134
135 /* child */
136 if (pid == 0) {
137 /* make sure parent opening the status file */
138 sleep(1);
139 exit(-1);
140 }
141
142 char buf[FREAD_0500_BUFSZ] = {0};
143 sprintf(buf, "/proc/%d/status", pid);
144 FILE *fStatus = fopen(buf, "rb");
145 EXPECT_PTRNE("fread_0500", fStatus, NULL);
146
147 /* wait child exit, and status file of child will disappear */
148 int status = 0;
149 pid_t w = wait(&status);
150
151 /* read >4K data from file, check if return correctly */
152 size_t rsize = fread(buf, 1, FREAD_0500_BUFSZ, fStatus);
153 EXPECT_EQ("fread_0500", rsize, 0);
154 fclose(fStatus);
155 }
156
157 TEST_FUN G_Fun_Array[] = {
158 fread_0100,
159 fread_0200,
160 fread_0300,
161 fread_0400,
162 fread_0500,
163 };
164
main(int argc,char * argv[])165 int main(int argc, char *argv[])
166 {
167 int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
168 for (int pos = 0; pos < num; ++pos) {
169 G_Fun_Array[pos]();
170 }
171
172 return t_status;
173 }
174