blob: a6664eb5af988ce11cbafe19644796ac42e44579 [file]
// Copyright (c) 2026 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "quiche/quic/moqt/moqt_key_value_pair.h"
#include <cstdint>
#include <optional>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "quiche/quic/core/quic_time.h"
#include "quiche/quic/moqt/moqt_names.h"
#include "quiche/quic/moqt/moqt_priority.h"
#include "quiche/quic/moqt/moqt_types.h"
#include "quiche/quic/platform/api/quic_test.h"
#include "quiche/common/platform/api/quiche_expect_bug.h"
#include "quiche/common/test_tools/quiche_test_utils.h"
namespace moqt::test {
static_assert(std::is_nothrow_move_constructible_v<KeyValuePairList>);
static_assert(std::is_nothrow_move_assignable_v<KeyValuePairList>);
static_assert(!std::is_copy_constructible_v<MoqtProperties>);
static_assert(!std::is_move_constructible_v<MoqtProperties>);
static_assert(!std::is_copy_assignable_v<MoqtProperties>);
static_assert(!std::is_move_assignable_v<MoqtProperties>);
static_assert(std::is_nothrow_move_constructible_v<TrackProperties>);
static_assert(std::is_nothrow_move_assignable_v<TrackProperties>);
static_assert(std::is_nothrow_move_constructible_v<ObjectProperties>);
static_assert(std::is_nothrow_move_assignable_v<ObjectProperties>);
class LocationTest : public quic::test::QuicTest {};
TEST_F(LocationTest, LocationTests) {
Location location;
EXPECT_EQ(location, Location(0, 0));
EXPECT_EQ(location.Next(), Location(0, 1));
EXPECT_LT(Location(4, 20), Location(5, 0));
EXPECT_LT(Location(4, 0), Location(4, 1));
}
class AuthTokenTest : public quic::test::QuicTest {};
TEST_F(AuthTokenTest, Delete) {
AuthToken token(1, AuthTokenAliasType::kDelete);
EXPECT_EQ(token.alias_type, AuthTokenAliasType::kDelete);
EXPECT_EQ(token.alias, 1);
EXPECT_FALSE(token.type.has_value());
EXPECT_FALSE(token.value.has_value());
}
TEST_F(AuthTokenTest, Register) {
AuthToken token(1, AuthTokenType::kOutOfBand, "token");
EXPECT_EQ(token.alias_type, AuthTokenAliasType::kRegister);
EXPECT_EQ(token.alias, 1);
EXPECT_EQ(token.type, AuthTokenType::kOutOfBand);
EXPECT_EQ(token.value, "token");
}
TEST_F(AuthTokenTest, UseAlias) {
AuthToken token(1, AuthTokenAliasType::kUseAlias);
EXPECT_EQ(token.alias_type, AuthTokenAliasType::kUseAlias);
EXPECT_EQ(token.alias, 1);
EXPECT_FALSE(token.type.has_value());
EXPECT_FALSE(token.value.has_value());
}
TEST_F(AuthTokenTest, UseValue) {
AuthToken token(AuthTokenType::kOutOfBand, "token");
EXPECT_EQ(token.alias_type, AuthTokenAliasType::kUseValue);
EXPECT_EQ(token.type, AuthTokenType::kOutOfBand);
EXPECT_EQ(token.value, "token");
}
class LocationFilterTest : public quic::test::QuicTest {};
TEST_F(LocationFilterTest, NoFilter) {
LocationFilter filter = LocationFilter::NoLocationFilter();
EXPECT_TRUE(filter.WindowKnown());
EXPECT_TRUE(filter.InWindow(Location(0, 0)));
EXPECT_TRUE(filter.InWindow(0));
}
TEST_F(LocationFilterTest, RelativeStart) {
LocationFilter filter = LocationFilter::RelativeStartFilter(0);
EXPECT_FALSE(filter.WindowKnown());
EXPECT_FALSE(filter.is_next_object());
EXPECT_EQ(filter.relative_start_offset(), 0);
filter.ConvertRelativeToAbsolute(Location(3, 6));
EXPECT_TRUE(filter.WindowKnown());
EXPECT_EQ(filter.relative_start_offset(), std::nullopt);
EXPECT_TRUE(filter.InWindow(Location(4, 0)));
EXPECT_FALSE(filter.InWindow(Location(3, 7)));
EXPECT_TRUE(filter.InWindow(4));
EXPECT_FALSE(filter.InWindow(3));
}
TEST_F(LocationFilterTest, RelativeStartWithOffset) {
LocationFilter filter = LocationFilter::RelativeStartFilter(2);
EXPECT_FALSE(filter.WindowKnown());
EXPECT_FALSE(filter.is_next_object());
EXPECT_EQ(filter.relative_start_offset(), 2);
filter.ConvertRelativeToAbsolute(Location(3, 6));
EXPECT_TRUE(filter.WindowKnown());
EXPECT_EQ(filter.start(), Location(2, 0));
EXPECT_TRUE(filter.InWindow(Location(2, 0)));
EXPECT_FALSE(filter.InWindow(Location(1, 7)));
}
TEST_F(LocationFilterTest, NextObject) {
LocationFilter filter = LocationFilter::NextObjectFilter();
EXPECT_FALSE(filter.WindowKnown());
EXPECT_TRUE(filter.is_next_object());
EXPECT_EQ(filter.relative_start_offset(), std::nullopt);
filter.ConvertRelativeToAbsolute(Location(3, 6));
EXPECT_TRUE(filter.WindowKnown());
EXPECT_FALSE(filter.is_next_object());
EXPECT_TRUE(filter.InWindow(Location(4, 0)));
EXPECT_TRUE(filter.InWindow(Location(3, 7)));
EXPECT_FALSE(filter.InWindow(Location(3, 6)));
EXPECT_TRUE(filter.InWindow(4));
EXPECT_TRUE(filter.InWindow(3));
EXPECT_FALSE(filter.InWindow(2));
}
TEST_F(LocationFilterTest, NextObjectNoObjectsYet) {
LocationFilter filter = LocationFilter::NextObjectFilter();
EXPECT_FALSE(filter.WindowKnown());
filter.ConvertRelativeToAbsolute(std::nullopt);
EXPECT_TRUE(filter.WindowKnown());
EXPECT_TRUE(filter.InWindow(Location(0, 0)));
EXPECT_TRUE(filter.InWindow(0));
}
TEST_F(LocationFilterTest, AbsoluteStart) {
LocationFilter filter = LocationFilter::AbsoluteStartFilter(Location(3, 6));
EXPECT_TRUE(filter.WindowKnown());
EXPECT_TRUE(filter.InWindow(Location(4, 0)));
EXPECT_TRUE(filter.InWindow(Location(3, 6)));
EXPECT_FALSE(filter.InWindow(Location(3, 5)));
EXPECT_TRUE(filter.InWindow(4));
EXPECT_TRUE(filter.InWindow(3));
EXPECT_FALSE(filter.InWindow(2));
}
TEST_F(LocationFilterTest, AbsoluteStartGroupEnd) {
LocationFilter filter =
LocationFilter::AbsoluteStartGroupEndFilter(Location(3, 6), 5);
EXPECT_TRUE(filter.WindowKnown());
EXPECT_EQ(filter.start(), Location(3, 6));
EXPECT_EQ(filter.end_group(), 5);
EXPECT_EQ(filter.end(), Location(5, kMaxObjectId));
EXPECT_TRUE(filter.InWindow(Location(4, 0)));
EXPECT_TRUE(filter.InWindow(Location(3, 6)));
EXPECT_TRUE(filter.InWindow(Location(5, kMaxObjectId)));
EXPECT_FALSE(filter.InWindow(Location(3, 5)));
EXPECT_FALSE(filter.InWindow(Location(6, 0)));
EXPECT_TRUE(filter.InWindow(5));
EXPECT_TRUE(filter.InWindow(3));
EXPECT_FALSE(filter.InWindow(2));
EXPECT_FALSE(filter.InWindow(6));
}
TEST_F(LocationFilterTest, AbsoluteRange) {
LocationFilter filter =
LocationFilter::AbsoluteRangeFilter(Location(3, 6), Location(5, 10));
EXPECT_TRUE(filter.WindowKnown());
EXPECT_EQ(filter.start(), Location(3, 6));
EXPECT_EQ(filter.end_group(), 5);
EXPECT_EQ(filter.end(), Location(5, 10));
EXPECT_TRUE(filter.InWindow(Location(4, 0)));
EXPECT_TRUE(filter.InWindow(Location(3, 6)));
EXPECT_TRUE(filter.InWindow(Location(5, 10)));
EXPECT_FALSE(filter.InWindow(Location(5, 11)));
EXPECT_FALSE(filter.InWindow(Location(3, 5)));
EXPECT_FALSE(filter.InWindow(Location(6, 0)));
EXPECT_TRUE(filter.InWindow(5));
EXPECT_TRUE(filter.InWindow(3));
EXPECT_FALSE(filter.InWindow(2));
EXPECT_FALSE(filter.InWindow(6));
LocationFilter single_object =
LocationFilter::AbsoluteRangeFilter(Location(3, 6), Location(3, 6));
EXPECT_TRUE(single_object.WindowKnown());
EXPECT_EQ(single_object.start(), Location(3, 6));
EXPECT_EQ(single_object.end(), Location(3, 6));
EXPECT_TRUE(single_object.InWindow(Location(3, 6)));
EXPECT_FALSE(single_object.InWindow(Location(3, 5)));
EXPECT_FALSE(single_object.InWindow(Location(3, 7)));
}
TEST_F(LocationFilterTest, TrimEndLocation) {
LocationFilter filter = LocationFilter::RelativeStartFilter(2);
filter.ConvertRelativeToAbsolute(Location(3, 6));
EXPECT_TRUE(filter.WindowKnown());
EXPECT_EQ(filter.start(), Location(2, 0));
EXPECT_EQ(filter.end(), Location(kMaxGroupId, kMaxObjectId));
EXPECT_THAT(filter.TrimEndLocation(Location(1, 9)),
quiche::test::StatusIs(absl::StatusCode::kOutOfRange));
EXPECT_EQ(filter.end(), Location(kMaxGroupId, kMaxObjectId));
QUICHE_EXPECT_OK(filter.TrimEndLocation(Location(3, 6)));
EXPECT_EQ(filter.end(), Location(3, 6));
EXPECT_TRUE(filter.InWindow(Location(3, 6)));
EXPECT_FALSE(filter.InWindow(Location(3, 7)));
QUICHE_EXPECT_OK(filter.TrimEndLocation(Location(4, 0)));
EXPECT_EQ(filter.end(), Location(3, 6));
}
TEST_F(LocationFilterTest, InvalidConstructors) {
LocationFilter filter = LocationFilter::NoLocationFilter();
EXPECT_QUICHE_BUG(
filter = LocationFilter::AbsoluteStartGroupEndFilter(Location(3, 6), 2),
"End group is before start location");
EXPECT_EQ(filter, LocationFilter::NoLocationFilter());
EXPECT_QUICHE_BUG(filter = LocationFilter::AbsoluteRangeFilter(
Location(3, 6), Location(3, 5)),
"End location is before start location");
EXPECT_EQ(filter, LocationFilter::NoLocationFilter());
EXPECT_QUICHE_BUG(filter = LocationFilter::AbsoluteRangeFilter(
Location(3, 6), Location(2, 10)),
"End location is before start location");
EXPECT_EQ(filter, LocationFilter::NoLocationFilter());
}
class MessageParametersTest : public quic::test::QuicTest {};
TEST_F(MessageParametersTest, Update) {
MessageParameters p1;
p1.object_delivery_timeout = quic::QuicTimeDelta::FromMilliseconds(10);
p1.expires = quic::QuicTimeDelta::FromMilliseconds(100);
p1.set_forward(false);
p1.subscriber_priority = 100;
p1.fill_parameters = FillParameters{
.fill_timeout = quic::QuicTimeDelta::FromMilliseconds(50),
.subscriber_priority = 5,
};
p1.new_group_request = 1;
p1.track_namespace_prefix = TrackNamespace({"foo"});
MessageParameters p2;
p2.object_delivery_timeout = quic::QuicTimeDelta::FromMilliseconds(20);
p2.authorization_tokens.push_back(
AuthToken(AuthTokenType::kOutOfBand, "token"));
p2.set_forward(true);
p2.group_order = MoqtGroupOrder::kDescending;
p2.track_namespace_prefix = TrackNamespace({"bar"});
p1.Update(p2);
EXPECT_EQ(p1.object_delivery_timeout,
quic::QuicTimeDelta::FromMilliseconds(20));
EXPECT_EQ(p1.expires, quic::QuicTimeDelta::FromMilliseconds(100));
ASSERT_EQ(p1.authorization_tokens.size(), 1);
EXPECT_EQ(p1.authorization_tokens[0],
AuthToken(AuthTokenType::kOutOfBand, "token"));
EXPECT_TRUE(p1.forward());
EXPECT_EQ(p1.subscriber_priority, 100);
EXPECT_EQ(p1.group_order, std::nullopt);
EXPECT_EQ(p1.fill_parameters, std::nullopt);
EXPECT_EQ(p1.new_group_request, 1);
EXPECT_EQ(p1.track_namespace_prefix, TrackNamespace({"bar"}));
}
TEST_F(MessageParametersTest, UpdateLocationFilter) {
// 1. Uninitialized existing filter gets initialized on Update even if
// largest_object is not set.
MessageParameters params_no_largest(LocationFilter::NextObjectFilter());
EXPECT_FALSE(params_no_largest.location_filter->WindowKnown());
params_no_largest.Update(MessageParameters());
EXPECT_TRUE(params_no_largest.location_filter->WindowKnown());
EXPECT_EQ(params_no_largest.location_filter->start(), Location(0, 0));
// 2. Uninitialized existing filter gets initialized with largest_object on
// Update.
MessageParameters params(LocationFilter::NextObjectFilter());
params.set_forward(false);
EXPECT_FALSE(params.location_filter->WindowKnown());
MessageParameters update1;
update1.largest_object = Location(1, 2);
params.Update(update1);
EXPECT_TRUE(params.location_filter->WindowKnown());
EXPECT_EQ(params.location_filter->start(), Location(1, 3));
// 3. Once the filter window is known, subsequent Updates do not re-initialize
// it even if largest_object or forward changes.
MessageParameters update2;
update2.largest_object = Location(2, 4);
update2.set_forward(true);
params.Update(update2);
EXPECT_TRUE(params.forward());
EXPECT_EQ(params.location_filter->start(), Location(1, 3));
// 4. New filter in Update is initialized with largest_object.
MessageParameters update3(LocationFilter::RelativeStartFilter(0));
update3.largest_object = Location(3, 1);
params.Update(update3);
EXPECT_TRUE(params.location_filter->WindowKnown());
EXPECT_EQ(params.location_filter->start(), Location(4, 0));
}
class TrackPropertiesTest : public quic::test::QuicTest {};
TEST_F(TrackPropertiesTest, DefaultConstructor) {
TrackProperties properties;
EXPECT_TRUE(properties.Validate());
EXPECT_EQ(properties.size(), 0);
EXPECT_EQ(properties.object_delivery_timeout(), kDefaultDeliveryTimeout);
EXPECT_EQ(properties.max_cache_duration(), kDefaultMaxCacheDuration);
EXPECT_EQ(properties.subgroup_delivery_timeout(), kDefaultDeliveryTimeout);
EXPECT_EQ(properties.default_publisher_priority(), kDefaultPublisherPriority);
EXPECT_EQ(properties.default_publisher_group_order(), kDefaultGroupOrder);
EXPECT_EQ(properties.dynamic_groups(), kDefaultDynamicGroups);
EXPECT_TRUE(properties.immutable_properties().empty());
}
TEST_F(TrackPropertiesTest, AllProperties) {
TrackProperties properties({
{PropertyType::kObjectDeliveryTimeout,
quic::QuicTimeDelta::FromMilliseconds(1)},
{PropertyType::kMaxCacheDuration,
quic::QuicTimeDelta::FromMilliseconds(2)},
{PropertyType::kSubgroupDeliveryTimeout,
quic::QuicTimeDelta::FromMilliseconds(3)},
{PropertyType::kDefaultPublisherPriority, MoqtPriority(10)},
{PropertyType::kDefaultPublisherGroupOrder, MoqtGroupOrder::kDescending},
{PropertyType::kDynamicGroups, true},
{PropertyType::kImmutableProperties, "properties"},
});
EXPECT_TRUE(properties.Validate());
EXPECT_EQ(properties.object_delivery_timeout(),
quic::QuicTimeDelta::FromMilliseconds(1));
EXPECT_EQ(properties.max_cache_duration(),
quic::QuicTimeDelta::FromMilliseconds(2));
EXPECT_EQ(properties.subgroup_delivery_timeout(),
quic::QuicTimeDelta::FromMilliseconds(3));
EXPECT_EQ(properties.default_publisher_priority(), MoqtPriority(10));
EXPECT_EQ(properties.default_publisher_group_order(),
MoqtGroupOrder::kDescending);
EXPECT_TRUE(properties.dynamic_groups());
EXPECT_EQ(properties.immutable_properties(), "properties");
}
TEST_F(TrackPropertiesTest, Validate) {
TrackProperties properties;
// Unknown property.
properties.insert({0x42, 15ULL});
properties.insert({0x42, 25ULL});
EXPECT_TRUE(properties.Validate());
properties.clear();
properties.insert({PropertyType::kDefaultPublisherPriority, 256ULL});
EXPECT_FALSE(properties.Validate());
properties.clear();
properties.insert({PropertyType::kDefaultPublisherGroupOrder, 0ULL});
EXPECT_FALSE(properties.Validate());
properties.clear();
properties.insert({PropertyType::kDefaultPublisherGroupOrder, 3ULL});
EXPECT_FALSE(properties.Validate());
properties.clear();
properties.insert({PropertyType::kDynamicGroups, 2ULL});
properties.insert({PropertyType::kDynamicGroups, 1ULL});
EXPECT_FALSE(properties.Validate());
properties.clear();
properties.insert({PropertyType::kDynamicGroups, 2ULL});
EXPECT_FALSE(properties.Validate());
// Out-of-range value inside immutable properties also fails Validate().
properties.clear();
KeyValuePairList immutable;
immutable.insert({PropertyType::kDynamicGroups, 2ULL});
properties.AddImmutablePropertyList(immutable);
EXPECT_FALSE(properties.Validate());
}
TEST_F(TrackPropertiesTest, IsMalformed) {
TrackProperties properties;
EXPECT_FALSE(properties.IsMalformed());
// Known track properties and unknown properties are not malformed.
properties.insert({PropertyType::kObjectDeliveryTimeout, 5ULL});
properties.insert({0x42, 15ULL});
properties.insert({0x42, 25ULL});
properties.insert({0x43, "unknown_odd"});
EXPECT_FALSE(properties.IsMalformed());
// Object-only properties render the track malformed.
TrackProperties prior_group_gap;
prior_group_gap.insert({PropertyType::kPriorGroupIdGap, 1ULL});
EXPECT_TRUE(prior_group_gap.IsMalformed());
TrackProperties prior_object_gap;
prior_object_gap.insert({PropertyType::kPriorObjectIdGap, 1ULL});
EXPECT_TRUE(prior_object_gap.IsMalformed());
// Duplicate known properties render the track malformed (but pass Validate).
properties.clear();
properties.insert({PropertyType::kObjectDeliveryTimeout, 5ULL});
properties.insert({PropertyType::kObjectDeliveryTimeout, 6ULL});
EXPECT_TRUE(properties.Validate());
EXPECT_TRUE(properties.IsMalformed());
properties.clear();
properties.insert({PropertyType::kMaxCacheDuration, 5ULL});
properties.insert({PropertyType::kMaxCacheDuration, 6ULL});
EXPECT_TRUE(properties.Validate());
EXPECT_TRUE(properties.IsMalformed());
properties.clear();
properties.insert({PropertyType::kSubgroupDeliveryTimeout, 5ULL});
properties.insert({PropertyType::kSubgroupDeliveryTimeout, 6ULL});
EXPECT_TRUE(properties.Validate());
EXPECT_TRUE(properties.IsMalformed());
properties.clear();
properties.insert({PropertyType::kDefaultPublisherPriority, 0ULL});
properties.insert({PropertyType::kDefaultPublisherPriority, 1ULL});
EXPECT_TRUE(properties.Validate());
EXPECT_TRUE(properties.IsMalformed());
properties.clear();
properties.insert({PropertyType::kDefaultPublisherGroupOrder, 1ULL});
properties.insert({PropertyType::kDefaultPublisherGroupOrder, 2ULL});
EXPECT_TRUE(properties.Validate());
EXPECT_TRUE(properties.IsMalformed());
properties.clear();
properties.insert({PropertyType::kDynamicGroups, 0ULL});
properties.insert({PropertyType::kDynamicGroups, 1ULL});
EXPECT_TRUE(properties.Validate());
EXPECT_TRUE(properties.IsMalformed());
properties.clear();
properties.insert({PropertyType::kImmutableProperties, "foo"});
properties.insert({PropertyType::kImmutableProperties, "bar"});
EXPECT_TRUE(properties.Validate());
EXPECT_TRUE(properties.IsMalformed());
}
TEST_F(TrackPropertiesTest, ImmutableProperties) {
TrackProperties properties;
properties.insert({PropertyType::kObjectDeliveryTimeout, 10ULL});
KeyValuePairList immutable;
immutable.insert({PropertyType::kMaxCacheDuration, 20ULL});
immutable.insert({PropertyType::kDynamicGroups, 1ULL});
immutable.insert({0x42, 99ULL});
properties.AddImmutablePropertyList(immutable);
EXPECT_TRUE(properties.Validate());
EXPECT_FALSE(properties.IsMalformed());
EXPECT_FALSE(properties.empty());
EXPECT_EQ(properties.object_delivery_timeout(),
quic::QuicTimeDelta::FromMilliseconds(10));
EXPECT_EQ(properties.max_cache_duration(),
quic::QuicTimeDelta::FromMilliseconds(20));
EXPECT_TRUE(properties.dynamic_groups());
EXPECT_EQ(properties.immutable_properties(), "\x04\x14\x2c\x01\x12\x63");
EXPECT_FALSE(properties.contains(
static_cast<uint64_t>(PropertyType::kMaxCacheDuration)));
EXPECT_FALSE(properties.contains(0x42));
// If kImmutableProperties is already present (e.g., from the parser),
// AddImmutablePropertyList preserves the raw wire bytes.
TrackProperties parsed_immutable;
parsed_immutable.insert(
{PropertyType::kImmutableProperties, "raw_immutable_bytes"});
parsed_immutable.AddImmutablePropertyList(immutable);
EXPECT_FALSE(parsed_immutable.IsMalformed());
EXPECT_EQ(parsed_immutable.immutable_properties(), "raw_immutable_bytes");
// ForEach only visits outer properties (including the serialized
// kImmutableProperties entry).
std::vector<uint64_t> visited_keys;
properties.ForEach(
[&](uint64_t key, std::variant<uint64_t, absl::string_view> /*value*/) {
visited_keys.push_back(key);
return true;
});
std::vector<uint64_t> expected_keys = {
static_cast<uint64_t>(PropertyType::kObjectDeliveryTimeout),
static_cast<uint64_t>(PropertyType::kImmutableProperties),
};
EXPECT_EQ(visited_keys, expected_keys);
// Unparseable immutable properties (std::nullopt) -> malformed.
TrackProperties unparseable;
unparseable.insert({PropertyType::kImmutableProperties, "bad"});
unparseable.AddImmutablePropertyList(std::nullopt);
EXPECT_TRUE(unparseable.IsMalformed());
// Nested kImmutableProperties -> malformed.
TrackProperties nested;
KeyValuePairList nested_inner;
nested_inner.insert({PropertyType::kImmutableProperties, "inner"});
nested.AddImmutablePropertyList(nested_inner);
EXPECT_TRUE(nested.IsMalformed());
// Known property duplicated between outer and immutable -> malformed.
TrackProperties duplicate_outer_inner;
duplicate_outer_inner.insert({PropertyType::kMaxCacheDuration, 10ULL});
duplicate_outer_inner.AddImmutablePropertyList(immutable);
EXPECT_TRUE(duplicate_outer_inner.IsMalformed());
// Object-only property inside immutable properties -> malformed.
TrackProperties gap_in_immutable;
KeyValuePairList gap_inner;
gap_inner.insert({PropertyType::kPriorGroupIdGap, 1ULL});
gap_in_immutable.AddImmutablePropertyList(gap_inner);
EXPECT_TRUE(gap_in_immutable.IsMalformed());
// Empty immutable property list is a no-op and not malformed.
TrackProperties empty_immutable;
empty_immutable.AddImmutablePropertyList(KeyValuePairList());
EXPECT_TRUE(empty_immutable.Validate());
EXPECT_FALSE(empty_immutable.IsMalformed());
EXPECT_TRUE(empty_immutable.empty());
EXPECT_TRUE(empty_immutable.immutable_properties().empty());
EXPECT_FALSE(empty_immutable.contains(
static_cast<uint64_t>(PropertyType::kImmutableProperties)));
}
TEST_F(TrackPropertiesTest, CheckForUnknownMandatoryProperty) {
TrackProperties properties;
QUICHE_EXPECT_OK(properties.CheckForUnknownMandatoryProperty());
// Non-mandatory property types (< 0x4000).
properties.insert({PropertyType::kObjectDeliveryTimeout, 5ULL});
properties.insert({0x3FFE, 1ULL});
properties.insert({0x3FFF, "odd_optional"});
QUICHE_EXPECT_OK(properties.CheckForUnknownMandatoryProperty());
// Non-mandatory property types (> 0x7FFF).
properties.insert({0x8000, 2ULL});
properties.insert({0x8001, "above_range"});
properties.insert({0x10000, 3ULL});
QUICHE_EXPECT_OK(properties.CheckForUnknownMandatoryProperty());
// Min boundary: 0x4000.
TrackProperties min_mandatory;
min_mandatory.insert({kMinMandatoryTrackProperty, 0ULL});
EXPECT_TRUE(
IsFailedPrecondition(min_mandatory.CheckForUnknownMandatoryProperty()));
EXPECT_EQ(min_mandatory.CheckForUnknownMandatoryProperty().message(),
"Unknown mandatory property: 0x4000");
// Max boundary: 0x7FFF.
TrackProperties max_mandatory;
max_mandatory.insert({kMaxMandatoryTrackProperty, "mandatory_string"});
EXPECT_TRUE(
IsFailedPrecondition(max_mandatory.CheckForUnknownMandatoryProperty()));
EXPECT_EQ(max_mandatory.CheckForUnknownMandatoryProperty().message(),
"Unknown mandatory property: 0x7fff");
// In between.
TrackProperties mid_mandatory;
mid_mandatory.insert({0x5000, 42ULL});
EXPECT_TRUE(
IsFailedPrecondition(mid_mandatory.CheckForUnknownMandatoryProperty()));
EXPECT_EQ(mid_mandatory.CheckForUnknownMandatoryProperty().message(),
"Unknown mandatory property: 0x5000");
// Unknown mandatory property inside kImmutableProperties.
TrackProperties immutable_mandatory;
KeyValuePairList immutable_inner;
immutable_inner.insert({0x4500, 1ULL});
immutable_mandatory.AddImmutablePropertyList(immutable_inner);
EXPECT_TRUE(absl::IsFailedPrecondition(
immutable_mandatory.CheckForUnknownMandatoryProperty()));
EXPECT_EQ(immutable_mandatory.CheckForUnknownMandatoryProperty().message(),
"Unknown mandatory property: 0x4500");
}
class ObjectPropertiesTest : public quic::test::QuicTest {};
TEST_F(ObjectPropertiesTest, IsMalformed) {
ObjectProperties properties;
EXPECT_FALSE(properties.IsMalformed());
// Object-valid properties and unknown non-mandatory properties (including
// boundaries just outside [0x4000, 0x7FFF], both outer and in immutable
// properties) are not malformed.
properties.insert({PropertyType::kObjectDeliveryTimeout, 5ULL});
properties.insert({PropertyType::kSubgroupDeliveryTimeout, 5ULL});
properties.insert({PropertyType::kPriorGroupIdGap, 1ULL});
properties.insert({PropertyType::kPriorObjectIdGap, 2ULL});
properties.insert({0x42, 15ULL});
properties.insert({0x43, "unknown_odd"});
properties.insert({kMinMandatoryTrackProperty - 2, 1ULL});
properties.insert({kMinMandatoryTrackProperty - 1, "below_mandatory"});
properties.insert({kMaxMandatoryTrackProperty + 1, 2ULL});
properties.insert({kMaxMandatoryTrackProperty + 2, "above_mandatory"});
KeyValuePairList optional_immutable;
optional_immutable.insert({kMinMandatoryTrackProperty - 2, 3ULL});
optional_immutable.insert({kMaxMandatoryTrackProperty + 1, 4ULL});
properties.AddImmutablePropertyList(optional_immutable);
EXPECT_FALSE(properties.IsMalformed());
// Unknown mandatory properties in [kMinMandatoryTrackProperty,
// kMaxMandatoryTrackProperty] (0x4000-0x7FFF) render ObjectProperties
// malformed, whether outer or inside kImmutableProperties.
for (const KeyValuePair& mandatory_pair :
{KeyValuePair{kMinMandatoryTrackProperty, 0ULL},
KeyValuePair{0x5000, 42ULL},
KeyValuePair{kMaxMandatoryTrackProperty, "mandatory_string"}}) {
ObjectProperties outer_mandatory;
outer_mandatory.insert(mandatory_pair);
EXPECT_TRUE(outer_mandatory.IsMalformed());
ObjectProperties inner_mandatory;
KeyValuePairList immutable_mandatory;
immutable_mandatory.insert(mandatory_pair);
inner_mandatory.AddImmutablePropertyList(immutable_mandatory);
EXPECT_TRUE(inner_mandatory.IsMalformed());
}
// Track-only properties render ObjectProperties malformed.
for (PropertyType track_only : {PropertyType::kMaxCacheDuration,
PropertyType::kDefaultPublisherPriority,
PropertyType::kDefaultPublisherGroupOrder,
PropertyType::kDynamicGroups}) {
ObjectProperties outer_track_only;
outer_track_only.insert({track_only, 1ULL});
EXPECT_TRUE(outer_track_only.IsMalformed());
ObjectProperties inner_track_only;
KeyValuePairList immutable;
immutable.insert({track_only, 1ULL});
inner_track_only.AddImmutablePropertyList(immutable);
EXPECT_TRUE(inner_track_only.IsMalformed());
}
// Duplicate object-valid properties also render ObjectProperties malformed.
for (PropertyType known_object_prop :
{PropertyType::kObjectDeliveryTimeout,
PropertyType::kSubgroupDeliveryTimeout, PropertyType::kPriorGroupIdGap,
PropertyType::kPriorObjectIdGap}) {
ObjectProperties duplicate;
duplicate.insert({known_object_prop, 1ULL});
duplicate.insert({known_object_prop, 2ULL});
EXPECT_TRUE(duplicate.IsMalformed());
}
}
TEST_F(ObjectPropertiesTest, MoveOperations) {
TrackProperties track_props({
{PropertyType::kObjectDeliveryTimeout,
quic::QuicTimeDelta::FromMilliseconds(10)},
});
KeyValuePairList track_immutable;
track_immutable.insert({PropertyType::kDynamicGroups, 1ULL});
track_props.AddImmutablePropertyList(track_immutable);
TrackProperties expected_track_props = track_props;
TrackProperties moved_track_props(std::move(track_props));
EXPECT_EQ(moved_track_props, expected_track_props);
EXPECT_TRUE(moved_track_props.dynamic_groups());
TrackProperties assigned_track_props;
assigned_track_props = std::move(moved_track_props);
EXPECT_EQ(assigned_track_props, expected_track_props);
EXPECT_TRUE(assigned_track_props.dynamic_groups());
ObjectProperties object_props({
{PropertyType::kPriorGroupIdGap, 2ULL},
{PropertyType::kPriorObjectIdGap, 3ULL},
});
KeyValuePairList object_immutable;
object_immutable.insert({PropertyType::kObjectDeliveryTimeout, 15ULL});
object_props.AddImmutablePropertyList(object_immutable);
ObjectProperties expected_object_props = object_props;
ObjectProperties moved_object_props(std::move(object_props));
EXPECT_EQ(moved_object_props, expected_object_props);
EXPECT_EQ(moved_object_props.object_delivery_timeout(),
quic::QuicTimeDelta::FromMilliseconds(15));
ObjectProperties assigned_object_props;
assigned_object_props = std::move(moved_object_props);
EXPECT_EQ(assigned_object_props, expected_object_props);
EXPECT_EQ(assigned_object_props.object_delivery_timeout(),
quic::QuicTimeDelta::FromMilliseconds(15));
}
} // namespace moqt::test