blob: b14d5466f683c98466c8f9431e816b70a2421e5c [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
46 * size of the drive is not divisable by the block size
47 */
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});
129 auto restOfData =
130 std::vector<std::byte>(size - shortSize * 3, std::byte{0});
131 stdplus::fd::FdMock mock;
132
133 // test write zeros with short blocks
134 EXPECT_CALL(mock, write(_))
135 .WillOnce(Return(shortData))
136 .WillOnce(Return(shortData))
137 .WillOnce(Return(restOfData))
138 .WillOnce(Return(shortData));
139
140 EXPECT_NO_THROW(pass.writeZero(size, mock));
141
142 // test read zeros with short blocks
143 EXPECT_CALL(mock, read(_))
144 .WillOnce(Return(shortData))
145 .WillOnce(Return(shortData))
146 .WillOnce(Return(restOfData))
147 .WillOnce(Return(shortData));
148
149 EXPECT_NO_THROW(pass.verifyZero(size, mock));
150}
151
152TEST(Zeros, shortReadWriteFail)
153{
154 std::string testFileName = "testfile_shortRead";
155
156 uint64_t size = 4096;
157 size_t shortSize = 128;
158 Zero tryZero(testFileName);
159 auto shortData = std::vector<std::byte>(shortSize, std::byte{0});
160 auto restOfData =
161 std::vector<std::byte>(size - shortSize * 3, std::byte{0});
162 // open the file and write none zero to it
163
164 stdplus::fd::FdMock mock;
165
166 // test writes
167 EXPECT_CALL(mock, write(_))
168 .WillOnce(Return(shortData))
169 .WillOnce(Return(shortData))
170 .WillOnce(Return(restOfData))
171 .WillOnce(Return(restOfData)); // return too much data!
172
173 EXPECT_THROW(tryZero.writeZero(size, mock), InternalFailure);
174
175 // test reads
176 EXPECT_CALL(mock, read(_))
177 .WillOnce(Return(shortData))
178 .WillOnce(Return(shortData))
179 .WillOnce(Return(restOfData))
180 .WillOnce(Return(restOfData)); // return too much data!
181
182 EXPECT_THROW(tryZero.verifyZero(size, mock), InternalFailure);
183}
184
185TEST(Zeros, driveIsSmaller)
186{
187 std::string testFileName = "testfile_driveIsSmaller";
188
189 uint64_t size = 4096;
190 Zero tryZero(testFileName);
191
192 stdplus::fd::FdMock mocks;
193 testing::InSequence s;
194
195 // test writes
196 EXPECT_CALL(mocks, write(_))
197 .Times(33)
198 .WillRepeatedly(Return(std::vector<std::byte>{}))
199 .RetiresOnSaturation();
200 EXPECT_THROW(tryZero.writeZero(size, mocks), InternalFailure);
201 // test reads
202 EXPECT_CALL(mocks, read(_))
203 .Times(33)
204 .WillRepeatedly(Return(std::vector<std::byte>{}))
205 .RetiresOnSaturation();
206 EXPECT_THROW(tryZero.verifyZero(size, mocks), InternalFailure);
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800207}
208
209} // namespace estoraged_test