Index: webrtc/base/stream_unittest.cc |
diff --git a/webrtc/base/stream_unittest.cc b/webrtc/base/stream_unittest.cc |
index 86eb722c2adb555104a1c818eabfc4cfd378a3d2..4d5066ab55fce576d2be82df4c4eef937511d147 100644 |
--- a/webrtc/base/stream_unittest.cc |
+++ b/webrtc/base/stream_unittest.cc |
@@ -8,7 +8,9 @@ |
* be found in the AUTHORS file in the root of the source tree. |
*/ |
+#include "webrtc/base/fileutils.h" |
#include "webrtc/base/gunit.h" |
+#include "webrtc/base/pathutils.h" |
#include "webrtc/base/stream.h" |
#include "webrtc/test/testsupport/gtest_disable.h" |
@@ -370,4 +372,162 @@ TEST(FifoBufferTest, WriteOffsetAndReadOffset) { |
EXPECT_EQ(SR_BLOCK, buf.ReadOffset(out, 10, 16, NULL)); |
} |
+class CircularFileStreamTest : public ::testing::Test { |
+ protected: |
+ static size_t const kMaxSize = 12; |
+ |
+ CircularFileStreamTest() : is_open_(false), stream_(kMaxSize) { |
+ Pathname temp_dir; |
+ if (Filesystem::GetAppTempFolder(&temp_dir)) { |
+ logfile_name_ = |
+ Filesystem::TempFilename(temp_dir, "CircularFileStreamTest"); |
+ } |
+ } |
+ |
+ virtual void SetUp() { |
+ int error = -1; |
+ is_open_ = stream_.Open(logfile_name_, "wb", &error); |
+ } |
+ |
+ virtual void TearDown() { |
+ if (!Filesystem::IsAbsent(logfile_name_)) { |
+ Filesystem::DeleteFile(logfile_name_); |
+ } |
+ } |
+ |
+ bool is_open_; |
+ CircularFileStream stream_; |
+ std::string logfile_name_; |
+}; |
+ |
+TEST_F(CircularFileStreamTest, ReadWriteWithinCapacity) { |
+ EXPECT_TRUE(is_open_); |
+ // Write contents. |
+ const uint8_t bytes[] = {1, 2, 3, 4, 5, 6}; |
+ size_t written = 0; |
+ int error = 0; |
+ EXPECT_EQ(SR_SUCCESS, stream_.Write(bytes, sizeof(bytes), &written, &error)); |
+ EXPECT_EQ(0, error); |
+ EXPECT_EQ(written, sizeof(bytes)); |
+ stream_.Close(); |
+ |
+ // Check file contents. |
+ uint8_t content_bytes[sizeof(bytes)] = {}; |
+ scoped_ptr<FileStream> content_stream( |
+ Filesystem::OpenFile(logfile_name_, "r")); |
+ size_t num_content_bytes_read = 0; |
+ EXPECT_TRUE(content_stream); |
+ error = 0; |
+ EXPECT_EQ(SR_SUCCESS, |
+ content_stream->Read(content_bytes, sizeof(content_bytes), |
+ &num_content_bytes_read, &error)); |
+ EXPECT_EQ(sizeof(content_bytes), num_content_bytes_read); |
+ ASSERT_EQ(sizeof(content_bytes), sizeof(bytes)); |
+ EXPECT_EQ(0, memcmp(content_bytes, bytes, sizeof(content_bytes))); |
+ |
+ // Check read result. |
+ error = 0; |
+ size_t file_size = 0; |
+ EXPECT_TRUE(stream_.Open(logfile_name_, "r", &error)); |
+ EXPECT_TRUE(stream_.GetSize(&file_size)); |
+ EXPECT_EQ(0, error); |
+ EXPECT_EQ(sizeof(bytes), file_size); |
+ scoped_ptr<uint8_t[]> read_bytes(new uint8_t[file_size]); |
+ size_t num_read_bytes = 0; |
+ error = 0; |
+ EXPECT_EQ(SR_SUCCESS, stream_.ReadAll(read_bytes.get(), file_size, |
+ &num_read_bytes, &error)); |
+ EXPECT_EQ(sizeof(bytes), num_read_bytes); |
+ EXPECT_EQ(0, memcmp(bytes, read_bytes.get(), file_size)); |
+} |
+ |
+TEST_F(CircularFileStreamTest, ReadWriteAtCapacity) { |
+ EXPECT_TRUE(is_open_); |
+ // Write contents. |
+ const uint8_t bytes[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; |
+ size_t written = 0; |
+ int error = 0; |
+ EXPECT_EQ(SR_SUCCESS, stream_.Write(bytes, sizeof(bytes), &written, &error)); |
+ EXPECT_EQ(0, error); |
+ EXPECT_EQ(written, sizeof(bytes)); |
+ stream_.Close(); |
+ |
+ // Check file contents. |
+ uint8_t content_bytes[sizeof(bytes)] = {}; |
+ scoped_ptr<FileStream> content_stream( |
+ Filesystem::OpenFile(logfile_name_, "r")); |
+ size_t num_content_bytes_read = 0; |
+ EXPECT_TRUE(content_stream); |
+ error = 0; |
+ EXPECT_EQ(SR_SUCCESS, |
+ content_stream->Read(content_bytes, sizeof(content_bytes), |
+ &num_content_bytes_read, &error)); |
+ EXPECT_EQ(sizeof(content_bytes), num_content_bytes_read); |
+ ASSERT_EQ(sizeof(content_bytes), sizeof(bytes)); |
+ EXPECT_EQ(0, memcmp(content_bytes, bytes, sizeof(content_bytes))); |
+ |
+ // Check read result. |
+ error = 0; |
+ size_t file_size = 0; |
+ EXPECT_TRUE(stream_.Open(logfile_name_, "r", &error)); |
+ EXPECT_TRUE(stream_.GetSize(&file_size)); |
+ EXPECT_EQ(0, error); |
+ EXPECT_EQ(sizeof(bytes), file_size); |
+ scoped_ptr<uint8_t[]> read_bytes(new uint8_t[file_size]); |
+ size_t num_read_bytes = 0; |
+ error = 0; |
+ EXPECT_EQ(SR_SUCCESS, stream_.ReadAll(read_bytes.get(), file_size, |
+ &num_read_bytes, &error)); |
+ EXPECT_EQ(sizeof(bytes), num_read_bytes); |
+ EXPECT_EQ(0, memcmp(bytes, read_bytes.get(), file_size)); |
+} |
+ |
+TEST_F(CircularFileStreamTest, ReadWriteOverCapacity) { |
+ EXPECT_TRUE(is_open_); |
+ // Write contents. |
+ const uint8_t bytes[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; |
+ size_t written = 0; |
+ int error = 0; |
+ EXPECT_EQ(SR_SUCCESS, |
+ stream_.WriteAll(bytes, sizeof(bytes), &written, &error)); |
+ EXPECT_EQ(0, error); |
+ EXPECT_EQ(written, sizeof(bytes)); |
+ stream_.Close(); |
+ |
+ // Check file contents. |
+ uint8_t content_bytes[kMaxSize] = {}; |
+ scoped_ptr<FileStream> content_stream( |
+ Filesystem::OpenFile(logfile_name_, "r")); |
+ size_t num_content_bytes_read = 0; |
+ EXPECT_TRUE(content_stream); |
+ error = 0; |
+ EXPECT_EQ(SR_SUCCESS, |
+ content_stream->Read(content_bytes, sizeof(content_bytes), |
+ &num_content_bytes_read, &error)); |
+ EXPECT_EQ(sizeof(content_bytes), num_content_bytes_read); |
+ const uint8_t expected_content_bytes[] = { |
+ 1, 2, 3, 4, 5, 6, 13, 14, 15, 10, 11, 12}; |
+ ASSERT_EQ(sizeof(content_bytes), sizeof(expected_content_bytes)); |
+ EXPECT_EQ( |
+ 0, memcmp(expected_content_bytes, content_bytes, sizeof(content_bytes))); |
+ |
+ // Check read result. |
+ error = 0; |
+ size_t file_size = 0; |
+ EXPECT_TRUE(stream_.Open(logfile_name_, "r", &error)); |
+ EXPECT_TRUE(stream_.GetSize(&file_size)); |
+ EXPECT_EQ(0, error); |
+ EXPECT_EQ(sizeof(content_bytes), file_size); |
+ scoped_ptr<uint8_t[]> read_bytes(new uint8_t[file_size]); |
+ size_t num_read_bytes = 0; |
+ error = 0; |
+ EXPECT_EQ(SR_SUCCESS, stream_.ReadAll(read_bytes.get(), file_size, |
+ &num_read_bytes, &error)); |
+ |
+ const uint8_t expected_read_bytes[] = { |
+ 1, 2, 3, 4, 5, 6, 10, 11, 12, 13, 14, 15}; |
+ EXPECT_EQ(sizeof(expected_read_bytes), num_read_bytes); |
+ EXPECT_EQ(0, memcmp(expected_read_bytes, read_bytes.get(), file_size)); |
+} |
+ |
} // namespace rtc |