blob: 4e181487cb6d5f90101abd545eb17a0f39ebd5a4 [file] [log] [blame]
John Edward Broadbent605085a2021-11-05 13:45:45 -07001#include "estoraged_conf.hpp"
2#include "sanitize.hpp"
3
4#include <sys/ioctl.h>
5
6#include <stdplus/fd/managed.hpp>
7#include <xyz/openbmc_project/Common/error.hpp>
8
9#include <string>
10#include <string_view>
11
12#include <gmock/gmock-matchers.h>
13#include <gmock/gmock.h>
14#include <gtest/gtest.h>
15
16namespace estoraged_test
17{
18
19using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
20using ::testing::_;
21using ::testing::Return;
22
23class IOCTLWrapperMock : public estoraged::IOCTLWrapperInterface
24{
25 public:
26 MOCK_METHOD(int, doIoctl,
27 (std::string_view devPath, unsigned long request,
28 struct mmc_ioc_cmd idata),
29 (override));
30
31 MOCK_METHOD(int, doIoctlMulti,
32 (std::string_view devPath, unsigned long request,
33 struct estoraged::mmc_io_multi_cmd_erase),
34 (override));
35};
36
37// mock ioctl returns 0, and everything passes
38TEST(Sanitize, Successful)
39{
40 std::unique_ptr<IOCTLWrapperMock> mockIOCTL =
41 std::make_unique<IOCTLWrapperMock>();
42 IOCTLWrapperMock* mockPtr = mockIOCTL.get();
43 estoraged::Sanitize goodSanitize("/dev/null", std::move(mockIOCTL));
44 EXPECT_CALL(*mockPtr, doIoctl(_, _, _)).WillRepeatedly(Return(0));
45 EXPECT_CALL(*mockPtr, doIoctlMulti(_, _, _)).WillRepeatedly(Return(0));
46 EXPECT_NO_THROW(goodSanitize.doSanitize(52428800));
47}
48
49// doIoctlMulti returns -1, and Sanitize::emmcErase throws InternalFailure
50TEST(Sanitize, Sanitize_emmcErase)
51{
52 std::unique_ptr<IOCTLWrapperMock> mockIOCTL =
53 std::make_unique<IOCTLWrapperMock>();
54 IOCTLWrapperMock* mockPtr = mockIOCTL.get();
55 estoraged::Sanitize emptySanitize("", std::move(mockIOCTL));
56 EXPECT_CALL(*mockPtr, doIoctlMulti(_, _, _)).WillRepeatedly(Return(-1));
57 EXPECT_THROW(emptySanitize.doSanitize(4000000000), InternalFailure);
58}
59
60// mock ioctl returns 1, and emmcSanitize throws
61TEST(Sanitize, Sanitize_emmcSanitize)
62{
63 std::unique_ptr<IOCTLWrapperMock> mockIOCTL =
64 std::make_unique<IOCTLWrapperMock>();
65 IOCTLWrapperMock* mockPtr = mockIOCTL.get();
66 estoraged::Sanitize ioctlSanitize("/dev/null", std::move(mockIOCTL));
67 EXPECT_CALL(*mockPtr, doIoctl(_, _, _)).WillRepeatedly(Return(1));
68 EXPECT_CALL(*mockPtr, doIoctlMulti(_, _, _)).WillRepeatedly(Return(0));
69 EXPECT_THROW(ioctlSanitize.doSanitize(4000000000), InternalFailure);
70}
71
72} // namespace estoraged_test