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