diff --git a/README.md b/README.md index 6802e1a..bbee4e4 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,8 @@ The CLI, library, and default CLI tests build without external dependencies. Set In the topic listing, `sensor_combined (0) - 1298` means instance 0 has 1,298 samples. Parsing warnings go to standard error. A fatal parse error or an unreadable file produces a nonzero exit status. The CLI keeps samples in memory while it builds the report. The C++ -reader can process chunks incrementally for large logs. Logs with appended data segments +reader can process chunks incrementally for large logs. Call `Reader::finish()` after the last +chunk to report incomplete input. Logs with appended data segments may yield partial results. ## C++ library diff --git a/include/flight_log/reader.hpp b/include/flight_log/reader.hpp index ce4b2bc..cc30702 100644 --- a/include/flight_log/reader.hpp +++ b/include/flight_log/reader.hpp @@ -26,16 +26,22 @@ class Reader { */ void readChunk(const uint8_t* data, int length); + /** + * Signal the end of the input, reporting an incomplete file header or final message. + * Further reads and repeated calls to finish() are ignored. + */ + void finish(); + private: static constexpr int kBufferSizeInit = 2048; static const std::set kKnownMessageTypes; - int readMagic(const uint8_t* data, int length); - int readFlagBits(const uint8_t* data, int length); + void readMagic(const ulog_file_header_s& header); + void readFlagBits(const ulog_message_flag_bits_s& flag_bits); void corruptionDetected(); int appendToPartialBuffer(const uint8_t* data, int length); - void tryToRecover(const uint8_t* data, int length); + void tryToRecover(const uint8_t*& data, int& length); void readHeaderMessage(const uint8_t* message); void readDataMessage(const uint8_t* message); @@ -46,13 +52,13 @@ class Reader { ReadHeader, ReadData, InvalidData, + Finished, }; State _state{State::ReadMagic}; const std::shared_ptr _data_handler_interface; - uint8_t* _partial_message_buffer{nullptr}; ///< contains at most one ULog message (unless - ///< _need_recovery==true) + uint8_t* _partial_message_buffer{nullptr}; ///< buffers incomplete input and recovery bytes int _partial_message_buffer_length_capacity{0}; int _partial_message_buffer_length{0}; diff --git a/src/main.cpp b/src/main.cpp index d573e4e..e4831a7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,6 +38,9 @@ int inspectLog(const char* path) return 1; } reader.readChunk(buffer.data(), static_cast(file.gcount())); + if (file.eof()) { + reader.finish(); + } } while (file && !data_container->hadFatalError()); for (const auto& parsing_error : data_container->parsingErrors()) { diff --git a/src/reader.cpp b/src/reader.cpp index 9e0597b..3846f1e 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -17,6 +17,10 @@ namespace flight_log { +namespace { +constexpr int kULogHeaderLength = sizeof(ulog_message_header_s); +} // namespace + const std::set Reader::kKnownMessageTypes{{ ULogMessageType::FORMAT, ULogMessageType::DATA, @@ -58,69 +62,87 @@ Reader::~Reader() void Reader::readChunk(const uint8_t* data, int length) { - if (_state == State::InvalidData) { + if (_state == State::InvalidData || _state == State::Finished || length <= 0) { return; } + auto ensure_enough_data_in_partial_buffer = [&](int required_data) { + if (_partial_message_buffer_length < required_data && length > 0) { + const int num_append = std::min(required_data - _partial_message_buffer_length, length); + const int required_capacity = _partial_message_buffer_length + num_append; + if (required_capacity > _partial_message_buffer_length_capacity) { + _partial_message_buffer_length_capacity = required_capacity; + _partial_message_buffer = + static_cast(realloc(_partial_message_buffer, required_capacity)); + DBG_PRINTF("%i: resized partial buffer to %i\n", _total_num_read, required_capacity); + } + appendToPartialBuffer(data, num_append); + data += num_append; + length -= num_append; + _total_num_read += num_append; + } + return _partial_message_buffer_length >= required_data; + }; + if (_state == State::ReadMagic) { - const int num_read = readMagic(data, length); - data += num_read; - length -= num_read; - _total_num_read += num_read; + if (!ensure_enough_data_in_partial_buffer(sizeof(ulog_file_header_s))) { + return; + } + readMagic(*reinterpret_cast(_partial_message_buffer)); + _partial_message_buffer_length = 0; } - if (_state == State::ReadFlagBits && length > 0) { - const int num_read = readFlagBits(data, length); - data += num_read; - length -= num_read; - _total_num_read += num_read; + if (_state == State::ReadFlagBits) { + if (!ensure_enough_data_in_partial_buffer(kULogHeaderLength)) { + return; + } + const auto* header = reinterpret_cast(_partial_message_buffer); + if (static_cast(header->msg_type) == ULogMessageType::FLAG_BITS) { + const int flag_length = header->msg_size + kULogHeaderLength; + if (flag_length < static_cast(sizeof(ulog_message_flag_bits_s))) { + _data_handler_interface->error("Not enough data to read file flags", false); + _state = State::InvalidData; + return; + } + if (!ensure_enough_data_in_partial_buffer(flag_length)) { + return; + } + readFlagBits(*reinterpret_cast(_partial_message_buffer)); + _partial_message_buffer_length = 0; + } else { + _data_handler_interface->fileHeader(FileHeader{_file_header}); + _state = State::ReadHeader; + } } - static constexpr int kULogHeaderLength = static_cast(sizeof(ulog_message_header_s)); - - while (length > 0 && !_need_recovery) { + if (_state == State::InvalidData) { + return; + } + while (length > 0 || _partial_message_buffer_length >= kULogHeaderLength) { + if (_need_recovery) { + tryToRecover(data, length); + if (_need_recovery) { + return; + } + } // Try to get a full ulog message. There's 2 options: // - we have some partial data in the buffer. We need to append and use that buffer // - no partial data left. Use 'data' if it contains a full message const uint8_t* ulog_message = nullptr; bool clear_from_partial_message_buffer = false; if (_partial_message_buffer_length > 0) { - auto ensure_enough_data_in_partial_buffer = [&](int required_data) -> bool { - if (_partial_message_buffer_length < required_data) { - // Try to append - const int num_append = std::min(required_data - _partial_message_buffer_length, length); - if (_partial_message_buffer_length + num_append > - _partial_message_buffer_length_capacity) { - // Overflow, resize buffer - _partial_message_buffer_length_capacity = _partial_message_buffer_length + num_append; - _partial_message_buffer = static_cast( - realloc(_partial_message_buffer, _partial_message_buffer_length_capacity)); - DBG_PRINTF("%i: resized partial buffer to %i\n", _total_num_read, - _partial_message_buffer_length_capacity); - } - memcpy(_partial_message_buffer + _partial_message_buffer_length, data, num_append); - _partial_message_buffer_length += num_append; - data += num_append; - length -= num_append; - _total_num_read += num_append; - } - return _partial_message_buffer_length >= required_data; - }; - if (ensure_enough_data_in_partial_buffer(kULogHeaderLength)) { - const ulog_message_header_s* header = - reinterpret_cast(_partial_message_buffer); - if (ensure_enough_data_in_partial_buffer(header->msg_size + kULogHeaderLength)) { - ulog_message = reinterpret_cast(_partial_message_buffer); - clear_from_partial_message_buffer = true; - } else { - // Not enough data yet (length == 0) or overflow - DBG_PRINTF("%i: not enough data (length=%i)\n", _total_num_read, length); - } + if (!ensure_enough_data_in_partial_buffer(kULogHeaderLength)) { + return; } - + const auto* header = reinterpret_cast(_partial_message_buffer); + if (!ensure_enough_data_in_partial_buffer(header->msg_size + kULogHeaderLength)) { + return; + } + ulog_message = _partial_message_buffer; + clear_from_partial_message_buffer = true; } else { int full_message_length = 0; - if (length > kULogHeaderLength) { + if (length >= kULogHeaderLength) { const ulog_message_header_s* header = reinterpret_cast(data); if (length >= header->msg_size + kULogHeaderLength) { full_message_length = header->msg_size + kULogHeaderLength; @@ -148,7 +170,6 @@ void Reader::readChunk(const uint8_t* data, int length) if (header->msg_size == 0 || header->msg_type == 0) { DBG_PRINTF("%i: Invalid msg detected\n", _total_num_read); corruptionDetected(); - // We'll exit the loop afterwards } else { // Parse the message try { @@ -174,33 +195,48 @@ void Reader::readChunk(const uint8_t* data, int length) } } } +} + +void Reader::finish() +{ + const auto previous_state = _state; + if (previous_state == State::InvalidData || previous_state == State::Finished) { + return; + } + _state = State::Finished; - if (_need_recovery) { - tryToRecover(data, length); + if (previous_state == State::ReadMagic) { + _data_handler_interface->error("Not enough data to read file magic", false); + } else if (previous_state == State::ReadFlagBits && + _partial_message_buffer_length >= kULogHeaderLength) { + _data_handler_interface->error("Not enough data to read file flags", false); + } else { + if (previous_state == State::ReadFlagBits) { + _data_handler_interface->fileHeader(FileHeader{_file_header}); + } + if (_partial_message_buffer_length > 0 && !_need_recovery) { + _data_handler_interface->error("Truncated message at end of log", true); + } } + _partial_message_buffer_length = 0; } -void Reader::tryToRecover(const uint8_t* data, int length) +void Reader::tryToRecover(const uint8_t*& data, int& length) { // Try to find a valid message in 'data' by moving data into the partial buffer and search for a // message - while (length > 0) { - const int num_append = appendToPartialBuffer(data, length); - data += num_append; - length -= num_append; - _total_num_read += num_append; + while (length > 0 || _partial_message_buffer_length >= kULogHeaderLength) { + if (length > 0) { + const int num_append = appendToPartialBuffer(data, length); + data += num_append; + length -= num_append; + _total_num_read += num_append; + } - if (_partial_message_buffer_length >= static_cast(sizeof(ulog_message_header_s))) { + if (_partial_message_buffer_length >= kULogHeaderLength) { bool found = false; int index = 0; - // If the partial buffer was already full, skip the first index, otherwise we risk infinite - // recursion - if (num_append == 0) { - index = 1; - } - for (; - index < _partial_message_buffer_length - static_cast(sizeof(ulog_message_header_s)); - ++index) { + for (; index <= _partial_message_buffer_length - kULogHeaderLength; ++index) { const ulog_message_header_s* header = reinterpret_cast(_partial_message_buffer + index); // Try to use it if it looks sane (we could also check for a SYNC message) @@ -216,12 +252,11 @@ void Reader::tryToRecover(const uint8_t* data, int length) // DataHandlerInterface::isValidDataMessage(). bool candidate_valid = true; if (static_cast(header->msg_type) == ULogMessageType::DATA) { - if (header->msg_size < 2 || _partial_message_buffer_length - index < - static_cast(sizeof(ulog_message_header_s)) + 2) { - // Too small to even hold the 2-byte msg_id, or not enough buffered data - // yet to see it - can't validate this candidate, so don't accept it - // (more data may still arrive for a later pass). + if (header->msg_size < 2) { candidate_valid = false; + } else if (_partial_message_buffer_length - index < kULogHeaderLength + 2) { + // Keep the candidate until the rest of its message ID arrives. + break; } else { uint16_t candidate_msg_id = 0; memcpy(&candidate_msg_id, @@ -248,16 +283,17 @@ void Reader::tryToRecover(const uint8_t* data, int length) if (found) { DBG_PRINTF( - "%i: recovered, recursive call (index = %i, length = %i, partial buf len = %i)\n", + "%i: recovered (index = %i, length = %i, partial buf len = %i)\n", _total_num_read, index, length, _partial_message_buffer_length); _need_recovery = false; - readChunk(data, length); - return; } DBG_PRINTF("%i: no valid msg found (length = %i, partial buf len = %i)\n", _total_num_read, length, _partial_message_buffer_length); } + if (length == 0) { + return; + } } } @@ -279,75 +315,40 @@ int Reader::appendToPartialBuffer(const uint8_t* data, int length) return num_append; } -int Reader::readMagic(const uint8_t* data, int length) +void Reader::readMagic(const ulog_file_header_s& header) { - // Assume we read the whole magic in one piece. If needed, we could handle reading it in multiple - // bits. Note that this could also happen for truncated files. - if (length < static_cast(sizeof(ulog_file_header_s))) { - _data_handler_interface->error("Not enough data to read file magic", false); - _state = State::InvalidData; - return 0; - } - - const ulog_file_header_s* header = reinterpret_cast(data); - // Check magic bytes - if (memcmp(header->magic, ulog_file_magic_bytes, sizeof(ulog_file_magic_bytes)) != 0) { + if (memcmp(header.magic, ulog_file_magic_bytes, sizeof(ulog_file_magic_bytes)) != 0) { _data_handler_interface->error("Invalid file format (incorrect header bytes)", false); _state = State::InvalidData; - return 0; + return; } _state = State::ReadFlagBits; - _file_header = *header; - - return sizeof(ulog_file_header_s); + _file_header = header; } -int Reader::readFlagBits(const uint8_t* data, int length) +void Reader::readFlagBits(const ulog_message_flag_bits_s& flag_bits) { - // Assume we read the whole flags in one piece (for simplicity of the parser) - int ret = 0; - if (length < static_cast(sizeof(ulog_message_flag_bits_s))) { - _data_handler_interface->error("Not enough data to read file flags", false); - _state = State::InvalidData; - return 0; + if (flag_bits.appended_offsets[0] != 0) { + // TODO: handle appended data + _data_handler_interface->error("File contains appended offsets - this is not supported", true); } - // This message is optional and follows directly the file magic - const ulog_message_flag_bits_s* flag_bits = - reinterpret_cast(data); - if (static_cast(flag_bits->msg_type) == ULogMessageType::FLAG_BITS) { - // This is expected to be the first message after the file magic - if (flag_bits->appended_offsets[0] != 0) { - // TODO: handle appended data - _data_handler_interface->error("File contains appended offsets - this is not supported", - true); - } - // Check incompat flags - bool has_incompat_flags = false; - if (flag_bits->incompat_flags[0] & ~(ULOG_INCOMPAT_FLAG0_DATA_APPENDED_MASK)) { + // Check incompat flags + bool has_incompat_flags = flag_bits.incompat_flags[0] & ~ULOG_INCOMPAT_FLAG0_DATA_APPENDED_MASK; + for (unsigned i = 1; + i < sizeof(flag_bits.incompat_flags) / sizeof(flag_bits.incompat_flags[0]); ++i) { + if (flag_bits.incompat_flags[i]) { has_incompat_flags = true; } - for (unsigned i = 1; - i < sizeof(flag_bits->incompat_flags) / sizeof(flag_bits->incompat_flags[0]); ++i) { - if (flag_bits->incompat_flags[i]) { - has_incompat_flags = true; - } - } - if (has_incompat_flags) { - _data_handler_interface->error("Unknown incompatible flag set: cannot parse the log", false); - _state = State::InvalidData; - } else { - _data_handler_interface->fileHeader({_file_header, *flag_bits}); - ret = flag_bits->msg_size + ULOG_MSG_HEADER_LEN; - _state = State::ReadHeader; - } + } + if (has_incompat_flags) { + _data_handler_interface->error("Unknown incompatible flag set: cannot parse the log", false); + _state = State::InvalidData; } else { - // Create header w/o flag bits - _data_handler_interface->fileHeader(FileHeader{_file_header}); + _data_handler_interface->fileHeader({_file_header, flag_bits}); _state = State::ReadHeader; } - return ret; } void Reader::readHeaderMessage(const uint8_t* message) diff --git a/tests/ulog_parsing_test.cpp b/tests/ulog_parsing_test.cpp index f841aa5..6a92721 100644 --- a/tests/ulog_parsing_test.cpp +++ b/tests/ulog_parsing_test.cpp @@ -233,11 +233,7 @@ TEST_CASE("ULog parsing - test corruption") const auto data_container = std::make_shared(flight_log::DataContainer::StorageConfig::FullLog); flight_log::Reader reader{data_container}; - // Read in multiple chunks, as the parser needs more readChunk calls to recover. - // We could change the parser to handle it, but in practice this should not be an issue. - const int last_chunk_size = 30; - reader.readChunk(written_data.data(), written_data.size() - last_chunk_size); - reader.readChunk(written_data.data() + written_data.size() - last_chunk_size, last_chunk_size); + reader.readChunk(written_data.data(), written_data.size()); // Expected to have errors CHECK_GT(data_container->parsingErrors().size(), 0); @@ -332,14 +328,7 @@ TEST_CASE("ULog parsing - corruption recovery rejects wrong-size data for a know const auto data_container = std::make_shared(flight_log::DataContainer::StorageConfig::FullLog); flight_log::Reader reader{data_container}; - // As in the "test corruption" case above: if recovery completes with no remaining - // external bytes to process (length == 0 at the point tryToRecover's recursive - // readChunk() call runs), the just-recovered message stays buffered but unprocessed - // until a later readChunk() call with length > 0 flushes it - readChunk()'s main loop - // only runs while length > 0. So a second, small trailing call is needed here too. - const int last_chunk_size = 5; - reader.readChunk(written_data.data(), written_data.size() - last_chunk_size); - reader.readChunk(written_data.data() + written_data.size() - last_chunk_size, last_chunk_size); + reader.readChunk(written_data.data(), written_data.size()); // Expected to have errors, but not be fatal CHECK_GT(data_container->parsingErrors().size(), 0); @@ -421,14 +410,209 @@ std::vector fabricatedDataMessage(uint16_t payload_size) return bytes; } -void readInTwoChunks(flight_log::Reader& reader, std::vector& written_data) +} // namespace + +TEST_CASE("ULog parsing - file magic can span chunks") { - // See the corruption tests above for why a small trailing chunk is needed after recovery. - const int last_chunk_size = 5; - reader.readChunk(written_data.data(), written_data.size() - last_chunk_size); - reader.readChunk(written_data.data() + written_data.size() - last_chunk_size, last_chunk_size); + flight_log::Data first{0, {}}; + flight_log::Data second{0, {}}; + const auto bytes = writeFileWithInjectedBytes({}, first, second); + const auto collector = std::make_shared(); + flight_log::Reader reader{collector}; + + const int split = 7; + CAPTURE(split); + reader.readChunk(bytes.data(), split); + CHECK(collector->parsingErrors().empty()); + reader.readChunk(bytes.data() + split, bytes.size() - split); + CHECK_FALSE(collector->hadFatalError()); + REQUIRE_EQ(collector->accepted.size(), 2); + CHECK_EQ(collector->accepted[1], second); +} + +TEST_CASE("ULog parsing - flag bits can span chunks") +{ + flight_log::Data first{0, {}}; + flight_log::Data second{0, {}}; + const auto bytes = writeFileWithInjectedBytes({}, first, second); + + const int flag_end = sizeof(flight_log::ulog_file_header_s) + + sizeof(flight_log::ulog_message_flag_bits_s); + for (const int split : {17, 19, flag_end - 1}) { + const auto collector = std::make_shared(); + flight_log::Reader reader{collector}; + CAPTURE(split); + reader.readChunk(bytes.data(), split); + CHECK(collector->parsingErrors().empty()); + reader.readChunk(bytes.data() + split, bytes.size() - split); + CHECK_FALSE(collector->hadFatalError()); + CHECK_EQ(collector->fileHeader().flagBits().msg_size, 40); + CHECK_EQ(collector->accepted.size(), 2); + } +} + +TEST_CASE("ULog parsing - short legacy log without flag bits") +{ + std::vector bytes; + TestWriter writer([&](const uint8_t* data, int length) { + bytes.insert(bytes.end(), data, data + length); + }); + const flight_log::FileHeader header{123}; + const flight_log::Logging logging{flight_log::Logging::Level::Warning, "legacy", 321}; + writer.fileHeader(flight_log::FileHeader{header.header()}); + writer.headerComplete(); + writer.logging(logging); + + const auto container = + std::make_shared(flight_log::DataContainer::StorageConfig::FullLog); + flight_log::Reader reader{container}; + CAPTURE(bytes.size()); + reader.readChunk(bytes.data(), bytes.size()); + CHECK(container->parsingErrors().empty()); + CHECK_FALSE(container->hadFatalError()); + CHECK_EQ(container->fileHeader().header().timestamp, 123); + REQUIRE_EQ(container->logging().size(), 1); + CHECK_EQ(container->logging()[0], logging); +} + +TEST_CASE("ULog parsing - extended flag bits can span chunks") +{ + flight_log::Data first{0, {}}; + flight_log::Data second{0, {}}; + auto bytes = writeFileWithInjectedBytes({}, first, second); + const int flag_start = sizeof(flight_log::ulog_file_header_s); + const int flag_end = flag_start + sizeof(flight_log::ulog_message_flag_bits_s); + const int extension_size = 20; + bytes[flag_start] += extension_size; + bytes.insert(bytes.begin() + flag_end, extension_size, 0); + + const auto collector = std::make_shared(); + flight_log::Reader reader{collector}; + const int split = flag_end + 6; + CAPTURE(split); + CAPTURE(extension_size); + reader.readChunk(bytes.data(), split); + reader.readChunk(bytes.data() + split, bytes.size() - split); + CHECK(collector->parsingErrors().empty()); + CHECK_FALSE(collector->hadFatalError()); + CHECK_EQ(collector->fileHeader().flagBits().msg_size, 60); + REQUIRE_EQ(collector->accepted.size(), 2); + CHECK_EQ(collector->accepted[1], second); +} + +TEST_CASE("ULog parsing - short flag record fails before consuming following messages") +{ + flight_log::Data first{0, {}}; + flight_log::Data second{0, {}}; + auto bytes = writeFileWithInjectedBytes({}, first, second); + const int flag_start = sizeof(flight_log::ulog_file_header_s); + const int flag_end = flag_start + sizeof(flight_log::ulog_message_flag_bits_s); + bytes[flag_start] = 39; + bytes.erase(bytes.begin() + flag_end - 1); + + const auto collector = std::make_shared(); + flight_log::Reader reader{collector}; + CAPTURE(flag_start); + CAPTURE(bytes[flag_start]); + reader.readChunk(bytes.data(), bytes.size()); + CHECK(collector->hadFatalError()); + CHECK(collector->accepted.empty()); +} + +TEST_CASE("ULog parsing - recovered messages are delivered before the chunk returns") +{ + flight_log::Data first{0, {}}; + flight_log::Data second{0, {}}; + const auto bytes = writeFileWithInjectedBytes(std::vector(50, 0), first, second); + const auto collector = std::make_shared(); + flight_log::Reader reader{collector}; + + CAPTURE(bytes.size()); + reader.readChunk(bytes.data(), bytes.size()); + CHECK_EQ(collector->parsingErrors().size(), 1); + CHECK_FALSE(collector->hadFatalError()); + REQUIRE_EQ(collector->accepted.size(), 2); + CHECK_EQ(collector->accepted[0], first); + CHECK_EQ(collector->accepted[1], second); +} + +TEST_CASE("ULog parsing - recovery preserves a DATA candidate with a split message ID") +{ + flight_log::Data first{0, {}}; + flight_log::Data second{0, {}}; + const auto bytes = writeFileWithInjectedBytes(std::vector(50, 0), first, second); + const auto collector = std::make_shared(); + flight_log::Reader reader{collector}; + + const auto candidate_offset = bytes.size() - (ULOG_MSG_HEADER_LEN + sizeof(uint16_t) + 12); + const auto split = candidate_offset + ULOG_MSG_HEADER_LEN + 1; + CAPTURE(candidate_offset); + CAPTURE(split); + reader.readChunk(bytes.data(), split); + CHECK_EQ(collector->parsingErrors().size(), 1); + CHECK_EQ(collector->accepted.size(), 1); + reader.readChunk(bytes.data() + split, bytes.size() - split - 1); + CHECK_EQ(collector->accepted.size(), 1); + reader.readChunk(bytes.data() + bytes.size() - 1, 1); + CHECK_FALSE(collector->hadFatalError()); + REQUIRE_EQ(collector->accepted.size(), 2); + CHECK_EQ(collector->accepted[1], second); +} + +TEST_CASE("ULog parsing - finishing rejects incomplete file magic") +{ + flight_log::Data first{0, {}}; + flight_log::Data second{0, {}}; + const auto bytes = writeFileWithInjectedBytes({}, first, second); + for (int size = 0; size < static_cast(sizeof(flight_log::ulog_file_header_s)); ++size) { + const auto collector = std::make_shared(); + flight_log::Reader reader{collector}; + CAPTURE(size); + reader.readChunk(bytes.data(), size); + CHECK(collector->parsingErrors().empty()); + reader.finish(); + CHECK(collector->hadFatalError()); + CHECK_EQ(collector->parsingErrors().size(), 1); + } +} + +TEST_CASE("ULog parsing - finishing rejects an incomplete flag record") +{ + flight_log::Data first{0, {}}; + flight_log::Data second{0, {}}; + const auto bytes = writeFileWithInjectedBytes({}, first, second); + const int size = sizeof(flight_log::ulog_file_header_s) + + sizeof(flight_log::ulog_message_flag_bits_s) - 1; + const auto collector = std::make_shared(); + flight_log::Reader reader{collector}; + + CAPTURE(size); + reader.readChunk(bytes.data(), size); + CHECK(collector->parsingErrors().empty()); + reader.finish(); + CHECK(collector->hadFatalError()); + CHECK_EQ(collector->parsingErrors().size(), 1); +} + +TEST_CASE("ULog parsing - finishing reports and discards an incomplete final sample") +{ + flight_log::Data first{0, {}}; + flight_log::Data second{0, {}}; + const auto bytes = writeFileWithInjectedBytes({}, first, second); + const auto collector = std::make_shared(); + flight_log::Reader reader{collector}; + + const auto size = bytes.size() - 1; + CAPTURE(size); + reader.readChunk(bytes.data(), size); + CHECK(collector->parsingErrors().empty()); + reader.finish(); + reader.finish(); + CHECK_FALSE(collector->hadFatalError()); + CHECK_EQ(collector->parsingErrors().size(), 1); + REQUIRE_EQ(collector->accepted.size(), 1); + CHECK_EQ(collector->accepted.front(), first); } -} // namespace TEST_CASE("ULog parsing - Header storage mode recovers on the next genuine data message") { @@ -441,7 +625,7 @@ TEST_CASE("ULog parsing - Header storage mode recovers on the next genuine data const auto collector = std::make_shared(); flight_log::Reader reader{collector}; - readInTwoChunks(reader, written_data); + reader.readChunk(written_data.data(), written_data.size()); CHECK_GT(collector->parsingErrors().size(), 0); REQUIRE_FALSE(collector->hadFatalError()); @@ -461,7 +645,7 @@ TEST_CASE("ULog parsing - Header storage mode rejects wrong-size data for a know const auto collector = std::make_shared(); flight_log::Reader reader{collector}; - readInTwoChunks(reader, written_data); + reader.readChunk(written_data.data(), written_data.size()); CHECK_GT(collector->parsingErrors().size(), 0); REQUIRE_FALSE(collector->hadFatalError());