| // 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 <span> |
| #include <string_view> |
| #include <vector> |
| |
| #include <gtest/gtest.h> |
| |
| namespace ipmi_hoth |
| { |
| |
| using ::testing::_; |
| using ::testing::Return; |
| |
| class HothSKMHSSOpenTest : public HothSKMHSSBasicTest |
| {}; |
| |
| TEST_F(HothSKMHSSOpenTest, OpenWithNoHothd) |
| { |
| /* Hoth SKM HSS handler opens without a backing hoth daemon present. */ |
| EXPECT_CALL(dbus, pingHothd(std::string_view(""))).WillOnce(Return(false)); |
| EXPECT_FALSE(hvn->open(session, hvn->requiredFlags(), legacyPath[1])); |
| } |
| |
| TEST_F(HothSKMHSSOpenTest, OpenRWUninitializedBlobSucceeds) |
| { |
| /* Hoth SKM HSS handler opens an uninitialized path. */ |
| hvn = createHandlerWithEmptyCache(); |
| EXPECT_CALL(dbus, pingHothd(std::string_view(""))).WillOnce(Return(true)); |
| fu2::unique_function<void()> cb; |
| testing::StrictMock<MockCancel> cancel; |
| std::array<uint8_t, 1> rspdata; |
| expectRead(0, cb, cancel, std::span<uint8_t>(rspdata).subspan(0, 0)); |
| EXPECT_TRUE( |
| hvn->open(session, blobs::OpenFlags::read | blobs::OpenFlags::write, |
| legacyPath[0])); |
| blobs::BlobMeta meta; |
| EXPECT_TRUE(hvn->stat(session, &meta)); |
| EXPECT_EQ(meta.blobState, blobs::StateFlags::committing); |
| cb(); |
| EXPECT_TRUE(hvn->stat(session, &meta)); |
| EXPECT_EQ(meta.blobState, |
| blobs::StateFlags::open_read | blobs::StateFlags::open_write | |
| blobs::StateFlags::commit_error); |
| } |
| |
| TEST_F(HothSKMHSSOpenTest, OpenROUninitializedBlobFails) |
| { |
| /* Hoth SKM HSS handler opens an uninitialized path. */ |
| hvn = createHandlerWithEmptyCache(); |
| EXPECT_CALL(dbus, pingHothd(std::string_view(""))).WillOnce(Return(true)); |
| fu2::unique_function<void()> cb; |
| testing::StrictMock<MockCancel> cancel; |
| std::array<uint8_t, 1> rspdata; |
| expectRead(0, cb, cancel, std::span<uint8_t>(rspdata).subspan(0, 0)); |
| EXPECT_TRUE(hvn->open(session, blobs::OpenFlags::read, legacyPath[0])); |
| blobs::BlobMeta meta; |
| EXPECT_TRUE(hvn->stat(session, &meta)); |
| EXPECT_EQ(meta.blobState, blobs::StateFlags::committing); |
| cb(); |
| EXPECT_TRUE(hvn->stat(session, &meta)); |
| EXPECT_EQ(meta.blobState, blobs::StateFlags::commit_error); |
| } |
| |
| TEST_F(HothSKMHSSOpenTest, OpenBlobWithSdBusErrorFails) |
| { |
| EXPECT_CALL(dbus, pingHothd(std::string_view(""))).WillOnce(Return(true)); |
| fu2::unique_function<void()> cb; |
| testing::StrictMock<MockCancel> cancel; |
| expectRead(0, cb, cancel, {}); |
| EXPECT_TRUE( |
| hvn->open(session, blobs::OpenFlags::read | blobs::OpenFlags::write, |
| legacyPath[0])); |
| blobs::BlobMeta meta; |
| EXPECT_TRUE(hvn->stat(session, &meta)); |
| EXPECT_EQ(meta.blobState, blobs::StateFlags::committing); |
| cb(); |
| EXPECT_TRUE(hvn->stat(session, &meta)); |
| EXPECT_EQ(meta.blobState, blobs::StateFlags::commit_error); |
| } |
| |
| TEST_F(HothSKMHSSOpenTest, OpenOverloadedSessionFails) |
| { |
| EXPECT_CALL(dbus, pingHothd(std::string_view(""))) |
| .WillRepeatedly(Return(true)); |
| uint16_t sessId = 0; |
| int low = std::min((uint16_t)legacyPath.size(), hvn->maxSessions()); |
| |
| for (int i = 0; i < low; i++) |
| { |
| EXPECT_TRUE( |
| hvn->open(sessId++, blobs::OpenFlags::write, legacyPath[i])); |
| } |
| |
| for (int i = low; i < hvn->maxSessions(); i++) |
| { |
| EXPECT_TRUE( |
| hvn->open(sessId++, blobs::OpenFlags::write, legacyPath[0])); |
| } |
| |
| EXPECT_FALSE(hvn->open(sessId++, blobs::OpenFlags::write, legacyPath[0])); |
| } |
| |
| } // namespace ipmi_hoth |