| #include "cper_decoder.hpp" |
| #include "mock_cper_encoder.hpp" |
| |
| #include <gmock/gmock.h> |
| #include <gtest/gtest.h> |
| |
| namespace uefi::cper |
| { |
| |
| TEST(CperDecoderTest, DecoderInvalidData) |
| { |
| // Too small for header |
| std::vector<uint8_t> smallData(kRecordHeaderSizeBytes - 1, 0); |
| EXPECT_THROW(CperDecoder{smallData}, std::invalid_argument); |
| |
| // Invalid signature |
| std::vector<uint8_t> invalidSignature(kRecordHeaderSizeBytes, 0); |
| EXPECT_THROW(CperDecoder{invalidSignature}, std::invalid_argument); |
| } |
| |
| TEST(CperDecoderTest, DecoderMalformedSections) |
| { |
| MockCperEncoder<uint8_t> mockEncoder; |
| const std::array<uint8_t, 10> sectionBody = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| mockEncoder.addSection(sectionBody, kMockSectionFlag, kMockSectionType); |
| std::vector<uint8_t> cperArray = mockEncoder.serializeToByteArray(); |
| |
| // Corrupt section count in header to be 2 instead of 1 |
| void* headerPtr = cperArray.data(); |
| uint16_t malformedCount = 2; |
| memcpy(static_cast<char*>(headerPtr) + offsetof(RecordHeader, sectionCount), |
| &malformedCount, sizeof(malformedCount)); |
| |
| CperDecoder decoder(cperArray); |
| EXPECT_THROW(decoder.getSections(), std::runtime_error); |
| } |
| |
| } // namespace uefi::cper |