| 1 | /* Copyright (c) 2018, 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_read. |
| 41 | extern ssize_t (*mock_read)(int fd, void *buf, size_t count); |
| 42 | |
| 43 | namespace mysys_my_read_unittest { |
| 44 | |
| 45 | using ::testing::_; |
| 46 | using ::testing::InSequence; |
| 47 | using ::testing::Return; |
| 48 | using ::testing::SetErrnoAndReturn; |
| 49 | |
| 50 | class MockRead { |
| 51 | public: |
| 52 | virtual ~MockRead() = default; |
| 53 | MOCK_METHOD3(mockread, ssize_t(int, void *, size_t)); |
| 54 | }; |
| 55 | |
| 56 | MockRead *mockfs = nullptr; |
| 57 | |
| 58 | ssize_t mockfs_read(int fd, void *buf, size_t count) { |
| 59 | return mockfs->mockread(fd, buf, count); |
| 60 | } |
| 61 | |
| 62 | class MysysMyReadTest : public ::testing::Test { |
| 63 | void SetUp() override { |
| 64 | mock_read = mockfs_read; |
| 65 | mockfs = new MockRead; |
| 66 | } |
| 67 | void TearDown() override { |
| 68 | mock_read = nullptr; |
| 69 | delete mockfs; |
| 70 | mockfs = nullptr; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | // Test of normal case: read OK |
| 75 | TEST_F(MysysMyReadTest, MyReadOK) { |
| 76 | uchar buf[4096]; |
| 77 | InSequence s; |
| 78 | EXPECT_CALL(*mockfs, mockread(_, _, 4096)).Times(1).WillOnce(Return(4096)); |
| 79 | |
| 80 | const size_t result = my_read(42, buf, 4096, 0); |
| 81 | EXPECT_EQ(4096U, result); |
| 82 | } |
| 83 | |
| 84 | // Test of normal case: read OK with MY_NABP |
| 85 | TEST_F(MysysMyReadTest, MyReadOKNABP) { |
| 86 | uchar buf[4096]; |
| 87 | InSequence s; |
| 88 | EXPECT_CALL(*mockfs, mockread(_, _, 4096)).Times(1).WillOnce(Return(4096)); |
| 89 | |
| 90 | const size_t result = my_read(42, buf, 4096, MYF(MY_NABP)); |
| 91 | EXPECT_EQ(0U, result); |
| 92 | } |
| 93 | |
| 94 | // Test of disk full: read not OK |
| 95 | TEST_F(MysysMyReadTest, MyReadFail) { |
| 96 | uchar buf[4096]; |
| 97 | InSequence s; |
| 98 | EXPECT_CALL(*mockfs, mockread(_, _, 4096)) |
| 99 | .Times(1) |
| 100 | .WillOnce(SetErrnoAndReturn(ENOSPC, -1)); |
| 101 | |
| 102 | const size_t result = my_read(42, buf, 4096, 0); |
| 103 | EXPECT_EQ(MY_FILE_ERROR, result); |
| 104 | } |
| 105 | |
| 106 | // Test of disk full: read not OK, with MY_NABP |
| 107 | TEST_F(MysysMyReadTest, MyReadFailNABP) { |
| 108 | uchar buf[4096]; |
| 109 | InSequence s; |
| 110 | EXPECT_CALL(*mockfs, mockread(_, _, 4096)) |
| 111 | .Times(1) |
| 112 | .WillOnce(SetErrnoAndReturn(ENOSPC, -1)); |
| 113 | |
| 114 | const size_t result = my_read(42, buf, 4096, MYF(MY_NABP)); |
| 115 | EXPECT_EQ(MY_FILE_ERROR, result); |
| 116 | } |
| 117 | |
| 118 | // Test of normal case: read OK, with MY_FULL_IO |
| 119 | TEST_F(MysysMyReadTest, MyReadOkFULLIO) { |
| 120 | uchar buf[132096]; // Read 129Kb |
| 121 | InSequence s; |
| 122 | EXPECT_CALL(*mockfs, mockread(_, _, 132096)) |
| 123 | .Times(1) |
| 124 | .WillOnce(Return(132096)); |
| 125 | |
| 126 | const size_t result = my_read(42, buf, 132096, MYF(MY_FULL_IO)); |
| 127 | EXPECT_EQ(132096U, result); |
| 128 | } |
| 129 | } // namespace mysys_my_read_unittest |
| 130 | #endif |