Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

25.6 KB
Raw Blame
// Copyright (C) 2018-2026 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#include <gtest/gtest.h>

#include <limits>

#include "common_test_utils/test_assertions.hpp"
#include "metadata.hpp"
#include "metadata_wrappers.hpp"

using namespace intel_npu;

using MetadataUnitTests = ::testing::Test;
using testing::_;

namespace {

// Compiler payload size + magic bytes
constexpr size_t FOOTER_SIZE = sizeof(uint64_t) + intel_npu::MAGIC_BYTES.size();
// Metadata version + compiler payload size + magic bytes
constexpr size_t MINIMUM_BLOB_SIZE = sizeof(uint32_t) + FOOTER_SIZE;
constexpr size_t SIZE_OF_INIT_SCHEDULE_SIZE = sizeof(uint64_t);
constexpr size_t SIZE_OF_LAYOUT_SIZE = sizeof(uint16_t);

// intel_npu::BlobSource create_tensor_blob_source_from_stream(istream& stream) {
//     size_t totalSize = source.get_total_size();
//     auto tensor = ov::Tensor(ov::element::u8, ov::Shape{totalSize});

//     source.seekg(0, std::ios::beg);
//     source.read_into_buffer(tensor.data<char>(), tensor.get_byte_size());
//     return intel_npu::BlobSource(tensor);
// }

intel_npu::BlobSource create_tensor_blob_source_from_stream_source(intel_npu::BlobSource& source, ov::Tensor& tensor) {
    source.seekg(0, std::ios::beg);
    source.read_into_buffer(tensor.data<char>(), tensor.get_byte_size());
    return intel_npu::BlobSource(tensor);
}

}  // namespace

TEST_F(MetadataUnitTests, readUnversionedBlob) {
    std::stringstream stream("this_is an_unversioned bl0b");
    BlobSource blob(stream);

    std::unique_ptr<MetadataBase> storedMeta;
    OV_EXPECT_THROW(storedMeta = read_metadata_from(blob), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{blob.get_total_size()});
    blob = create_tensor_blob_source_from_stream_source(blob, tensor);
    OV_EXPECT_THROW(storedMeta = read_metadata_from(blob), ov::Exception, _);
}

TEST_F(MetadataUnitTests, writeAndReadCurrentMetadataFromBlob) {
    uint64_t blobSize = 0;
    std::stringstream stream;
    auto meta = MetadataTest(blobSize, CURRENT_OPENVINO_VERSION);

    OV_ASSERT_NO_THROW(meta.write(stream));

    std::unique_ptr<MetadataBase> storedMeta;
    BlobSource blob(stream);
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(blob));

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{blob.get_total_size()});
    blob = create_tensor_blob_source_from_stream_source(blob, tensor);
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(blob));
}

TEST_F(MetadataUnitTests, writeAndReadBlobTypes) {
    for (const BlobType blobType : {BlobType::ELF, BlobType::BYTECODE}) {
        std::stringstream stream;
        MetadataTest(0,
                     CURRENT_OPENVINO_VERSION,
                     std::nullopt,
                     std::nullopt,
                     std::nullopt,
                     std::nullopt,
                     std::nullopt,
                     std::nullopt,
                     std::nullopt,
                     blobType)
            .write(stream);

        BlobSource source(stream);
        auto storedMeta = read_metadata_from(source);
        ASSERT_NE(storedMeta, nullptr);
        ASSERT_EQ(storedMeta->get_blob_type(), std::make_optional(blobType));
    }
}

TEST_F(MetadataUnitTests, writeAndReadCurrentMetadataFromBlobWithContent) {
    uint64_t blobSize = 64;
    std::stringstream stream;
    std::vector<uint8_t> content(blobSize, 0);
    stream.write(reinterpret_cast<const char*>(content.data()), static_cast<std::streamsize>(blobSize));

    auto meta = MetadataTest(blobSize, CURRENT_OPENVINO_VERSION);
    OV_ASSERT_NO_THROW(meta.write(stream));

    std::unique_ptr<MetadataBase> storedMeta;
    BlobSource source(stream);
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(source));
    ASSERT_TRUE(storedMeta->get_compiler_payload_size() == blobSize);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);

    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(source));
    ASSERT_TRUE(storedMeta->get_compiler_payload_size() == blobSize);
}

