1 #include "cleanup.hpp"
2 #include "filesystem_mock.hpp"
3 #include "util.hpp"
4 
5 #include <blobs-ipmid/blobs.hpp>
6 
7 #include <cstdint>
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include <gtest/gtest.h>
13 
14 namespace ipmi_flash
15 {
16 namespace
17 {
18 
19 using ::testing::Return;
20 using ::testing::UnorderedElementsAreArray;
21 
22 class CleanupHandlerTest : public ::testing::Test
23 {
24   protected:
CleanupHandlerTest()25     CleanupHandlerTest() : mock(std::make_unique<FileSystemMock>())
26     {
27         mock_ptr = mock.get();
28         handler = std::make_unique<FileCleanupHandler>(cleanupBlobId, blobs,
29                                                        std::move(mock));
30     }
31 
32     std::vector<std::string> blobs = {"abcd", "efgh"};
33     std::unique_ptr<FileSystemMock> mock;
34     FileSystemMock* mock_ptr;
35     std::unique_ptr<FileCleanupHandler> handler;
36 };
37 
TEST_F(CleanupHandlerTest,GetBlobListReturnsExpectedList)38 TEST_F(CleanupHandlerTest, GetBlobListReturnsExpectedList)
39 {
40     EXPECT_TRUE(handler->canHandleBlob(cleanupBlobId));
41     EXPECT_THAT(handler->getBlobIds(),
42                 UnorderedElementsAreArray({cleanupBlobId}));
43 }
44 
TEST_F(CleanupHandlerTest,CommitShouldDeleteFiles)45 TEST_F(CleanupHandlerTest, CommitShouldDeleteFiles)
46 {
47     EXPECT_CALL(*mock_ptr, remove("abcd")).WillOnce(Return());
48     EXPECT_CALL(*mock_ptr, remove("efgh")).WillOnce(Return());
49 
50     EXPECT_TRUE(handler->commit(1, {}));
51 }
52 
TEST_F(CleanupHandlerTest,VerifyDefaultBlobMethods)53 TEST_F(CleanupHandlerTest, VerifyDefaultBlobMethods)
54 {
55     // Test each of the blob handler commands.
56     EXPECT_TRUE(handler->open(/*session*/ 0, /*flags*/ 0, "abcd"));
57     EXPECT_TRUE(handler->close(/*session*/ 0));
58     EXPECT_TRUE(handler->expire(/*session*/ 0));
59     EXPECT_FALSE(handler->deleteBlob("abcd"));
60 
61     blobs::BlobMeta meta;
62     EXPECT_FALSE(handler->stat("abcd", &meta));
63     EXPECT_FALSE(handler->stat(/*session*/ 0, &meta));
64 
65     EXPECT_THAT(handler->read(/*session*/ 0, /*offset*/ 0, 1),
66                 ::testing::IsEmpty());
67 
68     std::vector<uint8_t> data = {0x01};
69     EXPECT_FALSE(handler->write(/*session*/ 0, /*offset*/ 0, data));
70 }
71 
72 } // namespace
73 } // namespace ipmi_flash
74