aboutsummaryrefslogtreecommitdiffstats
path: root/libtransport/src/test/test_indexer.cc
blob: 9c12e203768cda121015db3db26ec656087dc7cf (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
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
/*
 * Copyright (c) 2021 Cisco and/or its affiliates.
 * 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 <gtest/gtest.h>
#include <protocols/incremental_indexer_bytestream.h>
#include <protocols/indexer.h>
#include <protocols/rtc/rtc_indexer.h>

#include <algorithm>
#include <iostream>
#include <random>

namespace transport {
namespace protocol {

class IncrementalIndexerTest : public ::testing::Test {
 protected:
  IncrementalIndexerTest() : indexer_(nullptr, nullptr) {
    // You can do set-up work for each test here.
  }

  virtual ~IncrementalIndexerTest() {
    // You can do clean-up work that doesn't throw exceptions here.
  }

  // If the constructor and destructor are not enough for setting up
  // and cleaning up each test, you can define the following methods:

  virtual void SetUp() {
    // Code here will be called immediately after the constructor (right
    // before each test).
  }

  virtual void TearDown() {
    // Code here will be called immediately after each test (right
    // before the destructor).
  }

  IncrementalIndexer indexer_;
};

class RtcIndexerTest : public ::testing::Test {
 protected:
  RtcIndexerTest() : indexer_(nullptr, nullptr) {
    // You can do set-up work for each test here.
  }

  virtual ~RtcIndexerTest() {
    // You can do clean-up work that doesn't throw exceptions here.
  }

  // If the constructor and destructor are not enough for setting up
  // and cleaning up each test, you can define the following methods:

  virtual void SetUp() {
    // Code here will be called immediately after the constructor (right
    // before each test).
    indexer_.setFirstSuffix(0);
    indexer_.reset();
  }

  virtual void TearDown() {
    // Code here will be called immediately after each test (right
    // before the destructor).
  }

  static const constexpr uint32_t LIMIT = (1 << 31);
  rtc::RtcIndexer<LIMIT> indexer_;
};

void testIncrement(Indexer &indexer) {
  // As a first index we should get zero
  auto index = indexer.getNextSuffix();
  EXPECT_EQ(index, uint32_t(0));

  // Check if the sequence works for consecutive incremental numbers
  for (uint32_t i = 1; i < 4096; i++) {
    index = indexer.getNextSuffix();
    EXPECT_EQ(index, i);
  }

  index = indexer.getNextSuffix();
  EXPECT_NE(index, uint32_t(0));
}

void testJump(Indexer &indexer) {
  // Fist suffix is 0
  auto index = indexer.getNextSuffix();
  EXPECT_EQ(index, uint32_t(0));

  // Next suffix should be 1, but we jump to 12345
  uint32_t jump = 12345;
  indexer.jumpToIndex(jump);

  // This takes place immediately
  index = indexer.getNextSuffix();
  EXPECT_EQ(index, jump);
}

TEST_F(IncrementalIndexerTest, TestReset) {
  testIncrement(indexer_);

  // Reset the indexer
  indexer_.reset();

  // Now it should startfrom zero again
  for (uint32_t i = 0; i < 4096; i++) {
    auto index = indexer_.getNextSuffix();
    EXPECT_EQ(index, i);
  }
}

TEST_F(IncrementalIndexerTest, TestGetSuffix) { testIncrement(indexer_); }

TEST_F(IncrementalIndexerTest, TestGetNextReassemblySegment) {
  // Test suffixes for reassembly are not influenced by download suffixed
  // increment
  for (uint32_t i = 0; i < 4096; i++) {
    auto index = indexer_.getNextSuffix();
    EXPECT_EQ(index, i);
  }

  for (uint32_t i = 0; i < 4096; i++) {
    auto index = indexer_.getNextReassemblySegment();
    EXPECT_EQ(index, i);
  }
}

TEST_F(IncrementalIndexerTest, TestJumpToIndex) { testJump(indexer_); }

TEST_F(IncrementalIndexerTest, TestGetFinalSuffix) {
  // Since final suffix hasn't been discovered, it should be invalid_index
  auto final_suffix = indexer_.getFinalSuffix();
  ASSERT_EQ(final_suffix, Indexer::invalid_index);
}

TEST_F(IncrementalIndexerTest, TestMaxLimit) {
  // Jump to max value for uint32_t
  indexer_.jumpToIndex(std::numeric_limits<uint32_t>::max());
  auto ret = indexer_.getNextSuffix();
  ASSERT_EQ(ret, Indexer::invalid_index);

  // Now the indexer should always return invalid_index
  for (uint32_t i = 0; i < 4096; i++) {
    ret = indexer_.getNextSuffix();
    EXPECT_EQ(ret, Indexer::invalid_index);
  }
}

TEST_F(IncrementalIndexerTest, TestSetFirstSuffix) {
  // Set first suffix before starting
  uint32_t start = 1234567890;
  indexer_.setFirstSuffix(1234567890);

  // The first suffix set should take place only after a reset
  auto index = indexer_.getNextSuffix();
  EXPECT_EQ(index, uint32_t(0));

  indexer_.reset();
  index = indexer_.getNextSuffix();
  EXPECT_EQ(index, start);
}

TEST_F(IncrementalIndexerTest, TestIsFinalSuffixDiscovered) {
  // Final suffix should not be discovererd
  auto ret = indexer_.isFinalSuffixDiscovered();
  EXPECT_FALSE(ret);
}

TEST_F(RtcIndexerTest, TestReset) {
  // Without setting anything this indexer should behave exactly as the
  // incremental indexer for the getNextSuffix()
  testIncrement(indexer_);

  // Reset the indexer
  indexer_.reset();

  // Now it should startfrom zero again
  for (uint32_t i = 0; i < 4096; i++) {
    auto index = indexer_.getNextSuffix();
    EXPECT_EQ(index, i);
  }
}

TEST_F(RtcIndexerTest, TestGetNextSuffix) {
  // Without setting anything this indexer should behave exactly as the
  // incremental indexer for the getNextSuffix()
  testIncrement(indexer_);
}

TEST_F(RtcIndexerTest, TestGetNextReassemblySegment) {
  // This indexer should not provide reassembly segments since they are not
  // required for rtc
  try {
    indexer_.getNextReassemblySegment();
    // We should not reach this point
    FAIL() << "Exception expected here";
  } catch (const errors::RuntimeException &exc) {
    // OK correct exception
  } catch (...) {
    FAIL() << "Wrong exception thrown";
  }
}

TEST_F(RtcIndexerTest, TestGetFinalSuffix) {
  // Final suffix should be eqaul to LIMIT
  ASSERT_EQ(indexer_.getFinalSuffix(), uint32_t(LIMIT));
}

TEST_F(RtcIndexerTest, TestJumpToIndex) { testJump(indexer_); }

TEST_F(RtcIndexerTest, TestIsFinalSuffixDiscovered) {
  // This method should always return true
  EXPECT_TRUE(indexer_.isFinalSuffixDiscovered());
}

TEST_F(RtcIndexerTest, TestMaxLimit) {
  // Once reached the LIMIT, this indexer should restart from 0

  // Jump to max value for uint32_t
  indexer_.jumpToIndex(LIMIT);
  testIncrement(indexer_);
}

TEST_F(RtcIndexerTest, TestEnableFec) {
  // Here we enable the FEC and we check we receive indexes for souece packets
  // only
  indexer_.enableFec(fec::FECType::RS_K1_N3);

  // We did not set NFec, which should be zero. So we get only indexes for
  // Source packets.

  // With this FEC type we should get one source packet every 3 (0 . . 3 . . 6)
  auto index = indexer_.getNextSuffix();
  EXPECT_EQ(index, uint32_t(0));

  index = indexer_.getNextSuffix();
  EXPECT_EQ(index, uint32_t(3));

  index = indexer_.getNextSuffix();
  EXPECT_EQ(index, uint32_t(6));

  // Change FEC Type
  indexer_.enableFec(fec::FECType::RS_K10_N30);

  // With this FEC type we should get source packets from 7 to 9
  for (uint32_t i = 7; i < 10; i++) {
    index = indexer_.getNextSuffix();
    EXPECT_EQ(index, i);
  }

  // And then jump to 30
  index = indexer_.getNextSuffix();
  EXPECT_EQ(index, uint32_t(30));

  // Let's now jump to a high value
  indexer_.jumpToIndex(12365);
  for (uint32_t i = 12365; i < 12369; i++) {
    index = indexer_.getNextSuffix();
    EXPECT_EQ(index, i);
  }
}

TEST_F(RtcIndexerTest, TestSetNFec) {
  // Here we enable the FEC and we set a max of 20 fec packets
  indexer_.enableFec(fec::FECType::RS_K10_N90);
  indexer_.setNFec(20);

  // We should get indexes up to 29
  uint32_t index;
  for (uint32_t i = 0; i < 30; i++) {
    index = indexer_.getNextSuffix();
    EXPECT_EQ(i, index);
  }

  // Then it should jump to 90
  for (uint32_t i = 90; i < 99; i++) {
    index = indexer_.getNextSuffix();
    EXPECT_EQ(i, index);
  }

  // Let's set NFEC > 80
  indexer_.setNFec(150);
}

TEST_F(RtcIndexerTest, TestSetNFecWithOffset) {
  // Here we enable the FEC and we set a max of 20 fec packets
  const constexpr uint32_t first_suffix = 7;
  indexer_.setFirstSuffix(first_suffix);
  indexer_.reset();
  indexer_.enableFec(fec::FECType::RS_K16_N24);
  indexer_.setNFec(8);

  uint32_t index;
  for (uint32_t i = first_suffix; i < 16 + first_suffix; i++) {
    index = indexer_.getNextSuffix();
    EXPECT_FALSE(indexer_.isFec(index));
    EXPECT_EQ(i, index);
  }

  for (uint32_t i = first_suffix + 16; i < 16 + 8 + first_suffix; i++) {
    index = indexer_.getNextSuffix();
    EXPECT_TRUE(indexer_.isFec(index));
    EXPECT_EQ(i, index);
  }
}

}  // namespace protocol
}  // namespace transport