tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/arraysize.h" |
| 14 | #include "rtc_base/checks.h" |
| 15 | #include "rtc_base/filerotatingstream.h" |
| 16 | #include "rtc_base/fileutils.h" |
| 17 | #include "rtc_base/gunit.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "test/testsupport/fileutils.h" |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | void CleanupLogDirectory(const FileRotatingStream& stream) { |
| 25 | for (size_t i = 0; i < stream.GetNumFiles(); ++i) { |
| 26 | // Ignore return value, not all files are expected to exist. |
| 27 | webrtc::test::RemoveFile(stream.GetFilePath(i)); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 33 | #if defined(WEBRTC_ANDROID) |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 34 | // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
| 35 | #define MAYBE_FileRotatingStreamTest DISABLED_FileRotatingStreamTest |
| 36 | #else |
| 37 | #define MAYBE_FileRotatingStreamTest FileRotatingStreamTest |
| 38 | #endif |
| 39 | |
| 40 | class MAYBE_FileRotatingStreamTest : public ::testing::Test { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 41 | protected: |
| 42 | static const char* kFilePrefix; |
| 43 | static const size_t kMaxFileSize; |
| 44 | |
| 45 | void Init(const std::string& dir_name, |
| 46 | const std::string& file_prefix, |
| 47 | size_t max_file_size, |
| 48 | size_t num_log_files) { |
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 | 6b9dec0 | 2018-11-02 08:50:21 +0100 | [diff] [blame] | 53 | dir_path_.append(webrtc::test::kPathDelimiter); |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 54 | ASSERT_TRUE(webrtc::test::CreateDir(dir_path_)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 55 | stream_.reset(new FileRotatingStream(dir_path_, file_prefix, max_file_size, |
| 56 | num_log_files)); |
| 57 | } |
| 58 | |
| 59 | void TearDown() override { |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 60 | // On windows, open files can't be removed. |
| 61 | stream_->Close(); |
| 62 | CleanupLogDirectory(*stream_); |
| 63 | EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_)); |
| 64 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 65 | stream_.reset(); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // Writes the data to the stream and flushes it. |
| 69 | void WriteAndFlush(const void* data, const size_t data_len) { |
| 70 | EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); |
| 71 | EXPECT_TRUE(stream_->Flush()); |
| 72 | } |
| 73 | |
| 74 | // Checks that the stream reads in the expected contents and then returns an |
| 75 | // end of stream result. |
| 76 | void VerifyStreamRead(const char* expected_contents, |
| 77 | const size_t expected_length, |
| 78 | const std::string& dir_path, |
| 79 | const char* file_prefix) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 80 | std::unique_ptr<FileRotatingStream> stream; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 81 | stream.reset(new FileRotatingStream(dir_path, file_prefix)); |
| 82 | ASSERT_TRUE(stream->Open()); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 83 | size_t read = 0; |
| 84 | size_t stream_size = 0; |
| 85 | EXPECT_TRUE(stream->GetSize(&stream_size)); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 86 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 87 | EXPECT_EQ(SR_SUCCESS, |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 88 | stream->ReadAll(buffer.get(), expected_length, &read, nullptr)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 89 | EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); |
| 90 | EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 91 | EXPECT_EQ(stream_size, read); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void VerifyFileContents(const char* expected_contents, |
| 95 | const size_t expected_length, |
| 96 | const std::string& file_path) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 97 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); |
nisse | 2108326 | 2017-05-31 02:07:21 -0700 | [diff] [blame] | 98 | FileStream stream; |
| 99 | ASSERT_TRUE(stream.Open(file_path, "r", nullptr)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 100 | EXPECT_EQ(rtc::SR_SUCCESS, |
nisse | 2108326 | 2017-05-31 02:07:21 -0700 | [diff] [blame] | 101 | stream.ReadAll(buffer.get(), expected_length, nullptr, nullptr)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 102 | EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); |
| 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) { |
| 161 | EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i))); |
| 162 | } |
| 163 | std::string message("d"); |
| 164 | WriteAndFlush(message.c_str(), message.size()); |
| 165 | for (size_t i = 0; i < arraysize(messages); ++i) { |
| 166 | EXPECT_TRUE(Filesystem::IsFile(stream_->GetFilePath(i))); |
| 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 | |
| 176 | // Tests that writing data greater than the total capacity of the files |
| 177 | // overwrites the files correctly and is read correctly after. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 178 | TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 179 | Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize, |
| 180 | 3); |
| 181 | ASSERT_TRUE(stream_->Open()); |
| 182 | // This should cause overflow across all three files, such that the first file |
| 183 | // we wrote to also gets overwritten. |
| 184 | std::string message("foobarbaz"); |
| 185 | WriteAndFlush(message.c_str(), message.size()); |
| 186 | std::string expected_file_contents("z"); |
| 187 | VerifyFileContents(expected_file_contents.c_str(), |
| 188 | expected_file_contents.size(), stream_->GetFilePath(0)); |
| 189 | std::string expected_stream_contents("arbaz"); |
| 190 | VerifyStreamRead(expected_stream_contents.c_str(), |
| 191 | expected_stream_contents.size(), dir_path_, kFilePrefix); |
| 192 | } |
| 193 | |
| 194 | // Tests that the returned file paths have the right folder and prefix. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 195 | TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 196 | Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20); |
Niels Möller | 394b4eb | 2018-06-01 14:08:25 +0200 | [diff] [blame] | 197 | // dir_path_ includes a trailing delimiter. |
| 198 | const std::string prefix = dir_path_ + kFilePrefix; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 199 | for (auto i = 0; i < 20; ++i) { |
Niels Möller | 394b4eb | 2018-06-01 14:08:25 +0200 | [diff] [blame] | 200 | EXPECT_EQ(0, stream_->GetFilePath(i).compare(0, prefix.size(), prefix)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 204 | #if defined(WEBRTC_ANDROID) |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 205 | // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
| 206 | #define MAYBE_CallSessionFileRotatingStreamTest \ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 207 | DISABLED_CallSessionFileRotatingStreamTest |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 208 | #else |
| 209 | #define MAYBE_CallSessionFileRotatingStreamTest \ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 210 | CallSessionFileRotatingStreamTest |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 211 | #endif |
| 212 | |
| 213 | class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 214 | protected: |
| 215 | void Init(const std::string& dir_name, size_t max_total_log_size) { |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 216 | dir_path_ = webrtc::test::OutputPath(); |
| 217 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 218 | // Append per-test output path in order to run within gtest parallel. |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 219 | dir_path_.append(dir_name); |
Niels Möller | 6b9dec0 | 2018-11-02 08:50:21 +0100 | [diff] [blame] | 220 | dir_path_.append(webrtc::test::kPathDelimiter); |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 221 | ASSERT_TRUE(webrtc::test::CreateDir(dir_path_)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 222 | stream_.reset( |
| 223 | new CallSessionFileRotatingStream(dir_path_, max_total_log_size)); |
| 224 | } |
| 225 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 226 | void TearDown() override { |
nisse | 57efb03 | 2017-05-18 03:55:59 -0700 | [diff] [blame] | 227 | // On windows, open files can't be removed. |
| 228 | stream_->Close(); |
| 229 | CleanupLogDirectory(*stream_); |
| 230 | EXPECT_TRUE(webrtc::test::RemoveDir(dir_path_)); |
| 231 | |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 232 | stream_.reset(); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // Writes the data to the stream and flushes it. |
| 236 | void WriteAndFlush(const void* data, const size_t data_len) { |
| 237 | EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); |
| 238 | EXPECT_TRUE(stream_->Flush()); |
| 239 | } |
| 240 | |
| 241 | // Checks that the stream reads in the expected contents and then returns an |
| 242 | // end of stream result. |
| 243 | void VerifyStreamRead(const char* expected_contents, |
| 244 | const size_t expected_length, |
| 245 | const std::string& dir_path) { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 246 | std::unique_ptr<CallSessionFileRotatingStream> stream( |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 247 | new CallSessionFileRotatingStream(dir_path)); |
| 248 | ASSERT_TRUE(stream->Open()); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 249 | size_t read = 0; |
| 250 | size_t stream_size = 0; |
| 251 | EXPECT_TRUE(stream->GetSize(&stream_size)); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 252 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 253 | EXPECT_EQ(SR_SUCCESS, |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 254 | stream->ReadAll(buffer.get(), expected_length, &read, nullptr)); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 255 | EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); |
| 256 | EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
tkchin | 28bae02 | 2015-07-23 12:27:02 -0700 | [diff] [blame] | 257 | EXPECT_EQ(stream_size, read); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 258 | } |
| 259 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 260 | std::unique_ptr<CallSessionFileRotatingStream> stream_; |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 261 | std::string dir_path_; |
| 262 | }; |
| 263 | |
| 264 | // Tests that writing and reading to a stream with the smallest possible |
| 265 | // capacity works. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 266 | TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 267 | Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4); |
| 268 | |
| 269 | ASSERT_TRUE(stream_->Open()); |
| 270 | std::string message("abcde"); |
| 271 | WriteAndFlush(message.c_str(), message.size()); |
| 272 | std::string expected_contents("abe"); |
| 273 | VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), |
| 274 | dir_path_); |
| 275 | } |
| 276 | |
| 277 | // Tests that writing and reading to a stream with capacity lesser than 4MB |
| 278 | // behaves correctly. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 279 | TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 280 | Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8); |
| 281 | |
| 282 | ASSERT_TRUE(stream_->Open()); |
| 283 | std::string message("123456789"); |
| 284 | WriteAndFlush(message.c_str(), message.size()); |
| 285 | std::string expected_contents("1234789"); |
| 286 | VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), |
| 287 | dir_path_); |
| 288 | } |
| 289 | |
| 290 | // Tests that writing and reading to a stream with capacity greater than 4MB |
| 291 | // behaves correctly. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 292 | TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 293 | Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024); |
| 294 | |
| 295 | ASSERT_TRUE(stream_->Open()); |
| 296 | const size_t buffer_size = 1024 * 1024; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 297 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 298 | for (int i = 0; i < 8; i++) { |
| 299 | memset(buffer.get(), i, buffer_size); |
| 300 | EXPECT_EQ(SR_SUCCESS, |
| 301 | stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 302 | } |
| 303 | |
| 304 | stream_.reset(new CallSessionFileRotatingStream(dir_path_)); |
| 305 | ASSERT_TRUE(stream_->Open()); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 306 | std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 307 | int expected_vals[] = {0, 1, 2, 6, 7}; |
| 308 | for (size_t i = 0; i < arraysize(expected_vals); ++i) { |
| 309 | memset(expected_buffer.get(), expected_vals[i], buffer_size); |
| 310 | EXPECT_EQ(SR_SUCCESS, |
| 311 | stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 312 | EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); |
| 313 | } |
| 314 | EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
| 315 | } |
| 316 | |
| 317 | // Tests that writing and reading to a stream where only the first file is |
| 318 | // written to behaves correctly. |
phoglund | bb73873 | 2016-07-15 03:57:12 -0700 | [diff] [blame] | 319 | TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) { |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 320 | Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf", |
| 321 | 6 * 1024 * 1024); |
| 322 | ASSERT_TRUE(stream_->Open()); |
| 323 | const size_t buffer_size = 1024 * 1024; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 324 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 325 | for (int i = 0; i < 2; i++) { |
| 326 | memset(buffer.get(), i, buffer_size); |
| 327 | EXPECT_EQ(SR_SUCCESS, |
| 328 | stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 329 | } |
| 330 | |
| 331 | stream_.reset(new CallSessionFileRotatingStream(dir_path_)); |
| 332 | ASSERT_TRUE(stream_->Open()); |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 333 | std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); |
tkchin | 9341191 | 2015-07-22 12:12:17 -0700 | [diff] [blame] | 334 | int expected_vals[] = {0, 1}; |
| 335 | for (size_t i = 0; i < arraysize(expected_vals); ++i) { |
| 336 | memset(expected_buffer.get(), expected_vals[i], buffer_size); |
| 337 | EXPECT_EQ(SR_SUCCESS, |
| 338 | stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); |
| 339 | EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); |
| 340 | } |
| 341 | EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
| 342 | } |
| 343 | |
| 344 | } // namespace rtc |