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> |
| 8 | #include <stdplus/fd/managed.hpp> |
| 9 | #include <xyz/openbmc_project/Common/error.hpp> |
| 10 | |
| 11 | #include <system_error> |
| 12 | |
| 13 | #include <gmock/gmock-matchers.h> |
| 14 | #include <gmock/gmock.h> |
| 15 | #include <gtest/gtest.h> |
| 16 | |
| 17 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 18 | using stdplus::fd::ManagedFd; |
| 19 | |
| 20 | class MockManagedFd : public ManagedFd |
| 21 | { |
| 22 | public: |
| 23 | MockManagedFd(int fd) : ManagedFd(std::move(fd)) |
| 24 | {} |
| 25 | ~MockManagedFd() |
| 26 | {} |
| 27 | }; |
| 28 | |
| 29 | TEST(pattern, patternPass) |
| 30 | { |
| 31 | uint64_t size = 4096; |
| 32 | int fds[2]; |
| 33 | Pattern pass("fileName"); |
| 34 | if (pipe(fds)) |
| 35 | { |
| 36 | FAIL(); |
| 37 | } |
| 38 | MockManagedFd writeFd(fds[1]); |
| 39 | EXPECT_NO_THROW(pass.writePattern(size, writeFd)); |
| 40 | MockManagedFd verifyFd(fds[0]); |
| 41 | EXPECT_NO_THROW(pass.verifyPattern(size, verifyFd)); |
| 42 | } |
| 43 | |
| 44 | /* This test that pattern writes the correct number of bytes even if |
| 45 | * size of the drive is not divisable by the block size |
| 46 | */ |
| 47 | TEST(pattern, patternPassNotDivisible) |
| 48 | { |
| 49 | uint64_t size = 4097; |
| 50 | // 4097 is not divisible by the block size, and we expect no errors |
| 51 | int fds[2]; |
| 52 | Pattern pass("fileName"); |
| 53 | if (pipe(fds)) |
| 54 | { |
| 55 | FAIL(); |
| 56 | } |
| 57 | MockManagedFd writeFd(fds[1]); |
| 58 | EXPECT_NO_THROW(pass.writePattern(size, writeFd)); |
| 59 | MockManagedFd verifyFd(fds[0]); |
| 60 | EXPECT_NO_THROW(pass.verifyPattern(size, verifyFd)); |
| 61 | } |
| 62 | |
| 63 | TEST(pattern, patternsDontMatch) |
| 64 | { |
| 65 | uint64_t size = 4096; |
| 66 | int fds[2]; |
| 67 | Pattern pass("fileName"); |
| 68 | if (pipe(fds)) |
| 69 | { |
| 70 | FAIL(); |
| 71 | } |
| 72 | int dummyValue = 88; |
| 73 | if (::write(fds[1], &dummyValue, sizeof(dummyValue)) != sizeof(dummyValue)) |
| 74 | { |
| 75 | FAIL(); |
| 76 | } |
| 77 | MockManagedFd writeFd(fds[1]); |
| 78 | pass.writePattern(size - sizeof(dummyValue), writeFd); |
| 79 | MockManagedFd verifyFd(fds[0]); |
| 80 | EXPECT_THROW(pass.verifyPattern(size, verifyFd), InternalFailure); |
| 81 | } |