| // 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> |
| |
| namespace ipmi_hoth |
| { |
| |
| using ::testing::_; |
| using ::testing::Return; |
| |
| class HothSKMHSSWriteTest : public HothSKMHSSBasicTest |
| { |
| protected: |
| void openWriteCachedBlob(int) |
| { |
| EXPECT_CALL(dbus, pingHothd(std::string_view(""))) |
| .WillOnce(Return(true)); |
| EXPECT_TRUE(hvn->open(session, blobs::OpenFlags::write, legacyPath[1])); |
| } |
| }; |
| |
| TEST_F(HothSKMHSSWriteTest, InvalidSessionWriteIsRejected) |
| { |
| // Verify the hoth command handler checks for a valid session. |
| |
| std::vector<uint8_t> data = {0x1, 0x2}; |
| |
| EXPECT_FALSE(hvn->write(session, 0, data)); |
| blobs::BlobMeta meta; |
| EXPECT_FALSE(hvn->stat(session, &meta)); |
| } |
| |
| TEST_F(HothSKMHSSWriteTest, WritingTooMuchByOneByteFails) |
| { |
| // Test the edge case of writing 1 byte too much with an offset of 0. |
| // writing 65 at offset 0 is invalid. |
| |
| openWriteCachedBlob(0); |
| std::vector<uint8_t> data(hvn->maxBufferSize() + 1, 0x11); |
| EXPECT_FALSE(hvn->write(session, 0, data)); |
| blobs::BlobMeta meta; |
| EXPECT_TRUE(hvn->stat(session, &meta)); |
| EXPECT_EQ(meta.size, 0); |
| } |
| |
| TEST_F(HothSKMHSSWriteTest, WritingTooMuchByOffsetOfOneFails) |
| { |
| // Test the edge case of writing 64 bytes (which is fine) but at the |
| // offset 1, which makes it go over by 1 byte. Writing 64 at offset 1 is |
| // invalid. |
| |
| openWriteCachedBlob(1); |
| std::vector<uint8_t> data(hvn->maxBufferSize(), 0x11); |
| EXPECT_FALSE(hvn->write(session, 1, data)); |
| } |
| |
| TEST_F(HothSKMHSSWriteTest, WritingOneByteBeyondEndFromOffsetFails) |
| { |
| // Test the edge case of writing to the last byte offset but trying to |
| // write two bytes. Writing 2 bytes at 63 is invalid. |
| |
| openWriteCachedBlob(2); |
| std::vector<uint8_t> data = {0x01, 0x02}; |
| EXPECT_FALSE(hvn->write(session, (hvn->maxBufferSize() - 1), data)); |
| } |
| |
| TEST_F(HothSKMHSSWriteTest, WritingOneByteAtOffsetBeyondEndFails) |
| { |
| // Test the edge case of writing one byte but exactly one byte beyond the |
| // buffer. Writing 1 byte at 64 is invalid. |
| |
| openWriteCachedBlob(3); |
| std::vector<uint8_t> data = {0x01}; |
| EXPECT_FALSE(hvn->write(session, hvn->maxBufferSize(), data)); |
| } |
| |
| TEST_F(HothSKMHSSWriteTest, WritingFullBufferAtOffsetZeroSucceeds) |
| { |
| // Test the case where you write the full buffer length at once to the 0th |
| // offset. |
| |
| openWriteCachedBlob(0); |
| std::vector<uint8_t> data(hvn->maxBufferSize(), 0x11); |
| EXPECT_TRUE(hvn->write(session, 0, data)); |
| } |
| |
| TEST_F(HothSKMHSSWriteTest, WritingOneByteToTheLastOffsetSucceeds) |
| { |
| // Test the case where you write the last byte. |
| |
| openWriteCachedBlob(0); |
| std::vector<uint8_t> data = {0x01}; |
| EXPECT_TRUE(hvn->write(session, (hvn->maxBufferSize() - 1), data)); |
| blobs::BlobMeta meta; |
| EXPECT_TRUE(hvn->stat(session, &meta)); |
| EXPECT_EQ(meta.size, 64); |
| } |
| |
| } // namespace ipmi_hoth |