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 <fcntl.h> 17#include <stdbool.h> 18#include <stdio.h> 19#include <stdlib.h> 20#include "functionalext.h" 21 22const int32_t INIT_LEN = 0; 23const int32_t INCREASE_LEN = 1; 24const int32_t CREAT_MODE = 666; 25 26/** 27 * @tc.name : fopen_0100 28 * @tc.desc : Verify fopen("r") and fclose fread and fwrite, fopen success, fread success, fwrite failed 29 * @tc.level : level 0. 30 */ 31void fopen_0100(void) 32{ 33 char abc[100] = {0}; 34 const char *add = "this is tempory test!"; 35 const char *wrstring = "to write"; 36 const char *wstring = "this is tempory test!"; 37 int ret = creat("tempory_test.txt", CREAT_MODE); 38 if (ret < 0) { 39 EXPECT_MT("fopen_0100", ret, 0); 40 return; 41 } 42 FILE *fptr = fopen("tempory_test.txt", "w"); 43 if (fptr != NULL) { 44 size_t wrsize = fwrite(wstring, sizeof(char), strlen(wstring), fptr); 45 if (wrsize == INIT_LEN) { 46 EXPECT_EQ("fopen_0100", wrsize, INIT_LEN); 47 } 48 fclose(fptr); 49 } 50 fptr = fopen("tempory_test.txt", "r"); 51 bool writeFailedFlag = false; 52 bool consistentFlag = false; 53 if (fptr != NULL) { 54 while (!feof(fptr)) { 55 int32_t rsize = fread(abc, sizeof(abc), 1, fptr); 56 EXPECT_EQ("pread_0100", rsize, 0); 57 } 58 if (!strncmp(abc, add, strlen(add))) { 59 consistentFlag = true; 60 } 61 62 size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr); 63 if (wrsize == INIT_LEN) { 64 writeFailedFlag = true; 65 } 66 fclose(fptr); 67 } 68 69 EXPECT_TRUE("fopen_0100", writeFailedFlag); 70 EXPECT_TRUE("fopen_0100", consistentFlag); 71 remove("tempory_test.txt"); 72} 73 74/** 75 * @tc.name : fopen_0200 76 * @tc.desc : Verify fopen("r") and fclose and fopen failed 77 * @tc.level : level 2. 78 */ 79void fopen_0200(void) 80{ 81 FILE *fptr = fopen("tempory_test1.txt", "r"); 82 EXPECT_EQ("fopen_0200", fptr, NULL); 83} 84 85/** 86 * @tc.name : fopen_0300 87 * @tc.desc : Verify fopen("w") and fclose and fread and fwrite and fopen success, fread failed, fwrite success 88 * @tc.level : level 0. 89 */ 90void fopen_0300(void) 91{ 92 int32_t rsize = 0; 93 char abc[100] = {0}; 94 const char *wrstring = "to write"; 95 FILE *fptr = fopen("tempory_test3.txt", "w"); 96 bool writeSuccessFlag = false; 97 98 if (fptr != NULL) { 99 rsize = fread(abc, sizeof(abc), 1, fptr); 100 size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr); 101 if (wrsize == INIT_LEN) { 102 EXPECT_EQ("fopen_0300", wrsize, INIT_LEN); 103 ; 104 } else if (wrsize == strlen(wrstring)) { 105 writeSuccessFlag = true; 106 } 107 fclose(fptr); 108 } 109 110 EXPECT_EQ("fopen_0300", rsize, INIT_LEN); 111 EXPECT_TRUE("fopen_0300", writeSuccessFlag); 112 remove("tempory_test3.txt"); 113} 114 115/** 116 * @tc.name : fopen_0400 117 * @tc.desc : Verify fopen("w") and fclose and fread and fwrite and fopen success, fread failed, fwrite success 118 * @tc.level : level 1. 119 */ 120void fopen_0400(void) 121{ 122 int32_t rsize = 0; 123 char abc[100] = {0}; 124 const char *wrstring = "to write"; 125 FILE *fptr = fopen("tempory_test2.txt", "w"); 126 bool writeSuccessFlag = false; 127 128 if (fptr != NULL) { 129 rsize = fread(abc, sizeof(abc), 1, fptr); 130 131 size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr); 132 if (wrsize == INIT_LEN) { 133 EXPECT_EQ("fopen_0400", wrsize, INIT_LEN); 134 } else if (wrsize == strlen(wrstring)) { 135 writeSuccessFlag = true; 136 } 137 fclose(fptr); 138 } 139 140 EXPECT_EQ("fopen_0400", rsize, INIT_LEN); 141 EXPECT_TRUE("fopen_0400", writeSuccessFlag); 142 remove("tempory_test2.txt"); 143} 144 145/** 146 * @tc.name : fopen_0500 147 * @tc.desc : Verify fopen("a") and fclose and fread and fwrite and fopen success, fread success, fwrite success 148 * @tc.level : level 0. 149 */ 150void fopen_0500(void) 151{ 152 char abc[100] = {0}; 153 const char *add = "this is tempory test!to write"; 154 const char *wrstring = "to write"; 155 const char *wstring = "this is tempory test!"; 156 int ret = creat("tempory_test5.txt", CREAT_MODE); 157 if (ret < 0) { 158 EXPECT_MT("fopen_0500", ret, 0); 159 return; 160 } 161 FILE *fptr = fopen("tempory_test5.txt", "w"); 162 if (fptr != NULL) { 163 size_t wrsize = fwrite(wstring, sizeof(char), strlen(wstring), fptr); 164 if (wrsize == INIT_LEN) { 165 EXPECT_EQ("fopen_0500", wrsize, INIT_LEN); 166 } 167 fclose(fptr); 168 } 169 170 fptr = fopen("tempory_test5.txt", "a"); 171 bool writeSuccessFlag = false; 172 bool consistentFlag = false; 173 if (fptr != NULL) { 174 fseek(fptr, 0, SEEK_SET); 175 size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr); 176 if (wrsize == INIT_LEN) { 177 EXPECT_EQ("fopen_0500", wrsize, INIT_LEN); 178 } else if (wrsize == strlen(wrstring)) { 179 writeSuccessFlag = true; 180 } 181 fclose(fptr); 182 } 183 184 fptr = fopen("tempory_test5.txt", "r"); 185 if (fptr != NULL) { 186 while (!feof(fptr)) { 187 int32_t rsize = fread(abc, sizeof(abc), 1, fptr); 188 EXPECT_EQ("pread_0500", rsize, 0); 189 } 190 if (!strncmp(abc, add, strlen(add))) { 191 consistentFlag = true; 192 } 193 fclose(fptr); 194 } 195 196 EXPECT_TRUE("fopen_0500", writeSuccessFlag); 197 EXPECT_TRUE("fopen_0500", consistentFlag); 198 remove("tempory_test5.txt"); 199} 200 201/** 202 * @tc.name : fopen_0600 203 * @tc.desc : Verify fopen("a") and fclose and fread and fwrite and fopen success, fread success, fwrite success 204 * @tc.level : level 1. 205 */ 206void fopen_0600(void) 207{ 208 int32_t rsize = 0; 209 char abc[100] = {0}; 210 const char *wrstring = "to write"; 211 FILE *fptr = fopen("tempory_test6.txt", "w"); 212 bool writeSuccessFlag = false; 213 214 if (fptr != NULL) { 215 rsize = fread(abc, sizeof(abc), 1, fptr); 216 217 size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr); 218 if (wrsize == INIT_LEN) { 219 EXPECT_EQ("fopen_0600", wrsize, INIT_LEN); 220 ; 221 } else if (wrsize == strlen(wrstring)) { 222 writeSuccessFlag = true; 223 } 224 fclose(fptr); 225 } 226 227 EXPECT_EQ("fopen_0600", rsize, INIT_LEN); 228 EXPECT_TRUE("fopen_0600", writeSuccessFlag); 229 remove("tempory_test6.txt"); 230} 231 232/** 233 * @tc.name : fopen_0700 234 * @tc.desc : Verify fopen("b") and fclose and fread and fwrite and fopen failed 235 * @tc.level : level 2. 236 */ 237void fopen_0700(void) 238{ 239 FILE *fptr = fopen("./tempory_test.txt", "b"); 240 int32_t flag = INIT_LEN; 241 if (fptr == NULL) { 242 EXPECT_EQ("fopen_0700", errno, EINVAL); 243 } else { 244 flag++; 245 EXPECT_EQ("fopen_0700", flag, INCREASE_LEN); 246 } 247} 248 249/** 250 * @tc.name : fopen_0800 251 * @tc.desc : Verify fopen("a") and fclose and fread and fwrite and fopen failed 252 * @tc.level : level 2. 253 */ 254void fopen_0800(void) 255{ 256 FILE *fptr = fopen("./tmp/tempory_test.txt", "a"); 257 EXPECT_EQ("fopen_0800", fptr, NULL); 258} 259 260/** 261 * @tc.name : fopen_0900 262 * @tc.desc : Verify fopen("ae") and fclose and fread and fwrite and fopen success, fread success, fwrite success 263 * @tc.level : level 0. 264 */ 265void fopen_0900(void) 266{ 267 char abc[100] = {0}; 268 const char *add = "this is tempory test!to write"; 269 const char *wrstring = "to write"; 270 const char *wstring = "this is tempory test!"; 271 int ret = creat("tempory_test9.txt", CREAT_MODE); 272 if (ret < 0) { 273 EXPECT_MT("fopen_0900", ret, 0); 274 return; 275 } 276 FILE *fptr = fopen("tempory_test9.txt", "w"); 277 if (fptr != NULL) { 278 size_t wrsize = fwrite(wstring, sizeof(char), strlen(wstring), fptr); 279 if (wrsize == INIT_LEN) { 280 EXPECT_EQ("fopen_0900", wrsize, INIT_LEN); 281 ; 282 } 283 fclose(fptr); 284 } 285 286 fptr = fopen("tempory_test9.txt", "ae"); 287 bool writeSuccessFlag = false; 288 bool consistentFlag = false; 289 if (fptr != NULL) { 290 size_t wrsize = fwrite(wrstring, sizeof(char), strlen(wrstring), fptr); 291 if (wrsize == INIT_LEN) { 292 EXPECT_EQ("fopen_0900", wrsize, INIT_LEN); 293 ; 294 } else if (wrsize == strlen(wrstring)) { 295 writeSuccessFlag = true; 296 } 297 fclose(fptr); 298 } 299 300 fptr = fopen("tempory_test9.txt", "r"); 301 if (fptr != NULL) { 302 while (!feof(fptr)) { 303 int32_t rsize = fread(abc, sizeof(abc), 1, fptr); 304 EXPECT_EQ("pread_0900", rsize, 0); 305 } 306 if (!strncmp(abc, add, strlen(add))) { 307 consistentFlag = true; 308 } 309 fclose(fptr); 310 } 311 remove("tempory_test9.txt"); 312 313 EXPECT_TRUE("fopen_0900", writeSuccessFlag); 314 EXPECT_TRUE("fopen_0900", consistentFlag); 315} 316 317int main(void) 318{ 319 fopen_0100(); 320 fopen_0200(); 321 fopen_0300(); 322 fopen_0400(); 323 fopen_0500(); 324 fopen_0600(); 325 fopen_0700(); 326 fopen_0800(); 327 fopen_0900(); 328 return t_status; 329} 330