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 | |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 11 | #include <fstream> |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 12 | #include <system_error> |
| 13 | |
| 14 | #include <gmock/gmock-matchers.h> |
| 15 | #include <gmock/gmock.h> |
| 16 | #include <gtest/gtest.h> |
| 17 | |
John Edward Broadbent | d3bfa7b | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 18 | namespace estoraged_test |
| 19 | { |
| 20 | |
| 21 | using estoraged::Pattern; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 22 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 23 | |
| 24 | TEST(pattern, patternPass) |
| 25 | { |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 26 | std::string testFileName = "patternPass"; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 27 | uint64_t size = 4096; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 28 | std::ofstream testFile; |
| 29 | testFile.open(testFileName, |
| 30 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 31 | testFile.close(); |
| 32 | |
| 33 | Pattern pass(testFileName); |
| 34 | EXPECT_NO_THROW(pass.writePattern(size)); |
| 35 | EXPECT_NO_THROW(pass.verifyPattern(size)); |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* This test that pattern writes the correct number of bytes even if |
| 39 | * size of the drive is not divisable by the block size |
| 40 | */ |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 41 | TEST(pattern, patternNotDivisible) |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 42 | { |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 43 | std::string testFileName = "notDivisible"; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 44 | uint64_t size = 4097; |
| 45 | // 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] | 46 | std::ofstream testFile; |
| 47 | testFile.open(testFileName, |
| 48 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 49 | testFile.close(); |
| 50 | |
| 51 | Pattern pass(testFileName); |
| 52 | EXPECT_NO_THROW(pass.writePattern(size)); |
| 53 | EXPECT_NO_THROW(pass.verifyPattern(size)); |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | TEST(pattern, patternsDontMatch) |
| 57 | { |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 58 | std::string testFileName = "patternsDontMatch"; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 59 | uint64_t size = 4096; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 60 | std::ofstream testFile; |
| 61 | |
| 62 | Pattern pass(testFileName); |
| 63 | |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 64 | int dummyValue = 88; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 65 | testFile.open(testFileName, std::ios::binary | std::ios::out); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 66 | testFile.write((reinterpret_cast<const char*>(&dummyValue)), |
| 67 | sizeof(dummyValue)); |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 68 | testFile.close(); |
| 69 | |
| 70 | EXPECT_NO_THROW(pass.writePattern(size - sizeof(dummyValue))); |
| 71 | EXPECT_THROW(pass.verifyPattern(size), InternalFailure); |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 72 | } |
John Edward Broadbent | d3bfa7b | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 73 | |
| 74 | } // namespace estoraged_test |