OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
| 11 #include "webrtc/base/fileutils.h" |
11 #include "webrtc/base/gunit.h" | 12 #include "webrtc/base/gunit.h" |
| 13 #include "webrtc/base/pathutils.h" |
12 #include "webrtc/base/stream.h" | 14 #include "webrtc/base/stream.h" |
13 #include "webrtc/test/testsupport/gtest_disable.h" | 15 #include "webrtc/test/testsupport/gtest_disable.h" |
14 | 16 |
15 namespace rtc { | 17 namespace rtc { |
16 | 18 |
17 /////////////////////////////////////////////////////////////////////////////// | 19 /////////////////////////////////////////////////////////////////////////////// |
18 // TestStream | 20 // TestStream |
19 /////////////////////////////////////////////////////////////////////////////// | 21 /////////////////////////////////////////////////////////////////////////////// |
20 | 22 |
21 class TestStream : public StreamInterface { | 23 class TestStream : public StreamInterface { |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 EXPECT_EQ(0, memcmp(out, in, 8)); | 365 EXPECT_EQ(0, memcmp(out, in, 8)); |
364 | 366 |
365 // There should still be 16 bytes available for reading. | 367 // There should still be 16 bytes available for reading. |
366 EXPECT_TRUE(buf.GetBuffered(&buffered)); | 368 EXPECT_TRUE(buf.GetBuffered(&buffered)); |
367 EXPECT_EQ(16u, buffered); | 369 EXPECT_EQ(16u, buffered); |
368 | 370 |
369 // Read at offset 16, this should fail since we don't have that much data. | 371 // Read at offset 16, this should fail since we don't have that much data. |
370 EXPECT_EQ(SR_BLOCK, buf.ReadOffset(out, 10, 16, NULL)); | 372 EXPECT_EQ(SR_BLOCK, buf.ReadOffset(out, 10, 16, NULL)); |
371 } | 373 } |
372 | 374 |
| 375 class 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 |
| 403 TEST_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 |
| 444 TEST_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 |
| 485 TEST_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 |
373 } // namespace rtc | 533 } // namespace rtc |
OLD | NEW |