| // Copyright 2024 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "hoth_skmhss_unittest.hpp" |
| |
| #include <blobs-ipmid/blobs.hpp> |
| |
| #include <cstdint> |
| #include <string_view> |
| #include <vector> |
| |
| #include <gtest/gtest.h> |
| |
| using ::testing::_; |
| using ::testing::Return; |
| using ::testing::UnorderedElementsAreArray; |
| |
| namespace ipmi_hoth |
| { |
| |
| class HothSKMHSSDeleteTest : public HothSKMHSSBasicTest |
| {}; |
| |
| TEST_F(HothSKMHSSDeleteTest, DeleteWithNoHothd) |
| { |
| DbusCommand::Cb cb; |
| testing::StrictMock<MockCancel> cancel; |
| EXPECT_CALL(hvnutil, deleteSKMHSS).WillOnce(Return(std::vector<uint8_t>())); |
| EXPECT_CALL(dbus, SendHostCommand("", testing::ElementsAre(), _)) |
| .WillOnce([&](std::string_view, const std::vector<uint8_t>&, |
| DbusCommand::Cb&& icb) { |
| cb = std::move(icb); |
| return stdplus::Cancel(&cancel); |
| }); |
| EXPECT_TRUE(hvn->deleteBlob("/skm/hss-backup/0")); |
| // Delete while delete in progress should not trigger a new delete |
| EXPECT_TRUE(hvn->deleteBlob("/skm/hss-backup/0")); |
| blobs::BlobMeta meta; |
| EXPECT_TRUE(hvn->stat("/skm/hss-backup/0", &meta)); |
| EXPECT_CALL(cancel, cancel()); |
| cb(std::nullopt); |
| EXPECT_TRUE(hvn->stat("/skm/hss-backup/0", &meta)); |
| } |
| |
| TEST_F(HothSKMHSSDeleteTest, DeleteSlotSucceeds) |
| { |
| // Also check the cached blob list is updated. |
| std::vector<std::string> expected = { |
| "/skm/hss-backup/", "/skm/hss-backup/0", "/skm/hss-backup/1", |
| "/skm/hss-backup/2", "/skm/hss-backup/3", |
| }; |
| EXPECT_CALL(dbus, pingHothd(std::string_view(""))) |
| .WillRepeatedly(Return(true)); |
| EXPECT_THAT(hvn->getBlobIds(), UnorderedElementsAreArray(expected)); |
| blobs::BlobMeta meta; |
| EXPECT_TRUE(hvn->stat("/skm/hss-backup/0", &meta)); |
| |
| DbusCommand::Cb cb; |
| testing::StrictMock<MockCancel> cancel; |
| EXPECT_CALL(hvnutil, deleteSKMHSS).WillOnce(Return(std::vector<uint8_t>())); |
| EXPECT_CALL(dbus, SendHostCommand("", testing::ElementsAre(), _)) |
| .WillOnce([&](std::string_view, const std::vector<uint8_t>&, |
| DbusCommand::Cb&& icb) { |
| cb = std::move(icb); |
| return stdplus::Cancel(&cancel); |
| }); |
| EXPECT_CALL(hvnutil, payloadECResponse(_)) |
| .WillOnce(Return(std::vector<uint8_t>())); |
| |
| EXPECT_TRUE(hvn->deleteBlob(legacyPath[3])); |
| EXPECT_TRUE(hvn->stat("/skm/hss-backup/3", &meta)); |
| EXPECT_THAT(hvn->getBlobIds(), UnorderedElementsAreArray(expected)); |
| EXPECT_CALL(cancel, cancel()); |
| cb(std::vector<uint8_t>()); |
| expected.pop_back(); |
| EXPECT_THAT(hvn->getBlobIds(), UnorderedElementsAreArray(expected)); |
| EXPECT_FALSE(hvn->stat("/skm/hss-backup/3", &meta)); |
| |
| // Deleting a nonexistent slot will fall through to hoth |
| EXPECT_CALL(hvnutil, deleteSKMHSS).WillOnce(Return(std::vector<uint8_t>())); |
| EXPECT_CALL(dbus, SendHostCommand("", testing::ElementsAre(), _)) |
| .WillOnce([&](std::string_view, const std::vector<uint8_t>&, |
| DbusCommand::Cb&& icb) { |
| cb = std::move(icb); |
| return stdplus::Cancel(&cancel); |
| }); |
| EXPECT_TRUE(hvn->deleteBlob(legacyPath[3])); |
| EXPECT_CALL(cancel, cancel()); |
| cb(std::nullopt); |
| |
| // Opening deleted slot succeeds |
| EXPECT_CALL(dbus, pingHothd("")).WillOnce(Return(true)); |
| EXPECT_TRUE(hvn->open(session, blobs::OpenFlags::write, legacyPath[3])); |
| } |
| |
| } // namespace ipmi_hoth |