diff --git a/CMakeLists.txt b/CMakeLists.txt index 71fe81d..7b1f0d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ add_executable(runUnitTestsEFP ${CMAKE_CURRENT_SOURCE_DIR}/unitTests/UnitTest21.cpp ${CMAKE_CURRENT_SOURCE_DIR}/unitTests/UnitTest22.cpp ${CMAKE_CURRENT_SOURCE_DIR}/unitTests/UnitTest23.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/unitTests/UnitTest24.cpp ${CMAKE_CURRENT_SOURCE_DIR}/unitTests/UnitTestHelpers.cpp ) diff --git a/ElasticFrameProtocol.cpp b/ElasticFrameProtocol.cpp index 017f0c0..8f15208 100644 --- a/ElasticFrameProtocol.cpp +++ b/ElasticFrameProtocol.cpp @@ -167,6 +167,14 @@ ElasticFrameProtocolReceiver::unpackType1(const uint8_t *pSubPacket, size_t lPac pThisBucket->mActive = false; return ElasticFrameMessages::memoryAllocationError; } + + if (pThisBucket->mOfFragmentNo < lType1Frame->hFragmentNo) { + EFP_LOGGER(true, LOGG_FATAL, "bufferOutOfBounds") + mBucketMap.erase(pThisBucket->mDeliveryOrder); + pThisBucket->mActive = false; + return ElasticFrameMessages::bufferOutOfBounds; + } + std::copy_n(pSubPacket + sizeof(ElasticFrameType1), lPacketSize - sizeof(ElasticFrameType1), pThisBucket->mBucketData->pFrameData + lInsertDataPointer); return ElasticFrameMessages::noError; @@ -329,7 +337,7 @@ ElasticFrameProtocolReceiver::unpackType2(const uint8_t *pSubPacket, size_t lPac return ElasticFrameMessages::noError; } -// Unpack method for type3 packets. Type3 packets are the parts of frames where the reminder data does not fit a type2 packet. Then a type 3 is added +// Unpack method for type3 packets. Type3 packets are the parts of frames where the remainder data does not fit a type2 packet. Then a type 3 is added // in front of a type2 packet to catch the data overshoot. // Type 3 frames MUST be the same header size as type1 headers (FIXME part of the opportunistic data discussion) ElasticFrameMessages diff --git a/efp_c_api/main.c b/efp_c_api/main.c index d500937..e083cf1 100644 --- a/efp_c_api/main.c +++ b/efp_c_api/main.c @@ -42,7 +42,7 @@ void send_data_callback(const uint8_t *data, size_t size, uint8_t stream_id, voi } void receive_embedded_data_callback(uint8_t *data, size_t size, uint8_t data_type, uint64_t pts, void* ctx) { - printf("Got embedded data: %zu bytes size and of type %d pts: %lu\n", size, data_type, pts); + printf("Got embedded data: %zu bytes size and of type %d pts: %llu\n", size, data_type, pts); //In this example we know it's a string, print it. printf("Data: %s \n", data); printf("Context: %d \n\n", *(int*)ctx); @@ -72,8 +72,8 @@ void receive_data_callback(uint8_t *data, printf("mFrameSize: %zu\n", size); printf("mDataContent: %d\n", data_content); printf("mBroken: %d\n", broken); - printf("mPts: %lu\n", pts); - printf("mDts: %lu\n", dts); + printf("mPts: %llu\n", pts); + printf("mDts: %llu\n", dts); printf("mCode: %d\n", code); printf("mStreamID: %d\n", stream_id); printf("mSource: %d\n", source); diff --git a/unitTests/UnitTest24.cpp b/unitTests/UnitTest24.cpp new file mode 100644 index 0000000..caa179e --- /dev/null +++ b/unitTests/UnitTest24.cpp @@ -0,0 +1,40 @@ +#include + +#include +#include + +#include "ElasticFrameProtocol.h" +#include "UnitTestHelpers.h" + +// UnitTest24 +// Test receiving corrupted packages +TEST(UnitTest24, SendPacketFrameType1AndFrameType2) { + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dis(0, 255); + + std::unique_ptr myEFPReceiver = + std::make_unique(50, 20); + std::unique_ptr myEFPPacker = + std::make_unique(MTU); + + for (int i = 0; i < 10000; i++) { + myEFPPacker->sendCallback = [&](const std::vector &subPacket, + uint8_t lStreamID, + ElasticFrameProtocolContext *pCTX) { + std::vector garbage = subPacket; + + std::generate(garbage.begin(), garbage.end(), + [&]() { return static_cast(dis(gen)); }); + }; + + std::vector mydata; + size_t randSize = rand() % 10000 + 1; + mydata.resize(randSize); + + uint8_t streamID = 4; + ElasticFrameMessages result = myEFPPacker->packAndSend( + mydata, ElasticFrameContent::adts, 1001, 1, 2, streamID, NO_FLAGS); + EXPECT_EQ(result, ElasticFrameMessages::noError); + } +}