TEST_F(MetadataUnitTests, writeAndReadCurrentMetadataFromBlobWeightsSeparation) {
    uint64_t blobSize = 0;
    std::stringstream stream;
    auto meta = MetadataTest(blobSize, CURRENT_OPENVINO_VERSION, std::vector<uint64_t>{0, 0});

    OV_ASSERT_NO_THROW(meta.write(stream));

    BlobSource source(stream);
    std::unique_ptr<MetadataBase> storedMeta;
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(source));

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(source));
}

TEST_F(MetadataUnitTests, writeAndReadCurrentMetadataFromBlobWithContentAllAttributes) {
    uint64_t blobSize = 64;
    std::vector<uint64_t> initSizes{16, 16};
    int64_t batchSize = 32;
    std::vector<ov::Layout> inputLayouts{"12345", "1...3?"};
    std::vector<ov::Layout> outputLayouts{"layout"};

    std::stringstream stream;
    std::vector<uint8_t> content(blobSize, 0);
    stream.write(reinterpret_cast<const char*>(content.data()), static_cast<std::streamsize>(blobSize));

    auto meta = MetadataTest(blobSize, CURRENT_OPENVINO_VERSION, initSizes, batchSize, inputLayouts, outputLayouts);
    OV_ASSERT_NO_THROW(meta.write(stream));

    BlobSource source(stream);
    std::unique_ptr<MetadataBase> storedMeta;
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(source));
    ASSERT_TRUE(storedMeta->get_compiler_payload_size() == blobSize);
    ASSERT_TRUE(storedMeta->get_init_sizes().has_value());
    ASSERT_TRUE(storedMeta->get_init_sizes()->size() == initSizes.size());
    for (size_t i = 0; i < initSizes.size(); ++i) {
        ASSERT_TRUE(storedMeta->get_init_sizes()->at(i) == initSizes.at(i));
    }
    ASSERT_TRUE(storedMeta->get_batch_size().has_value());
    ASSERT_TRUE(storedMeta->get_batch_size() == batchSize);
    ASSERT_TRUE(storedMeta->get_input_layouts().has_value());
    ASSERT_TRUE(storedMeta->get_output_layouts().has_value());
    ASSERT_TRUE(storedMeta->get_input_layouts()->size() == inputLayouts.size());
    ASSERT_TRUE(storedMeta->get_output_layouts()->size() == outputLayouts.size());
    for (size_t i = 0; i < inputLayouts.size(); ++i) {
        ASSERT_TRUE(storedMeta->get_input_layouts()->at(i) == inputLayouts.at(i));
    }
    for (size_t i = 0; i < outputLayouts.size(); ++i) {
        ASSERT_TRUE(storedMeta->get_output_layouts()->at(i) == outputLayouts.at(i));
    }

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(source));
    ASSERT_TRUE(storedMeta->get_compiler_payload_size() == blobSize);
    ASSERT_TRUE(storedMeta->get_init_sizes().has_value());
    ASSERT_TRUE(storedMeta->get_init_sizes()->size() == initSizes.size());
    for (size_t i = 0; i < initSizes.size(); ++i) {
        ASSERT_TRUE(storedMeta->get_init_sizes()->at(i) == initSizes.at(i));
    }
    ASSERT_TRUE(storedMeta->get_batch_size().has_value());
    ASSERT_TRUE(storedMeta->get_batch_size() == batchSize);
    ASSERT_TRUE(storedMeta->get_input_layouts().has_value());
    ASSERT_TRUE(storedMeta->get_output_layouts().has_value());
    ASSERT_TRUE(storedMeta->get_input_layouts()->size() == inputLayouts.size());
    ASSERT_TRUE(storedMeta->get_output_layouts()->size() == outputLayouts.size());
    for (size_t i = 0; i < inputLayouts.size(); ++i) {
        ASSERT_TRUE(storedMeta->get_input_layouts()->at(i) == inputLayouts.at(i));
    }
    for (size_t i = 0; i < outputLayouts.size(); ++i) {
        ASSERT_TRUE(storedMeta->get_output_layouts()->at(i) == outputLayouts.at(i));
    }
}

