blob: b3b91ec9b1891f5e06d4b5e2372b309fe3018db3 [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
11#include <system_error>
12
13#include <gmock/gmock-matchers.h>
14#include <gmock/gmock.h>
15#include <gtest/gtest.h>
16
17using estoraged::Zero;
18using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
19using stdplus::fd::ManagedFd;
20
21namespace estoraged_test
22{
23
24TEST(Zeros, zeroPass)
25{
26 uint64_t size = 4096;
27 int fds[2];
28 Zero pass("fileName");
29 if (pipe(fds))
30 {
31 FAIL();
32 }
33 ManagedFd writeFd(std::move(fds[1]));
34 EXPECT_NO_THROW(pass.writeZero(size, writeFd));
35 ManagedFd verifyFd(std::move(fds[0]));
36 EXPECT_NO_THROW(pass.verifyZero(size, verifyFd));
37}
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{
44 uint64_t size = 4097;
45 // 4097 is not divisible by the block size, and we expect no errors
46 int fds[2];
47 Zero pass("fileName");
48 if (pipe(fds))
49 {
50 FAIL();
51 }
52 ManagedFd writeFd(std::move(fds[1]));
53 EXPECT_NO_THROW(pass.writeZero(size, writeFd));
54 ManagedFd verifyFd(std::move(fds[0]));
55 EXPECT_NO_THROW(pass.verifyZero(size, verifyFd));
56}
57
58TEST(Zeros, notZeroStart)
59{
60 uint64_t size = 4096;
61 int fds[2];
62 Zero pass("fileName");
63 if (pipe(fds))
64 {
65 FAIL();
66 }
67 int dummyValue = 88;
68 if (::write(fds[1], &dummyValue, sizeof(dummyValue)) != sizeof(dummyValue))
69 {
70 FAIL();
71 }
72 ManagedFd writeFd(std::move(fds[1]));
73 EXPECT_NO_THROW(pass.writeZero(size - sizeof(dummyValue), writeFd));
74 ManagedFd verifyFd(std::move(fds[0]));
75 EXPECT_THROW(pass.verifyZero(size, verifyFd), InternalFailure);
76}
77
78TEST(Zeros, notZeroEnd)
79{
80 uint64_t size = 4096;
81 int fds[2];
82 Zero pass("fileName");
83 if (pipe(fds))
84 {
85 FAIL();
86 }
87 int dummyValue = 88;
88 int tmpFd = fds[1];
89 ManagedFd writeFd(std::move(tmpFd));
90 EXPECT_NO_THROW(pass.writeZero(size - sizeof(dummyValue), writeFd));
91 if (::write(fds[1], &dummyValue, sizeof(dummyValue)) != sizeof(dummyValue))
92 {
93 FAIL();
94 }
95 ManagedFd verifyFd(std::move(fds[0]));
96 EXPECT_THROW(pass.verifyZero(size, verifyFd), InternalFailure);
97}
98
99} // namespace estoraged_test