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/arraysize.h" | |
12 #include "webrtc/base/checks.h" | |
11 #include "webrtc/base/fileutils.h" | 13 #include "webrtc/base/fileutils.h" |
12 #include "webrtc/base/gunit.h" | 14 #include "webrtc/base/gunit.h" |
13 #include "webrtc/base/pathutils.h" | 15 #include "webrtc/base/pathutils.h" |
14 #include "webrtc/base/stream.h" | 16 #include "webrtc/base/stream.h" |
15 #include "webrtc/test/testsupport/gtest_disable.h" | 17 #include "webrtc/test/testsupport/gtest_disable.h" |
16 | 18 |
17 namespace rtc { | 19 namespace rtc { |
18 | 20 |
19 /////////////////////////////////////////////////////////////////////////////// | 21 /////////////////////////////////////////////////////////////////////////////// |
20 // TestStream | 22 // TestStream |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
523 error = 0; | 525 error = 0; |
524 EXPECT_EQ(SR_SUCCESS, stream_.ReadAll(read_bytes.get(), file_size, | 526 EXPECT_EQ(SR_SUCCESS, stream_.ReadAll(read_bytes.get(), file_size, |
525 &num_read_bytes, &error)); | 527 &num_read_bytes, &error)); |
526 | 528 |
527 const uint8_t expected_read_bytes[] = { | 529 const uint8_t expected_read_bytes[] = { |
528 1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15}; | 530 1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15}; |
529 EXPECT_EQ(sizeof(expected_read_bytes), num_read_bytes); | 531 EXPECT_EQ(sizeof(expected_read_bytes), num_read_bytes); |
530 EXPECT_EQ(0, memcmp(expected_read_bytes, read_bytes.get(), file_size)); | 532 EXPECT_EQ(0, memcmp(expected_read_bytes, read_bytes.get(), file_size)); |
531 } | 533 } |
532 | 534 |
535 class FileRotatingStreamTest : public ::testing::Test { | |
jiayl2
2015/07/20 23:27:06
ditto separate file.
tkchin_webrtc
2015/07/21 20:39:13
Done.
| |
536 protected: | |
537 static const char* kFilePrefix; | |
538 static const size_t kMaxFileSize; | |
539 | |
540 void Init(const std::string& dir_name, | |
541 const std::string& file_prefix, | |
542 size_t max_file_size, | |
543 size_t num_log_files) { | |
544 Pathname test_path; | |
545 ASSERT_TRUE(Filesystem::GetAppTempFolder(&test_path)); | |
546 // Append per-test output path in order to run within gtest parallel. | |
547 test_path.AppendFolder(dir_name); | |
548 ASSERT_TRUE(Filesystem::CreateFolder(test_path)); | |
549 dir_path_ = test_path.pathname(); | |
550 ASSERT_TRUE(dir_path_.size()); | |
551 stream_.reset(new FileRotatingStream(dir_path_, file_prefix, max_file_size, | |
552 num_log_files)); | |
553 } | |
554 | |
555 void TearDown() override { | |
556 stream_.reset(); | |
557 if (dir_path_.size() && Filesystem::IsFolder(dir_path_) && | |
558 Filesystem::IsTemporaryPath(dir_path_)) { | |
559 Filesystem::DeleteFolderAndContents(dir_path_); | |
560 } | |
561 } | |
562 | |
563 void WriteAndFlush(const void* data, const size_t data_len) { | |
564 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); | |
565 EXPECT_TRUE(stream_->Flush()); | |
566 } | |
567 | |
568 void VerifyStreamRead(const char* expected_contents, | |
569 const size_t expected_length, | |
570 const std::string& dir_path, | |
571 const char* file_prefix) { | |
572 scoped_ptr<FileRotatingStream> stream; | |
573 stream.reset(new FileRotatingStream(dir_path, file_prefix)); | |
574 ASSERT_TRUE(stream->Open()); | |
575 scoped_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); | |
576 EXPECT_EQ(SR_SUCCESS, | |
577 stream->ReadAll(buffer.get(), expected_length, nullptr, nullptr)); | |
578 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); | |
579 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); | |
580 } | |
581 | |
582 void VerifyFileContents(const char* expected_contents, | |
583 const size_t expected_length, | |
584 const std::string& file_path) { | |
585 scoped_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); | |
586 scoped_ptr<FileStream> stream(Filesystem::OpenFile(file_path, "r")); | |
587 EXPECT_TRUE(stream); | |
588 if (!stream) { | |
589 return; | |
590 } | |
591 EXPECT_EQ(rtc::SR_SUCCESS, | |
592 stream->ReadAll(buffer.get(), expected_length, nullptr, nullptr)); | |
593 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); | |
594 size_t file_size = 0; | |
595 EXPECT_TRUE(stream->GetSize(&file_size)); | |
596 EXPECT_EQ(file_size, expected_length); | |
597 } | |
598 | |
599 scoped_ptr<FileRotatingStream> stream_; | |
600 std::string dir_path_; | |
601 }; | |
602 | |
603 const char* FileRotatingStreamTest::kFilePrefix = "FileRotatingStreamTest"; | |
604 const size_t FileRotatingStreamTest::kMaxFileSize = 2; | |
605 | |
606 TEST_F(FileRotatingStreamTest, State) { | |
607 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3); | |
608 | |
609 EXPECT_EQ(SS_CLOSED, stream_->GetState()); | |
610 ASSERT_TRUE(stream_->Open()); | |
611 EXPECT_EQ(SS_OPEN, stream_->GetState()); | |
612 stream_->Close(); | |
613 EXPECT_EQ(SS_CLOSED, stream_->GetState()); | |
614 } | |
615 | |
616 TEST_F(FileRotatingStreamTest, EmptyWrite) { | |
jiayl2
2015/07/20 23:27:06
nit: please comment on what the test does and veri
tkchin_webrtc
2015/07/21 20:39:13
Done.
| |
617 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3); | |
618 | |
619 ASSERT_TRUE(stream_->Open()); | |
620 WriteAndFlush("a", 0); | |
621 | |
622 std::string logfile_path = stream_->GetFilePath(0); | |
623 scoped_ptr<FileStream> stream(Filesystem::OpenFile(logfile_path, "r")); | |
624 size_t file_size = 0; | |
625 EXPECT_TRUE(stream->GetSize(&file_size)); | |
626 EXPECT_EQ(0u, file_size); | |
627 } | |
628 | |
629 TEST_F(FileRotatingStreamTest, WriteAndRead) { | |
630 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3); | |
631 | |
632 ASSERT_TRUE(stream_->Open()); | |
633 // The test is set up to create three log files of length 2. Write and check | |
634 // contents. | |
635 std::string messages[3] = {"aa", "bb", "cc"}; | |
636 for (size_t i = 0; i < arraysize(messages); ++i) { | |
637 const std::string& message = messages[i]; | |
638 WriteAndFlush(message.c_str(), message.size()); | |
639 // Since the max log size is 2, we will be causing rotation. Read from the | |
640 // next file. | |
641 VerifyFileContents(message.c_str(), message.size(), | |
642 stream_->GetFilePath(1)); | |
643 } | |
644 // Check that exactly three files exist. | |
645 for (size_t i = 0; i < arraysize(messages); ++i) { | |
646 EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i))); | |
647 } | |
648 std::string message("d"); | |
649 WriteAndFlush(message.c_str(), message.size()); | |
650 for (size_t i = 0; i < arraysize(messages); ++i) { | |
651 EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i))); | |
652 } | |
653 // TODO(tkchin): Maybe check all the files in the dir. | |
654 | |
655 // Reopen for read. | |
656 std::string expected_contents("bbccd"); | |
657 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), | |
658 dir_path_, kFilePrefix); | |
659 } | |
660 | |
661 TEST_F(FileRotatingStreamTest, WriteOverflowAndRead) { | |
662 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize, | |
663 3); | |
664 ASSERT_TRUE(stream_->Open()); | |
665 // This should cause overflow across all three files, such that the first file | |
666 // we wrote to also gets overwritten. | |
667 std::string message("foobarbaz"); | |
668 WriteAndFlush(message.c_str(), message.size()); | |
669 std::string expected_file_contents("z"); | |
670 VerifyFileContents(expected_file_contents.c_str(), | |
671 expected_file_contents.size(), stream_->GetFilePath(0)); | |
672 std::string expected_stream_contents("arbaz"); | |
673 VerifyStreamRead(expected_stream_contents.c_str(), | |
674 expected_stream_contents.size(), dir_path_, kFilePrefix); | |
675 } | |
676 | |
677 TEST_F(FileRotatingStreamTest, GetFilePath) { | |
678 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20); | |
679 for (auto i = 0; i < 20; ++i) { | |
680 Pathname path(stream_->GetFilePath(i)); | |
681 EXPECT_EQ(0, path.folder().compare(dir_path_)); | |
682 EXPECT_EQ(0, path.filename().compare(0, strlen(kFilePrefix), kFilePrefix)); | |
683 } | |
684 } | |
685 | |
686 class MobileDeviceLogStreamTest : public ::testing::Test { | |
687 protected: | |
688 void Init(const std::string& dir_name, size_t max_total_log_size) { | |
689 Pathname test_path; | |
690 ASSERT_TRUE(Filesystem::GetAppTempFolder(&test_path)); | |
691 // Append per-test output path in order to run within gtest parallel. | |
692 test_path.AppendFolder(dir_name); | |
693 ASSERT_TRUE(Filesystem::CreateFolder(test_path)); | |
694 dir_path_ = test_path.pathname(); | |
695 ASSERT_TRUE(dir_path_.size()); | |
696 stream_.reset(new MobileDeviceLogStream(dir_path_, max_total_log_size)); | |
697 } | |
698 | |
699 virtual void TearDown() { | |
700 stream_.reset(); | |
701 if (dir_path_.size() && Filesystem::IsFolder(dir_path_) && | |
702 Filesystem::IsTemporaryPath(dir_path_)) { | |
703 Filesystem::DeleteFolderAndContents(dir_path_); | |
704 } | |
705 } | |
706 | |
707 void WriteAndFlush(const void* data, const size_t data_len) { | |
708 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); | |
709 EXPECT_TRUE(stream_->Flush()); | |
710 } | |
711 | |
712 void VerifyStreamRead(const char* expected_contents, | |
713 const size_t expected_length, | |
714 const std::string& dir_path) { | |
715 scoped_ptr<MobileDeviceLogStream> stream( | |
716 new MobileDeviceLogStream(dir_path)); | |
717 ASSERT_TRUE(stream->Open()); | |
718 scoped_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); | |
719 EXPECT_EQ(SR_SUCCESS, | |
720 stream->ReadAll(buffer.get(), expected_length, nullptr, nullptr)); | |
721 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); | |
722 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); | |
723 } | |
724 | |
725 scoped_ptr<MobileDeviceLogStream> stream_; | |
726 std::string dir_path_; | |
727 }; | |
728 | |
729 TEST_F(MobileDeviceLogStreamTest, WriteAndReadSmallest) { | |
730 Init("MobileDeviceLogStreamTestWriteAndReadSmallest", 4); | |
731 | |
732 ASSERT_TRUE(stream_->Open()); | |
733 std::string message("abcde"); | |
734 WriteAndFlush(message.c_str(), message.size()); | |
735 std::string expected_contents("abe"); | |
736 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), | |
737 dir_path_); | |
738 } | |
739 | |
740 TEST_F(MobileDeviceLogStreamTest, WriteAndReadSmall) { | |
741 Init("MobileDeviceLogStreamTestWriteAndReadSmall", 8); | |
742 | |
743 ASSERT_TRUE(stream_->Open()); | |
744 std::string message("123456789"); | |
745 WriteAndFlush(message.c_str(), message.size()); | |
746 std::string expected_contents("1234789"); | |
747 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), | |
748 dir_path_); | |
749 } | |
750 | |
751 TEST_F(MobileDeviceLogStreamTest, WriteAndReadLarge) { | |
752 Init("MobileDeviceLogStreamTestWriteAndReadLarge", 6 * 1024 * 1024); | |
753 | |
754 ASSERT_TRUE(stream_->Open()); | |
755 const size_t buffer_size = 1024 * 1024; | |
756 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | |
757 for (int i = 0; i < 8; i++) { | |
758 memset(buffer.get(), i, buffer_size); | |
759 EXPECT_EQ(SR_SUCCESS, | |
760 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); | |
761 } | |
762 | |
763 stream_.reset(new MobileDeviceLogStream(dir_path_)); | |
764 ASSERT_TRUE(stream_->Open()); | |
765 scoped_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); | |
766 int expected_vals[] = {0, 1, 2, 6, 7}; | |
767 for (size_t i = 0; i < arraysize(expected_vals); ++i) { | |
768 memset(expected_buffer.get(), expected_vals[i], buffer_size); | |
769 EXPECT_EQ(SR_SUCCESS, | |
770 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); | |
771 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); | |
772 } | |
773 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); | |
774 } | |
775 | |
776 TEST_F(MobileDeviceLogStreamTest, WriteAndReadFirstHalf) { | |
777 Init("MobileDeviceLogStreamTestWriteAndReadFirstHalf", 6 * 1024 * 1024); | |
778 ASSERT_TRUE(stream_->Open()); | |
779 const size_t buffer_size = 1024 * 1024; | |
780 scoped_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | |
781 for (int i = 0; i < 2; i++) { | |
782 memset(buffer.get(), i, buffer_size); | |
783 EXPECT_EQ(SR_SUCCESS, | |
784 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); | |
785 } | |
786 | |
787 stream_.reset(new MobileDeviceLogStream(dir_path_)); | |
788 ASSERT_TRUE(stream_->Open()); | |
789 scoped_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); | |
790 int expected_vals[] = {0, 1}; | |
791 for (size_t i = 0; i < arraysize(expected_vals); ++i) { | |
792 memset(expected_buffer.get(), expected_vals[i], buffer_size); | |
793 EXPECT_EQ(SR_SUCCESS, | |
794 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); | |
795 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); | |
796 } | |
797 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); | |
798 } | |
799 | |
533 } // namespace rtc | 800 } // namespace rtc |
OLD | NEW |