TEST_F(MetadataUnitTests, writeAndReadCurrentMetadataFromBlobWithCompatibilityDesc) {
    uint64_t blobSize = 64;
    const std::string compatDesc = "platform=NPU3720;tiles=2;etc=...";
    std::stringstream stream;
    std::vector<uint8_t> content(blobSize, 0);
    stream.write(reinterpret_cast<const char*>(content.data()), static_cast<std::streamsize>(blobSize));

    auto meta = MetadataTest(blobSize,
                             CURRENT_OPENVINO_VERSION,
                             std::nullopt,
                             std::nullopt,
                             std::nullopt,
                             std::nullopt,
                             std::nullopt,
                             std::nullopt,
                             compatDesc);
    OV_ASSERT_NO_THROW(meta.write(stream));

    BlobSource source(stream);
    std::unique_ptr<MetadataBase> storedMeta;
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(source));
    ASSERT_TRUE(storedMeta->get_compiler_payload_size() == blobSize);
    ASSERT_TRUE(storedMeta->get_compatibility_descriptor().has_value());
    ASSERT_EQ(storedMeta->get_compatibility_descriptor().value(), compatDesc);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(source));
    ASSERT_TRUE(storedMeta->get_compiler_payload_size() == blobSize);
    ASSERT_TRUE(storedMeta->get_compatibility_descriptor().has_value());
    ASSERT_EQ(storedMeta->get_compatibility_descriptor().value(), compatDesc);
}

TEST_F(MetadataUnitTests, writeAndReadCurrentMetadataFromBlobWithEmptyCompatibilityDescriptor) {
    uint64_t blobSize = 0;
    std::stringstream stream;

    auto meta = MetadataTest(blobSize,
                             CURRENT_OPENVINO_VERSION,
                             std::nullopt,
                             std::nullopt,
                             std::nullopt,
                             std::nullopt,
                             std::nullopt,
                             std::nullopt,
                             std::string{});
    OV_ASSERT_NO_THROW(meta.write(stream));

    BlobSource source(stream);
    std::unique_ptr<MetadataBase> storedMeta;
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(source));
    ASSERT_FALSE(storedMeta->get_compatibility_descriptor().has_value());

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);
    OV_ASSERT_NO_THROW(storedMeta = read_metadata_from(source));
    ASSERT_FALSE(storedMeta->get_compatibility_descriptor().has_value());
}

TEST_F(MetadataUnitTests, compatibilityDescriptorLenExceedsBounds) {
    std::stringstream stream;
    const std::string compatDesc = "platform=NPU3720;tiles=2;etc=...";
    auto meta = Metadata<METADATA_VERSION_2_6>(0,
                                               std::nullopt,
                                               std::nullopt,
                                               std::nullopt,
                                               std::nullopt,
                                               std::nullopt,
                                               std::nullopt,
                                               std::nullopt,
                                               compatDesc);
    meta.write(stream);
    std::string blob = stream.str();

    const uint64_t badCompatibilityDescriptorSize = compatDesc.size() + 0xFF;
    const size_t compatDescLenOffset =
        blob.size() - FOOTER_SIZE - sizeof(badCompatibilityDescriptorSize) - compatDesc.size();
    std::memcpy(&blob[compatDescLenOffset], &badCompatibilityDescriptorSize, sizeof(badCompatibilityDescriptorSize));

    std::stringstream malformedStream(blob);
    BlobSource source(malformedStream);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{blob.size()});
    std::memcpy(tensor.data<char>(), blob.data(), blob.size());
    source = BlobSource(tensor);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);
}

TEST_F(MetadataUnitTests, writeAndReadInvalidMetadataVersion) {
    uint64_t blobSize = 0;
    std::stringstream stream;
    auto meta = MetadataTest(blobSize, std::nullopt);

    constexpr uint32_t dummyVersion = MetadataBase::make_version(0x00007E57, 0x0000AC3D);
    meta.set_version(dummyVersion);

    OV_ASSERT_NO_THROW(meta.write(stream));
    BlobSource source(stream);
    OV_EXPECT_THROW(auto storedMeta = read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);
    OV_EXPECT_THROW(auto storedMeta = read_metadata_from(source), ov::Exception, _);
}

TEST_F(MetadataUnitTests, writeAndReadMetadataWithNewerMinorVersion) {
    uint64_t blobSize = 0;
    std::stringstream stream;
    auto meta = MetadataTest(blobSize, CURRENT_OPENVINO_VERSION);

    constexpr uint32_t dummyVersion =
        MetadataBase::make_version(CURRENT_METADATA_MAJOR_VERSION, CURRENT_METADATA_MINOR_VERSION + 1);
    meta.set_version(dummyVersion);

    OV_ASSERT_NO_THROW(meta.write(stream));

    BlobSource source(stream);
    std::unique_ptr<MetadataBase> storedMeta;
    OV_EXPECT_THROW(storedMeta = read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);
    OV_EXPECT_THROW(storedMeta = read_metadata_from(source), ov::Exception, _);
}

