blob: 4d5066ab55fce576d2be82df4c4eef937511d147 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Zeke Chin2d3b7e22015-07-14 12:55:44 -070011#include "webrtc/base/fileutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012#include "webrtc/base/gunit.h"
Zeke Chin2d3b7e22015-07-14 12:55:44 -070013#include "webrtc/base/pathutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014#include "webrtc/base/stream.h"
kjellander@webrtc.org34ac7762014-09-19 13:47:47 +000015#include "webrtc/test/testsupport/gtest_disable.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
17namespace rtc {
18
19///////////////////////////////////////////////////////////////////////////////
20// TestStream
21///////////////////////////////////////////////////////////////////////////////
22
23class TestStream : public StreamInterface {
24 public:
25 TestStream() : pos_(0) { }
26
27 virtual StreamState GetState() const { return SS_OPEN; }
28 virtual StreamResult Read(void* buffer, size_t buffer_len,
29 size_t* read, int* error) {
30 unsigned char* uc_buffer = static_cast<unsigned char*>(buffer);
31 for (size_t i = 0; i < buffer_len; ++i) {
32 uc_buffer[i] = static_cast<unsigned char>(pos_++);
33 }
34 if (read)
35 *read = buffer_len;
36 return SR_SUCCESS;
37 }
38 virtual StreamResult Write(const void* data, size_t data_len,
39 size_t* written, int* error) {
40 if (error)
41 *error = -1;
42 return SR_ERROR;
43 }
44 virtual void Close() { }
45 virtual bool SetPosition(size_t position) {
46 pos_ = position;
47 return true;
48 }
49 virtual bool GetPosition(size_t* position) const {
50 if (position) *position = pos_;
51 return true;
52 }
53 virtual bool GetSize(size_t* size) const {
54 return false;
55 }
56 virtual bool GetAvailable(size_t* size) const {
57 return false;
58 }
59
60 private:
61 size_t pos_;
62};
63
64bool VerifyTestBuffer(unsigned char* buffer, size_t len,
65 unsigned char value) {
66 bool passed = true;
67 for (size_t i = 0; i < len; ++i) {
68 if (buffer[i] != value++) {
69 passed = false;
70 break;
71 }
72 }
73 // Ensure that we don't pass again without re-writing
74 memset(buffer, 0, len);
75 return passed;
76}
77
78void SeekTest(StreamInterface* stream, const unsigned char value) {
79 size_t bytes;
80 unsigned char buffer[13] = { 0 };
81 const size_t kBufSize = sizeof(buffer);
82
83 EXPECT_EQ(stream->Read(buffer, kBufSize, &bytes, NULL), SR_SUCCESS);
84 EXPECT_EQ(bytes, kBufSize);
85 EXPECT_TRUE(VerifyTestBuffer(buffer, kBufSize, value));
86 EXPECT_TRUE(stream->GetPosition(&bytes));
87 EXPECT_EQ(13U, bytes);
88
89 EXPECT_TRUE(stream->SetPosition(7));
90
91 EXPECT_EQ(stream->Read(buffer, kBufSize, &bytes, NULL), SR_SUCCESS);
92 EXPECT_EQ(bytes, kBufSize);
93 EXPECT_TRUE(VerifyTestBuffer(buffer, kBufSize, value + 7));
94 EXPECT_TRUE(stream->GetPosition(&bytes));
95 EXPECT_EQ(20U, bytes);
96}
97
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000098TEST(FifoBufferTest, TestAll) {
99 const size_t kSize = 16;
100 const char in[kSize * 2 + 1] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
101 char out[kSize * 2];
102 void* p;
103 const void* q;
104 size_t bytes;
105 FifoBuffer buf(kSize);
106 StreamInterface* stream = &buf;
107
108 // Test assumptions about base state
109 EXPECT_EQ(SS_OPEN, stream->GetState());
110 EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, NULL));
111 EXPECT_TRUE(NULL != stream->GetReadData(&bytes));
112 EXPECT_EQ((size_t)0, bytes);
113 stream->ConsumeReadData(0);
114 EXPECT_TRUE(NULL != stream->GetWriteBuffer(&bytes));
115 EXPECT_EQ(kSize, bytes);
116 stream->ConsumeWriteBuffer(0);
117
118 // Try a full write
119 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, NULL));
120 EXPECT_EQ(kSize, bytes);
121
122 // Try a write that should block
123 EXPECT_EQ(SR_BLOCK, stream->Write(in, kSize, &bytes, NULL));
124
125 // Try a full read
126 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, NULL));
127 EXPECT_EQ(kSize, bytes);
128 EXPECT_EQ(0, memcmp(in, out, kSize));
129
130 // Try a read that should block
131 EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, NULL));
132
133 // Try a too-big write
134 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize * 2, &bytes, NULL));
135 EXPECT_EQ(bytes, kSize);
136
137 // Try a too-big read
138 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 2, &bytes, NULL));
139 EXPECT_EQ(kSize, bytes);
140 EXPECT_EQ(0, memcmp(in, out, kSize));
141
142 // Try some small writes and reads
143 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, NULL));
144 EXPECT_EQ(kSize / 2, bytes);
145 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, NULL));
146 EXPECT_EQ(kSize / 2, bytes);
147 EXPECT_EQ(0, memcmp(in, out, kSize / 2));
148 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, NULL));
149 EXPECT_EQ(kSize / 2, bytes);
150 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, NULL));
151 EXPECT_EQ(kSize / 2, bytes);
152 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, NULL));
153 EXPECT_EQ(kSize / 2, bytes);
154 EXPECT_EQ(0, memcmp(in, out, kSize / 2));
155 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, NULL));
156 EXPECT_EQ(kSize / 2, bytes);
157 EXPECT_EQ(0, memcmp(in, out, kSize / 2));
158
159 // Try wraparound reads and writes in the following pattern
160 // WWWWWWWWWWWW.... 0123456789AB....
161 // RRRRRRRRXXXX.... ........89AB....
162 // WWWW....XXXXWWWW 4567....89AB0123
163 // XXXX....RRRRXXXX 4567........0123
164 // XXXXWWWWWWWWXXXX 4567012345670123
165 // RRRRXXXXXXXXRRRR ....01234567....
166 // ....RRRRRRRR.... ................
167 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize * 3 / 4, &bytes, NULL));
168 EXPECT_EQ(kSize * 3 / 4, bytes);
169 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, NULL));
170 EXPECT_EQ(kSize / 2, bytes);
171 EXPECT_EQ(0, memcmp(in, out, kSize / 2));
172 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, NULL));
173 EXPECT_EQ(kSize / 2, bytes);
174 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 4, &bytes, NULL));
175 EXPECT_EQ(kSize / 4 , bytes);
176 EXPECT_EQ(0, memcmp(in + kSize / 2, out, kSize / 4));
177 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, NULL));
178 EXPECT_EQ(kSize / 2, bytes);
179 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, NULL));
180 EXPECT_EQ(kSize / 2 , bytes);
181 EXPECT_EQ(0, memcmp(in, out, kSize / 2));
182 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, NULL));
183 EXPECT_EQ(kSize / 2 , bytes);
184 EXPECT_EQ(0, memcmp(in, out, kSize / 2));
185
186 // Use GetWriteBuffer to reset the read_position for the next tests
187 stream->GetWriteBuffer(&bytes);
188 stream->ConsumeWriteBuffer(0);
189
190 // Try using GetReadData to do a full read
191 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, NULL));
192 q = stream->GetReadData(&bytes);
193 EXPECT_TRUE(NULL != q);
194 EXPECT_EQ(kSize, bytes);
195 EXPECT_EQ(0, memcmp(q, in, kSize));
196 stream->ConsumeReadData(kSize);
197 EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, NULL));
198
199 // Try using GetReadData to do some small reads
200 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, NULL));
201 q = stream->GetReadData(&bytes);
202 EXPECT_TRUE(NULL != q);
203 EXPECT_EQ(kSize, bytes);
204 EXPECT_EQ(0, memcmp(q, in, kSize / 2));
205 stream->ConsumeReadData(kSize / 2);
206 q = stream->GetReadData(&bytes);
207 EXPECT_TRUE(NULL != q);
208 EXPECT_EQ(kSize / 2, bytes);
209 EXPECT_EQ(0, memcmp(q, in + kSize / 2, kSize / 2));
210 stream->ConsumeReadData(kSize / 2);
211 EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, NULL));
212
213 // Try using GetReadData in a wraparound case
214 // WWWWWWWWWWWWWWWW 0123456789ABCDEF
215 // RRRRRRRRRRRRXXXX ............CDEF
216 // WWWWWWWW....XXXX 01234567....CDEF
217 // ............RRRR 01234567........
218 // RRRRRRRR........ ................
219 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, NULL));
220 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 3 / 4, &bytes, NULL));
221 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, NULL));
222 q = stream->GetReadData(&bytes);
223 EXPECT_TRUE(NULL != q);
224 EXPECT_EQ(kSize / 4, bytes);
225 EXPECT_EQ(0, memcmp(q, in + kSize * 3 / 4, kSize / 4));
226 stream->ConsumeReadData(kSize / 4);
227 q = stream->GetReadData(&bytes);
228 EXPECT_TRUE(NULL != q);
229 EXPECT_EQ(kSize / 2, bytes);
230 EXPECT_EQ(0, memcmp(q, in, kSize / 2));
231 stream->ConsumeReadData(kSize / 2);
232
233 // Use GetWriteBuffer to reset the read_position for the next tests
234 stream->GetWriteBuffer(&bytes);
235 stream->ConsumeWriteBuffer(0);
236
237 // Try using GetWriteBuffer to do a full write
238 p = stream->GetWriteBuffer(&bytes);
239 EXPECT_TRUE(NULL != p);
240 EXPECT_EQ(kSize, bytes);
241 memcpy(p, in, kSize);
242 stream->ConsumeWriteBuffer(kSize);
243 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, NULL));
244 EXPECT_EQ(kSize, bytes);
245 EXPECT_EQ(0, memcmp(in, out, kSize));
246
247 // Try using GetWriteBuffer to do some small writes
248 p = stream->GetWriteBuffer(&bytes);
249 EXPECT_TRUE(NULL != p);
250 EXPECT_EQ(kSize, bytes);
251 memcpy(p, in, kSize / 2);
252 stream->ConsumeWriteBuffer(kSize / 2);
253 p = stream->GetWriteBuffer(&bytes);
254 EXPECT_TRUE(NULL != p);
255 EXPECT_EQ(kSize / 2, bytes);
256 memcpy(p, in + kSize / 2, kSize / 2);
257 stream->ConsumeWriteBuffer(kSize / 2);
258 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, NULL));
259 EXPECT_EQ(kSize, bytes);
260 EXPECT_EQ(0, memcmp(in, out, kSize));
261
262 // Try using GetWriteBuffer in a wraparound case
263 // WWWWWWWWWWWW.... 0123456789AB....
264 // RRRRRRRRXXXX.... ........89AB....
265 // ........XXXXWWWW ........89AB0123
266 // WWWW....XXXXXXXX 4567....89AB0123
267 // RRRR....RRRRRRRR ................
268 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize * 3 / 4, &bytes, NULL));
269 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, NULL));
270 p = stream->GetWriteBuffer(&bytes);
271 EXPECT_TRUE(NULL != p);
272 EXPECT_EQ(kSize / 4, bytes);
273 memcpy(p, in, kSize / 4);
274 stream->ConsumeWriteBuffer(kSize / 4);
275 p = stream->GetWriteBuffer(&bytes);
276 EXPECT_TRUE(NULL != p);
277 EXPECT_EQ(kSize / 2, bytes);
278 memcpy(p, in + kSize / 4, kSize / 4);
279 stream->ConsumeWriteBuffer(kSize / 4);
280 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 3 / 4, &bytes, NULL));
281 EXPECT_EQ(kSize * 3 / 4, bytes);
282 EXPECT_EQ(0, memcmp(in + kSize / 2, out, kSize / 4));
283 EXPECT_EQ(0, memcmp(in, out + kSize / 4, kSize / 4));
284
285 // Check that the stream is now empty
286 EXPECT_EQ(SR_BLOCK, stream->Read(out, kSize, &bytes, NULL));
287
288 // Try growing the buffer
289 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, NULL));
290 EXPECT_EQ(kSize, bytes);
291 EXPECT_TRUE(buf.SetCapacity(kSize * 2));
292 EXPECT_EQ(SR_SUCCESS, stream->Write(in + kSize, kSize, &bytes, NULL));
293 EXPECT_EQ(kSize, bytes);
294 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize * 2, &bytes, NULL));
295 EXPECT_EQ(kSize * 2, bytes);
296 EXPECT_EQ(0, memcmp(in, out, kSize * 2));
297
298 // Try shrinking the buffer
299 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize, &bytes, NULL));
300 EXPECT_EQ(kSize, bytes);
301 EXPECT_TRUE(buf.SetCapacity(kSize));
302 EXPECT_EQ(SR_BLOCK, stream->Write(in, kSize, &bytes, NULL));
303 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize, &bytes, NULL));
304 EXPECT_EQ(kSize, bytes);
305 EXPECT_EQ(0, memcmp(in, out, kSize));
306
307 // Write to the stream, close it, read the remaining bytes
308 EXPECT_EQ(SR_SUCCESS, stream->Write(in, kSize / 2, &bytes, NULL));
309 stream->Close();
310 EXPECT_EQ(SS_CLOSED, stream->GetState());
311 EXPECT_EQ(SR_EOS, stream->Write(in, kSize / 2, &bytes, NULL));
312 EXPECT_EQ(SR_SUCCESS, stream->Read(out, kSize / 2, &bytes, NULL));
313 EXPECT_EQ(0, memcmp(in, out, kSize / 2));
314 EXPECT_EQ(SR_EOS, stream->Read(out, kSize / 2, &bytes, NULL));
315}
316
317TEST(FifoBufferTest, FullBufferCheck) {
318 FifoBuffer buff(10);
319 buff.ConsumeWriteBuffer(10);
320
321 size_t free;
322 EXPECT_TRUE(buff.GetWriteBuffer(&free) != NULL);
323 EXPECT_EQ(0U, free);
324}
325
326TEST(FifoBufferTest, WriteOffsetAndReadOffset) {
327 const size_t kSize = 16;
328 const char in[kSize * 2 + 1] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
329 char out[kSize * 2];
330 FifoBuffer buf(kSize);
331
332 // Write 14 bytes.
333 EXPECT_EQ(SR_SUCCESS, buf.Write(in, 14, NULL, NULL));
334
335 // Make sure data is in |buf|.
336 size_t buffered;
337 EXPECT_TRUE(buf.GetBuffered(&buffered));
338 EXPECT_EQ(14u, buffered);
339
340 // Read 10 bytes.
341 buf.ConsumeReadData(10);
342
343 // There should be now 12 bytes of available space.
344 size_t remaining;
345 EXPECT_TRUE(buf.GetWriteRemaining(&remaining));
346 EXPECT_EQ(12u, remaining);
347
348 // Write at offset 12, this should fail.
349 EXPECT_EQ(SR_BLOCK, buf.WriteOffset(in, 10, 12, NULL));
350
351 // Write 8 bytes at offset 4, this wraps around the buffer.
352 EXPECT_EQ(SR_SUCCESS, buf.WriteOffset(in, 8, 4, NULL));
353
354 // Number of available space remains the same until we call
355 // ConsumeWriteBuffer().
356 EXPECT_TRUE(buf.GetWriteRemaining(&remaining));
357 EXPECT_EQ(12u, remaining);
358 buf.ConsumeWriteBuffer(12);
359
360 // There's 4 bytes bypassed and 4 bytes no read so skip them and verify the
361 // 8 bytes written.
362 size_t read;
363 EXPECT_EQ(SR_SUCCESS, buf.ReadOffset(out, 8, 8, &read));
364 EXPECT_EQ(8u, read);
365 EXPECT_EQ(0, memcmp(out, in, 8));
366
367 // There should still be 16 bytes available for reading.
368 EXPECT_TRUE(buf.GetBuffered(&buffered));
369 EXPECT_EQ(16u, buffered);
370
371 // Read at offset 16, this should fail since we don't have that much data.
372 EXPECT_EQ(SR_BLOCK, buf.ReadOffset(out, 10, 16, NULL));
373}
374
Zeke Chin2d3b7e22015-07-14 12:55:44 -0700375class CircularFileStreamTest : public ::testing::Test {
376 protected:
377 static size_t const kMaxSize = 12;
378
379 CircularFileStreamTest() : is_open_(false), stream_(kMaxSize) {
380 Pathname temp_dir;
381 if (Filesystem::GetAppTempFolder(&temp_dir)) {
382 logfile_name_ =
383 Filesystem::TempFilename(temp_dir, "CircularFileStreamTest");
384 }
385 }
386
387 virtual void SetUp() {
388 int error = -1;
389 is_open_ = stream_.Open(logfile_name_, "wb", &error);
390 }
391
392 virtual void TearDown() {
393 if (!Filesystem::IsAbsent(logfile_name_)) {
394 Filesystem::DeleteFile(logfile_name_);
395 }
396 }
397
398 bool is_open_;
399 CircularFileStream stream_;
400 std::string logfile_name_;
401};
402
403TEST_F(CircularFileStreamTest, ReadWriteWithinCapacity) {
404 EXPECT_TRUE(is_open_);
405 // Write contents.
406 const uint8_t bytes[] = {1, 2, 3, 4, 5, 6};
407 size_t written = 0;
408 int error = 0;
409 EXPECT_EQ(SR_SUCCESS, stream_.Write(bytes, sizeof(bytes), &written, &error));
410 EXPECT_EQ(0, error);
411 EXPECT_EQ(written, sizeof(bytes));
412 stream_.Close();
413
414 // Check file contents.
415 uint8_t content_bytes[sizeof(bytes)] = {};
416 scoped_ptr<FileStream> content_stream(
417 Filesystem::OpenFile(logfile_name_, "r"));
418 size_t num_content_bytes_read = 0;
419 EXPECT_TRUE(content_stream);
420 error = 0;
421 EXPECT_EQ(SR_SUCCESS,
422 content_stream->Read(content_bytes, sizeof(content_bytes),
423 &num_content_bytes_read, &error));
424 EXPECT_EQ(sizeof(content_bytes), num_content_bytes_read);
425 ASSERT_EQ(sizeof(content_bytes), sizeof(bytes));
426 EXPECT_EQ(0, memcmp(content_bytes, bytes, sizeof(content_bytes)));
427
428 // Check read result.
429 error = 0;
430 size_t file_size = 0;
431 EXPECT_TRUE(stream_.Open(logfile_name_, "r", &error));
432 EXPECT_TRUE(stream_.GetSize(&file_size));
433 EXPECT_EQ(0, error);
434 EXPECT_EQ(sizeof(bytes), file_size);
435 scoped_ptr<uint8_t[]> read_bytes(new uint8_t[file_size]);
436 size_t num_read_bytes = 0;
437 error = 0;
438 EXPECT_EQ(SR_SUCCESS, stream_.ReadAll(read_bytes.get(), file_size,
439 &num_read_bytes, &error));
440 EXPECT_EQ(sizeof(bytes), num_read_bytes);
441 EXPECT_EQ(0, memcmp(bytes, read_bytes.get(), file_size));
442}
443
444TEST_F(CircularFileStreamTest, ReadWriteAtCapacity) {
445 EXPECT_TRUE(is_open_);
446 // Write contents.
447 const uint8_t bytes[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
448 size_t written = 0;
449 int error = 0;
450 EXPECT_EQ(SR_SUCCESS, stream_.Write(bytes, sizeof(bytes), &written, &error));
451 EXPECT_EQ(0, error);
452 EXPECT_EQ(written, sizeof(bytes));
453 stream_.Close();
454
455 // Check file contents.
456 uint8_t content_bytes[sizeof(bytes)] = {};
457 scoped_ptr<FileStream> content_stream(
458 Filesystem::OpenFile(logfile_name_, "r"));
459 size_t num_content_bytes_read = 0;
460 EXPECT_TRUE(content_stream);
461 error = 0;
462 EXPECT_EQ(SR_SUCCESS,
463 content_stream->Read(content_bytes, sizeof(content_bytes),
464 &num_content_bytes_read, &error));
465 EXPECT_EQ(sizeof(content_bytes), num_content_bytes_read);
466 ASSERT_EQ(sizeof(content_bytes), sizeof(bytes));
467 EXPECT_EQ(0, memcmp(content_bytes, bytes, sizeof(content_bytes)));
468
469 // Check read result.
470 error = 0;
471 size_t file_size = 0;
472 EXPECT_TRUE(stream_.Open(logfile_name_, "r", &error));
473 EXPECT_TRUE(stream_.GetSize(&file_size));
474 EXPECT_EQ(0, error);
475 EXPECT_EQ(sizeof(bytes), file_size);
476 scoped_ptr<uint8_t[]> read_bytes(new uint8_t[file_size]);
477 size_t num_read_bytes = 0;
478 error = 0;
479 EXPECT_EQ(SR_SUCCESS, stream_.ReadAll(read_bytes.get(), file_size,
480 &num_read_bytes, &error));
481 EXPECT_EQ(sizeof(bytes), num_read_bytes);
482 EXPECT_EQ(0, memcmp(bytes, read_bytes.get(), file_size));
483}
484
485TEST_F(CircularFileStreamTest, ReadWriteOverCapacity) {
486 EXPECT_TRUE(is_open_);
487 // Write contents.
488 const uint8_t bytes[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
489 size_t written = 0;
490 int error = 0;
491 EXPECT_EQ(SR_SUCCESS,
492 stream_.WriteAll(bytes, sizeof(bytes), &written, &error));
493 EXPECT_EQ(0, error);
494 EXPECT_EQ(written, sizeof(bytes));
495 stream_.Close();
496
497 // Check file contents.
498 uint8_t content_bytes[kMaxSize] = {};
499 scoped_ptr<FileStream> content_stream(
500 Filesystem::OpenFile(logfile_name_, "r"));
501 size_t num_content_bytes_read = 0;
502 EXPECT_TRUE(content_stream);
503 error = 0;
504 EXPECT_EQ(SR_SUCCESS,
505 content_stream->Read(content_bytes, sizeof(content_bytes),
506 &num_content_bytes_read, &error));
507 EXPECT_EQ(sizeof(content_bytes), num_content_bytes_read);
508 const uint8_t expected_content_bytes[] = {
509 1, 2, 3, 4, 5, 6, 13, 14, 15, 10, 11, 12};
510 ASSERT_EQ(sizeof(content_bytes), sizeof(expected_content_bytes));
511 EXPECT_EQ(
512 0, memcmp(expected_content_bytes, content_bytes, sizeof(content_bytes)));
513
514 // Check read result.
515 error = 0;
516 size_t file_size = 0;
517 EXPECT_TRUE(stream_.Open(logfile_name_, "r", &error));
518 EXPECT_TRUE(stream_.GetSize(&file_size));
519 EXPECT_EQ(0, error);
520 EXPECT_EQ(sizeof(content_bytes), file_size);
521 scoped_ptr<uint8_t[]> read_bytes(new uint8_t[file_size]);
522 size_t num_read_bytes = 0;
523 error = 0;
524 EXPECT_EQ(SR_SUCCESS, stream_.ReadAll(read_bytes.get(), file_size,
525 &num_read_bytes, &error));
526
527 const uint8_t expected_read_bytes[] = {
528 1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15};
529 EXPECT_EQ(sizeof(expected_read_bytes), num_read_bytes);
530 EXPECT_EQ(0, memcmp(expected_read_bytes, read_bytes.get(), file_size));
531}
532
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000533} // namespace rtc