| 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_write. |
| 41 | extern ssize_t (*mock_write)(int fd, const void *buf, size_t count); |
| 42 | |
| 43 | namespace mysys_my_write_unittest { |
| 44 | |
| 45 | using ::testing::_; |
| 46 | using ::testing::InSequence; |
| 47 | using ::testing::Return; |
| 48 | using ::testing::SetErrnoAndReturn; |
| 49 | |
| 50 | class MockWrite { |
| 51 | public: |
| 52 | virtual ~MockWrite() = default; |
| 53 | MOCK_METHOD3(mockwrite, ssize_t(int, const void *, size_t)); |
| 54 | }; |
| 55 | |
| 56 | MockWrite *mockfs = nullptr; |
| 57 | |
| 58 | ssize_t mockfs_write(int fd, const void *buf, size_t count) { |
| 59 | return mockfs->mockwrite(fd, buf, count); |
| 60 | } |
| 61 | |
| 62 | class MysysMyWriteTest : public ::testing::Test { |
| 63 | void SetUp() override { |
| 64 | mock_write = mockfs_write; |
| 65 | mockfs = new MockWrite; |
| 66 | } |
| 67 | void TearDown() override { |
| 68 | mock_write = nullptr; |
| 69 | delete mockfs; |
| 70 | mockfs = nullptr; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | // Test of normal case: write OK |
| 75 | TEST_F(MysysMyWriteTest, MyWriteOK) { |
| 76 | uchar buf[4096]; |
| 77 | InSequence s; |
| 78 | EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)).Times(1).WillOnce(Return(4096)); |
| 79 | |
| 80 | const size_t result = my_write(42, buf, 4096, 0); |
| 81 | EXPECT_EQ(4096U, result); |
| 82 | } |
| 83 | |
| 84 | // Test of normal case: write OK with MY_NABP |
| 85 | TEST_F(MysysMyWriteTest, MyWriteOKNABP) { |
| 86 | uchar buf[4096]; |
| 87 | InSequence s; |
| 88 | EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)).Times(1).WillOnce(Return(4096)); |
| 89 | |
| 90 | const size_t result = my_write(42, buf, 4096, MYF(MY_NABP)); |
| 91 | EXPECT_EQ(0U, result); |
| 92 | } |
| 93 | |
| 94 | // Test of disk full: write not OK |
| 95 | TEST_F(MysysMyWriteTest, MyWriteFail) { |
| 96 | uchar buf[4096]; |
| 97 | InSequence s; |
| 98 | EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 99 | .Times(1) |
| 100 | .WillOnce(SetErrnoAndReturn(ENOSPC, -1)); |
| 101 | |
| 102 | const size_t result = my_write(42, buf, 4096, 0); |
| 103 | EXPECT_EQ(MY_FILE_ERROR, result); |
| 104 | } |
| 105 | |
| 106 | // Test of disk full: write not OK, with MY_NABP |
| 107 | TEST_F(MysysMyWriteTest, MyWriteFailNABP) { |
| 108 | uchar buf[4096]; |
| 109 | InSequence s; |
| 110 | EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 111 | .Times(1) |
| 112 | .WillOnce(SetErrnoAndReturn(ENOSPC, -1)); |
| 113 | |
| 114 | const size_t result = my_write(42, buf, 4096, MYF(MY_NABP)); |
| 115 | EXPECT_EQ(MY_FILE_ERROR, result); |
| 116 | } |
| 117 | |
| 118 | // Test of disk full after partial write. |
| 119 | TEST_F(MysysMyWriteTest, MyWrite8192) { |
| 120 | uchar buf[8192]; |
| 121 | InSequence s; |
| 122 | // Expect call to write 8192 bytes, return 4096. |
| 123 | EXPECT_CALL(*mockfs, mockwrite(_, _, 8192)).Times(1).WillOnce(Return(4096)); |
| 124 | // Expect second call to write remaining 4096 bytes, return disk full. |
| 125 | EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 126 | .Times(1) |
| 127 | .WillOnce(SetErrnoAndReturn(ENOSPC, -1)); |
| 128 | |
| 129 | const size_t result = my_write(42, buf, 8192, 0); |
| 130 | EXPECT_EQ(4096U, result); |
| 131 | } |
| 132 | |
| 133 | // Test of disk full after partial write. |
| 134 | TEST_F(MysysMyWriteTest, MyWrite8192NABP) { |
| 135 | uchar buf[8192]; |
| 136 | InSequence s; |
| 137 | // Expect call to write 8192 bytes, return 4096. |
| 138 | EXPECT_CALL(*mockfs, mockwrite(_, _, 8192)).Times(1).WillOnce(Return(4096)); |
| 139 | // Expect second call to write remaining 4096 bytes, return disk full. |
| 140 | EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 141 | .Times(1) |
| 142 | .WillOnce(SetErrnoAndReturn(ENOSPC, -1)); |
| 143 | |
| 144 | const size_t result = my_write(42, buf, 8192, MYF(MY_NABP)); |
| 145 | EXPECT_EQ(MY_FILE_ERROR, result); |
| 146 | } |
| 147 | |
| 148 | // Test of partial write, followed by interrupt, followed by successful write. |
| 149 | TEST_F(MysysMyWriteTest, MyWrite8192Interrupt) { |
| 150 | uchar buf[8192]; |
| 151 | InSequence s; |
| 152 | // Expect call to write 8192 bytes, return 4096. |
| 153 | EXPECT_CALL(*mockfs, mockwrite(_, _, 8192)).Times(1).WillOnce(Return(4096)); |
| 154 | // Expect second call to write remaining 4096 bytes, return interrupt. |
| 155 | EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 156 | .Times(1) |
| 157 | .WillOnce(SetErrnoAndReturn(EINTR, -1)); |
| 158 | // Expect third call to write remaining 4096 bytes, return 4096. |
| 159 | EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)).Times(1).WillOnce(Return(4096)); |
| 160 | |
| 161 | const size_t result = my_write(42, buf, 8192, 0); |
| 162 | EXPECT_EQ(8192U, result); |
| 163 | } |
| 164 | |
| 165 | // Test of partial write, followed by interrupt, followed by successful write. |
| 166 | TEST_F(MysysMyWriteTest, MyWrite8192InterruptNABP) { |
| 167 | uchar buf[8192]; |
| 168 | InSequence s; |
| 169 | // Expect call to write 8192 bytes, return 4096. |
| 170 | EXPECT_CALL(*mockfs, mockwrite(_, _, 8192)).Times(1).WillOnce(Return(4096)); |
| 171 | // Expect second call to write remaining 4096 bytes, return interrupt. |
| 172 | EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 173 | .Times(1) |
| 174 | .WillOnce(SetErrnoAndReturn(EINTR, -1)); |
| 175 | // Expect third call to write remaining 4096 bytes, return 4096. |
| 176 | EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)).Times(1).WillOnce(Return(4096)); |
| 177 | |
| 178 | const size_t result = my_write(42, buf, 8192, MYF(MY_NABP)); |
| 179 | EXPECT_EQ(0U, result); |
| 180 | } |
| 181 | |
| 182 | // Test of partial write, followed successful write. |
| 183 | TEST_F(MysysMyWriteTest, MyWrite400) { |
| 184 | uchar buf[400]; |
| 185 | InSequence s; |
| 186 | EXPECT_CALL(*mockfs, mockwrite(_, _, 400)).Times(1).WillOnce(Return(200)); |
| 187 | EXPECT_CALL(*mockfs, mockwrite(_, _, 200)).Times(1).WillOnce(Return(200)); |
| 188 | |
| 189 | const size_t result = my_write(42, buf, 400, 0); |
| 190 | EXPECT_EQ(400U, result); |
| 191 | } |
| 192 | |
| 193 | // Test of partial write, followed successful write. |
| 194 | TEST_F(MysysMyWriteTest, MyWrite400NABP) { |
| 195 | uchar buf[400]; |
| 196 | InSequence s; |
| 197 | EXPECT_CALL(*mockfs, mockwrite(_, _, 400)).Times(1).WillOnce(Return(200)); |
| 198 | EXPECT_CALL(*mockfs, mockwrite(_, _, 200)).Times(1).WillOnce(Return(200)); |
| 199 | |
| 200 | const size_t result = my_write(42, buf, 400, MYF(MY_NABP)); |
| 201 | EXPECT_EQ(0U, result); |
| 202 | } |
| 203 | |
| 204 | // Test of partial write, followed by failure, followed successful write. |
| 205 | TEST_F(MysysMyWriteTest, MyWrite300) { |
| 206 | uchar buf[300]; |
| 207 | InSequence s; |
| 208 | EXPECT_CALL(*mockfs, mockwrite(_, _, 300)).Times(1).WillOnce(Return(100)); |
| 209 | EXPECT_CALL(*mockfs, mockwrite(_, _, 200)) |
| 210 | .Times(1) |
| 211 | .WillOnce(SetErrnoAndReturn(EAGAIN, 0)); |
| 212 | EXPECT_CALL(*mockfs, mockwrite(_, _, 200)).Times(1).WillOnce(Return(200)); |
| 213 | |
| 214 | const size_t result = my_write(42, buf, 300, 0); |
| 215 | EXPECT_EQ(300U, result); |
| 216 | } |
| 217 | |
| 218 | } // namespace mysys_my_write_unittest |
| 219 | #endif |