TEST_P(MetadataVersionTestFixture, writeAndReadInvalidMetadataVersion) {
    uint32_t metaVersion = GetParam();
    if (CURRENT_METADATA_MAJOR_VERSION == MetadataBase::get_major(metaVersion) && CURRENT_METADATA_MINOR_VERSION == 0) {
        GTEST_SKIP() << "Skipping single test since there is no case of lower minor version than actual.";
    }

    OpenvinoVersion dummyOvVersion(0x5A, 0x1A, 0xD);
    MetadataVersionTestFixture dummyMeta = MetadataVersionTestFixture(0, dummyOvVersion);
    dummyMeta.set_version(metaVersion);

    OV_ASSERT_NO_THROW(dummyMeta.write(blob));
    BlobSource source(blob);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);
}

const std::vector badMetadataVersions = {
    MetadataBase::make_version(CURRENT_METADATA_MAJOR_VERSION + 1, CURRENT_METADATA_MINOR_VERSION),
    MetadataBase::make_version(CURRENT_METADATA_MAJOR_VERSION + 1, CURRENT_METADATA_MINOR_VERSION + 1),
    MetadataBase::make_version(CURRENT_METADATA_MAJOR_VERSION + 1, CURRENT_METADATA_MINOR_VERSION - 1),
    MetadataBase::make_version(CURRENT_METADATA_MAJOR_VERSION - 1, CURRENT_METADATA_MINOR_VERSION),
    MetadataBase::make_version(CURRENT_METADATA_MAJOR_VERSION - 1, CURRENT_METADATA_MINOR_VERSION + 1),
    MetadataBase::make_version(CURRENT_METADATA_MAJOR_VERSION - 1, CURRENT_METADATA_MINOR_VERSION - 1)};

INSTANTIATE_TEST_SUITE_P(MetadataUnitTests,
                         MetadataVersionTestFixture,
                         ::testing::ValuesIn(badMetadataVersions),
                         MetadataVersionTestFixture::getTestCaseName);

TEST_F(MetadataUnitTests, writeAndReadMetadataWithNewerFieldAtEnd) {
    uint64_t blobSize = 0;
    std::stringstream stream;
    OpenvinoVersion dummyOvVersion(0x0FF, 0xC0FF, 0xEEEE);
    auto meta = MetadataTest(blobSize, dummyOvVersion);

    constexpr uint32_t dummyVersion =
        MetadataBase::make_version(CURRENT_METADATA_MAJOR_VERSION, CURRENT_METADATA_MINOR_VERSION + 1);
    meta.set_version(dummyVersion);

    OV_ASSERT_NO_THROW(meta.write(stream));

    // inserting a new field at the end of the blob, between last metadata field and blobDataSize
    std::string temp = stream.str();
    size_t offset = MAGIC_BYTES.size() + sizeof(uint64_t);
    temp.insert(temp.length() - offset, "new metadata field");
    stream.str("");
    stream << temp;

    BlobSource source(stream);
    std::unique_ptr<MetadataBase> storedMeta;
    OV_EXPECT_THROW(storedMeta = read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);
    OV_EXPECT_THROW(storedMeta = read_metadata_from(source), ov::Exception, _);
}

TEST_F(MetadataUnitTests, writeAndReadMetadataWithNewerFieldAtMiddle) {
    uint64_t blobSize = 0;
    std::stringstream stream;
    OpenvinoVersion dummyOvVersion(0xFA, 0x1A, 0xFE1);
    auto meta = MetadataTest(blobSize, dummyOvVersion);

    constexpr uint32_t dummyVersion =
        MetadataBase::make_version(CURRENT_METADATA_MAJOR_VERSION + 1, CURRENT_METADATA_MINOR_VERSION);
    meta.set_version(dummyVersion);

    OV_ASSERT_NO_THROW(meta.write(stream));

    // inserting a new field at the middle of the blob, between metadata version and OV version size
    std::string temp = stream.str();
    size_t offset = sizeof(CURRENT_METADATA_VERSION);
    temp.insert(offset, "new metadata field");
    stream.str("");
    stream << temp;

    BlobSource source(stream);
    std::unique_ptr<MetadataBase> storedMeta;
    OV_EXPECT_THROW(storedMeta = read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);
    OV_EXPECT_THROW(storedMeta = read_metadata_from(source), ov::Exception, _);
}

