|
| 1 | +/* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
| 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 as published by |
| 5 | + the Free Software Foundation; version 2 of the License. |
| 6 | +
|
| 7 | + This program is distributed in the hope that it will be useful, |
| 8 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU General Public License for more details. |
| 11 | +
|
| 12 | + You should have received a copy of the GNU General Public License |
| 13 | + along with this program; if not, write to the Free Software |
| 14 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA */ |
| 15 | + |
| 16 | +// First include (the generated) my_config.h, to get correct platform defines. |
| 17 | +#include "my_config.h" |
| 18 | +#include <gtest/gtest.h> |
| 19 | +#include <gmock/gmock.h> |
| 20 | + |
| 21 | +// Ignore test on windows, as we are mocking away a unix function, see below. |
| 22 | +#ifndef __WIN__ |
| 23 | +namespace mysys_my_write_unittest { |
| 24 | + |
| 25 | +using ::testing::_; |
| 26 | +using ::testing::InSequence; |
| 27 | +using ::testing::Return; |
| 28 | +using ::testing::SetErrnoAndReturn; |
| 29 | + |
| 30 | +class MockWrite |
| 31 | +{ |
| 32 | +public: |
| 33 | + virtual ~MockWrite() {} |
| 34 | + MOCK_METHOD3(mockwrite, ssize_t(int, const void *, size_t)); |
| 35 | +}; |
| 36 | + |
| 37 | +MockWrite *mockfs= NULL; |
| 38 | + |
| 39 | +// We need to mock away write(2), do it with a macro: |
| 40 | +#define write(fd, buf, count) mockfs->mockwrite(fd, buf, count) |
| 41 | + |
| 42 | +/* |
| 43 | + Include the source file, which will give us |
| 44 | + mysys_my_write_unittest::my_write() for testing. |
| 45 | +*/ |
| 46 | +#include "../../mysys/my_write.c" |
| 47 | + |
| 48 | +#undef write |
| 49 | + |
| 50 | +class MysysMyWriteTest : public ::testing::Test |
| 51 | +{ |
| 52 | + virtual void SetUp() |
| 53 | + { |
| 54 | + mockfs= new MockWrite; |
| 55 | + } |
| 56 | + virtual void TearDown() |
| 57 | + { |
| 58 | + delete mockfs; |
| 59 | + mockfs= NULL; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | + |
| 64 | +// Test of normal case: write OK |
| 65 | +TEST_F(MysysMyWriteTest, MyWriteOK) |
| 66 | +{ |
| 67 | + uchar buf[4096]; |
| 68 | + InSequence s; |
| 69 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 70 | + .Times(1) |
| 71 | + .WillOnce(Return(4096)); |
| 72 | + |
| 73 | + const size_t result= my_write(42, buf, 4096, 0); |
| 74 | + EXPECT_EQ(4096U, result); |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +// Test of normal case: write OK with MY_NABP |
| 79 | +TEST_F(MysysMyWriteTest, MyWriteOKNABP) |
| 80 | +{ |
| 81 | + uchar buf[4096]; |
| 82 | + InSequence s; |
| 83 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 84 | + .Times(1) |
| 85 | + .WillOnce(Return(4096)); |
| 86 | + |
| 87 | + const size_t result= my_write(42, buf, 4096, MYF(MY_NABP)); |
| 88 | + EXPECT_EQ(0U, result); |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +// Test of disk full: write not OK |
| 93 | +TEST_F(MysysMyWriteTest, MyWriteFail) |
| 94 | +{ |
| 95 | + uchar buf[4096]; |
| 96 | + InSequence s; |
| 97 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 98 | + .Times(1) |
| 99 | + .WillOnce(SetErrnoAndReturn(ENOSPC, -1)); |
| 100 | + |
| 101 | + const size_t result= my_write(42, buf, 4096, 0); |
| 102 | + EXPECT_EQ(MY_FILE_ERROR, result); |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +// Test of disk full: write not OK, with MY_NABP |
| 107 | +TEST_F(MysysMyWriteTest, MyWriteFailNABP) |
| 108 | +{ |
| 109 | + uchar buf[4096]; |
| 110 | + InSequence s; |
| 111 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 112 | + .Times(1) |
| 113 | + .WillOnce(SetErrnoAndReturn(ENOSPC, -1)); |
| 114 | + |
| 115 | + const size_t result= my_write(42, buf, 4096, MYF(MY_NABP)); |
| 116 | + EXPECT_EQ(MY_FILE_ERROR, result); |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +// Test of disk full after partial write. |
| 121 | +TEST_F(MysysMyWriteTest, MyWrite8192) |
| 122 | +{ |
| 123 | + uchar buf[8192]; |
| 124 | + InSequence s; |
| 125 | + // Expect call to write 8192 bytes, return 4096. |
| 126 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 8192)) |
| 127 | + .Times(1) |
| 128 | + .WillOnce(Return(4096)); |
| 129 | + // Expect second call to write remaining 4096 bytes, return disk full. |
| 130 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 131 | + .Times(1) |
| 132 | + .WillOnce(SetErrnoAndReturn(ENOSPC, -1)); |
| 133 | + |
| 134 | + const size_t result= my_write(42, buf, 8192, 0); |
| 135 | + EXPECT_EQ(4096U, result); |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +// Test of disk full after partial write. |
| 140 | +TEST_F(MysysMyWriteTest, MyWrite8192NABP) |
| 141 | +{ |
| 142 | + uchar buf[8192]; |
| 143 | + InSequence s; |
| 144 | + // Expect call to write 8192 bytes, return 4096. |
| 145 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 8192)) |
| 146 | + .Times(1) |
| 147 | + .WillOnce(Return(4096)); |
| 148 | + // Expect second call to write remaining 4096 bytes, return disk full. |
| 149 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 150 | + .Times(1) |
| 151 | + .WillOnce(SetErrnoAndReturn(ENOSPC, -1)); |
| 152 | + |
| 153 | + const size_t result= my_write(42, buf, 8192, MYF(MY_NABP)); |
| 154 | + EXPECT_EQ(MY_FILE_ERROR, result); |
| 155 | +} |
| 156 | + |
| 157 | + |
| 158 | +// Test of partial write, followed by interrupt, followed by successful write. |
| 159 | +TEST_F(MysysMyWriteTest, MyWrite8192Interrupt) |
| 160 | +{ |
| 161 | + uchar buf[8192]; |
| 162 | + InSequence s; |
| 163 | + // Expect call to write 8192 bytes, return 4096. |
| 164 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 8192)) |
| 165 | + .Times(1) |
| 166 | + .WillOnce(Return(4096)); |
| 167 | + // Expect second call to write remaining 4096 bytes, return interrupt. |
| 168 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 169 | + .Times(1) |
| 170 | + .WillOnce(SetErrnoAndReturn(EINTR, -1)); |
| 171 | + // Expect third call to write remaining 4096 bytes, return 4096. |
| 172 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 173 | + .Times(1) |
| 174 | + .WillOnce(Return(4096)); |
| 175 | + |
| 176 | + const size_t result= my_write(42, buf, 8192, 0); |
| 177 | + EXPECT_EQ(8192U, result); |
| 178 | +} |
| 179 | + |
| 180 | + |
| 181 | +// Test of partial write, followed by interrupt, followed by successful write. |
| 182 | +TEST_F(MysysMyWriteTest, MyWrite8192InterruptNABP) |
| 183 | +{ |
| 184 | + uchar buf[8192]; |
| 185 | + InSequence s; |
| 186 | + // Expect call to write 8192 bytes, return 4096. |
| 187 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 8192)) |
| 188 | + .Times(1) |
| 189 | + .WillOnce(Return(4096)); |
| 190 | + // Expect second call to write remaining 4096 bytes, return interrupt. |
| 191 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 192 | + .Times(1) |
| 193 | + .WillOnce(SetErrnoAndReturn(EINTR, -1)); |
| 194 | + // Expect third call to write remaining 4096 bytes, return 4096. |
| 195 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 4096)) |
| 196 | + .Times(1) |
| 197 | + .WillOnce(Return(4096)); |
| 198 | + |
| 199 | + const size_t result= my_write(42, buf, 8192, MYF(MY_NABP)); |
| 200 | + EXPECT_EQ(0U, result); |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +// Test of partial write, followed successful write. |
| 205 | +TEST_F(MysysMyWriteTest, MyWrite400) |
| 206 | +{ |
| 207 | + uchar buf[400]; |
| 208 | + InSequence s; |
| 209 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 400)) |
| 210 | + .Times(1) |
| 211 | + .WillOnce(Return(200)); |
| 212 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 200)) |
| 213 | + .Times(1) |
| 214 | + .WillOnce(Return(200)); |
| 215 | + |
| 216 | + const size_t result= my_write(42, buf, 400, 0); |
| 217 | + EXPECT_EQ(400U, result); |
| 218 | +} |
| 219 | + |
| 220 | + |
| 221 | +// Test of partial write, followed successful write. |
| 222 | +TEST_F(MysysMyWriteTest, MyWrite400NABP) |
| 223 | +{ |
| 224 | + uchar buf[400]; |
| 225 | + InSequence s; |
| 226 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 400)) |
| 227 | + .Times(1) |
| 228 | + .WillOnce(Return(200)); |
| 229 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 200)) |
| 230 | + .Times(1) |
| 231 | + .WillOnce(Return(200)); |
| 232 | + |
| 233 | + const size_t result= my_write(42, buf, 400, MYF(MY_NABP)); |
| 234 | + EXPECT_EQ(0U, result); |
| 235 | +} |
| 236 | + |
| 237 | + |
| 238 | +// Test of partial write, followed by failure, followed successful write. |
| 239 | +TEST_F(MysysMyWriteTest, MyWrite300) |
| 240 | +{ |
| 241 | + uchar buf[300]; |
| 242 | + InSequence s; |
| 243 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 300)) |
| 244 | + .Times(1) |
| 245 | + .WillOnce(Return(100)); |
| 246 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 200)) |
| 247 | + .Times(1) |
| 248 | + .WillOnce(SetErrnoAndReturn(EAGAIN, 0)); |
| 249 | + EXPECT_CALL(*mockfs, mockwrite(_, _, 200)) |
| 250 | + .Times(1) |
| 251 | + .WillOnce(Return(200)); |
| 252 | + |
| 253 | + const size_t result= my_write(42, buf, 300, 0); |
| 254 | + EXPECT_EQ(300U, result); |
| 255 | +} |
| 256 | + |
| 257 | +} |
| 258 | +#endif |
0 commit comments