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