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