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