• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unistd.h>
18 #include "functionalext.h"
19 
20 const int SUCCESS = 0;
21 const int FAILED = -1;
22 const int FILE_ZERO = 0;
23 const int FILE_FIRST = 10;
24 const int FILE_SECOND = 100;
25 
26 /**
27  * @tc.name      : ftruncate_0100
28  * @tc.desc      : The parameter length is 0, which can clear the file content.
29  * @tc.level     : Level 0
30  */
ftruncate_0100(void)31 void ftruncate_0100(void)
32 {
33     const char *ptr = "test.txt";
34     FILE *fptr = fopen(ptr, "w");
35     struct stat statbuff;
36     fprintf(fptr, "%s", "this is a sample!");
37     int freturn = ftruncate(fileno(fptr), 0);
38     EXPECT_EQ("ftruncate_0100", freturn, SUCCESS);
39     stat(ptr, &statbuff);
40     EXPECT_EQ("ftruncate_0100", statbuff.st_size, FILE_ZERO);
41     fclose(fptr);
42     remove(ptr);
43     fptr = NULL;
44     ptr = NULL;
45 }
46 
47 /**
48  * @tc.name      : ftruncate_0200
49  * @tc.desc      : The parameter length is greater than 0 and less than the file size,
50  *                 which can clear the file content.
51  * @tc.level     : Level 0
52  */
ftruncate_0200(void)53 void ftruncate_0200(void)
54 {
55     const char *ptr = "test.txt";
56     FILE *fptr = fopen(ptr, "w");
57     struct stat statbuff;
58     fprintf(fptr, "%s", "this is a sample!");
59     int freturn = ftruncate(fileno(fptr), 10);
60     EXPECT_EQ("ftruncate_0200", freturn, SUCCESS);
61     stat(ptr, &statbuff);
62     EXPECT_EQ("ftruncate_0200", (int)statbuff.st_size, FILE_FIRST);
63     fclose(fptr);
64     remove(ptr);
65     fptr = NULL;
66     ptr = NULL;
67 }
68 
69 /**
70  * @tc.name      : ftruncate_0300
71  * @tc.desc      : The parameter length is greater than the file size,which can clear the file content.
72  * @tc.level     : Level 0
73  */
ftruncate_0300(void)74 void ftruncate_0300(void)
75 {
76     const char *ptr = "test.txt";
77     FILE *fptr = fopen(ptr, "w");
78     struct stat statbuff;
79     fprintf(fptr, "%s", "this is a sample!");
80     int freturn = ftruncate(fileno(fptr), 100);
81     EXPECT_EQ("ftruncate_0300", freturn, SUCCESS);
82     stat(ptr, &statbuff);
83     EXPECT_EQ("ftruncate_0300", (int)statbuff.st_size, FILE_SECOND);
84     fclose(fptr);
85     remove(ptr);
86     fptr = NULL;
87     ptr = NULL;
88 }
89 
90 /**
91  * @tc.name      : ftruncate_0400
92  * @tc.desc      : The fd parameter is invalid,the file content cannot be cleared.
93  * @tc.level     : Level 2
94  */
ftruncate_0400(void)95 void ftruncate_0400(void)
96 {
97     const char *ptr = "test.txt";
98     FILE *fptr = fopen(ptr, "w");
99     fprintf(fptr, "%s", "this is a sample!");
100     fclose(fptr);
101     int freturn = ftruncate(fileno(fptr), 10);
102     EXPECT_EQ("ftruncate_0400", freturn, FAILED);
103     remove(ptr);
104     fptr = NULL;
105     ptr = NULL;
106 }
107 
108 /**
109  * @tc.name      : ftruncate_0500
110  * @tc.desc      : The fd parameter is NULL,the file content cannot be cleared.
111  * @tc.level     : Level 2
112  */
ftruncate_0500(void)113 void ftruncate_0500(void)
114 {
115     const char *ptr = "test.txt";
116     FILE *fptr = fopen(ptr, "w");
117     fprintf(fptr, "%s", "this is a sample!");
118     int freturn = ftruncate(0, 10);
119     EXPECT_EQ("ftruncate_0500", freturn, FAILED);
120     fclose(fptr);
121     remove(ptr);
122     fptr = NULL;
123     ptr = NULL;
124 }
125 
main(int argc,char * argv[])126 int main(int argc, char *argv[])
127 {
128     ftruncate_0100();
129     ftruncate_0200();
130     ftruncate_0300();
131     ftruncate_0400();
132     ftruncate_0500();
133 
134     return t_status;
135 }