Skip to content

Commit

Permalink
FIX: Aligment zero length sequence bug (#54)
Browse files Browse the repository at this point in the history
* Add regression test

* Update tests

* Clean tests

* Clean tests

* Fix

* FastCDR required

* Add dependencies
  • Loading branch information
pablogs9 committed Nov 12, 2020
1 parent 9e34df7 commit 5b188cc
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -55,6 +55,8 @@ else()
return()
endif()

set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${PROJECT_BINARY_DIR}/temp_install)

###############################################################################
# Config
###############################################################################
Expand Down
26 changes: 26 additions & 0 deletions cmake/SuperBuild.cmake
Expand Up @@ -49,6 +49,32 @@ if(UCDR_BUILD_TESTS)
set(GMOCK_ROOT ${PROJECT_BINARY_DIR}/temp_install/googletest CACHE PATH "" FORCE)
list(APPEND _deps googletest)
endif()

unset(fastcdr_DIR CACHE)
enable_language(CXX)
unset(FASTCDR_ROOT CACHE)
ExternalProject_Add(fastcdr
GIT_REPOSITORY
https://github.com/eProsima/Fast-CDR
GIT_TAG
v1.0.13
PREFIX
${PROJECT_BINARY_DIR}/fastcdr
INSTALL_DIR
${PROJECT_BINARY_DIR}/temp_install/fastcdr
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE=${DCMAKE_BUILD_TYPE}
BUILD_COMMAND
COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config Release --target install
COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config Debug --target install
INSTALL_COMMAND
""
)
set(FASTCDR_ROOT ${PROJECT_BINARY_DIR}/temp_install/fastcdr CACHE PATH "" FORCE)
set(FASTCDR_INCLUDE_DIRS ${PROJECT_BINARY_DIR}/temp_install/fastcdr/include CACHE PATH "" FORCE)

list(APPEND _deps fastcdr)
endif()

