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 */
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 remove(TRUNCATE_TEST_FILE);
93 }
94
95 /*
96 * @tc.number : SUB_KERNEL_SYSCALL_TRUNCATE_0200
97 * @tc.name : TruncateNotExistFileFail_0002
98 * @tc.desc : Truncate file that does not exist fail.
99 * @tc.size : MediumTest
100 * @tc.type : Function
101 * @tc.level : Level 2
102 */
103 HWTEST_F(HatsTruncateTest, TruncateNotExistFileFail_0002, Function | MediumTest | Level2)
104 {
105 int ret;
106 errno = 0;
107 ret = truncate(TRUNCATE_TEST_FILE, TEST_DATA_LEN / 2);
108 EXPECT_TRUE(ret == -1);
109 EXPECT_EQ(errno, ENOENT);
110 }
111
112 /*
113 * @tc.number : SUB_KERNEL_SYSCALL_TRUNCATE_0300
114 * @tc.name : TruncateExtendFileSuccess_0003
115 * @tc.desc : Truncate extend file success.
116 * @tc.size : MediumTest
117 * @tc.type : Function
118 * @tc.level : Level 1
119 */
120 HWTEST_F(HatsTruncateTest, TruncateExtendFileSuccess_0003, Function | MediumTest | Level1)
121 {
122 int fd;
123 int ret;
124 struct stat fileStat;
125 ssize_t fileSize1;
126 ssize_t fileSize2;
127
128 fd = open(TRUNCATE_TEST_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
129 EXPECT_TRUE(fd >= 3);
130 ret = write(fd, TEST_DATA, TEST_DATA_LEN);
131 EXPECT_TRUE(ret == TEST_DATA_LEN);
132 close(fd);
133
134 ret = stat(TRUNCATE_TEST_FILE, &fileStat);
135 EXPECT_TRUE(ret == 0);
136 fileSize1 = fileStat.st_size;
137
138 ret = truncate(TRUNCATE_TEST_FILE, fileSize1 * 2);
139 EXPECT_TRUE(ret == 0);
140
141 ret = stat(TRUNCATE_TEST_FILE, &fileStat);
142 EXPECT_TRUE(ret == 0);
143 fileSize2 = fileStat.st_size;
144
145 ret = (fileSize1 * 2 == fileSize2);
146 EXPECT_TRUE(ret);
147 remove(TRUNCATE_TEST_FILE);
148 }
149
150 /*
151 * @tc.number : SUB_KERNEL_SYSCALL_TRUNCATE_0400
152 * @tc.name : TruncateInvaliLenFail_0004
153 * @tc.desc : Truncate using invalid length fail.
154 * @tc.size : MediumTest
155 * @tc.type : Function
156 * @tc.level : Level 2
157 */
158 HWTEST_F(HatsTruncateTest, TruncateInvaliLenFail_0004, Function | MediumTest | Level2)
159 {
160 int ret;
161 int truncateLen;
162
163 truncateLen = -1;
164 errno = 0;
165 ret = truncate(TRUNCATE_TEST_FILE, truncateLen);
166 EXPECT_TRUE(ret == -1);
167 EXPECT_EQ(errno, EINVAL);
168 }