John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 1 | #include "estoraged_conf.hpp" |
| 2 | #include "pattern.hpp" |
| 3 | |
| 4 | #include <fcntl.h> |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | #include <stdplus/fd/create.hpp> |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 8 | #include <stdplus/fd/gmock.hpp> |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 9 | #include <stdplus/fd/managed.hpp> |
| 10 | #include <xyz/openbmc_project/Common/error.hpp> |
| 11 | |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 12 | #include <fstream> |
John Edward Broadbent | 4906f4e | 2022-04-28 17:15:18 -0700 | [diff] [blame] | 13 | #include <span> |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 14 | #include <system_error> |
| 15 | |
| 16 | #include <gmock/gmock-matchers.h> |
| 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | |
John Edward Broadbent | d3bfa7b | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 20 | namespace estoraged_test |
| 21 | { |
| 22 | |
| 23 | using estoraged::Pattern; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 24 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 25 | using testing::_; |
| 26 | using testing::Invoke; |
| 27 | using testing::Return; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 28 | |
| 29 | TEST(pattern, patternPass) |
| 30 | { |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 31 | std::string testFileName = "patternPass"; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 32 | uint64_t size = 4096; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 33 | std::ofstream testFile; |
| 34 | testFile.open(testFileName, |
| 35 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 36 | testFile.close(); |
| 37 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 38 | stdplus::fd::Fd&& writeFd = |
| 39 | stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::WriteOnly); |
| 40 | |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 41 | Pattern pass(testFileName); |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 42 | EXPECT_NO_THROW(pass.writePattern(size, writeFd)); |
| 43 | |
| 44 | stdplus::fd::Fd&& readFd = |
| 45 | stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::ReadOnly); |
| 46 | |
| 47 | EXPECT_NO_THROW(pass.verifyPattern(size, readFd)); |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* This test that pattern writes the correct number of bytes even if |
Manojkiran Eda | d4554f2 | 2024-06-17 14:11:30 +0530 | [diff] [blame] | 51 | * size of the drive is not divisible by the block size |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 52 | */ |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 53 | |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 54 | TEST(pattern, patternNotDivisible) |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 55 | { |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 56 | std::string testFileName = "notDivisible"; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 57 | uint64_t size = 4097; |
| 58 | // 4097 is not divisible by the block size, and we expect no errors |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 59 | std::ofstream testFile; |
| 60 | testFile.open(testFileName, |
| 61 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 62 | testFile.close(); |
| 63 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 64 | stdplus::fd::Fd&& writeFd = |
| 65 | stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::WriteOnly); |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 66 | Pattern pass(testFileName); |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 67 | EXPECT_NO_THROW(pass.writePattern(size, writeFd)); |
| 68 | |
| 69 | stdplus::fd::Fd&& readFd = |
| 70 | stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::ReadOnly); |
| 71 | EXPECT_NO_THROW(pass.verifyPattern(size, readFd)); |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TEST(pattern, patternsDontMatch) |
| 75 | { |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 76 | std::string testFileName = "patternsDontMatch"; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 77 | uint64_t size = 4096; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 78 | std::ofstream testFile; |
| 79 | |
| 80 | Pattern pass(testFileName); |
| 81 | |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 82 | int dummyValue = 88; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 83 | testFile.open(testFileName, std::ios::binary | std::ios::out); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 84 | testFile.write((reinterpret_cast<const char*>(&dummyValue)), |
| 85 | sizeof(dummyValue)); |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 86 | testFile.close(); |
| 87 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 88 | stdplus::fd::Fd&& writeFd = |
| 89 | stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::WriteOnly); |
| 90 | |
| 91 | EXPECT_NO_THROW(pass.writePattern(size - sizeof(dummyValue), writeFd)); |
| 92 | |
| 93 | stdplus::fd::Fd&& readFd = |
| 94 | stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::ReadOnly); |
| 95 | |
| 96 | EXPECT_THROW(pass.verifyPattern(size, readFd), InternalFailure); |
| 97 | } |
| 98 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 99 | TEST(pattern, shortReadWritePass) |
| 100 | { |
| 101 | std::string testFileName = "testfile_shortRead"; |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 102 | uint64_t size = 4096; |
John Edward Broadbent | 34a75f0 | 2022-04-14 18:59:40 -0700 | [diff] [blame] | 103 | size_t shortSize = 128; |
| 104 | static auto shortData1 = std::vector<std::byte>(shortSize); |
| 105 | static auto shortData2 = std::vector<std::byte>(shortSize); |
| 106 | static auto restOfData = std::vector<std::byte>(size - shortSize * 2); |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 107 | Pattern pass(testFileName); |
| 108 | stdplus::fd::FdMock mock; |
| 109 | |
| 110 | testing::Sequence s; |
| 111 | |
| 112 | // test write pattern with short blocks |
| 113 | EXPECT_CALL(mock, write(_)) |
| 114 | .WillOnce(Invoke([](std::span<const std::byte> x) { |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 115 | std::copy_n(x.begin(), shortData1.size(), shortData1.begin()); |
| 116 | return shortData1; |
| 117 | })) |
| 118 | .RetiresOnSaturation(); |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 119 | EXPECT_CALL(mock, write(_)) |
| 120 | .WillOnce(Invoke([](std::span<const std::byte> x) { |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 121 | std::copy_n(x.begin(), restOfData.size(), restOfData.begin()); |
| 122 | return restOfData; |
| 123 | })) |
| 124 | .RetiresOnSaturation(); |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 125 | EXPECT_CALL(mock, write(_)) |
| 126 | .WillOnce(Invoke([](std::span<const std::byte> x) { |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 127 | std::copy_n(x.begin(), shortData2.size(), shortData2.begin()); |
| 128 | return shortData2; |
| 129 | })) |
| 130 | .RetiresOnSaturation(); |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 131 | |
| 132 | // test read pattern |
| 133 | EXPECT_CALL(mock, read(_)) |
John Edward Broadbent | 4906f4e | 2022-04-28 17:15:18 -0700 | [diff] [blame] | 134 | .WillOnce(Invoke([](std::span<std::byte> x) { |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 135 | std::copy_n(shortData1.begin(), shortData1.size(), x.data()); |
| 136 | std::span ret(shortData1); |
| 137 | return ret; |
| 138 | })) |
| 139 | .RetiresOnSaturation(); |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 140 | |
| 141 | EXPECT_CALL(mock, read(_)) |
John Edward Broadbent | 4906f4e | 2022-04-28 17:15:18 -0700 | [diff] [blame] | 142 | .WillOnce(Invoke([](std::span<std::byte> x) { |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 143 | std::copy_n(restOfData.begin(), restOfData.size(), x.data()); |
| 144 | std::span ret(restOfData); |
| 145 | return ret; |
| 146 | })) |
| 147 | .RetiresOnSaturation(); |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 148 | |
| 149 | EXPECT_CALL(mock, read(_)) |
John Edward Broadbent | 4906f4e | 2022-04-28 17:15:18 -0700 | [diff] [blame] | 150 | .WillOnce(Invoke([](std::span<std::byte> x) { |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 151 | std::copy_n(shortData2.begin(), shortData2.size(), x.data()); |
| 152 | std::span ret(shortData2); |
| 153 | return ret; |
| 154 | })) |
| 155 | .RetiresOnSaturation(); |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 156 | |
| 157 | EXPECT_NO_THROW(pass.writePattern(size, mock)); |
| 158 | EXPECT_NO_THROW(pass.verifyPattern(size, mock)); |
| 159 | } |
| 160 | |
| 161 | TEST(Zeros, shortReadWriteFail) |
| 162 | { |
| 163 | std::string testFileName = "testfile_shortRead"; |
| 164 | |
| 165 | uint64_t size = 4096; |
| 166 | size_t shortSize = 128; |
| 167 | Pattern tryPattern(testFileName); |
| 168 | auto shortData = std::vector<std::byte>(shortSize, std::byte{0}); |
Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame^] | 169 | auto restOfData = |
| 170 | std::vector<std::byte>(size - shortSize * 3, std::byte{0}); |
John Wedig | be47c8f | 2022-09-30 16:40:08 -0700 | [diff] [blame] | 171 | std::span shortDataSpan{shortData}; |
| 172 | std::span restOfDataSpan{restOfData}; |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 173 | // open the file and write none to it |
| 174 | |
| 175 | stdplus::fd::FdMock mock; |
| 176 | |
| 177 | // test writes |
| 178 | EXPECT_CALL(mock, write(_)) |
John Wedig | be47c8f | 2022-09-30 16:40:08 -0700 | [diff] [blame] | 179 | .WillOnce(Return(shortDataSpan)) |
| 180 | .WillOnce(Return(shortDataSpan)) |
| 181 | .WillOnce(Return(restOfDataSpan)) |
| 182 | .WillOnce(Return(restOfDataSpan)); // return too much data! |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 183 | |
| 184 | EXPECT_THROW(tryPattern.writePattern(size, mock), InternalFailure); |
| 185 | |
| 186 | // test reads |
| 187 | EXPECT_CALL(mock, read(_)) |
John Wedig | be47c8f | 2022-09-30 16:40:08 -0700 | [diff] [blame] | 188 | .WillOnce(Return(shortDataSpan)) |
| 189 | .WillOnce(Return(shortDataSpan)) |
| 190 | .WillOnce(Return(restOfDataSpan)) |
| 191 | .WillOnce(Return(restOfDataSpan)); // return too much data! |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 192 | |
| 193 | EXPECT_THROW(tryPattern.verifyPattern(size, mock), InternalFailure); |
| 194 | } |
| 195 | |
| 196 | TEST(pattern, driveIsSmaller) |
| 197 | { |
| 198 | std::string testFileName = "testfile_driveIsSmaller"; |
| 199 | |
| 200 | uint64_t size = 4096; |
| 201 | Pattern tryPattern(testFileName); |
| 202 | |
| 203 | stdplus::fd::FdMock mocks; |
| 204 | testing::InSequence s; |
| 205 | |
| 206 | // test writes |
| 207 | EXPECT_CALL(mocks, write(_)) |
| 208 | .Times(33) |
John Wedig | be47c8f | 2022-09-30 16:40:08 -0700 | [diff] [blame] | 209 | .WillRepeatedly(Return(std::span<std::byte>{})) |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 210 | .RetiresOnSaturation(); |
| 211 | |
| 212 | EXPECT_THROW(tryPattern.writePattern(size, mocks), InternalFailure); |
| 213 | |
| 214 | // test reads |
| 215 | EXPECT_CALL(mocks, read(_)) |
| 216 | .Times(33) |
John Wedig | be47c8f | 2022-09-30 16:40:08 -0700 | [diff] [blame] | 217 | .WillRepeatedly(Return(std::span<std::byte>{})) |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 218 | .RetiresOnSaturation(); |
| 219 | |
| 220 | EXPECT_THROW(tryPattern.verifyPattern(size, mocks), InternalFailure); |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 221 | } |
John Edward Broadbent | d3bfa7b | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 222 | |
| 223 | } // namespace estoraged_test |