2017-03-22 22:53:30 +08:00
|
|
|
#include "bolt_common.hpp"
|
2017-04-06 20:30:04 +08:00
|
|
|
#include "communication/bolt/v1/encoder/chunked_encoder_buffer.hpp"
|
2017-03-22 22:53:30 +08:00
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
// aliases
|
2018-03-23 23:32:17 +08:00
|
|
|
using BufferT = communication::bolt::ChunkedEncoderBuffer<TestOutputStream>;
|
2017-03-22 22:53:30 +08:00
|
|
|
|
2017-09-06 20:57:20 +08:00
|
|
|
// constants
|
|
|
|
using communication::bolt::CHUNK_END_MARKER_SIZE;
|
2018-03-23 23:32:17 +08:00
|
|
|
using communication::bolt::CHUNK_HEADER_SIZE;
|
2017-09-06 20:57:20 +08:00
|
|
|
using communication::bolt::MAX_CHUNK_SIZE;
|
|
|
|
using communication::bolt::WHOLE_CHUNK_SIZE;
|
|
|
|
|
|
|
|
// test data
|
|
|
|
constexpr const int TEST_DATA_SIZE = 100000;
|
|
|
|
uint8_t test_data[TEST_DATA_SIZE];
|
2017-03-22 22:53:30 +08:00
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
/**
|
|
|
|
* Verifies a single chunk. The chunk should be constructed from header
|
|
|
|
* (chunk size), data and end marker. The header is two bytes long number
|
2017-09-06 20:57:20 +08:00
|
|
|
* written in big endian format. Data is array of elements from test_data
|
|
|
|
* which max size is 0xFFFF. The end marker is always two bytes long array of
|
|
|
|
* two zeros.
|
2017-03-28 18:42:04 +08:00
|
|
|
*
|
|
|
|
* @param data pointer on data array (array of bytes)
|
|
|
|
* @param size of data array
|
2017-09-06 20:57:20 +08:00
|
|
|
* @param offset offset from the begining of the test data
|
2017-09-06 18:51:56 +08:00
|
|
|
* @param final_chunk if set to true then check for 0x00 0x00 after the chunk
|
2017-03-28 18:42:04 +08:00
|
|
|
*/
|
2017-09-06 20:57:20 +08:00
|
|
|
void VerifyChunkOfTestData(uint8_t *data, int size, uint64_t offset = 0,
|
|
|
|
bool final_chunk = true) {
|
2017-03-28 18:42:04 +08:00
|
|
|
// first two bytes are size (big endian)
|
|
|
|
uint8_t lower_byte = size & 0xFF;
|
|
|
|
uint8_t higher_byte = (size & 0xFF00) >> 8;
|
|
|
|
ASSERT_EQ(*data, higher_byte);
|
|
|
|
ASSERT_EQ(*(data + 1), lower_byte);
|
2017-03-22 22:53:30 +08:00
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
// in the data array should be size number of ones
|
|
|
|
// the header is skipped
|
2017-09-06 20:57:20 +08:00
|
|
|
for (auto i = 0; i < size; ++i) {
|
|
|
|
ASSERT_EQ(data[i + CHUNK_HEADER_SIZE], test_data[i + offset]);
|
2017-03-22 22:53:30 +08:00
|
|
|
}
|
2017-03-28 18:42:04 +08:00
|
|
|
|
|
|
|
// last two bytes should be zeros
|
|
|
|
// next to header and data
|
2017-09-06 18:51:56 +08:00
|
|
|
if (final_chunk) {
|
2017-09-06 20:57:20 +08:00
|
|
|
ASSERT_EQ(data[CHUNK_HEADER_SIZE + size], 0x00);
|
|
|
|
ASSERT_EQ(data[CHUNK_HEADER_SIZE + size + 1], 0x00);
|
2017-09-06 18:51:56 +08:00
|
|
|
}
|
2017-03-22 22:53:30 +08:00
|
|
|
}
|
|
|
|
|
2017-04-06 20:30:04 +08:00
|
|
|
TEST(BoltChunkedEncoderBuffer, OneSmallChunk) {
|
2017-03-28 18:42:04 +08:00
|
|
|
int size = 100;
|
2017-03-22 22:53:30 +08:00
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
// initialize tested buffer
|
2018-03-23 23:32:17 +08:00
|
|
|
TestOutputStream output_stream;
|
|
|
|
BufferT buffer(output_stream);
|
2017-03-28 18:42:04 +08:00
|
|
|
|
|
|
|
// write into buffer
|
2017-09-06 20:57:20 +08:00
|
|
|
buffer.Write(test_data, size);
|
2017-03-28 18:42:04 +08:00
|
|
|
buffer.Flush();
|
|
|
|
|
|
|
|
// check the output array
|
2017-09-06 20:57:20 +08:00
|
|
|
// the array should look like: [0, 100, first 100 bytes of test data, 0, 0]
|
2018-03-23 23:32:17 +08:00
|
|
|
VerifyChunkOfTestData(output_stream.output.data(), size);
|
2017-03-28 18:42:04 +08:00
|
|
|
}
|
|
|
|
|
2017-04-06 20:30:04 +08:00
|
|
|
TEST(BoltChunkedEncoderBuffer, TwoSmallChunks) {
|
2017-03-28 18:42:04 +08:00
|
|
|
int size1 = 100;
|
|
|
|
int size2 = 200;
|
|
|
|
|
|
|
|
// initialize tested buffer
|
2018-03-23 23:32:17 +08:00
|
|
|
TestOutputStream output_stream;
|
|
|
|
BufferT buffer(output_stream);
|
2017-03-28 18:42:04 +08:00
|
|
|
|
|
|
|
// write into buffer
|
2017-09-06 20:57:20 +08:00
|
|
|
buffer.Write(test_data, size1);
|
2017-03-28 18:42:04 +08:00
|
|
|
buffer.Chunk();
|
2017-09-06 20:57:20 +08:00
|
|
|
buffer.Write(test_data + size1, size2);
|
2017-03-28 18:42:04 +08:00
|
|
|
buffer.Flush();
|
|
|
|
|
|
|
|
// check the output array
|
2017-09-06 20:57:20 +08:00
|
|
|
// the output array should look like this:
|
|
|
|
// [0, 100, first 100 bytes of test data, 0, 0] +
|
|
|
|
// [0, 100, second 100 bytes of test data, 0, 0]
|
2018-03-23 23:32:17 +08:00
|
|
|
auto data = output_stream.output.data();
|
2017-09-06 20:57:20 +08:00
|
|
|
VerifyChunkOfTestData(data, size1);
|
|
|
|
VerifyChunkOfTestData(
|
|
|
|
data + CHUNK_HEADER_SIZE + size1 + CHUNK_END_MARKER_SIZE, size2, size1);
|
2017-03-22 22:53:30 +08:00
|
|
|
}
|
|
|
|
|
2017-04-06 20:30:04 +08:00
|
|
|
TEST(BoltChunkedEncoderBuffer, OneAndAHalfOfMaxChunk) {
|
2017-03-28 18:42:04 +08:00
|
|
|
// initialize tested buffer
|
2018-03-23 23:32:17 +08:00
|
|
|
TestOutputStream output_stream;
|
|
|
|
BufferT buffer(output_stream);
|
2017-03-28 18:42:04 +08:00
|
|
|
|
|
|
|
// write into buffer
|
2017-09-06 20:57:20 +08:00
|
|
|
buffer.Write(test_data, TEST_DATA_SIZE);
|
2017-03-28 18:42:04 +08:00
|
|
|
buffer.Flush();
|
|
|
|
|
|
|
|
// check the output array
|
|
|
|
// the output array should look like this:
|
2017-09-06 20:57:20 +08:00
|
|
|
// [0xFF, 0xFF, first 65535 bytes of test data,
|
|
|
|
// 0x86, 0xA1, 34465 bytes of test data after the first 65535 bytes, 0, 0]
|
2018-03-23 23:32:17 +08:00
|
|
|
auto output = output_stream.output.data();
|
2017-09-06 20:57:20 +08:00
|
|
|
VerifyChunkOfTestData(output, MAX_CHUNK_SIZE, 0, false);
|
|
|
|
VerifyChunkOfTestData(output + WHOLE_CHUNK_SIZE,
|
|
|
|
TEST_DATA_SIZE - MAX_CHUNK_SIZE, MAX_CHUNK_SIZE);
|
2017-03-28 18:42:04 +08:00
|
|
|
}
|
2017-03-22 22:53:30 +08:00
|
|
|
|
2017-03-28 18:42:04 +08:00
|
|
|
int main(int argc, char **argv) {
|
2017-09-06 20:57:20 +08:00
|
|
|
InitializeData(test_data, TEST_DATA_SIZE);
|
2017-06-21 17:29:13 +08:00
|
|
|
google::InitGoogleLogging(argv[0]);
|
2017-03-22 22:53:30 +08:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|