blob: 49809f8ee2be0229e1d02625c02a96042f950954 [file] [log] [blame]
Patrick Venture9e5aab22018-11-09 20:49:28 -08001#include "data_mock.hpp"
2#include "firmware_handler.hpp"
3#include "image_mock.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -07004#include "util.hpp"
Patrick Venture9e5aab22018-11-09 20:49:28 -08005
Patrick Venture4eb55952018-11-16 15:36:24 -08006#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venture9e5aab22018-11-09 20:49:28 -08007#include <vector>
8
9#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
12namespace blobs
13{
14using ::testing::Eq;
15using ::testing::Return;
16using ::testing::StrEq;
17
18TEST(FirmwareHandlerDeleteTest, DeleteActiveHashSucceeds)
19{
20 /* Delete active image succeeds. */
21 DataHandlerMock dataMock;
22 ImageHandlerMock imageMock;
23
24 std::vector<HandlerPack> blobs = {
Patrick Venture7dad86f2019-05-17 08:52:20 -070025 {hashBlobId, &imageMock},
Patrick Venture9e5aab22018-11-09 20:49:28 -080026 {"asdf", &imageMock},
27 };
28 std::vector<DataHandlerPack> data = {
29 {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
30 {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
31 };
32
Patrick Venture4eb55952018-11-16 15:36:24 -080033 sdbusplus::SdBusMock sdbus_mock;
34 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
35
36 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
37 std::move(bus_mock), blobs, data);
Patrick Venture9e5aab22018-11-09 20:49:28 -080038
Patrick Venture7dad86f2019-05-17 08:52:20 -070039 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture9e5aab22018-11-09 20:49:28 -080040
41 EXPECT_TRUE(handler->open(
42 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070043 hashBlobId));
Patrick Venture9e5aab22018-11-09 20:49:28 -080044
45 /* The active hash blob_id was added. */
46 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080047 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture9e5aab22018-11-09 20:49:28 -080048 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070049 activeHashBlobId));
Patrick Venture9e5aab22018-11-09 20:49:28 -080050
51 /* Set up close() expectations. */
52 EXPECT_CALL(imageMock, close());
53 EXPECT_TRUE(handler->close(0));
54
55 currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080056 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture9e5aab22018-11-09 20:49:28 -080057 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070058 activeHashBlobId));
Patrick Venture9e5aab22018-11-09 20:49:28 -080059
60 /* Delete the blob. */
Patrick Venture7dad86f2019-05-17 08:52:20 -070061 EXPECT_TRUE(handler->deleteBlob(activeHashBlobId));
Patrick Venture9e5aab22018-11-09 20:49:28 -080062
63 currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080064 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture9e5aab22018-11-09 20:49:28 -080065 EXPECT_EQ(0, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070066 activeHashBlobId));
Patrick Venture9e5aab22018-11-09 20:49:28 -080067}
68
69} // namespace blobs