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 <sys/stat.h> 17570af302Sopenharmony_ci#include <unistd.h> 18570af302Sopenharmony_ci#include "functionalext.h" 19570af302Sopenharmony_ci 20570af302Sopenharmony_ciconst int SUCCESS = 0; 21570af302Sopenharmony_ciconst int FAILED = -1; 22570af302Sopenharmony_ciconst int FILE_ZERO = 0; 23570af302Sopenharmony_ciconst int FILE_FIRST = 10; 24570af302Sopenharmony_ciconst int FILE_SECOND = 100; 25570af302Sopenharmony_ci 26570af302Sopenharmony_ci/** 27570af302Sopenharmony_ci * @tc.name : ftruncate_0100 28570af302Sopenharmony_ci * @tc.desc : The parameter length is 0, which can clear the file content. 29570af302Sopenharmony_ci * @tc.level : Level 0 30570af302Sopenharmony_ci */ 31570af302Sopenharmony_civoid ftruncate_0100(void) 32570af302Sopenharmony_ci{ 33570af302Sopenharmony_ci const char *ptr = "test.txt"; 34570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w"); 35570af302Sopenharmony_ci struct stat statbuff; 36570af302Sopenharmony_ci fprintf(fptr, "%s", "this is a sample!"); 37570af302Sopenharmony_ci int freturn = ftruncate(fileno(fptr), 0); 38570af302Sopenharmony_ci EXPECT_EQ("ftruncate_0100", freturn, SUCCESS); 39570af302Sopenharmony_ci stat(ptr, &statbuff); 40570af302Sopenharmony_ci EXPECT_EQ("ftruncate_0100", statbuff.st_size, FILE_ZERO); 41570af302Sopenharmony_ci fclose(fptr); 42570af302Sopenharmony_ci remove(ptr); 43570af302Sopenharmony_ci fptr = NULL; 44570af302Sopenharmony_ci ptr = NULL; 45570af302Sopenharmony_ci} 46570af302Sopenharmony_ci 47570af302Sopenharmony_ci/** 48570af302Sopenharmony_ci * @tc.name : ftruncate_0200 49570af302Sopenharmony_ci * @tc.desc : The parameter length is greater than 0 and less than the file size, 50570af302Sopenharmony_ci * which can clear the file content. 51570af302Sopenharmony_ci * @tc.level : Level 0 52570af302Sopenharmony_ci */ 53570af302Sopenharmony_civoid ftruncate_0200(void) 54570af302Sopenharmony_ci{ 55570af302Sopenharmony_ci const char *ptr = "test.txt"; 56570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w"); 57570af302Sopenharmony_ci struct stat statbuff; 58570af302Sopenharmony_ci fprintf(fptr, "%s", "this is a sample!"); 59570af302Sopenharmony_ci int freturn = ftruncate(fileno(fptr), 10); 60570af302Sopenharmony_ci EXPECT_EQ("ftruncate_0200", freturn, SUCCESS); 61570af302Sopenharmony_ci stat(ptr, &statbuff); 62570af302Sopenharmony_ci EXPECT_EQ("ftruncate_0200", (int)statbuff.st_size, FILE_FIRST); 63570af302Sopenharmony_ci fclose(fptr); 64570af302Sopenharmony_ci remove(ptr); 65570af302Sopenharmony_ci fptr = NULL; 66570af302Sopenharmony_ci ptr = NULL; 67570af302Sopenharmony_ci} 68570af302Sopenharmony_ci 69570af302Sopenharmony_ci/** 70570af302Sopenharmony_ci * @tc.name : ftruncate_0300 71570af302Sopenharmony_ci * @tc.desc : The parameter length is greater than the file size,which can clear the file content. 72570af302Sopenharmony_ci * @tc.level : Level 0 73570af302Sopenharmony_ci */ 74570af302Sopenharmony_civoid ftruncate_0300(void) 75570af302Sopenharmony_ci{ 76570af302Sopenharmony_ci const char *ptr = "test.txt"; 77570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w"); 78570af302Sopenharmony_ci struct stat statbuff; 79570af302Sopenharmony_ci fprintf(fptr, "%s", "this is a sample!"); 80570af302Sopenharmony_ci int freturn = ftruncate(fileno(fptr), 100); 81570af302Sopenharmony_ci EXPECT_EQ("ftruncate_0300", freturn, SUCCESS); 82570af302Sopenharmony_ci stat(ptr, &statbuff); 83570af302Sopenharmony_ci EXPECT_EQ("ftruncate_0300", (int)statbuff.st_size, FILE_SECOND); 84570af302Sopenharmony_ci fclose(fptr); 85570af302Sopenharmony_ci remove(ptr); 86570af302Sopenharmony_ci fptr = NULL; 87570af302Sopenharmony_ci ptr = NULL; 88570af302Sopenharmony_ci} 89570af302Sopenharmony_ci 90570af302Sopenharmony_ci/** 91570af302Sopenharmony_ci * @tc.name : ftruncate_0400 92570af302Sopenharmony_ci * @tc.desc : The fd parameter is NULL,the file content cannot be cleared. 93570af302Sopenharmony_ci * @tc.level : Level 2 94570af302Sopenharmony_ci */ 95570af302Sopenharmony_civoid ftruncate_0400(void) 96570af302Sopenharmony_ci{ 97570af302Sopenharmony_ci const char *ptr = "test.txt"; 98570af302Sopenharmony_ci FILE *fptr = fopen(ptr, "w"); 99570af302Sopenharmony_ci fprintf(fptr, "%s", "this is a sample!"); 100570af302Sopenharmony_ci int freturn = ftruncate(0, 10); 101570af302Sopenharmony_ci EXPECT_EQ("ftruncate_0400", freturn, FAILED); 102570af302Sopenharmony_ci fclose(fptr); 103570af302Sopenharmony_ci remove(ptr); 104570af302Sopenharmony_ci fptr = NULL; 105570af302Sopenharmony_ci ptr = NULL; 106570af302Sopenharmony_ci} 107570af302Sopenharmony_ci 108570af302Sopenharmony_ciint main(int argc, char *argv[]) 109570af302Sopenharmony_ci{ 110570af302Sopenharmony_ci ftruncate_0100(); 111570af302Sopenharmony_ci ftruncate_0200(); 112570af302Sopenharmony_ci ftruncate_0300(); 113570af302Sopenharmony_ci ftruncate_0400(); 114570af302Sopenharmony_ci 115570af302Sopenharmony_ci return t_status; 116570af302Sopenharmony_ci}