Skip to content

Commit db2100b

Browse files
Check out of bounds for inactive type1 packet
1 parent 3ccd50b commit db2100b

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

ElasticFrameProtocol.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ ElasticFrameProtocolReceiver::unpackType1(const uint8_t *pSubPacket, size_t lPac
168168
return ElasticFrameMessages::memoryAllocationError;
169169
}
170170

171-
if (pThisBucket->mOfFragmentNo < lType1Frame->hFragmentNo ||
172-
lType1Frame->hOfFragmentNo != pThisBucket->mOfFragmentNo) {
171+
if (pThisBucket->mOfFragmentNo < lType1Frame->hFragmentNo) {
173172
EFP_LOGGER(true, LOGG_FATAL, "bufferOutOfBounds")
174173
mBucketMap.erase(pThisBucket->mDeliveryOrder);
175174
pThisBucket->mActive = false;
@@ -338,7 +337,7 @@ ElasticFrameProtocolReceiver::unpackType2(const uint8_t *pSubPacket, size_t lPac
338337
return ElasticFrameMessages::noError;
339338
}
340339

341-
// 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
340+
// 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
342341
// in front of a type2 packet to catch the data overshoot.
343342
// Type 3 frames MUST be the same header size as type1 headers (FIXME part of the opportunistic data discussion)
344343
ElasticFrameMessages

unitTests/UnitTest24.cpp

+29-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
#include <gtest/gtest.h>
22

33
#include <memory>
4+
#include <random>
45

56
#include "ElasticFrameProtocol.h"
6-
#include "ElasticInternal.h"
77
#include "UnitTestHelpers.h"
88

99
// UnitTest24
10-
// Test unpack type1 packet
11-
TEST(UnitTest24, UnpackType1) {
12-
const size_t FRAME_SIZE = ((MTU - ElasticFrameProtocolSender::getType1Size()) * 5) + 12;
10+
// Test receiving corrupted packages
11+
TEST(UnitTest24, SendPacketFrameType1AndFrameType2) {
12+
std::random_device rd;
13+
std::mt19937 gen(rd());
14+
std::uniform_int_distribution<uint8_t> dis(0, 255);
15+
16+
const size_t FRAME_SIZE =
17+
(MTU - ElasticFrameProtocolSender::getType1Size()) + 1;
1318
std::unique_ptr<ElasticFrameProtocolReceiver> myEFPReceiver =
1419
std::make_unique<ElasticFrameProtocolReceiver>(50, 20);
15-
ElasticFrameType1 lType1Frame;
20+
std::unique_ptr<ElasticFrameProtocolSender> myEFPPacker =
21+
std::make_unique<ElasticFrameProtocolSender>(MTU);
22+
23+
for (int i = 0; i < 10000; i++) {
24+
myEFPPacker->sendCallback = [&](const std::vector<uint8_t> &subPacket,
25+
uint8_t lStreamID,
26+
ElasticFrameProtocolContext *pCTX) {
27+
std::vector<uint8_t> garbage = subPacket;
28+
for (size_t b = 0; b < garbage.size(); b++) {
29+
garbage[b] = dis(gen);
30+
}
31+
ElasticFrameMessages info = myEFPReceiver->receiveFragment(garbage, 0);
32+
};
1633

17-
// test with nice type1 packet
18-
lType1Frame.hSuperFrameNo = 0;
19-
lType1Frame.hFrameType = Frametype::type1;
20-
ElasticFrameMessages result = myEFPReceiver->receiveFragmentFromPtr(reinterpret_cast<uint8_t*>(&lType1Frame), FRAME_SIZE, 0);
21-
EXPECT_EQ(result, ElasticFrameMessages::noError);
34+
std::vector<uint8_t> mydata;
35+
mydata.resize(FRAME_SIZE);
2236

23-
// test with corrupt type1 packet
24-
lType1Frame.hSuperFrameNo = 1;
25-
lType1Frame.hFragmentNo = 12345;
26-
result = myEFPReceiver->receiveFragmentFromPtr(reinterpret_cast<uint8_t*>(&lType1Frame), FRAME_SIZE, 0);
27-
EXPECT_EQ(result, ElasticFrameMessages::bufferOutOfBounds);
37+
uint8_t streamID = 4;
38+
ElasticFrameMessages result = myEFPPacker->packAndSend(
39+
mydata, ElasticFrameContent::adts, 1001, 1, 2, streamID, NO_FLAGS);
40+
EXPECT_EQ(result, ElasticFrameMessages::noError);
41+
}
2842
}

0 commit comments

Comments
 (0)