TEST_F(MetadataUnitTests, writeAndReadMetadataWithRemovedField) {
    uint64_t blobSize = 0;
    std::stringstream stream;
    OpenvinoVersion dummyOvVersion(0xBA, 0xB1, 0xC);
    auto meta = MetadataTest(blobSize, dummyOvVersion);

    constexpr uint32_t dummyVersion =
        MetadataBase::make_version(CURRENT_METADATA_MAJOR_VERSION + 1, CURRENT_METADATA_MINOR_VERSION);
    meta.set_version(dummyVersion);

    OV_ASSERT_NO_THROW(meta.write(stream));

    // removing fields between metadata version and blob data size
    std::string temp = stream.str();
    size_t offset = sizeof(CURRENT_METADATA_VERSION), size = offset + MAGIC_BYTES.size() + sizeof(uint64_t);
    temp.replace(offset, temp.length() - size, "");
    stream.str("");
    stream << temp;

    BlobSource source(stream);
    std::unique_ptr<MetadataBase> storedMeta;
    OV_EXPECT_THROW(storedMeta = read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{source.get_total_size()});
    source = create_tensor_blob_source_from_stream_source(source, tensor);
    OV_EXPECT_THROW(storedMeta = read_metadata_from(source), ov::Exception, _);
}

/**
 * @brief Throw if the size of the compiler payload is too big relative to the size of the blob
 */
TEST_F(MetadataUnitTests, compilerPayloadSizeExceedsBlobLimit) {
    std::stringstream stream;

    auto meta = Metadata<METADATA_VERSION_2_0>(0, std::nullopt);
    meta.write(stream);
    std::string blob = stream.str();

    const uint64_t badCompilerPayloadSize = 0xFF;
    const size_t compilerPayloadSizeOffset = blob.size() - FOOTER_SIZE;
    std::memcpy(&blob[compilerPayloadSizeOffset], &badCompilerPayloadSize, sizeof(badCompilerPayloadSize));

    std::stringstream malformedStream(blob);
    BlobSource source(malformedStream);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{blob.size()});
    std::memcpy(tensor.data<char>(), blob.data(), blob.size());
    source = BlobSource(tensor);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);
}

/**
 * @brief CWE-190: payloadSize near UINT64_MAX wraps MINIMUM_BLOB_SIZE + payloadSize to a small value,
 * bypassing the additive check and causing an OOB read at tensor.data() + payloadSize.
 * The subtraction-form guard must reject this before any dereference.
 */
TEST_F(MetadataUnitTests, payloadSizeIntegerOverflowIsRejected) {
    const size_t blobSize = MINIMUM_BLOB_SIZE + 16;
    std::string blob(blobSize, '\0');

    std::memcpy(&blob[blobSize - MAGIC_BYTES.size()], MAGIC_BYTES.data(), MAGIC_BYTES.size());

    // Chosen so MINIMUM_BLOB_SIZE + payloadSize == 0 (mod 2^64): the additive check wraps to 0.
    const uint64_t payloadSize = std::numeric_limits<uint64_t>::max() - MINIMUM_BLOB_SIZE + 1;
    std::memcpy(&blob[blobSize - MAGIC_BYTES.size() - sizeof(payloadSize)], &payloadSize, sizeof(payloadSize));

    std::stringstream malformedStream(blob);
    BlobSource source(malformedStream);
    OV_EXPECT_THROW(read_metadata_from(source),
                    ov::Exception,
                    testing::HasSubstr("compiler payload parsed from the blob"));

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{blobSize});
    std::memcpy(tensor.data<char>(), blob.data(), blobSize);
    source = BlobSource(tensor);
    OV_EXPECT_THROW(read_metadata_from(source),
                    ov::Exception,
                    testing::HasSubstr("compiler payload parsed from the blob"));
}

/**
 * @brief Throw if the number of init schedules is too big relative to the size of the blob
 */
TEST_F(MetadataUnitTests, numberOfInitSchedulesExceedsBlobLimit) {
    std::stringstream stream;
    std::vector<uint64_t> initSizes{16, 32};

    auto meta = Metadata<METADATA_VERSION_2_1>(0, std::nullopt, initSizes);
    meta.write(stream);
    std::string blob = stream.str();

    const uint64_t badNumberOfInits = initSizes.size() + 1;
    const size_t numberOfInitsOffset =
        blob.size() - FOOTER_SIZE - initSizes.size() * SIZE_OF_INIT_SCHEDULE_SIZE - sizeof(badNumberOfInits);
    std::memcpy(&blob[numberOfInitsOffset], &badNumberOfInits, sizeof(badNumberOfInits));

    std::stringstream malformedStream(blob);
    BlobSource source(malformedStream);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{blob.size()});
    std::memcpy(tensor.data<char>(), blob.data(), blob.size());
    source = BlobSource(tensor);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);
}

