tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/arraysize.h" |
| 14 | #include "rtc_base/checks.h" |
| 15 | #include "rtc_base/filerotatingstream.h" |
| 16 | #include "rtc_base/fileutils.h" |
| 17 | #include "rtc_base/gunit.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "test/testsupport/fileutils.h" |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | void CleanupLogDirectory(const FileRotatingStream& stream) { |
| 25 | for (size_t i = 0; i < stream.GetNumFiles(); ++i) { |
| 26 | // Ignore return value, not all files are expected to exist. |
| 27 | webrtc::test::RemoveFile(stream.GetFilePath(i)); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 33 | #if defined(WEBRTC_ANDROID) |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 34 | // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
| 35 | #define MAYBE_FileRotatingStreamTest DISABLED_FileRotatingStreamTest |
| 36 | #else |
| 37 | #define MAYBE_FileRotatingStreamTest FileRotatingStreamTest |
| 38 | #endif |
| 39 | |
| 40 | class MAYBE_FileRotatingStreamTest : public ::testing::Test { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 41 | protected: |
| 42 | static const char* kFilePrefix; |
| 43 | static const size_t kMaxFileSize; |
| 44 | |
| 45 | void Init(const std::string& dir_name, |
| 46 | const std::string& file_prefix, |
| 47 | size_t max_file_size, |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame^] | 48 | size_t num_log_files, |
| 49 | bool ensure_trailing_delimiter = true) { |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 50 | dir_path_ = webrtc::test::OutputPath(); |
| 51 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 52 | // Append per-test output path in order to run within gtest parallel. |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 53 | dir_path_.append(dir_name); |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame^] | 54 | if (ensure_trailing_delimiter) { |
| 55 | dir_path_.append(webrtc::test::kPathDelimiter); |
| 56 | } |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 57 | ASSERT_TRUE(webrtc::test::CreateDir(dir_path_)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 58 | stream_.reset(new FileRotatingStream(dir_path_, file_prefix, max_file_size, |
| 59 | num_log_files)); |
| 60 | } |
| 61 | |
| 62 | void TearDown() override { |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 63 | // On windows, open files can't be removed. |
| 64 | stream_->Close(); |
| 65 | CleanupLogDirectory(*stream_); |
| 66 | EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_)); |
| 67 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 68 | stream_.reset(); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Writes the data to the stream and flushes it. |
| 72 | void WriteAndFlush(const void* data, const size_t data_len) { |
| 73 | EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); |
| 74 | EXPECT_TRUE(stream_->Flush()); |
| 75 | } |
| 76 | |
| 77 | // Checks that the stream reads in the expected contents and then returns an |
| 78 | // end of stream result. |
| 79 | void VerifyStreamRead(const char* expected_contents, |
| 80 | const size_t expected_length, |
| 81 | const std::string& dir_path, |
| 82 | const char* file_prefix) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 83 | std::unique_ptr<FileRotatingStream> stream; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 84 | stream.reset(new FileRotatingStream(dir_path, file_prefix)); |
| 85 | ASSERT_TRUE(stream->Open()); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 86 | size_t read = 0; |
| 87 | size_t stream_size = 0; |
| 88 | EXPECT_TRUE(stream->GetSize(&stream_size)); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 89 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 90 | EXPECT_EQ(SR_SUCCESS, |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 91 | stream->ReadAll(buffer.get(), expected_length, &read, nullptr)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 92 | EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); |
| 93 | EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 94 | EXPECT_EQ(stream_size, read); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void VerifyFileContents(const char* expected_contents, |
| 98 | const size_t expected_length, |
| 99 | const std::string& file_path) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 100 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); |
nisse | 2108326 | 2017-05-31 02:07:21 -0700 | [diff] [blame] | 101 | FileStream stream; |
| 102 | ASSERT_TRUE(stream.Open(file_path, "r", nullptr)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 103 | EXPECT_EQ(rtc::SR_SUCCESS, |
nisse | 2108326 | 2017-05-31 02:07:21 -0700 | [diff] [blame] | 104 | stream.ReadAll(buffer.get(), expected_length, nullptr, nullptr)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 105 | EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); |
| 106 | size_t file_size = 0; |
nisse | 2108326 | 2017-05-31 02:07:21 -0700 | [diff] [blame] | 107 | EXPECT_TRUE(stream.GetSize(&file_size)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 108 | EXPECT_EQ(file_size, expected_length); |
| 109 | } |
| 110 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 111 | std::unique_ptr<FileRotatingStream> stream_; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 112 | std::string dir_path_; |
| 113 | }; |
| 114 | |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 115 | const char* MAYBE_FileRotatingStreamTest::kFilePrefix = |
| 116 | "FileRotatingStreamTest"; |
| 117 | const size_t MAYBE_FileRotatingStreamTest::kMaxFileSize = 2; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 118 | |
| 119 | // Tests that stream state is correct before and after Open / Close. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 120 | TEST_F(MAYBE_FileRotatingStreamTest, State) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 121 | Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3); |
| 122 | |
| 123 | EXPECT_EQ(SS_CLOSED, stream_->GetState()); |
| 124 | ASSERT_TRUE(stream_->Open()); |
| 125 | EXPECT_EQ(SS_OPEN, stream_->GetState()); |
| 126 | stream_->Close(); |
| 127 | EXPECT_EQ(SS_CLOSED, stream_->GetState()); |
| 128 | } |
| 129 | |
| 130 | // Tests that nothing is written to file when data of length zero is written. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 131 | TEST_F(MAYBE_FileRotatingStreamTest, EmptyWrite) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 132 | Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3); |
| 133 | |
| 134 | ASSERT_TRUE(stream_->Open()); |
| 135 | WriteAndFlush("a", 0); |
| 136 | |
| 137 | std::string logfile_path = stream_->GetFilePath(0); |
nisse | 2108326 | 2017-05-31 02:07:21 -0700 | [diff] [blame] | 138 | FileStream stream; |
| 139 | ASSERT_TRUE(stream.Open(logfile_path, "r", nullptr)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 140 | size_t file_size = 0; |
nisse | 2108326 | 2017-05-31 02:07:21 -0700 | [diff] [blame] | 141 | EXPECT_TRUE(stream.GetSize(&file_size)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 142 | EXPECT_EQ(0u, file_size); |
| 143 | } |
| 144 | |
| 145 | // Tests that a write operation followed by a read returns the expected data |
| 146 | // and writes to the expected files. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 147 | TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 148 | Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3); |
| 149 | |
| 150 | ASSERT_TRUE(stream_->Open()); |
| 151 | // The test is set up to create three log files of length 2. Write and check |
| 152 | // contents. |
| 153 | std::string messages[3] = {"aa", "bb", "cc"}; |
| 154 | for (size_t i = 0; i < arraysize(messages); ++i) { |
| 155 | const std::string& message = messages[i]; |
| 156 | WriteAndFlush(message.c_str(), message.size()); |
| 157 | // Since the max log size is 2, we will be causing rotation. Read from the |
| 158 | // next file. |
| 159 | VerifyFileContents(message.c_str(), message.size(), |
| 160 | stream_->GetFilePath(1)); |
| 161 | } |
| 162 | // Check that exactly three files exist. |
| 163 | for (size_t i = 0; i < arraysize(messages); ++i) { |
| 164 | EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i))); |
| 165 | } |
| 166 | std::string message("d"); |
| 167 | WriteAndFlush(message.c_str(), message.size()); |
| 168 | for (size_t i = 0; i < arraysize(messages); ++i) { |
| 169 | EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i))); |
| 170 | } |
| 171 | // TODO(tkchin): Maybe check all the files in the dir. |
| 172 | |
| 173 | // Reopen for read. |
| 174 | std::string expected_contents("bbccd"); |
| 175 | VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), |
| 176 | dir_path_, kFilePrefix); |
| 177 | } |
| 178 | |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame^] | 179 | // Tests that a write operation (with dir name without delimiter) followed by a |
| 180 | // read returns the expected data and writes to the expected files. |
| 181 | TEST_F(MAYBE_FileRotatingStreamTest, WriteWithoutDelimiterAndRead) { |
| 182 | Init("FileRotatingStreamTestWriteWithoutDelimiterAndRead", kFilePrefix, |
| 183 | kMaxFileSize, 3, |
| 184 | /* ensure_trailing_delimiter*/ false); |
| 185 | |
| 186 | ASSERT_TRUE(stream_->Open()); |
| 187 | // The test is set up to create three log files of length 2. Write and check |
| 188 | // contents. |
| 189 | std::string messages[3] = {"aa", "bb", "cc"}; |
| 190 | for (size_t i = 0; i < arraysize(messages); ++i) { |
| 191 | const std::string& message = messages[i]; |
| 192 | WriteAndFlush(message.c_str(), message.size()); |
| 193 | } |
| 194 | std::string message("d"); |
| 195 | WriteAndFlush(message.c_str(), message.size()); |
| 196 | |
| 197 | // Reopen for read. |
| 198 | std::string expected_contents("bbccd"); |
| 199 | VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), |
| 200 | dir_path_ + webrtc::test::kPathDelimiter, kFilePrefix); |
| 201 | } |
| 202 | |
| 203 | // Tests that a write operation followed by a read (without trailing delimiter) |
| 204 | // returns the expected data and writes to the expected files. |
| 205 | TEST_F(MAYBE_FileRotatingStreamTest, WriteAndReadWithoutDelimiter) { |
| 206 | Init("FileRotatingStreamTestWriteAndReadWithoutDelimiter", kFilePrefix, |
| 207 | kMaxFileSize, 3); |
| 208 | |
| 209 | ASSERT_TRUE(stream_->Open()); |
| 210 | // The test is set up to create three log files of length 2. Write and check |
| 211 | // contents. |
| 212 | std::string messages[3] = {"aa", "bb", "cc"}; |
| 213 | for (size_t i = 0; i < arraysize(messages); ++i) { |
| 214 | const std::string& message = messages[i]; |
| 215 | WriteAndFlush(message.c_str(), message.size()); |
| 216 | } |
| 217 | std::string message("d"); |
| 218 | WriteAndFlush(message.c_str(), message.size()); |
| 219 | |
| 220 | // Reopen for read. |
| 221 | std::string expected_contents("bbccd"); |
| 222 | VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), |
| 223 | dir_path_.substr(0, dir_path_.size() - 1), kFilePrefix); |
| 224 | } |
| 225 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 226 | // Tests that writing data greater than the total capacity of the files |
| 227 | // overwrites the files correctly and is read correctly after. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 228 | TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 229 | Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize, |
| 230 | 3); |
| 231 | ASSERT_TRUE(stream_->Open()); |
| 232 | // This should cause overflow across all three files, such that the first file |
| 233 | // we wrote to also gets overwritten. |
| 234 | std::string message("foobarbaz"); |
| 235 | WriteAndFlush(message.c_str(), message.size()); |
| 236 | std::string expected_file_contents("z"); |
| 237 | VerifyFileContents(expected_file_contents.c_str(), |
| 238 | expected_file_contents.size(), stream_->GetFilePath(0)); |
| 239 | std::string expected_stream_contents("arbaz"); |
| 240 | VerifyStreamRead(expected_stream_contents.c_str(), |
| 241 | expected_stream_contents.size(), dir_path_, kFilePrefix); |
| 242 | } |
| 243 | |
| 244 | // Tests that the returned file paths have the right folder and prefix. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 245 | TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 246 | Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20); |
Niels Möller | 394b4eb | 2018-06-01 14:08:25 +0200 | [diff] [blame] | 247 | // dir_path_ includes a trailing delimiter. |
| 248 | const std::string prefix = dir_path_ + kFilePrefix; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 249 | for (auto i = 0; i < 20; ++i) { |
Niels Möller | 394b4eb | 2018-06-01 14:08:25 +0200 | [diff] [blame] | 250 | EXPECT_EQ(0, stream_->GetFilePath(i).compare(0, prefix.size(), prefix)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 254 | #if defined(WEBRTC_ANDROID) |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 255 | // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
| 256 | #define MAYBE_CallSessionFileRotatingStreamTest \ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 257 | DISABLED_CallSessionFileRotatingStreamTest |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 258 | #else |
| 259 | #define MAYBE_CallSessionFileRotatingStreamTest \ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 260 | CallSessionFileRotatingStreamTest |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 261 | #endif |
| 262 | |
| 263 | class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 264 | protected: |
| 265 | void Init(const std::string& dir_name, size_t max_total_log_size) { |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 266 | dir_path_ = webrtc::test::OutputPath(); |
| 267 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 268 | // Append per-test output path in order to run within gtest parallel. |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 269 | dir_path_.append(dir_name); |
Niels Möller | 7b3c76b | 2018-11-07 09:54:28 +0100 | [diff] [blame^] | 270 | dir_path_.append(webrtc::test::kPathDelimiter); |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 271 | ASSERT_TRUE(webrtc::test::CreateDir(dir_path_)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 272 | stream_.reset( |
| 273 | new CallSessionFileRotatingStream(dir_path_, max_total_log_size)); |
| 274 | } |
| 275 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 276 | void TearDown() override { |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 277 | // On windows, open files can't be removed. |
| 278 | stream_->Close(); |
| 279 | CleanupLogDirectory(*stream_); |
| 280 | EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_)); |
| 281 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 282 | stream_.reset(); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // Writes the data to the stream and flushes it. |
| 286 | void WriteAndFlush(const void* data, const size_t data_len) { |
| 287 | EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); |
| 288 | EXPECT_TRUE(stream_->Flush()); |
| 289 | } |
| 290 | |
| 291 | // Checks that the stream reads in the expected contents and then returns an |
| 292 | // end of stream result. |
| 293 | void VerifyStreamRead(const char* expected_contents, |
| 294 | const size_t expected_length, |
| 295 | const std::string& dir_path) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 296 | std::unique_ptr<CallSessionFileRotatingStream> stream( |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 297 | new CallSessionFileRotatingStream(dir_path)); |
| 298 | ASSERT_TRUE(stream->Open()); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 299 | size_t read = 0; |
| 300 | size_t stream_size = 0; |
| 301 | EXPECT_TRUE(stream->GetSize(&stream_size)); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 302 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 303 | EXPECT_EQ(SR_SUCCESS, |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 304 | stream->ReadAll(buffer.get(), expected_length, &read, nullptr)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 305 | EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); |
| 306 | EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 307 | EXPECT_EQ(stream_size, read); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 308 | } |
| 309 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 310 | std::unique_ptr<CallSessionFileRotatingStream> stream_; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 311 | std::string dir_path_; |
| 312 | }; |
| 313 | |
| 314 | // Tests that writing and reading to a stream with the smallest possible |
| 315 | // capacity works. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 316 | TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 317 | Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4); |
| 318 | |
| 319 | ASSERT_TRUE(stream_->Open()); |
| 320 | std::string message("abcde"); |
| 321 | WriteAndFlush(message.c_str(), message.size()); |
| 322 | std::string expected_contents("abe"); |
| 323 | VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), |
| 324 | dir_path_); |
| 325 | } |
| 326 | |
| 327 | // Tests that writing and reading to a stream with capacity lesser than 4MB |
| 328 | // behaves correctly. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 329 | TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 330 | Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8); |
| 331 | |
| 332 | ASSERT_TRUE(stream_->Open()); |
| 333 | std::string message("123456789"); |
| 334 | WriteAndFlush(message.c_str(), message.size()); |
| 335 | std::string expected_contents("1234789"); |
| 336 | VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), |
| 337 | dir_path_); |
| 338 | } |
| 339 | |
| 340 | // Tests that writing and reading to a stream with capacity greater than 4MB |
| 341 | // behaves correctly. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 342 | TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 343 | Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024); |
| 344 | |
| 345 | ASSERT_TRUE(stream_->Open()); |
| 346 | const size_t buffer_size = 1024 * 1024; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 347 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 348 | for (int i = 0; i < 8; i++) { |
| 349 | memset(buffer.get(), i, buffer_size); |
| 350 | EXPECT_EQ(SR_SUCCESS, |
| 351 | stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 352 | } |
| 353 | |
| 354 | stream_.reset(new CallSessionFileRotatingStream(dir_path_)); |
| 355 | ASSERT_TRUE(stream_->Open()); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 356 | std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 357 | int expected_vals[] = {0, 1, 2, 6, 7}; |
| 358 | for (size_t i = 0; i < arraysize(expected_vals); ++i) { |
| 359 | memset(expected_buffer.get(), expected_vals[i], buffer_size); |
| 360 | EXPECT_EQ(SR_SUCCESS, |
| 361 | stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 362 | EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); |
| 363 | } |
| 364 | EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
| 365 | } |
| 366 | |
| 367 | // Tests that writing and reading to a stream where only the first file is |
| 368 | // written to behaves correctly. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 369 | TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 370 | Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf", |
| 371 | 6 * 1024 * 1024); |
| 372 | ASSERT_TRUE(stream_->Open()); |
| 373 | const size_t buffer_size = 1024 * 1024; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 374 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 375 | for (int i = 0; i < 2; i++) { |
| 376 | memset(buffer.get(), i, buffer_size); |
| 377 | EXPECT_EQ(SR_SUCCESS, |
| 378 | stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 379 | } |
| 380 | |
| 381 | stream_.reset(new CallSessionFileRotatingStream(dir_path_)); |
| 382 | ASSERT_TRUE(stream_->Open()); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 383 | std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 384 | int expected_vals[] = {0, 1}; |
| 385 | for (size_t i = 0; i < arraysize(expected_vals); ++i) { |
| 386 | memset(expected_buffer.get(), expected_vals[i], buffer_size); |
| 387 | EXPECT_EQ(SR_SUCCESS, |
| 388 | stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 389 | EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); |
| 390 | } |
| 391 | EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
| 392 | } |
| 393 | |
| 394 | } // namespace rtc |