blob: a681d6d273f0d01c5ee0d8de81e921786cda8e87 [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>
John Edward Broadbentd6071fc2022-03-31 19:33:21 -07008#include <stdplus/fd/gmock.hpp>
John Edward Broadbent4bc8a102021-12-30 16:11:49 -08009#include <stdplus/fd/managed.hpp>
10#include <xyz/openbmc_project/Common/error.hpp>
11
John Edward Broadbent69786762022-01-21 14:16:23 -080012#include <fstream>
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080013#include <system_error>
14
15#include <gmock/gmock-matchers.h>
16#include <gmock/gmock.h>
17#include <gtest/gtest.h>
18
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080019namespace estoraged_test
20{
21
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070022using estoraged::Zero;
23using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
24using testing::_;
25using testing::Return;
26
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080027TEST(Zeros, zeroPass)
28{
John Edward Broadbent69786762022-01-21 14:16:23 -080029 std::string testFileName = "testfile_pass";
30 std::ofstream testFile;
31
32 testFile.open(testFileName,
33 std::ios::out | std::ios::binary | std::ios::trunc);
34 testFile.close();
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080035 uint64_t size = 4096;
John Edward Broadbent69786762022-01-21 14:16:23 -080036 Zero pass(testFileName);
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070037 stdplus::fd::Fd&& write =
38 stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::ReadWrite);
39 EXPECT_NO_THROW(pass.writeZero(size, write));
40 stdplus::fd::Fd&& read =
41 stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::ReadWrite);
42 EXPECT_NO_THROW(pass.verifyZero(size, read));
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080043}
44
45/* This test that pattern writes the correct number of bytes even if
Manojkiran Edad4554f22024-06-17 14:11:30 +053046 * size of the drive is not divisible by the block size
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080047 */
48TEST(Zeros, notDivisible)
49{
John Edward Broadbent69786762022-01-21 14:16:23 -080050 std::string testFileName = "testfile_notDivisible";
51 std::ofstream testFile;
52
53 testFile.open(testFileName,
54 std::ios::out | std::ios::binary | std::ios::trunc);
55 testFile.close();
56
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080057 uint64_t size = 4097;
58 // 4097 is not divisible by the block size, and we expect no errors
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070059
60 stdplus::fd::Fd&& write =
61 stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::ReadWrite);
John Edward Broadbent69786762022-01-21 14:16:23 -080062 Zero pass(testFileName);
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070063 EXPECT_NO_THROW(pass.writeZero(size, write));
64 stdplus::fd::Fd&& read =
65 stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::ReadWrite);
66 EXPECT_NO_THROW(pass.verifyZero(size, read));
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080067}
68
69TEST(Zeros, notZeroStart)
70{
John Edward Broadbent69786762022-01-21 14:16:23 -080071 std::string testFileName = "testfile_notZeroStart";
72 std::ofstream testFile;
73
74 // open the file and write none zero to it
Ed Tanous82897c32022-02-21 14:11:59 -080075 uint32_t dummyValue = 0x88776655;
John Edward Broadbent69786762022-01-21 14:16:23 -080076 testFile.open(testFileName, std::ios::binary | std::ios::out);
Ed Tanous82897c32022-02-21 14:11:59 -080077 testFile.write((reinterpret_cast<const char*>(&dummyValue)),
78 sizeof(dummyValue));
John Edward Broadbent69786762022-01-21 14:16:23 -080079 testFile.close();
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080080 uint64_t size = 4096;
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070081 stdplus::fd::Fd&& readWrite =
82 stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::ReadWrite);
83
John Edward Broadbent69786762022-01-21 14:16:23 -080084 Zero pass(testFileName);
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070085 EXPECT_NO_THROW(pass.writeZero(size - sizeof(dummyValue), readWrite));
John Edward Broadbent69786762022-01-21 14:16:23 -080086
87 // force flush, needed for unit testing
88 std::ofstream file;
89 file.open(testFileName);
90 file.close();
91
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070092 EXPECT_THROW(pass.verifyZero(size, readWrite), InternalFailure);
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080093}
94
95TEST(Zeros, notZeroEnd)
96{
John Edward Broadbent69786762022-01-21 14:16:23 -080097 std::string testFileName = "testfile_notZeroEnd";
98 std::ofstream testFile;
99
100 testFile.open(testFileName,
101 std::ios::out | std::ios::binary | std::ios::trunc);
102 testFile.close();
103
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800104 uint64_t size = 4096;
John Edward Broadbent69786762022-01-21 14:16:23 -0800105 Zero pass(testFileName);
Ed Tanous82897c32022-02-21 14:11:59 -0800106 uint32_t dummyValue = 88;
John Edward Broadbentd6071fc2022-03-31 19:33:21 -0700107 stdplus::fd::Fd&& write =
108 stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::ReadWrite);
109 EXPECT_NO_THROW(pass.writeZero(size - sizeof(dummyValue), write));
John Edward Broadbent69786762022-01-21 14:16:23 -0800110
111 // open the file and write none zero to it
112 testFile.open(testFileName, std::ios::out);
113 testFile << dummyValue;
114 testFile.close();
115
John Edward Broadbentd6071fc2022-03-31 19:33:21 -0700116 stdplus::fd::Fd&& read =
117 stdplus::fd::open(testFileName, stdplus::fd::OpenAccess::ReadWrite);
118 EXPECT_THROW(pass.verifyZero(size, read), InternalFailure);
119}
120
121TEST(Zeros, shortReadWritePass)
122{
123 std::string testFileName = "testfile_shortRead";
124
125 uint64_t size = 4096;
126 size_t shortSize = 128;
127 Zero pass(testFileName);
128 auto shortData = std::vector<std::byte>(shortSize, std::byte{0});
Patrick Williams15b63e12024-08-16 15:22:01 -0400129 auto restOfData =
130 std::vector<std::byte>(size - shortSize * 3, std::byte{0});
John Wedigbe47c8f2022-09-30 16:40:08 -0700131 std::span shortDataSpan{shortData};
132 std::span restOfDataSpan{restOfData};
John Edward Broadbentd6071fc2022-03-31 19:33:21 -0700133 stdplus::fd::FdMock mock;
134
135 // test write zeros with short blocks
136 EXPECT_CALL(mock, write(_))
John Wedigbe47c8f2022-09-30 16:40:08 -0700137 .WillOnce(Return(shortDataSpan))
138 .WillOnce(Return(shortDataSpan))
139 .WillOnce(Return(restOfDataSpan))
140 .WillOnce(Return(shortDataSpan));
John Edward Broadbentd6071fc2022-03-31 19:33:21 -0700141
142 EXPECT_NO_THROW(pass.writeZero(size, mock));
143
144 // test read zeros with short blocks
145 EXPECT_CALL(mock, read(_))
John Wedigbe47c8f2022-09-30 16:40:08 -0700146 .WillOnce(Return(shortDataSpan))
147 .WillOnce(Return(shortDataSpan))
148 .WillOnce(Return(restOfDataSpan))
149 .WillOnce(Return(shortDataSpan));
John Edward Broadbentd6071fc2022-03-31 19:33:21 -0700150
151 EXPECT_NO_THROW(pass.verifyZero(size, mock));
152}
153
154TEST(Zeros, shortReadWriteFail)
155{
156 std::string testFileName = "testfile_shortRead";
157
158 uint64_t size = 4096;
159 size_t shortSize = 128;
160 Zero tryZero(testFileName);
161 auto shortData = std::vector<std::byte>(shortSize, std::byte{0});
Patrick Williams15b63e12024-08-16 15:22:01 -0400162 auto restOfData =
163 std::vector<std::byte>(size - shortSize * 3, std::byte{0});
John Wedigbe47c8f2022-09-30 16:40:08 -0700164 std::span shortDataSpan{shortData};
165 std::span restOfDataSpan{restOfData};
John Edward Broadbentd6071fc2022-03-31 19:33:21 -0700166 // open the file and write none zero to it
167
168 stdplus::fd::FdMock mock;
169
170 // test writes
171 EXPECT_CALL(mock, write(_))
John Wedigbe47c8f2022-09-30 16:40:08 -0700172 .WillOnce(Return(shortDataSpan))
173 .WillOnce(Return(shortDataSpan))
174 .WillOnce(Return(restOfDataSpan))
175 .WillOnce(Return(restOfDataSpan)); // return too much data!
John Edward Broadbentd6071fc2022-03-31 19:33:21 -0700176
177 EXPECT_THROW(tryZero.writeZero(size, mock), InternalFailure);
178
179 // test reads
180 EXPECT_CALL(mock, read(_))
John Wedigbe47c8f2022-09-30 16:40:08 -0700181 .WillOnce(Return(shortDataSpan))
182 .WillOnce(Return(shortDataSpan))
183 .WillOnce(Return(restOfDataSpan))
184 .WillOnce(Return(restOfDataSpan)); // return too much data!
John Edward Broadbentd6071fc2022-03-31 19:33:21 -0700185
186 EXPECT_THROW(tryZero.verifyZero(size, mock), InternalFailure);
187}
188
189TEST(Zeros, driveIsSmaller)
190{
191 std::string testFileName = "testfile_driveIsSmaller";
192
193 uint64_t size = 4096;
194 Zero tryZero(testFileName);
195
196 stdplus::fd::FdMock mocks;
197 testing::InSequence s;
198
199 // test writes
200 EXPECT_CALL(mocks, write(_))
201 .Times(33)
John Wedigbe47c8f2022-09-30 16:40:08 -0700202 .WillRepeatedly(Return(std::span<std::byte>{}))
John Edward Broadbentd6071fc2022-03-31 19:33:21 -0700203 .RetiresOnSaturation();
204 EXPECT_THROW(tryZero.writeZero(size, mocks), InternalFailure);
205 // test reads
206 EXPECT_CALL(mocks, read(_))
207 .Times(33)
John Wedigbe47c8f2022-09-30 16:40:08 -0700208 .WillRepeatedly(Return(std::span<std::byte>{}))
John Edward Broadbentd6071fc2022-03-31 19:33:21 -0700209 .RetiresOnSaturation();
210 EXPECT_THROW(tryZero.verifyZero(size, mocks), InternalFailure);
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800211}
212
213} // namespace estoraged_test