1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
// Copyright 2024 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 <array>
#include <memory>
#include <tuple>
#include <vector>
#include <gtest/gtest.h>
#include <fastcdr/Cdr.h>
#include "utility.hpp"
using namespace eprosima::fastcdr;
using XCdrStreamValues =
std::array<std::vector<uint8_t>,
1 + EncodingAlgorithmFlag::PL_CDR2 + Cdr::Endianness::LITTLE_ENDIANNESS>;
class XCdrFinalTest : public ::testing::TestWithParam< std::tuple<EncodingAlgorithmFlag, Cdr::Endianness>>
{
};
struct FiInnerStructure
{
public:
FiInnerStructure() = default;
FiInnerStructure(
eprosima::fastcdr::EncodingAlgorithmFlag e1,
eprosima::fastcdr::EncodingAlgorithmFlag e2
)
: enc_xcdrv1(e1)
, enc_xcdrv2(e2)
{
}
FiInnerStructure(
eprosima::fastcdr::EncodingAlgorithmFlag e1,
eprosima::fastcdr::EncodingAlgorithmFlag e2,
uint8_t value
)
: value1(value)
, enc_xcdrv1(e1)
, enc_xcdrv2(e2)
{
}
bool operator ==(
const FiInnerStructure& other) const
{
return value1 == other.value1 &&
value2.has_value() == other.value2.has_value() &&
(!value2.has_value() || value2.value() == other.value2.value());
}
//! First being serialized.
uint32_t value1 {0};
//! Second being serialized.
eprosima::fastcdr::optional<uint32_t> value2;
eprosima::fastcdr::EncodingAlgorithmFlag enc_xcdrv1 {eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR};
eprosima::fastcdr::EncodingAlgorithmFlag enc_xcdrv2 {eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR2};
};
namespace eprosima {
namespace fastcdr {
template<>
void serialize(
Cdr& cdr,
const FiInnerStructure& data)
{
Cdr::state current_status(cdr);
cdr.begin_serialize_type(current_status, cdr.get_cdr_version() == eprosima::fastcdr::CdrVersion::XCDRv1
? data.enc_xcdrv1
: data.enc_xcdrv2);
cdr << MemberId(0) << data.value1;
cdr << MemberId(1) << data.value2;
cdr.end_serialize_type(current_status);
}
template<>
void deserialize(
Cdr& cdr,
FiInnerStructure& data)
{
cdr.deserialize_type(cdr.get_cdr_version() == eprosima::fastcdr::CdrVersion::XCDRv1
? data.enc_xcdrv1
: data.enc_xcdrv2,
[&data](Cdr& cdr_inner, const MemberId& mid) -> bool
{
bool ret_value {true};
switch (mid.id)
{
case 0:
cdr_inner >> data.value1;
break;
case 1:
cdr_inner >> data.value2;
break;
default:
ret_value = false;
break;
}
return ret_value;
});
}
} // namespace fastcdr
} // namespace eprosima
/*!
* @test Test an inner appendable structure inside a final structure.
* @code{.idl}
* @appendable
* struct InnerAppendableStructure
* {
* @id(0)
* unsigned long value1;
* @id(1) @optional
* unsigned long value2;
* };
*
* @final
* struct FinalWithInnerAppendableStruct
* {
* @id(1)
* unsigned long value1;
* @id(2)
* InnerAppendableStructure value2;
* };
* @endcode
*/
TEST_P(XCdrFinalTest, inner_appendable_structure)
{
constexpr uint8_t ival {0xCD};
//{ Defining expected XCDR streams
XCdrStreamValues expected_streams;
expected_streams[0 + EncodingAlgorithmFlag::PLAIN_CDR + Cdr::Endianness::BIG_ENDIANNESS] =
{
0x00, 0x00, 0x00, 0x00, // Encapsulation
0x00, 0x00, 0x00, ival, // ULong
0x00, 0x00, 0x00, ival, // ULong
0x00, 0x01, 0x00, 0x00, // ShortMemberHeader (optional)
};
expected_streams[0 + EncodingAlgorithmFlag::PLAIN_CDR + Cdr::Endianness::LITTLE_ENDIANNESS] =
{
0x00, 0x01, 0x00, 0x00, // Encapsulation
ival, 0x00, 0x00, 0x00, // ULong
ival, 0x00, 0x00, 0x00, // ULong
0x01, 0x00, 0x00, 0x00, // ShortMemberHeader (optional)
};
expected_streams[0 + EncodingAlgorithmFlag::PLAIN_CDR2 + Cdr::Endianness::BIG_ENDIANNESS] =
{
0x00, 0x06, 0x00, 0x03, // Encapsulation
0x00, 0x00, 0x00, ival, // ULong
0x00, 0x00, 0x00, 0x05, // DHEADER
0x00, 0x00, 0x00, ival, // ULong
0x00, // Optional not present
};
expected_streams[0 + EncodingAlgorithmFlag::PLAIN_CDR2 + Cdr::Endianness::LITTLE_ENDIANNESS] =
{
0x00, 0x07, 0x00, 0x03, // Encapsulation
ival, 0x00, 0x00, 0x00, // ULong
0x05, 0x00, 0x00, 0x00, // DHEADER
ival, 0x00, 0x00, 0x00, // ULong
0x00, // Optional not present
};
//}
EncodingAlgorithmFlag encoding = std::get<0>(GetParam());
Cdr::Endianness endianness = std::get<1>(GetParam());
//{ Prepare buffer
uint8_t tested_stream = 0 + encoding + endianness;
auto buffer =
std::unique_ptr<char, void (*)(
void*)>{reinterpret_cast<char*>(calloc(expected_streams[tested_stream].size(), sizeof(char))), free};
FastBuffer fast_buffer(buffer.get(), expected_streams[tested_stream].size());
Cdr cdr(fast_buffer, endianness, get_version_from_algorithm(encoding));
//}
//{ Encode
uint32_t value1 {ival};
FiInnerStructure value2 {eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR,
eprosima::fastcdr::EncodingAlgorithmFlag::DELIMIT_CDR2, ival};
cdr.set_encoding_flag(encoding);
cdr.serialize_encapsulation();
Cdr::state enc_state(cdr);
cdr.begin_serialize_type(enc_state, encoding);
cdr << MemberId(1) << value1;
cdr << MemberId(2) << value2;
cdr.end_serialize_type(enc_state);
cdr.set_dds_cdr_options({0, 0});
Cdr::state enc_state_end(cdr);
//}
//{ Test encoded content
ASSERT_EQ(cdr.get_serialized_data_length(), expected_streams[tested_stream].size());
ASSERT_EQ(0, memcmp(buffer.get(), expected_streams[tested_stream].data(),
expected_streams[tested_stream].size()));
//}
//{ Decoding
uint32_t dvalue1 {0};
FiInnerStructure dvalue2 {eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR,
eprosima::fastcdr::EncodingAlgorithmFlag::DELIMIT_CDR2, ival};
cdr.reset();
cdr.read_encapsulation();
ASSERT_EQ(cdr.get_encoding_flag(), encoding);
ASSERT_EQ(cdr.endianness(), endianness);
cdr.deserialize_type(encoding, [&](Cdr& cdr_inner, const MemberId& mid)->bool
{
bool ret_value {true};
switch (mid.id)
{
case 0:
cdr_inner >> dvalue1;
break;
case 1:
cdr_inner >> dvalue2;
break;
default:
ret_value = false;
break;
}
return ret_value;
});
ASSERT_EQ(value1, dvalue1);
ASSERT_EQ(value2, dvalue2);
Cdr::state dec_state_end(cdr);
ASSERT_EQ(enc_state_end, dec_state_end);
//}
}
/*!
* @test Test an inner mutable structure inside a final structure.
* @code{.idl}
* @mutable
* struct InnerMutableStructure
* {
* @id(0)
* unsigned long value1;
* @id(1) @optional
* unsigned long value2;
* };
*
* @final
* struct FinalWithInnerMutableStruct
* {
* @id(1)
* unsigned long value1;
* @id(2)
* InnerMutableStructure value2;
* };
* @endcode
*/
TEST_P(XCdrFinalTest, inner_mutable_structure)
{
constexpr uint8_t ival {0xCD};
//{ Defining expected XCDR streams
XCdrStreamValues expected_streams;
expected_streams[0 + EncodingAlgorithmFlag::PLAIN_CDR + Cdr::Endianness::BIG_ENDIANNESS] =
{
0x00, 0x00, 0x00, 0x00, // Encapsulation
0x00, 0x00, 0x00, ival, // ULong
0x00, 0x00, 0x00, 0x04, // ShortMemberHeader
0x00, 0x00, 0x00, ival, // ULong
0x3F, 0x02, 0x00, 0x00, // Sentinel
};
expected_streams[0 + EncodingAlgorithmFlag::PLAIN_CDR + Cdr::Endianness::LITTLE_ENDIANNESS] =
{
0x00, 0x01, 0x00, 0x00, // Encapsulation
ival, 0x00, 0x00, 0x00, // ULong
0x00, 0x00, 0x04, 0x00, // ShortMemberHeader
ival, 0x00, 0x00, 0x00, // ULong
0x02, 0x3F, 0x00, 0x00, // Sentinel
};
expected_streams[0 + EncodingAlgorithmFlag::PLAIN_CDR2 + Cdr::Endianness::BIG_ENDIANNESS] =
{
0x00, 0x06, 0x00, 0x00, // Encapsulation
0x00, 0x00, 0x00, ival, // ULong
0x00, 0x00, 0x00, 0x08, // DHEADER
0x20, 0x00, 0x00, 0x00, // EMHEADER1(M) without NEXTINT
0x00, 0x00, 0x00, ival, // ULong
};
expected_streams[0 + EncodingAlgorithmFlag::PLAIN_CDR2 + Cdr::Endianness::LITTLE_ENDIANNESS] =
{
0x00, 0x07, 0x00, 0x00, // Encapsulation
ival, 0x00, 0x00, 0x00, // ULong
0x08, 0x00, 0x00, 0x00, // DHEADER
0x00, 0x00, 0x00, 0x20, // EMHEADER1(M) without NEXTINT
ival, 0x00, 0x00, 0x00, // ULong
};
//}
EncodingAlgorithmFlag encoding = std::get<0>(GetParam());
Cdr::Endianness endianness = std::get<1>(GetParam());
//{ Prepare buffer
uint8_t tested_stream = 0 + encoding + endianness;
auto buffer =
std::unique_ptr<char, void (*)(
void*)>{reinterpret_cast<char*>(calloc(expected_streams[tested_stream].size(), sizeof(char))), free};
FastBuffer fast_buffer(buffer.get(), expected_streams[tested_stream].size());
Cdr cdr(fast_buffer, endianness, get_version_from_algorithm(encoding));
//}
//{ Encode
uint32_t value1 {ival};
FiInnerStructure value2 {eprosima::fastcdr::EncodingAlgorithmFlag::PL_CDR,
eprosima::fastcdr::EncodingAlgorithmFlag::PL_CDR2, ival};
cdr.set_encoding_flag(encoding);
cdr.serialize_encapsulation();
Cdr::state enc_state(cdr);
cdr.begin_serialize_type(enc_state, encoding);
cdr << MemberId(1) << value1;
cdr << MemberId(2) << value2;
cdr.end_serialize_type(enc_state);
cdr.set_dds_cdr_options({0, 0});
Cdr::state enc_state_end(cdr);
//}
//{ Test encoded content
ASSERT_EQ(cdr.get_serialized_data_length(), expected_streams[tested_stream].size());
ASSERT_EQ(0, memcmp(buffer.get(), expected_streams[tested_stream].data(),
expected_streams[tested_stream].size()));
//}
//{ Decoding
uint32_t dvalue1 {0};
FiInnerStructure dvalue2 {eprosima::fastcdr::EncodingAlgorithmFlag::PL_CDR,
eprosima::fastcdr::EncodingAlgorithmFlag::PL_CDR2, ival};
cdr.reset();
cdr.read_encapsulation();
ASSERT_EQ(cdr.get_encoding_flag(), encoding);
ASSERT_EQ(cdr.endianness(), endianness);
cdr.deserialize_type(encoding, [&](Cdr& cdr_inner, const MemberId& mid)->bool
{
bool ret_value {true};
switch (mid.id)
{
case 0:
cdr_inner >> dvalue1;
break;
case 1:
cdr_inner >> dvalue2;
break;
default:
ret_value = false;
break;
}
return ret_value;
});
ASSERT_EQ(value1, dvalue1);
ASSERT_EQ(value2, dvalue2);
Cdr::state dec_state_end(cdr);
ASSERT_EQ(enc_state_end, dec_state_end);
//}
}
INSTANTIATE_TEST_SUITE_P(
XCdrTest,
XCdrFinalTest,
::testing::Values(
std::make_tuple(EncodingAlgorithmFlag::PLAIN_CDR, Cdr::Endianness::BIG_ENDIANNESS),
std::make_tuple(EncodingAlgorithmFlag::PLAIN_CDR, Cdr::Endianness::LITTLE_ENDIANNESS),
std::make_tuple(EncodingAlgorithmFlag::PLAIN_CDR2, Cdr::Endianness::BIG_ENDIANNESS),
std::make_tuple(EncodingAlgorithmFlag::PLAIN_CDR2, Cdr::Endianness::LITTLE_ENDIANNESS)
));
|