blob: 70a62e2f536af2bebbb5c4a1ae047eae27f59930 [file] [log] [blame]
John Edward Broadbent4bc8a102021-12-30 16:11:49 -08001#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 Broadbent69786762022-01-21 14:16:23 -080011#include <fstream>
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080012#include <system_error>
13
14#include <gmock/gmock-matchers.h>
15#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17
18using estoraged::Zero;
19using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
20using stdplus::fd::ManagedFd;
21
22namespace estoraged_test
23{
24
25TEST(Zeros, zeroPass)
26{
John Edward Broadbent69786762022-01-21 14:16:23 -080027 std::string testFileName = "testfile_pass";
28 std::ofstream testFile;
29
30 testFile.open(testFileName,
31 std::ios::out | std::ios::binary | std::ios::trunc);
32 testFile.close();
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080033 uint64_t size = 4096;
John Edward Broadbent69786762022-01-21 14:16:23 -080034 Zero pass(testFileName);
35 EXPECT_NO_THROW(pass.writeZero(size));
36 EXPECT_NO_THROW(pass.verifyZero(size));
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080037}
38
39/* This test that pattern writes the correct number of bytes even if
40 * size of the drive is not divisable by the block size
41 */
42TEST(Zeros, notDivisible)
43{
John Edward Broadbent69786762022-01-21 14:16:23 -080044 std::string testFileName = "testfile_notDivisible";
45 std::ofstream testFile;
46
47 testFile.open(testFileName,
48 std::ios::out | std::ios::binary | std::ios::trunc);
49 testFile.close();
50
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080051 uint64_t size = 4097;
52 // 4097 is not divisible by the block size, and we expect no errors
John Edward Broadbent69786762022-01-21 14:16:23 -080053 Zero pass(testFileName);
54 EXPECT_NO_THROW(pass.writeZero(size));
55 EXPECT_NO_THROW(pass.verifyZero(size));
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080056}
57
58TEST(Zeros, notZeroStart)
59{
John Edward Broadbent69786762022-01-21 14:16:23 -080060 std::string testFileName = "testfile_notZeroStart";
61 std::ofstream testFile;
62
63 // open the file and write none zero to it
64 int dummyValue = 0x88776655;
65 testFile.open(testFileName, std::ios::binary | std::ios::out);
66 testFile.write((const char*)&dummyValue, sizeof(dummyValue));
67 testFile.close();
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080068 uint64_t size = 4096;
John Edward Broadbent69786762022-01-21 14:16:23 -080069 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 Broadbent4bc8a102021-12-30 16:11:49 -080078}
79
80TEST(Zeros, notZeroEnd)
81{
John Edward Broadbent69786762022-01-21 14:16:23 -080082 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 Broadbent4bc8a102021-12-30 16:11:49 -080089 uint64_t size = 4096;
John Edward Broadbent69786762022-01-21 14:16:23 -080090 Zero pass(testFileName);
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080091 int dummyValue = 88;
John Edward Broadbent69786762022-01-21 14:16:23 -080092 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 Broadbent4bc8a102021-12-30 16:11:49 -0800100}
101
102} // namespace estoraged_test