/**
 * @brief Throw if the number of input layouts is too big relative to the size of the blob
 */
TEST_F(MetadataUnitTests, numberOfInputLayoutsExceedsBlobLimit) {
    std::stringstream stream;
    std::vector<ov::Layout> inputLayouts{"1"};
    std::vector<ov::Layout> outputLayouts{"2"};

    auto meta =
        Metadata<METADATA_VERSION_2_3>(0, std::nullopt, std::nullopt, std::nullopt, inputLayouts, outputLayouts);
    meta.write(stream);
    std::string blob = stream.str();

    const uint64_t badNumberOfLayouts = inputLayouts.size() + 0xFF;
    const size_t numberOfLayoutsOffset =
        blob.size() - FOOTER_SIZE - (inputLayouts.size() + outputLayouts.size()) * SIZE_OF_LAYOUT_SIZE -
        inputLayouts.at(0).to_string().size() - outputLayouts.at(0).to_string().size() - 2 * sizeof(badNumberOfLayouts);
    std::memcpy(&blob[numberOfLayoutsOffset], &badNumberOfLayouts, sizeof(badNumberOfLayouts));

    std::stringstream malformedStream(blob);
    BlobSource source(malformedStream);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{blob.size()});
    std::memcpy(tensor.data<char>(), blob.data(), blob.size());
    source = BlobSource(tensor);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);
}

/**
 * @brief Throw if the number of output layouts is too big relative to the size of the blob
 */
TEST_F(MetadataUnitTests, numberOfOutputLayoutsExceedsBlobLimit) {
    std::stringstream stream;
    std::vector<ov::Layout> inputLayouts{"1"};
    std::vector<ov::Layout> outputLayouts{"2"};

    auto meta =
        Metadata<METADATA_VERSION_2_3>(0, std::nullopt, std::nullopt, std::nullopt, inputLayouts, outputLayouts);
    meta.write(stream);
    std::string blob = stream.str();

    const uint64_t badNumberOfLayouts = outputLayouts.size() + 0xFF;
    const size_t numberOfLayoutsOffset =
        blob.size() - FOOTER_SIZE - (inputLayouts.size() + outputLayouts.size()) * SIZE_OF_LAYOUT_SIZE -
        inputLayouts.at(0).to_string().size() - outputLayouts.at(0).to_string().size() - sizeof(badNumberOfLayouts);
    std::memcpy(&blob[numberOfLayoutsOffset], &badNumberOfLayouts, sizeof(badNumberOfLayouts));

    std::stringstream malformedStream(blob);
    BlobSource source(malformedStream);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{blob.size()});
    std::memcpy(tensor.data<char>(), blob.data(), blob.size());
    source = BlobSource(tensor);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);
}

/**
 * @brief Throw if the size of at least one layout is too big relative to the size of the blob
 */
TEST_F(MetadataUnitTests, layoutLengthExceedsBlobLimit) {
    std::stringstream stream;
    std::vector<ov::Layout> inputLayouts{"1"};
    std::vector<ov::Layout> outputLayouts{};

    auto meta =
        Metadata<METADATA_VERSION_2_3>(0, std::nullopt, std::nullopt, std::nullopt, inputLayouts, outputLayouts);
    meta.write(stream);
    std::string blob = stream.str();

    const uint64_t badNumberOfLayouts = inputLayouts.at(0).to_string().size() + 0xFF;
    const size_t numberOfLayoutsOffset = blob.size() - FOOTER_SIZE -
                                         (inputLayouts.size() + outputLayouts.size()) * SIZE_OF_LAYOUT_SIZE -
                                         inputLayouts.at(0).to_string().size();
    std::memcpy(&blob[numberOfLayoutsOffset], &badNumberOfLayouts, sizeof(badNumberOfLayouts));

    std::stringstream malformedStream(blob);
    BlobSource source(malformedStream);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);

    auto tensor = ov::Tensor(ov::element::u8, ov::Shape{blob.size()});
    std::memcpy(tensor.data<char>(), blob.data(), blob.size());
    source = BlobSource(tensor);
    OV_EXPECT_THROW(read_metadata_from(source), ov::Exception, _);
}