summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/pc/used_ids_unittest.cc
blob: 6362f2773ad80aa3ca5e42f34b5c80ca7312ff3f (plain)
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
/*
 *  Copyright 2019 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "pc/used_ids.h"

#include "absl/strings/string_view.h"
#include "test/gtest.h"

using cricket::UsedIds;
using cricket::UsedRtpHeaderExtensionIds;

struct Foo {
  int id;
};

TEST(UsedIdsTest, UniqueIdsAreUnchanged) {
  UsedIds<Foo> used_ids(1, 5);
  for (int i = 1; i <= 5; ++i) {
    Foo id = {i};
    used_ids.FindAndSetIdUsed(&id);
    EXPECT_EQ(id.id, i);
  }
}

TEST(UsedIdsTest, IdsOutsideRangeAreUnchanged) {
  UsedIds<Foo> used_ids(1, 5);

  Foo id_11 = {11};
  Foo id_12 = {12};
  Foo id_12_collision = {12};
  Foo id_13 = {13};
  Foo id_13_collision = {13};

  used_ids.FindAndSetIdUsed(&id_11);
  EXPECT_EQ(id_11.id, 11);
  used_ids.FindAndSetIdUsed(&id_12);
  EXPECT_EQ(id_12.id, 12);
  used_ids.FindAndSetIdUsed(&id_12_collision);
  EXPECT_EQ(id_12_collision.id, 12);
  used_ids.FindAndSetIdUsed(&id_13);
  EXPECT_EQ(id_13.id, 13);
  used_ids.FindAndSetIdUsed(&id_13_collision);
  EXPECT_EQ(id_13_collision.id, 13);
}

TEST(UsedIdsTest, CollisionsAreReassignedIdsInReverseOrder) {
  UsedIds<Foo> used_ids(1, 10);
  Foo id_1 = {1};
  Foo id_2 = {2};
  Foo id_2_collision = {2};
  Foo id_3 = {3};
  Foo id_3_collision = {3};

  used_ids.FindAndSetIdUsed(&id_1);
  used_ids.FindAndSetIdUsed(&id_2);
  used_ids.FindAndSetIdUsed(&id_2_collision);
  EXPECT_EQ(id_2_collision.id, 10);
  used_ids.FindAndSetIdUsed(&id_3);
  used_ids.FindAndSetIdUsed(&id_3_collision);
  EXPECT_EQ(id_3_collision.id, 9);
}

struct TestParams {
  UsedRtpHeaderExtensionIds::IdDomain id_domain;
  int max_id;
};

class UsedRtpHeaderExtensionIdsTest
    : public ::testing::TestWithParam<TestParams> {};

constexpr TestParams kOneByteTestParams = {
    UsedRtpHeaderExtensionIds::IdDomain::kOneByteOnly, 14};
constexpr TestParams kTwoByteTestParams = {
    UsedRtpHeaderExtensionIds::IdDomain::kTwoByteAllowed, 255};

INSTANTIATE_TEST_SUITE_P(All,
                         UsedRtpHeaderExtensionIdsTest,
                         ::testing::Values(kOneByteTestParams,
                                           kTwoByteTestParams));

TEST_P(UsedRtpHeaderExtensionIdsTest, UniqueIdsAreUnchanged) {
  UsedRtpHeaderExtensionIds used_ids(GetParam().id_domain);

  // Fill all IDs.
  for (int j = 1; j <= GetParam().max_id; ++j) {
    webrtc::RtpExtension extension("", j);
    used_ids.FindAndSetIdUsed(&extension);
    EXPECT_EQ(extension.id, j);
  }
}

TEST_P(UsedRtpHeaderExtensionIdsTest, PrioritizeReassignmentToOneByteIds) {
  UsedRtpHeaderExtensionIds used_ids(GetParam().id_domain);
  webrtc::RtpExtension id_1("", 1);
  webrtc::RtpExtension id_2("", 2);
  webrtc::RtpExtension id_2_collision("", 2);
  webrtc::RtpExtension id_3("", 3);
  webrtc::RtpExtension id_3_collision("", 3);

  // Expect that colliding IDs are reassigned to one-byte IDs.
  used_ids.FindAndSetIdUsed(&id_1);
  used_ids.FindAndSetIdUsed(&id_2);
  used_ids.FindAndSetIdUsed(&id_2_collision);
  EXPECT_EQ(id_2_collision.id, 14);
  used_ids.FindAndSetIdUsed(&id_3);
  used_ids.FindAndSetIdUsed(&id_3_collision);
  EXPECT_EQ(id_3_collision.id, 13);
}

TEST_F(UsedRtpHeaderExtensionIdsTest, TwoByteIdsAllowed) {
  UsedRtpHeaderExtensionIds used_ids(
      UsedRtpHeaderExtensionIds::IdDomain::kTwoByteAllowed);

  // Fill all one byte IDs.
  for (int i = 1; i < 15; ++i) {
    webrtc::RtpExtension id("", i);
    used_ids.FindAndSetIdUsed(&id);
  }

  // Add new extensions with colliding IDs.
  webrtc::RtpExtension id1_collision("", 1);
  webrtc::RtpExtension id2_collision("", 2);
  webrtc::RtpExtension id3_collision("", 3);

  // Expect to reassign to two-byte header extension IDs.
  used_ids.FindAndSetIdUsed(&id1_collision);
  EXPECT_EQ(id1_collision.id, 15);
  used_ids.FindAndSetIdUsed(&id2_collision);
  EXPECT_EQ(id2_collision.id, 16);
  used_ids.FindAndSetIdUsed(&id3_collision);
  EXPECT_EQ(id3_collision.id, 17);
}

// Death tests.
// Disabled on Android because death tests misbehave on Android, see
// base/test/gtest_util.h.
#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST(UsedIdsDeathTest, DieWhenAllIdsAreOccupied) {
  UsedIds<Foo> used_ids(1, 5);
  for (int i = 1; i <= 5; ++i) {
    Foo id = {i};
    used_ids.FindAndSetIdUsed(&id);
  }
  Foo id_collision = {3};
  EXPECT_DEATH(used_ids.FindAndSetIdUsed(&id_collision), "");
}

using UsedRtpHeaderExtensionIdsDeathTest = UsedRtpHeaderExtensionIdsTest;
INSTANTIATE_TEST_SUITE_P(All,
                         UsedRtpHeaderExtensionIdsDeathTest,
                         ::testing::Values(kOneByteTestParams,
                                           kTwoByteTestParams));

TEST_P(UsedRtpHeaderExtensionIdsDeathTest, DieWhenAllIdsAreOccupied) {
  UsedRtpHeaderExtensionIds used_ids(GetParam().id_domain);

  // Fill all IDs.
  for (int j = 1; j <= GetParam().max_id; ++j) {
    webrtc::RtpExtension id("", j);
    used_ids.FindAndSetIdUsed(&id);
  }

  webrtc::RtpExtension id1_collision("", 1);
  webrtc::RtpExtension id2_collision("", 2);
  webrtc::RtpExtension id3_collision("", GetParam().max_id);

  EXPECT_DEATH(used_ids.FindAndSetIdUsed(&id1_collision), "");
  EXPECT_DEATH(used_ids.FindAndSetIdUsed(&id2_collision), "");
  EXPECT_DEATH(used_ids.FindAndSetIdUsed(&id3_collision), "");
}
#endif  // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)