Debian Codegraph @

unittest/gunit/mysys_my_pwrite-t.cc

raw symbols SCIP
1 /* Copyright (c) 2013, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include "my_config.h"
25
26 #include <errno.h>
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29 #include <stddef.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #include "my_inttypes.h"
35 #include "my_sys.h"
36
37 // Ignore test on windows, as we are mocking away a unix function, see below.
38 #ifndef _WIN32
39
40 // For testing my_pwrite.
41 extern ssize_t (*mock_pwrite)(int fd, const void *buf, size_t count,
42 off_t offset);
43
44 namespace mysys_my_pwrite_unittest {
45
46 using ::testing::_;
47 using ::testing::InSequence;
48 using ::testing::Return;
49 using ::testing::ReturnPointee;
50 using ::testing::SetErrnoAndReturn;
51
52 class MockWrite {
53 public:
54 virtual ~MockWrite() = default;
55 MOCK_METHOD4(mockwrite, ssize_t(int, const void *, size_t, off_t));
56 };
57
58 MockWrite *mockfs = nullptr;
59
60 ssize_t mockfs_pwrite(int fd, const void *buf, size_t count, off_t offset) {
61 return mockfs->mockwrite(fd, buf, count, offset);
62 }
63
64 class MysysMyPWriteTest : public ::testing::Test {
65 void SetUp() override {
66 mock_pwrite = mockfs_pwrite;
67 mockfs = new MockWrite;
68 m_offset = 0;
69 }
70 void TearDown() override {
71 mock_pwrite = nullptr;
72 delete mockfs;
73 mockfs = nullptr;
74 }
75
76 public:
77 my_off_t m_offset;
78 };
79
80 // Test of normal case: write OK
81 TEST_F(MysysMyPWriteTest, MyPWriteOK) {
82 uchar buf[4096];
83 InSequence s;
84 EXPECT_CALL(*mockfs, mockwrite(_, _, 4096, _))
85 .Times(1)
86 .WillOnce(Return(4096));
87
88 const size_t result = my_pwrite(42, buf, 4096, m_offset, 0);
89 EXPECT_EQ(4096U, result);
90 }
91
92 // Test of normal case: write OK with MY_NABP
93 TEST_F(MysysMyPWriteTest, MyPWriteOKNABP) {
94 uchar buf[4096];
95 InSequence s;
96 EXPECT_CALL(*mockfs, mockwrite(_, _, 4096, _))
97 .Times(1)
98 .WillOnce(Return(4096));
99
100 const size_t result = my_pwrite(42, buf, 4096, m_offset, MYF(MY_NABP));
101 EXPECT_EQ(0U, result);
102 }
103
104 // Test of disk full: write not OK
105 TEST_F(MysysMyPWriteTest, MyPWriteFail) {
106 uchar buf[4096];
107 InSequence s;
108 EXPECT_CALL(*mockfs, mockwrite(_, _, 4096, _))
109 .Times(1)
110 .WillOnce(SetErrnoAndReturn(ENOSPC, -1));
111
112 const size_t result = my_pwrite(42, buf, 4096, m_offset, 0);
113 EXPECT_EQ(MY_FILE_ERROR, result);
114 }
115
116 // Test of disk full: write not OK, with MY_NABP
117 TEST_F(MysysMyPWriteTest, MyPWriteFailNABP) {
118 uchar buf[4096];
119 InSequence s;
120 EXPECT_CALL(*mockfs, mockwrite(_, _, 4096, _))
121 .Times(1)
122 .WillOnce(SetErrnoAndReturn(ENOSPC, -1));
123
124 const size_t result = my_pwrite(42, buf, 4096, m_offset, MYF(MY_NABP));
125 EXPECT_EQ(MY_FILE_ERROR, result);
126 }
127
128 // Test of disk full after partial write.
129 TEST_F(MysysMyPWriteTest, MyPWrite8192) {
130 uchar buf[8192];
131 InSequence s;
132 // Expect call to write 8192 bytes, return 4096.
133 EXPECT_CALL(*mockfs, mockwrite(_, _, 8192, _))
134 .Times(1)
135 .WillOnce(Return(4096));
136 // Expect second call to write remaining 4096 bytes, return disk full.
137 EXPECT_CALL(*mockfs, mockwrite(_, _, 4096, _))
138 .Times(1)
139 .WillOnce(SetErrnoAndReturn(ENOSPC, -1));
140
141 const size_t result = my_pwrite(42, buf, 8192, m_offset, 0);
142 EXPECT_EQ(4096U, result);
143 }
144
145 // Test of disk full after partial write.
146 TEST_F(MysysMyPWriteTest, MyPWrite8192NABP) {
147 uchar buf[8192];
148 InSequence s;
149 // Expect call to write 8192 bytes, return 4096.
150 EXPECT_CALL(*mockfs, mockwrite(_, _, 8192, _))
151 .Times(1)
152 .WillOnce(Return(4096));
153 // Expect second call to write remaining 4096 bytes, return disk full.
154 EXPECT_CALL(*mockfs, mockwrite(_, _, 4096, _))
155 .Times(1)
156 .WillOnce(SetErrnoAndReturn(ENOSPC, -1));
157
158 const size_t result = my_pwrite(42, buf, 8192, m_offset, MYF(MY_NABP));
159 EXPECT_EQ(MY_FILE_ERROR, result);
160 }
161
162 // Test of partial write, followed by interrupt, followed by successful write.
163 TEST_F(MysysMyPWriteTest, MyPWrite8192Interrupt) {
164 uchar buf[8192];
165 InSequence s;
166 // Expect call to write 8192 bytes, return 4096.
167 EXPECT_CALL(*mockfs, mockwrite(_, _, 8192, _))
168 .Times(1)
169 .WillOnce(Return(4096));
170 // Expect second call to write remaining 4096 bytes, return interrupt.
171 EXPECT_CALL(*mockfs, mockwrite(_, _, 4096, _))
172 .Times(1)
173 .WillOnce(SetErrnoAndReturn(EINTR, -1));
174 // Expect third call to write remaining 4096 bytes, return 4096.
175 EXPECT_CALL(*mockfs, mockwrite(_, _, 4096, _))
176 .Times(1)
177 .WillOnce(Return(4096));
178
179 const size_t result = my_pwrite(42, buf, 8192, m_offset, 0);
180 EXPECT_EQ(8192U, result);
181 }
182
183 // Test of partial write, followed by interrupt, followed by successful write.
184 TEST_F(MysysMyPWriteTest, MyPWrite8192InterruptNABP) {
185 uchar buf[8192];
186 InSequence s;
187 // Expect call to write 8192 bytes, return 4096.
188 EXPECT_CALL(*mockfs, mockwrite(_, _, 8192, _))
189 .Times(1)
190 .WillOnce(Return(4096));
191 // Expect second call to write remaining 4096 bytes, return interrupt.
192 EXPECT_CALL(*mockfs, mockwrite(_, _, 4096, _))
193 .Times(1)
194 .WillOnce(SetErrnoAndReturn(EINTR, -1));
195 // Expect third call to write remaining 4096 bytes, return 4096.
196 EXPECT_CALL(*mockfs, mockwrite(_, _, 4096, _))
197 .Times(1)
198 .WillOnce(Return(4096));
199
200 const size_t result = my_pwrite(42, buf, 8192, m_offset, MYF(MY_NABP));
201 EXPECT_EQ(0U, result);
202 }
203
204 // Test of partial write, followed successful write.
205 TEST_F(MysysMyPWriteTest, MyPWrite400) {
206 uchar buf[400];
207 InSequence s;
208 EXPECT_CALL(*mockfs, mockwrite(_, _, 400, _)).Times(1).WillOnce(Return(200));
209 EXPECT_CALL(*mockfs, mockwrite(_, _, 200, _)).Times(1).WillOnce(Return(200));
210
211 const size_t result = my_pwrite(42, buf, 400, m_offset, 0);
212 EXPECT_EQ(400U, result);
213 }
214
215 // Test of partial write, followed successful write.
216 TEST_F(MysysMyPWriteTest, MyPWrite400NABP) {
217 uchar buf[400];
218 InSequence s;
219 EXPECT_CALL(*mockfs, mockwrite(_, _, 400, _)).Times(1).WillOnce(Return(200));
220 EXPECT_CALL(*mockfs, mockwrite(_, _, 200, _)).Times(1).WillOnce(Return(200));
221
222 const size_t result = my_pwrite(42, buf, 400, m_offset, MYF(MY_NABP));
223 EXPECT_EQ(0U, result);
224 }
225
226 // Test of partial write, followed by failure, followed successful write.
227 TEST_F(MysysMyPWriteTest, MyPWrite300) {
228 uchar buf[300];
229 InSequence s;
230 EXPECT_CALL(*mockfs, mockwrite(_, _, 300, _)).Times(1).WillOnce(Return(100));
231 EXPECT_CALL(*mockfs, mockwrite(_, _, 200, _))
232 .Times(1)
233 .WillOnce(SetErrnoAndReturn(EAGAIN, 0));
234 EXPECT_CALL(*mockfs, mockwrite(_, _, 200, _)).Times(1).WillOnce(Return(200));
235
236 const size_t result = my_pwrite(42, buf, 300, m_offset, 0);
237 EXPECT_EQ(300U, result);
238 }
239
240 } // namespace mysys_my_pwrite_unittest
241 #endif