blob: de67a152eb528ba9dfc38e443272ff2a92fb87fa [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 Venture3ecb3502019-05-17 11:03:51 -07005#include "verification_mock.hpp"
Patrick Venture9e5aab22018-11-09 20:49:28 -08006
7#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 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
Patrick Venture3ecb3502019-05-17 11:03:51 -070034 blobs, data, CreateVerifyMock());
Patrick Venture9e5aab22018-11-09 20:49:28 -080035
Patrick Venture7dad86f2019-05-17 08:52:20 -070036 EXPECT_CALL(imageMock, open(StrEq(hashBlobId))).WillOnce(Return(true));
Patrick Venture9e5aab22018-11-09 20:49:28 -080037
38 EXPECT_TRUE(handler->open(
39 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
Patrick Venture7dad86f2019-05-17 08:52:20 -070040 hashBlobId));
Patrick Venture9e5aab22018-11-09 20:49:28 -080041
42 /* The active hash blob_id was added. */
43 auto currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080044 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture9e5aab22018-11-09 20:49:28 -080045 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070046 activeHashBlobId));
Patrick Venture9e5aab22018-11-09 20:49:28 -080047
48 /* Set up close() expectations. */
49 EXPECT_CALL(imageMock, close());
50 EXPECT_TRUE(handler->close(0));
51
52 currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080053 EXPECT_EQ(4, currentBlobs.size());
Patrick Venture9e5aab22018-11-09 20:49:28 -080054 EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070055 activeHashBlobId));
Patrick Venture9e5aab22018-11-09 20:49:28 -080056
57 /* Delete the blob. */
Patrick Venture7dad86f2019-05-17 08:52:20 -070058 EXPECT_TRUE(handler->deleteBlob(activeHashBlobId));
Patrick Venture9e5aab22018-11-09 20:49:28 -080059
60 currentBlobs = handler->getBlobIds();
Patrick Ventureffcc5502018-11-16 12:32:38 -080061 EXPECT_EQ(3, currentBlobs.size());
Patrick Venture9e5aab22018-11-09 20:49:28 -080062 EXPECT_EQ(0, std::count(currentBlobs.begin(), currentBlobs.end(),
Patrick Venture7dad86f2019-05-17 08:52:20 -070063 activeHashBlobId));
Patrick Venture9e5aab22018-11-09 20:49:28 -080064}
65
66} // namespace blobs