John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 1 | #include "estoraged_conf.hpp" |
| 2 | #include "zero.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 | 4bc8a10 | 2021-12-30 16:11:49 -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 | |
| 18 | using estoraged::Zero; |
| 19 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 20 | |
| 21 | namespace estoraged_test |
| 22 | { |
| 23 | |
| 24 | TEST(Zeros, zeroPass) |
| 25 | { |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 26 | std::string testFileName = "testfile_pass"; |
| 27 | std::ofstream testFile; |
| 28 | |
| 29 | testFile.open(testFileName, |
| 30 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 31 | testFile.close(); |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 32 | uint64_t size = 4096; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 33 | Zero pass(testFileName); |
| 34 | EXPECT_NO_THROW(pass.writeZero(size)); |
| 35 | EXPECT_NO_THROW(pass.verifyZero(size)); |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -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 | */ |
| 41 | TEST(Zeros, notDivisible) |
| 42 | { |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 43 | std::string testFileName = "testfile_notDivisible"; |
| 44 | std::ofstream testFile; |
| 45 | |
| 46 | testFile.open(testFileName, |
| 47 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 48 | testFile.close(); |
| 49 | |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 50 | uint64_t size = 4097; |
| 51 | // 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] | 52 | Zero pass(testFileName); |
| 53 | EXPECT_NO_THROW(pass.writeZero(size)); |
| 54 | EXPECT_NO_THROW(pass.verifyZero(size)); |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | TEST(Zeros, notZeroStart) |
| 58 | { |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 59 | std::string testFileName = "testfile_notZeroStart"; |
| 60 | std::ofstream testFile; |
| 61 | |
| 62 | // open the file and write none zero to it |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 63 | uint32_t dummyValue = 0x88776655; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 64 | testFile.open(testFileName, std::ios::binary | std::ios::out); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 65 | testFile.write((reinterpret_cast<const char*>(&dummyValue)), |
| 66 | sizeof(dummyValue)); |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 67 | testFile.close(); |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 68 | uint64_t size = 4096; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 69 | Zero pass(testFileName); |
| 70 | EXPECT_NO_THROW(pass.writeZero(size - sizeof(dummyValue))); |
| 71 | |
| 72 | // force flush, needed for unit testing |
| 73 | std::ofstream file; |
| 74 | file.open(testFileName); |
| 75 | file.close(); |
| 76 | |
| 77 | EXPECT_THROW(pass.verifyZero(size), InternalFailure); |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST(Zeros, notZeroEnd) |
| 81 | { |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 82 | std::string testFileName = "testfile_notZeroEnd"; |
| 83 | std::ofstream testFile; |
| 84 | |
| 85 | testFile.open(testFileName, |
| 86 | std::ios::out | std::ios::binary | std::ios::trunc); |
| 87 | testFile.close(); |
| 88 | |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 89 | uint64_t size = 4096; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 90 | Zero pass(testFileName); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 91 | uint32_t dummyValue = 88; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 92 | EXPECT_NO_THROW(pass.writeZero(size - sizeof(dummyValue))); |
| 93 | |
| 94 | // open the file and write none zero to it |
| 95 | testFile.open(testFileName, std::ios::out); |
| 96 | testFile << dummyValue; |
| 97 | testFile.close(); |
| 98 | |
| 99 | EXPECT_THROW(pass.verifyZero(size), InternalFailure); |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | } // namespace estoraged_test |