# Client project.
Expand Down
8 changes: 4 additions & 4 deletions src/c/types/sequence.c
Expand Up @@ -35,12 +35,12 @@ inline void ucdr_deserialize_sequence_header(ucdrBuffer* ub, ucdrEndianness endi
bool ucdr_serialize_sequence ## SUFFIX(ucdrBuffer* ub, const TYPE* array, uint32_t length) \
{ \
ucdr_serialize_endian_uint32_t(ub, ub->endianness, length); \
return ucdr_serialize_endian_array ## SUFFIX(ub, ub->endianness, array, length); \
return length == 0 || ucdr_serialize_endian_array ## SUFFIX(ub, ub->endianness, array, length); \
} \
bool ucdr_serialize_endian_sequence ## SUFFIX(ucdrBuffer* ub, ucdrEndianness endianness, const TYPE* array, uint32_t length) \
{ \
ucdr_serialize_endian_uint32_t(ub, endianness, length); \
return ucdr_serialize_endian_array ## SUFFIX(ub, endianness, array, length); \
return length == 0 || ucdr_serialize_endian_array ## SUFFIX(ub, endianness, array, length); \
} \

// -------------------------------------------------------------------
Expand All @@ -50,12 +50,12 @@ inline void ucdr_deserialize_sequence_header(ucdrBuffer* ub, ucdrEndianness endi
bool ucdr_deserialize_sequence ## SUFFIX(ucdrBuffer* ub, TYPE* array, size_t array_capacity, uint32_t* length) \
{ \
ucdr_deserialize_sequence_header(ub, ub->endianness, array_capacity, length); \
return ucdr_deserialize_endian_array ## SUFFIX(ub, ub->endianness, array, *length); \
return *length == 0 || ucdr_deserialize_endian_array ## SUFFIX(ub, ub->endianness, array, *length); \
} \
bool ucdr_deserialize_endian_sequence ## SUFFIX(ucdrBuffer* ub, ucdrEndianness endianness, TYPE* array, size_t array_capacity, uint32_t* length) \
{ \
ucdr_deserialize_sequence_header(ub, endianness, array_capacity, length); \
return ucdr_deserialize_endian_array ## SUFFIX(ub, endianness, array, *length); \
return *length == 0 || ucdr_deserialize_endian_array ## SUFFIX(ub, endianness, array, *length); \
} \

// -------------------------------------------------------------------
Expand Down
7 changes: 5 additions & 2 deletions test/CMakeLists.txt
Expand Up @@ -25,6 +25,7 @@ endif()
check_gtest()
if(GTEST_FOUND)
include(CTest)
find_package(fastcdr REQUIRED)
set(SRCS
CommonFunctions.cpp
serialization/BasicSerialization.cpp
Expand All @@ -40,6 +41,7 @@ if(GTEST_FOUND)
Alignment.cpp
FullBuffer.cpp
FullBufferCallback.cpp
cross_serialization/FastCDRSerialization.cpp
)
add_executable(${PROJECT_NAME} ${SRCS})
set_common_compile_options(${PROJECT_NAME})
Expand All @@ -48,9 +50,10 @@ if(GTEST_FOUND)
${SRCS}
DEPENDENCIES
microcdr
fastcdr
)
target_include_directories(${PROJECT_NAME} PRIVATE ${GTEST_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} microcdr ${GTEST_BOTH_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${GTEST_INCLUDE_DIRS} ${FASTCDR_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} microcdr fastcdr ${GTEST_BOTH_LIBRARIES})

set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 11
Expand Down
22 changes: 22 additions & 0 deletions test/cross_serialization/FastCDRSerialization.cpp
@@ -0,0 +1,22 @@
// Copyright 2017 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "FastCDRSerialization.hpp"

TEST_F(FastCDRSerialization, double_sequence_with_aligment_serialization)
{
double_sequence_with_aligment_serialization();
}


78 changes: 78 additions & 0 deletions test/cross_serialization/FastCDRSerialization.hpp
@@ -0,0 +1,78 @@
// Copyright 2017 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef _FASTCDR_SERIALIZATION_HPP_
#define _FASTCDR_SERIALIZATION_HPP_

#include <gtest/gtest.h>
#include <ucdr/microcdr.h>

#include <fastcdr/Cdr.h>
#include <fastcdr/FastCdr.h>
#include <fastcdr/exceptions/Exception.h>

#define BUFFER_LENGTH 2014

class FastCDRSerialization : public ::testing::Test
{
public:

void double_sequence_with_aligment_serialization()
{
using namespace eprosima::fastcdr;

char buffer_fastcdr[BUFFER_LENGTH] = {0};

ucdrBuffer reader_fastcdr;
ucdr_init_buffer(&reader_fastcdr, reinterpret_cast<uint8_t *>(buffer_fastcdr), BUFFER_LENGTH);

FastBuffer cdrbuffer(buffer_fastcdr, BUFFER_LENGTH);
Cdr cdr_ser(cdrbuffer);

// Serialize 2 bytes
const uint8_t octet_array[2] = {0xAA, 0xAA};
cdr_ser.serializeArray(octet_array, 2);

uint8_t octet_array_out[2] = {};
EXPECT_TRUE(ucdr_deserialize_array_uint8_t(&reader_fastcdr, octet_array_out, 2));
EXPECT_EQ(octet_array[0], octet_array_out[0]);
EXPECT_EQ(octet_array[1], octet_array_out[1]);

// Serialize 2 doubles = 16 Bytes + 1 Header
const double double_seq[2] = {3.14, 3.14};
cdr_ser.serializeSequence(double_seq, 2);

double double_seq_out[2];
uint32_t double_seq_len;
EXPECT_TRUE(ucdr_deserialize_sequence_double(&reader_fastcdr, double_seq_out, 2, &double_seq_len));
EXPECT_EQ(double_seq_len, 2u);

// Serialize 0 doubles = 1 header
std::vector<double> double_vector;
cdr_ser.serializeSequence(double_seq, 0);

EXPECT_TRUE(ucdr_deserialize_sequence_double(&reader_fastcdr, double_seq_out, 2, &double_seq_len));
EXPECT_EQ(double_seq_len, 0u);

// Serialize 2 bytes
cdr_ser.serializeArray(octet_array, 2);

EXPECT_TRUE(ucdr_deserialize_array_uint8_t(&reader_fastcdr, octet_array_out, 2));
EXPECT_EQ(octet_array[0], octet_array_out[0]);
EXPECT_EQ(octet_array[1], octet_array_out[1]);

}
};

#endif //_FASTCDR_SERIALIZATION_HPP_

0 comments on commit 5b188cc

Please sign in to comment.