1 /*
2  * Copyright (C) 2024 HiHope Open Source Organization.
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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <string>
20 #include <vector>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <arpa/inet.h>
24 #include <gtest/gtest.h>
25 #include <netinet/in.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <sys/types.h>
29 
30 using namespace testing::ext;
31 
32 class HatsTruncateTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp();
37 void TearDown();
38 private:
39 };
SetUp()40 void HatsTruncateTest::SetUp()
41 {
42 }
TearDown()43 void HatsTruncateTest::TearDown()
44 {
45 }
SetUpTestCase()46 void HatsTruncateTest::SetUpTestCase()
47 {
48 }
TearDownTestCase()49 void HatsTruncateTest::TearDownTestCase()
50 {
51 }
52 
53 const char *TRUNCATE_TEST_FILE = "/data/local/tmp/tryTruncate.txt";
54 const char *TEST_DATA = "1234567890";
55 const int TEST_DATA_LEN = strlen(TEST_DATA);
56 
57 
58 /*
59  * @tc.number : SUB_KERNEL_SYSCALL_TRUNCATE_0100
60  * @tc.name   : TruncateFileSuccess_0001
61  * @tc.desc   : Truncate file success.
62  * @tc.size   : MediumTest
63  * @tc.type   : Function
64  * @tc.level  : Level 1
65  */
HWTEST_F(HatsTruncateTest, TruncateFileSuccess_0001, Function | MediumTest | Level1)66 HWTEST_F(HatsTruncateTest, TruncateFileSuccess_0001, Function | MediumTest | Level1)
67 {
68     int fd;
69     int ret;
70     char *buf = nullptr;
71     buf = new char[TEST_DATA_LEN / 2 + 1];
72 
73     fd = open(TRUNCATE_TEST_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
74     EXPECT_TRUE(fd >= 3);
75     ret = write(fd, TEST_DATA, TEST_DATA_LEN);
76     EXPECT_TRUE(ret == TEST_DATA_LEN);
77     close(fd);
78 
79     ret = truncate(TRUNCATE_TEST_FILE, TEST_DATA_LEN / 2);
80     EXPECT_TRUE(ret == 0);
81 
82     fd = open(TRUNCATE_TEST_FILE, O_RDONLY, 0644);
83     EXPECT_TRUE(fd >= 3);
84     ret = read(fd, buf, TEST_DATA_LEN);
85     EXPECT_TRUE(ret == TEST_DATA_LEN / 2);
86     buf[TEST_DATA_LEN / 2] = '\0';
87     ret = strcmp(buf, "12345");
88     EXPECT_TRUE(ret == 0);
89     close(fd);
90 
91     delete[] buf;
92 }
93 
94 /*
95  * @tc.number : SUB_KERNEL_SYSCALL_TRUNCATE_0200
96  * @tc.name   : TruncateNotExistFileFail_0002
97  * @tc.desc   : Truncate file that does not exist fail.
98  * @tc.size   : MediumTest
99  * @tc.type   : Function
100  * @tc.level  : Level 2
101  */
HWTEST_F(HatsTruncateTest, TruncateNotExistFileFail_0002, Function | MediumTest | Level2)102 HWTEST_F(HatsTruncateTest, TruncateNotExistFileFail_0002, Function | MediumTest | Level2)
103 {
104     int ret;
105 
106     ret = remove(TRUNCATE_TEST_FILE);
107     EXPECT_TRUE(ret == 0);
108 
109     errno = 0;
110     ret = truncate(TRUNCATE_TEST_FILE, TEST_DATA_LEN / 2);
111     EXPECT_TRUE(ret == -1);
112     EXPECT_EQ(errno, ENOENT);
113 }
114 
115 /*
116  * @tc.number : SUB_KERNEL_SYSCALL_TRUNCATE_0300
117  * @tc.name   : TruncateExtendFileSuccess_0003
118  * @tc.desc   : Truncate extend file success.
119  * @tc.size   : MediumTest
120  * @tc.type   : Function
121  * @tc.level  : Level 1
122  */
HWTEST_F(HatsTruncateTest, TruncateExtendFileSuccess_0003, Function | MediumTest | Level1)123 HWTEST_F(HatsTruncateTest, TruncateExtendFileSuccess_0003, Function | MediumTest | Level1)
124 {
125     int fd;
126     int ret;
127     struct stat fileStat;
128     ssize_t fileSize1;
129     ssize_t fileSize2;
130 
131     fd = open(TRUNCATE_TEST_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
132     EXPECT_TRUE(fd >= 3);
133     ret = write(fd, TEST_DATA, TEST_DATA_LEN);
134     EXPECT_TRUE(ret == TEST_DATA_LEN);
135     close(fd);
136 
137     ret = stat(TRUNCATE_TEST_FILE, &fileStat);
138     EXPECT_TRUE(ret == 0);
139     fileSize1 = fileStat.st_size;
140 
141     ret = truncate(TRUNCATE_TEST_FILE, fileSize1 * 2);
142     EXPECT_TRUE(ret == 0);
143 
144     ret = stat(TRUNCATE_TEST_FILE, &fileStat);
145     EXPECT_TRUE(ret == 0);
146     fileSize2 = fileStat.st_size;
147 
148     ret = (fileSize1 * 2 == fileSize2);
149     EXPECT_TRUE(ret);
150 }
151 
152 /*
153  * @tc.number : SUB_KERNEL_SYSCALL_TRUNCATE_0400
154  * @tc.name   : TruncateInvaliLenFail_0004
155  * @tc.desc   : Truncate using invalid length fail.
156  * @tc.size   : MediumTest
157  * @tc.type   : Function
158  * @tc.level  : Level 2
159  */
HWTEST_F(HatsTruncateTest, TruncateInvaliLenFail_0004, Function | MediumTest | Level2)160 HWTEST_F(HatsTruncateTest, TruncateInvaliLenFail_0004, Function | MediumTest | Level2)
161 {
162     int ret;
163     int truncateLen;
164 
165     truncateLen = -1;
166     errno = 0;
167     ret = truncate(TRUNCATE_TEST_FILE, truncateLen);
168     EXPECT_TRUE(ret == -1);
169     EXPECT_EQ(errno, EINVAL);
170 }