firmware: start implementing deleteBlob
If a client sends a deleteBlob, two things should happen. The active
blob is cleared and the states are all reset, effectively like an abort.
Change-Id: I54f2986b7cad9b7fc117d36a8e631e84c8e8feb8
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/firmware_handler.cpp b/firmware_handler.cpp
index 3ce65aa..3545fd2 100644
--- a/firmware_handler.cpp
+++ b/firmware_handler.cpp
@@ -81,10 +81,42 @@
/*
* Per the design, this mean abort, and this will trigger whatever
* appropriate actions are required to abort the process.
+ *
+ * You cannot delete a blob that has an open handle in the system, therefore
+ * this is never called if there's an open session. Guaranteed by the blob
+ * manager.
*/
bool FirmwareBlobHandler::deleteBlob(const std::string& path)
{
- return false;
+ const std::string* toDelete;
+
+ if (path == hashBlobID || path == activeHashBlobID)
+ {
+ /* They're deleting the hash. */
+ toDelete = &activeHashBlobID;
+ }
+ else
+ {
+ /* They're deleting the image. */
+ toDelete = &activeImageBlobID;
+ }
+
+ auto it = std::find_if(
+ blobIDs.begin(), blobIDs.end(),
+ [toDelete](const auto& iter) { return (iter == *toDelete); });
+ if (it == blobIDs.end())
+ {
+ /* Somehow they've asked to delete something we didn't say we could
+ * handle.
+ */
+ return false;
+ }
+
+ blobIDs.erase(it);
+
+ /* TODO: Handle aborting the process and fixing up the state. */
+
+ return true;
}
/*
diff --git a/test/Makefile.am b/test/Makefile.am
index e914c14..4144e4b 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -16,7 +16,8 @@
firmware_open_unittest \
firmware_write_unittest \
firmware_writemeta_unittest \
- firmware_close_unittest
+ firmware_close_unittest \
+ firmware_delete_unittest
TESTS = $(check_PROGRAMS)
@@ -40,3 +41,6 @@
firmware_close_unittest_SOURCES = firmware_close_unittest.cpp
firmware_close_unittest_LDADD = $(top_builddir)/firmware_handler.o
+
+firmware_delete_unittest_SOURCES = firmware_delete_unittest.cpp
+firmware_delete_unittest_LDADD = $(top_builddir)/firmware_handler.o
diff --git a/test/firmware_delete_unittest.cpp b/test/firmware_delete_unittest.cpp
new file mode 100644
index 0000000..0697bbd
--- /dev/null
+++ b/test/firmware_delete_unittest.cpp
@@ -0,0 +1,64 @@
+#include "data_mock.hpp"
+#include "firmware_handler.hpp"
+#include "image_mock.hpp"
+
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+namespace blobs
+{
+using ::testing::Eq;
+using ::testing::Return;
+using ::testing::StrEq;
+
+TEST(FirmwareHandlerDeleteTest, DeleteActiveHashSucceeds)
+{
+ /* Delete active image succeeds. */
+ DataHandlerMock dataMock;
+ ImageHandlerMock imageMock;
+
+ std::vector<HandlerPack> blobs = {
+ {FirmwareBlobHandler::hashBlobID, &imageMock},
+ {"asdf", &imageMock},
+ };
+ std::vector<DataHandlerPack> data = {
+ {FirmwareBlobHandler::UpdateFlags::ipmi, nullptr},
+ {FirmwareBlobHandler::UpdateFlags::lpc, &dataMock},
+ };
+
+ auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(blobs, data);
+
+ EXPECT_CALL(imageMock, open(Eq(FirmwareBlobHandler::hashBlobID)))
+ .WillOnce(Return(true));
+
+ EXPECT_TRUE(handler->open(
+ 0, OpenFlags::write | FirmwareBlobHandler::UpdateFlags::ipmi,
+ FirmwareBlobHandler::hashBlobID));
+
+ /* The active hash blob_id was added. */
+ auto currentBlobs = handler->getBlobIds();
+ EXPECT_EQ(3, currentBlobs.size());
+ EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
+ FirmwareBlobHandler::activeHashBlobID));
+
+ /* Set up close() expectations. */
+ EXPECT_CALL(imageMock, close());
+ EXPECT_TRUE(handler->close(0));
+
+ currentBlobs = handler->getBlobIds();
+ EXPECT_EQ(3, currentBlobs.size());
+ EXPECT_EQ(1, std::count(currentBlobs.begin(), currentBlobs.end(),
+ FirmwareBlobHandler::activeHashBlobID));
+
+ /* Delete the blob. */
+ EXPECT_TRUE(handler->deleteBlob(FirmwareBlobHandler::activeHashBlobID));
+
+ currentBlobs = handler->getBlobIds();
+ EXPECT_EQ(2, currentBlobs.size());
+ EXPECT_EQ(0, std::count(currentBlobs.begin(), currentBlobs.end(),
+ FirmwareBlobHandler::activeHashBlobID));
+}
+
+} // namespace blobs