Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions include/quicr/detail/ctrl_message_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,9 @@ namespace quicr::messages {
{
if constexpr (std::is_arithmetic_v<T> || std::is_enum_v<T>) {
if (static_cast<uint64_t>(type) % 2 == 0) {
UintVar u_value(static_cast<uint64_t>(value));
return Bytes{ u_value.begin(), u_value.end() };
const std::uint64_t val = static_cast<std::uint64_t>(value);
auto* val_bytes = reinterpret_cast<const std::uint8_t*>(&val);
return Bytes{ val_bytes, val_bytes + sizeof(val) };
}
}

Expand Down Expand Up @@ -459,7 +460,10 @@ namespace quicr::messages {
{
if constexpr (std::is_arithmetic_v<T>) {
if (static_cast<std::uint64_t>(type) % 2 == 0) {
return static_cast<T>(UintVar(extensions.at(static_cast<std::uint64_t>(type))).Get());
std::uint64_t val = 0;
const auto& bytes = extensions.at(static_cast<std::uint64_t>(type));
std::memcpy(&val, bytes.data(), std::min(bytes.size(), sizeof(val)));
return static_cast<T>(val);
}
}

Expand All @@ -477,7 +481,10 @@ namespace quicr::messages {
{
if constexpr (std::is_arithmetic_v<T>) {
if (static_cast<std::uint64_t>(type) % 2 == 0) {
return static_cast<T>(UintVar(immutable_extensions.at(static_cast<std::uint64_t>(type))).Get());
std::uint64_t val = 0;
const auto& bytes = immutable_extensions.at(static_cast<std::uint64_t>(type));
std::memcpy(&val, bytes.data(), std::min(bytes.size(), sizeof(val)));
return static_cast<T>(val);
}
}

Expand Down Expand Up @@ -523,8 +530,9 @@ namespace quicr::messages {
{
if constexpr (std::is_arithmetic_v<T> || std::is_enum_v<T>) {
if (static_cast<uint64_t>(type) % 2 == 0) {
UintVar u_value(static_cast<uint64_t>(value));
parameters.push_back({ type, Bytes{ u_value.begin(), u_value.end() } });
const std::uint64_t val = static_cast<std::uint64_t>(value);
auto* val_bytes = reinterpret_cast<const std::uint8_t*>(&val);
parameters.push_back({ type, Bytes{ val_bytes, val_bytes + sizeof(val) } });
return *this;
}
}
Expand Down Expand Up @@ -584,7 +592,9 @@ namespace quicr::messages {

if constexpr (std::is_arithmetic_v<T>) {
if (static_cast<std::uint64_t>(type) % 2 == 0) {
return static_cast<T>(UintVar(bytes).Get());
std::uint64_t val = 0;
std::memcpy(&val, bytes.data(), std::min(bytes.size(), sizeof(val)));
return static_cast<T>(val);
}
}

Expand Down
76 changes: 76 additions & 0 deletions test/moq_ctrl_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,79 @@ TEST_CASE("uint16_t encode/decode")
{
IntegerEncodeDecode<std::uint16_t>(true);
}

TEST_CASE("KeyValuePair even-type round-trip preserves values")
{
const std::vector<std::uint64_t> test_values = {
0, 1,
63, // Max 1-byte varint
64, // Min 2-byte varint
127, 128, 255,
16383, // Max 2-byte varint
16384, // Min 4-byte varint
100000,
};

for (const auto value : test_values) {
CAPTURE(value);

Parameters params;
params.Add(ParameterType::kDeliveryTimeout, value);

Bytes buffer;
buffer << params;

// Should have encoded as uintvar.
UintVar expected(value);
Bytes expected_bytes{ expected.begin(), expected.end() };
REQUIRE(buffer.size() >= expected_bytes.size());
Bytes tail(buffer.end() - expected_bytes.size(), buffer.end());
CHECK_EQ(tail, expected_bytes);

Parameters out;
BytesSpan span{ buffer };
span >> out;

// Roundtrip.
CHECK_NOTHROW(out.Get<std::uint64_t>(ParameterType::kDeliveryTimeout));
CHECK_EQ(out.Get<std::uint64_t>(ParameterType::kDeliveryTimeout), value);
}
}

TEST_CASE("TrackExtensions even-type round-trip preserves values")
{
const std::vector<std::uint64_t> test_values = {
0, 1,
63, // Max 1-byte varint
64, // Min 2-byte varint
127, 128, 255,
16383, // Max 2-byte varint
16384, // Min 4-byte varint
100000,
};

for (const auto value : test_values) {
CAPTURE(value);

TrackExtensions ext;
ext.Add(ExtensionType::kDeliveryTimeout, value);

Bytes buffer;
buffer << ext;

// Should have been encoded as uintvar.
UintVar expected(value);
Bytes expected_bytes{ expected.begin(), expected.end() };
REQUIRE(buffer.size() >= expected_bytes.size());
Bytes tail(buffer.end() - expected_bytes.size(), buffer.end());
CHECK_EQ(tail, expected_bytes);

TrackExtensions out;
BytesSpan span{ buffer };
span >> out;

// Roundtrip.
CHECK_NOTHROW(out.Get<std::uint64_t>(ExtensionType::kDeliveryTimeout));
CHECK_EQ(out.Get<std::uint64_t>(ExtensionType::kDeliveryTimeout), value);
}
}
Loading