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
|
/*
* Copyright 2004 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 "rtc_base/stream.h"
#include "rtc_base/gunit.h"
namespace rtc {
///////////////////////////////////////////////////////////////////////////////
// TestStream
///////////////////////////////////////////////////////////////////////////////
class TestStream : public StreamInterface {
public:
TestStream() : pos_(0) { }
StreamState GetState() const override { return SS_OPEN; }
StreamResult Read(void* buffer,
size_t buffer_len,
size_t* read,
int* error) override {
unsigned char* uc_buffer = static_cast<unsigned char*>(buffer);
for (size_t i = 0; i < buffer_len; ++i) {
uc_buffer[i] = static_cast<unsigned char>(pos_++);
}
if (read)
*read = buffer_len;
return SR_SUCCESS;
}
StreamResult Write(const void* data,
size_t data_len,
size_t* written,
int* error) override {
if (error)
*error = -1;
return SR_ERROR;
}
void Close() override {}
bool SetPosition(size_t position) override {
pos_ = position;
return true;
}
bool GetPosition(size_t* position) const override {
if (position) *position = pos_;
return true;
}
bool GetSize(size_t* size) const override { return false; }
bool GetAvailable(size_t* size) const override { return false; }
private:
size_t pos_;
};
bool VerifyTestBuffer(unsigned char* buffer, size_t len,
unsigned char value) {
bool passed = true;
for (size_t i = 0; i < len; ++i) {
if (buffer[i] != value++) {
passed = false;
break;
}
}
// Ensure that we don't pass again without re-writing
memset(buffer, 0, len);
return passed;
}
void SeekTest(StreamInterface* stream, const unsigned char value) {
size_t bytes;
unsigned char buffer[13] = { 0 };
const size_t kBufSize = sizeof(buffer);
EXPECT_EQ(stream->Read(buffer, kBufSize, &bytes, nullptr), SR_SUCCESS);
EXPECT_EQ(bytes, kBufSize);
EXPECT_TRUE(VerifyTestBuffer(buffer, kBufSize, value));
EXPECT_TRUE(stream->GetPosition(&bytes));
EXPECT_EQ(13U, bytes);
EXPECT_TRUE(stream->SetPosition(7));
EXPECT_EQ(stream->Read(buffer, kBufSize, &bytes, nullptr), SR_SUCCESS);
EXPECT_EQ(bytes, kBufSize);
EXPECT_TRUE(VerifyTestBuffer(buffer, kBufSize, value + 7));
EXPECT_TRUE(stream->GetPosition(&bytes));
EXPECT_EQ(20U, bytes);
}
TEST(FifoBufferTest, TestAll) {
const size_t kSize = 16;
const char in[kSize * 2 + 1] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
char out[kSize * 2];
void* p;
const void* q;
size_t bytes;
FifoBuffer buf(kSize);
StreamInterface* stream = &buf;
// Test assumptions about base state
EXPECT_EQ(SS_OPEN, stream->GetState());
EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, nullptr));
EXPECT_TRUE(nullptr != stream->GetReadData(&bytes));
EXPECT_EQ((size_t)0, bytes);
stream->ConsumeReadData(0);
EXPECT_TRUE(nullptr != stream->GetWriteBuffer(&bytes));
EXPECT_EQ(kSize, bytes);
stream->ConsumeWriteBuffer(0);
// Try a full write
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr));
EXPECT_EQ(kSize, bytes);
// Try a write that should block
EXPECT_EQ(SR_BLOCK, stream->Write(in, kSize, &bytes, nullptr));
// Try a full read
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, nullptr));
EXPECT_EQ(kSize, bytes);
EXPECT_EQ(0, memcmp(in, out, kSize));
// Try a read that should block
EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, nullptr));
// Try a too-big write
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize * 2, &bytes, nullptr));
EXPECT_EQ(bytes, kSize);
// Try a too-big read
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 2, &bytes, nullptr));
EXPECT_EQ(kSize, bytes);
EXPECT_EQ(0, memcmp(in, out, kSize));
// Try some small writes and reads
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(0, memcmp(in, out, kSize / 2));
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(0, memcmp(in, out, kSize / 2));
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(0, memcmp(in, out, kSize / 2));
// Try wraparound reads and writes in the following pattern
// WWWWWWWWWWWW.... 0123456789AB....
// RRRRRRRRXXXX.... ........89AB....
// WWWW....XXXXWWWW 4567....89AB0123
// XXXX....RRRRXXXX 4567........0123
// XXXXWWWWWWWWXXXX 4567012345670123
// RRRRXXXXXXXXRRRR ....01234567....
// ....RRRRRRRR.... ................
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize * 3 / 4, &bytes, nullptr));
EXPECT_EQ(kSize * 3 / 4, bytes);
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(0, memcmp(in, out, kSize / 2));
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 4, &bytes, nullptr));
EXPECT_EQ(kSize / 4 , bytes);
EXPECT_EQ(0, memcmp(in + kSize / 2, out, kSize / 4));
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2 , bytes);
EXPECT_EQ(0, memcmp(in, out, kSize / 2));
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr));
EXPECT_EQ(kSize / 2 , bytes);
EXPECT_EQ(0, memcmp(in, out, kSize / 2));
// Use GetWriteBuffer to reset the read_position for the next tests
stream->GetWriteBuffer(&bytes);
stream->ConsumeWriteBuffer(0);
// Try using GetReadData to do a full read
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr));
q = stream->GetReadData(&bytes);
EXPECT_TRUE(nullptr != q);
EXPECT_EQ(kSize, bytes);
EXPECT_EQ(0, memcmp(q, in, kSize));
stream->ConsumeReadData(kSize);
EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, nullptr));
// Try using GetReadData to do some small reads
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr));
q = stream->GetReadData(&bytes);
EXPECT_TRUE(nullptr != q);
EXPECT_EQ(kSize, bytes);
EXPECT_EQ(0, memcmp(q, in, kSize / 2));
stream->ConsumeReadData(kSize / 2);
q = stream->GetReadData(&bytes);
EXPECT_TRUE(nullptr != q);
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(0, memcmp(q, in + kSize / 2, kSize / 2));
stream->ConsumeReadData(kSize / 2);
EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, nullptr));
// Try using GetReadData in a wraparound case
// WWWWWWWWWWWWWWWW 0123456789ABCDEF
// RRRRRRRRRRRRXXXX ............CDEF
// WWWWWWWW....XXXX 01234567....CDEF
// ............RRRR 01234567........
// RRRRRRRR........ ................
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr));
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 3 / 4, &bytes, nullptr));
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr));
q = stream->GetReadData(&bytes);
EXPECT_TRUE(nullptr != q);
EXPECT_EQ(kSize / 4, bytes);
EXPECT_EQ(0, memcmp(q, in + kSize * 3 / 4, kSize / 4));
stream->ConsumeReadData(kSize / 4);
q = stream->GetReadData(&bytes);
EXPECT_TRUE(nullptr != q);
EXPECT_EQ(kSize / 2, bytes);
EXPECT_EQ(0, memcmp(q, in, kSize / 2));
stream->ConsumeReadData(kSize / 2);
// Use GetWriteBuffer to reset the read_position for the next tests
stream->GetWriteBuffer(&bytes);
stream->ConsumeWriteBuffer(0);
// Try using GetWriteBuffer to do a full write
p = stream->GetWriteBuffer(&bytes);
EXPECT_TRUE(nullptr != p);
EXPECT_EQ(kSize, bytes);
memcpy(p, in, kSize);
stream->ConsumeWriteBuffer(kSize);
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, nullptr));
EXPECT_EQ(kSize, bytes);
EXPECT_EQ(0, memcmp(in, out, kSize));
// Try using GetWriteBuffer to do some small writes
p = stream->GetWriteBuffer(&bytes);
EXPECT_TRUE(nullptr != p);
EXPECT_EQ(kSize, bytes);
memcpy(p, in, kSize / 2);
stream->ConsumeWriteBuffer(kSize / 2);
p = stream->GetWriteBuffer(&bytes);
EXPECT_TRUE(nullptr != p);
EXPECT_EQ(kSize / 2, bytes);
memcpy(p, in + kSize / 2, kSize / 2);
stream->ConsumeWriteBuffer(kSize / 2);
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, nullptr));
EXPECT_EQ(kSize, bytes);
EXPECT_EQ(0, memcmp(in, out, kSize));
// Try using GetWriteBuffer in a wraparound case
// WWWWWWWWWWWW.... 0123456789AB....
// RRRRRRRRXXXX.... ........89AB....
// ........XXXXWWWW ........89AB0123
// WWWW....XXXXXXXX 4567....89AB0123
// RRRR....RRRRRRRR ................
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize * 3 / 4, &bytes, nullptr));
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr));
p = stream->GetWriteBuffer(&bytes);
EXPECT_TRUE(nullptr != p);
EXPECT_EQ(kSize / 4, bytes);
memcpy(p, in, kSize / 4);
stream->ConsumeWriteBuffer(kSize / 4);
p = stream->GetWriteBuffer(&bytes);
EXPECT_TRUE(nullptr != p);
EXPECT_EQ(kSize / 2, bytes);
memcpy(p, in + kSize / 4, kSize / 4);
stream->ConsumeWriteBuffer(kSize / 4);
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 3 / 4, &bytes, nullptr));
EXPECT_EQ(kSize * 3 / 4, bytes);
EXPECT_EQ(0, memcmp(in + kSize / 2, out, kSize / 4));
EXPECT_EQ(0, memcmp(in, out + kSize / 4, kSize / 4));
// Check that the stream is now empty
EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, nullptr));
// Try growing the buffer
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr));
EXPECT_EQ(kSize, bytes);
EXPECT_TRUE(buf.SetCapacity(kSize * 2));
EXPECT_EQ(SR_SUCCESS, stream->Write(in + kSize, kSize, &bytes, nullptr));
EXPECT_EQ(kSize, bytes);
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 2, &bytes, nullptr));
EXPECT_EQ(kSize * 2, bytes);
EXPECT_EQ(0, memcmp(in, out, kSize * 2));
// Try shrinking the buffer
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, nullptr));
EXPECT_EQ(kSize, bytes);
EXPECT_TRUE(buf.SetCapacity(kSize));
EXPECT_EQ(SR_BLOCK, stream->Write(in, kSize, &bytes, nullptr));
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, nullptr));
EXPECT_EQ(kSize, bytes);
EXPECT_EQ(0, memcmp(in, out, kSize));
// Write to the stream, close it, read the remaining bytes
EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, nullptr));
stream->Close();
EXPECT_EQ(SS_CLOSED, stream->GetState());
EXPECT_EQ(SR_EOS, stream->Write(in, kSize / 2, &bytes, nullptr));
EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, nullptr));
EXPECT_EQ(0, memcmp(in, out, kSize / 2));
EXPECT_EQ(SR_EOS, stream->Read(out, kSize / 2, &bytes, nullptr));
}
TEST(FifoBufferTest, FullBufferCheck) {
FifoBuffer buff(10);
buff.ConsumeWriteBuffer(10);
size_t free;
EXPECT_TRUE(buff.GetWriteBuffer(&free) != nullptr);
EXPECT_EQ(0U, free);
}
TEST(FifoBufferTest, WriteOffsetAndReadOffset) {
const size_t kSize = 16;
const char in[kSize * 2 + 1] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
char out[kSize * 2];
FifoBuffer buf(kSize);
// Write 14 bytes.
EXPECT_EQ(SR_SUCCESS, buf.Write(in, 14, nullptr, nullptr));
// Make sure data is in |buf|.
size_t buffered;
EXPECT_TRUE(buf.GetBuffered(&buffered));
EXPECT_EQ(14u, buffered);
// Read 10 bytes.
buf.ConsumeReadData(10);
// There should be now 12 bytes of available space.
size_t remaining;
EXPECT_TRUE(buf.GetWriteRemaining(&remaining));
EXPECT_EQ(12u, remaining);
// Write at offset 12, this should fail.
EXPECT_EQ(SR_BLOCK, buf.WriteOffset(in, 10, 12, nullptr));
// Write 8 bytes at offset 4, this wraps around the buffer.
EXPECT_EQ(SR_SUCCESS, buf.WriteOffset(in, 8, 4, nullptr));
// Number of available space remains the same until we call
// ConsumeWriteBuffer().
EXPECT_TRUE(buf.GetWriteRemaining(&remaining));
EXPECT_EQ(12u, remaining);
buf.ConsumeWriteBuffer(12);
// There's 4 bytes bypassed and 4 bytes no read so skip them and verify the
// 8 bytes written.
size_t read;
EXPECT_EQ(SR_SUCCESS, buf.ReadOffset(out, 8, 8, &read));
EXPECT_EQ(8u, read);
EXPECT_EQ(0, memcmp(out, in, 8));
// There should still be 16 bytes available for reading.
EXPECT_TRUE(buf.GetBuffered(&buffered));
EXPECT_EQ(16u, buffered);
// Read at offset 16, this should fail since we don't have that much data.
EXPECT_EQ(SR_BLOCK, buf.ReadOffset(out, 10, 16, nullptr));
}
} // namespace rtc
|