From 639c06b33f4d0d3de743208c86b574d9db286aa6 Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 26 Apr 2021 16:35:26 +0100 Subject: [PATCH] Fix bug in StreamTransformer - ending fragment not getting sent (#2317) * Fix bug in StreamTransformer - ending fragment not getting sent * Add test to verify fix Before: ``` > ChunkedStream / StreamTransformer 758 OK: output.moveString(s) OUTPUT: 65 0d 0a 53 6f 6d 65 20 74 65 73 74 20 64 61 74 e..Some test dat 61 0d 0a a.. 792 FAIL: FS_OUTPUT == s FAIL in `virtual void StreamTest::execute()` ``` After: ``` >> ChunkedStream / StreamTransformer 741 OK: output.moveString(s) OUTPUT: 65 0d 0a 53 6f 6d 65 20 74 65 73 74 20 64 61 74 e..Some test dat 61 0d 0a 30 0d 0a 0d 0a a..0.... 760 OK: FS_OUTPUT == s ``` --- Sming/Core/Data/Buffer/CircularBuffer.cpp | 2 +- Sming/Core/Data/StreamTransformer.cpp | 6 ++++-- tests/HostTests/modules/Stream.cpp | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Sming/Core/Data/Buffer/CircularBuffer.cpp b/Sming/Core/Data/Buffer/CircularBuffer.cpp index 6f0a221384..9e9064bf71 100644 --- a/Sming/Core/Data/Buffer/CircularBuffer.cpp +++ b/Sming/Core/Data/Buffer/CircularBuffer.cpp @@ -68,7 +68,7 @@ size_t CircularBuffer::room() const size_t CircularBuffer::write(uint8_t charToWrite) { - if(!room()) { + if(room() == 0) { return 0; } diff --git a/Sming/Core/Data/StreamTransformer.cpp b/Sming/Core/Data/StreamTransformer.cpp index 4090ec6122..f719e4698f 100644 --- a/Sming/Core/Data/StreamTransformer.cpp +++ b/Sming/Core/Data/StreamTransformer.cpp @@ -37,7 +37,7 @@ void StreamTransformer::fillTempStream(char* buffer, size_t bufSize) while((room = tempStream->room()) >= maxChunkSize) { auto chunkSize = sourceStream->readMemoryBlock(buffer, maxChunkSize); if(chunkSize == 0) { - return; + break; } saveState(); @@ -55,7 +55,9 @@ void StreamTransformer::fillTempStream(char* buffer, size_t bufSize) if(sourceStream->isFinished()) { auto outLength = transform(nullptr, 0, result, resultSize); - tempStream->write(result, outLength); + auto written = tempStream->write(result, outLength); + (void)written; + assert(written == outLength); } } diff --git a/tests/HostTests/modules/Stream.cpp b/tests/HostTests/modules/Stream.cpp index 90802023ac..a3d98ffa83 100644 --- a/tests/HostTests/modules/Stream.cpp +++ b/tests/HostTests/modules/Stream.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include DEFINE_FSTR_LOCAL(template1, "Stream containing {var1}, {var2} and {var3}. {} {{}} {{12345") @@ -86,6 +87,19 @@ class StreamTest : public TestGroup REQUIRE(Resource::image_png == s); } + TEST_CASE("ChunkedStream / StreamTransformer") + { + DEFINE_FSTR_LOCAL(FS_INPUT, "Some test data"); + DEFINE_FSTR_LOCAL(FS_OUTPUT, "e\r\nSome test data\r\n0\r\n\r\n"); + ChunkedStream chunked(new FlashMemoryStream(FS_INPUT)); + MemoryDataStream output; + output.copyFrom(&chunked); + String s; + REQUIRE(output.moveString(s)); + m_printHex("OUTPUT", s.c_str(), s.length()); + REQUIRE(FS_OUTPUT == s); + } + TEST_CASE("MultipartStream / MultiStream") { unsigned itemIndex{0};