OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 | 12 |
13 #include "webrtc/base/arraysize.h" | 13 #include "webrtc/base/arraysize.h" |
14 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
15 #include "webrtc/base/filerotatingstream.h" | 15 #include "webrtc/base/filerotatingstream.h" |
16 #include "webrtc/base/fileutils.h" | 16 #include "webrtc/base/fileutils.h" |
17 #include "webrtc/base/gunit.h" | 17 #include "webrtc/base/gunit.h" |
18 #include "webrtc/base/pathutils.h" | 18 #include "webrtc/base/pathutils.h" |
19 | 19 |
20 namespace rtc { | 20 namespace rtc { |
21 | 21 |
22 class FileRotatingStreamTest : public ::testing::Test { | 22 #if defined (WEBRTC_ANDROID) |
| 23 // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
| 24 #define MAYBE_FileRotatingStreamTest DISABLED_FileRotatingStreamTest |
| 25 #else |
| 26 #define MAYBE_FileRotatingStreamTest FileRotatingStreamTest |
| 27 #endif |
| 28 |
| 29 class MAYBE_FileRotatingStreamTest : public ::testing::Test { |
23 protected: | 30 protected: |
24 static const char* kFilePrefix; | 31 static const char* kFilePrefix; |
25 static const size_t kMaxFileSize; | 32 static const size_t kMaxFileSize; |
26 | 33 |
27 void Init(const std::string& dir_name, | 34 void Init(const std::string& dir_name, |
28 const std::string& file_prefix, | 35 const std::string& file_prefix, |
29 size_t max_file_size, | 36 size_t max_file_size, |
30 size_t num_log_files) { | 37 size_t num_log_files) { |
31 Pathname test_path; | 38 Pathname test_path; |
32 ASSERT_TRUE(Filesystem::GetAppTempFolder(&test_path)); | 39 ASSERT_TRUE(Filesystem::GetAppTempFolder(&test_path)); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); | 94 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); |
88 size_t file_size = 0; | 95 size_t file_size = 0; |
89 EXPECT_TRUE(stream->GetSize(&file_size)); | 96 EXPECT_TRUE(stream->GetSize(&file_size)); |
90 EXPECT_EQ(file_size, expected_length); | 97 EXPECT_EQ(file_size, expected_length); |
91 } | 98 } |
92 | 99 |
93 std::unique_ptr<FileRotatingStream> stream_; | 100 std::unique_ptr<FileRotatingStream> stream_; |
94 std::string dir_path_; | 101 std::string dir_path_; |
95 }; | 102 }; |
96 | 103 |
97 const char* FileRotatingStreamTest::kFilePrefix = "FileRotatingStreamTest"; | 104 const char* MAYBE_FileRotatingStreamTest::kFilePrefix = |
98 const size_t FileRotatingStreamTest::kMaxFileSize = 2; | 105 "FileRotatingStreamTest"; |
| 106 const size_t MAYBE_FileRotatingStreamTest::kMaxFileSize = 2; |
99 | 107 |
100 // Tests that stream state is correct before and after Open / Close. | 108 // Tests that stream state is correct before and after Open / Close. |
101 TEST_F(FileRotatingStreamTest, State) { | 109 TEST_F(MAYBE_FileRotatingStreamTest, State) { |
102 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3); | 110 Init("FileRotatingStreamTestState", kFilePrefix, kMaxFileSize, 3); |
103 | 111 |
104 EXPECT_EQ(SS_CLOSED, stream_->GetState()); | 112 EXPECT_EQ(SS_CLOSED, stream_->GetState()); |
105 ASSERT_TRUE(stream_->Open()); | 113 ASSERT_TRUE(stream_->Open()); |
106 EXPECT_EQ(SS_OPEN, stream_->GetState()); | 114 EXPECT_EQ(SS_OPEN, stream_->GetState()); |
107 stream_->Close(); | 115 stream_->Close(); |
108 EXPECT_EQ(SS_CLOSED, stream_->GetState()); | 116 EXPECT_EQ(SS_CLOSED, stream_->GetState()); |
109 } | 117 } |
110 | 118 |
111 // Tests that nothing is written to file when data of length zero is written. | 119 // Tests that nothing is written to file when data of length zero is written. |
112 TEST_F(FileRotatingStreamTest, EmptyWrite) { | 120 TEST_F(MAYBE_FileRotatingStreamTest, EmptyWrite) { |
113 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3); | 121 Init("FileRotatingStreamTestEmptyWrite", kFilePrefix, kMaxFileSize, 3); |
114 | 122 |
115 ASSERT_TRUE(stream_->Open()); | 123 ASSERT_TRUE(stream_->Open()); |
116 WriteAndFlush("a", 0); | 124 WriteAndFlush("a", 0); |
117 | 125 |
118 std::string logfile_path = stream_->GetFilePath(0); | 126 std::string logfile_path = stream_->GetFilePath(0); |
119 std::unique_ptr<FileStream> stream(Filesystem::OpenFile(logfile_path, "r")); | 127 std::unique_ptr<FileStream> stream(Filesystem::OpenFile(logfile_path, "r")); |
120 size_t file_size = 0; | 128 size_t file_size = 0; |
121 EXPECT_TRUE(stream->GetSize(&file_size)); | 129 EXPECT_TRUE(stream->GetSize(&file_size)); |
122 EXPECT_EQ(0u, file_size); | 130 EXPECT_EQ(0u, file_size); |
123 } | 131 } |
124 | 132 |
125 // Tests that a write operation followed by a read returns the expected data | 133 // Tests that a write operation followed by a read returns the expected data |
126 // and writes to the expected files. | 134 // and writes to the expected files. |
127 TEST_F(FileRotatingStreamTest, WriteAndRead) { | 135 TEST_F(MAYBE_FileRotatingStreamTest, WriteAndRead) { |
128 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3); | 136 Init("FileRotatingStreamTestWriteAndRead", kFilePrefix, kMaxFileSize, 3); |
129 | 137 |
130 ASSERT_TRUE(stream_->Open()); | 138 ASSERT_TRUE(stream_->Open()); |
131 // The test is set up to create three log files of length 2. Write and check | 139 // The test is set up to create three log files of length 2. Write and check |
132 // contents. | 140 // contents. |
133 std::string messages[3] = {"aa", "bb", "cc"}; | 141 std::string messages[3] = {"aa", "bb", "cc"}; |
134 for (size_t i = 0; i < arraysize(messages); ++i) { | 142 for (size_t i = 0; i < arraysize(messages); ++i) { |
135 const std::string& message = messages[i]; | 143 const std::string& message = messages[i]; |
136 WriteAndFlush(message.c_str(), message.size()); | 144 WriteAndFlush(message.c_str(), message.size()); |
137 // Since the max log size is 2, we will be causing rotation. Read from the | 145 // Since the max log size is 2, we will be causing rotation. Read from the |
(...skipping 13 matching lines...) Expand all Loading... |
151 // TODO(tkchin): Maybe check all the files in the dir. | 159 // TODO(tkchin): Maybe check all the files in the dir. |
152 | 160 |
153 // Reopen for read. | 161 // Reopen for read. |
154 std::string expected_contents("bbccd"); | 162 std::string expected_contents("bbccd"); |
155 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), | 163 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), |
156 dir_path_, kFilePrefix); | 164 dir_path_, kFilePrefix); |
157 } | 165 } |
158 | 166 |
159 // Tests that writing data greater than the total capacity of the files | 167 // Tests that writing data greater than the total capacity of the files |
160 // overwrites the files correctly and is read correctly after. | 168 // overwrites the files correctly and is read correctly after. |
161 TEST_F(FileRotatingStreamTest, WriteOverflowAndRead) { | 169 TEST_F(MAYBE_FileRotatingStreamTest, WriteOverflowAndRead) { |
162 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize, | 170 Init("FileRotatingStreamTestWriteOverflowAndRead", kFilePrefix, kMaxFileSize, |
163 3); | 171 3); |
164 ASSERT_TRUE(stream_->Open()); | 172 ASSERT_TRUE(stream_->Open()); |
165 // This should cause overflow across all three files, such that the first file | 173 // This should cause overflow across all three files, such that the first file |
166 // we wrote to also gets overwritten. | 174 // we wrote to also gets overwritten. |
167 std::string message("foobarbaz"); | 175 std::string message("foobarbaz"); |
168 WriteAndFlush(message.c_str(), message.size()); | 176 WriteAndFlush(message.c_str(), message.size()); |
169 std::string expected_file_contents("z"); | 177 std::string expected_file_contents("z"); |
170 VerifyFileContents(expected_file_contents.c_str(), | 178 VerifyFileContents(expected_file_contents.c_str(), |
171 expected_file_contents.size(), stream_->GetFilePath(0)); | 179 expected_file_contents.size(), stream_->GetFilePath(0)); |
172 std::string expected_stream_contents("arbaz"); | 180 std::string expected_stream_contents("arbaz"); |
173 VerifyStreamRead(expected_stream_contents.c_str(), | 181 VerifyStreamRead(expected_stream_contents.c_str(), |
174 expected_stream_contents.size(), dir_path_, kFilePrefix); | 182 expected_stream_contents.size(), dir_path_, kFilePrefix); |
175 } | 183 } |
176 | 184 |
177 // Tests that the returned file paths have the right folder and prefix. | 185 // Tests that the returned file paths have the right folder and prefix. |
178 TEST_F(FileRotatingStreamTest, GetFilePath) { | 186 TEST_F(MAYBE_FileRotatingStreamTest, GetFilePath) { |
179 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20); | 187 Init("FileRotatingStreamTestGetFilePath", kFilePrefix, kMaxFileSize, 20); |
180 for (auto i = 0; i < 20; ++i) { | 188 for (auto i = 0; i < 20; ++i) { |
181 Pathname path(stream_->GetFilePath(i)); | 189 Pathname path(stream_->GetFilePath(i)); |
182 EXPECT_EQ(0, path.folder().compare(dir_path_)); | 190 EXPECT_EQ(0, path.folder().compare(dir_path_)); |
183 EXPECT_EQ(0, path.filename().compare(0, strlen(kFilePrefix), kFilePrefix)); | 191 EXPECT_EQ(0, path.filename().compare(0, strlen(kFilePrefix), kFilePrefix)); |
184 } | 192 } |
185 } | 193 } |
186 | 194 |
187 class CallSessionFileRotatingStreamTest : public ::testing::Test { | 195 #if defined (WEBRTC_ANDROID) |
| 196 // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
| 197 #define MAYBE_CallSessionFileRotatingStreamTest \ |
| 198 DISABLED_CallSessionFileRotatingStreamTest |
| 199 #else |
| 200 #define MAYBE_CallSessionFileRotatingStreamTest \ |
| 201 CallSessionFileRotatingStreamTest |
| 202 #endif |
| 203 |
| 204 class MAYBE_CallSessionFileRotatingStreamTest : public ::testing::Test { |
188 protected: | 205 protected: |
189 void Init(const std::string& dir_name, size_t max_total_log_size) { | 206 void Init(const std::string& dir_name, size_t max_total_log_size) { |
190 Pathname test_path; | 207 Pathname test_path; |
191 ASSERT_TRUE(Filesystem::GetAppTempFolder(&test_path)); | 208 ASSERT_TRUE(Filesystem::GetAppTempFolder(&test_path)); |
192 // Append per-test output path in order to run within gtest parallel. | 209 // Append per-test output path in order to run within gtest parallel. |
193 test_path.AppendFolder(dir_name); | 210 test_path.AppendFolder(dir_name); |
194 ASSERT_TRUE(Filesystem::CreateFolder(test_path)); | 211 ASSERT_TRUE(Filesystem::CreateFolder(test_path)); |
195 dir_path_ = test_path.pathname(); | 212 dir_path_ = test_path.pathname(); |
196 ASSERT_TRUE(dir_path_.size()); | 213 ASSERT_TRUE(dir_path_.size()); |
197 stream_.reset( | 214 stream_.reset( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); | 247 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
231 EXPECT_EQ(stream_size, read); | 248 EXPECT_EQ(stream_size, read); |
232 } | 249 } |
233 | 250 |
234 std::unique_ptr<CallSessionFileRotatingStream> stream_; | 251 std::unique_ptr<CallSessionFileRotatingStream> stream_; |
235 std::string dir_path_; | 252 std::string dir_path_; |
236 }; | 253 }; |
237 | 254 |
238 // Tests that writing and reading to a stream with the smallest possible | 255 // Tests that writing and reading to a stream with the smallest possible |
239 // capacity works. | 256 // capacity works. |
240 TEST_F(CallSessionFileRotatingStreamTest, WriteAndReadSmallest) { | 257 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) { |
241 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4); | 258 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4); |
242 | 259 |
243 ASSERT_TRUE(stream_->Open()); | 260 ASSERT_TRUE(stream_->Open()); |
244 std::string message("abcde"); | 261 std::string message("abcde"); |
245 WriteAndFlush(message.c_str(), message.size()); | 262 WriteAndFlush(message.c_str(), message.size()); |
246 std::string expected_contents("abe"); | 263 std::string expected_contents("abe"); |
247 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), | 264 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), |
248 dir_path_); | 265 dir_path_); |
249 } | 266 } |
250 | 267 |
251 // Tests that writing and reading to a stream with capacity lesser than 4MB | 268 // Tests that writing and reading to a stream with capacity lesser than 4MB |
252 // behaves correctly. | 269 // behaves correctly. |
253 TEST_F(CallSessionFileRotatingStreamTest, WriteAndReadSmall) { | 270 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmall) { |
254 Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8); | 271 Init("CallSessionFileRotatingStreamTestWriteAndReadSmall", 8); |
255 | 272 |
256 ASSERT_TRUE(stream_->Open()); | 273 ASSERT_TRUE(stream_->Open()); |
257 std::string message("123456789"); | 274 std::string message("123456789"); |
258 WriteAndFlush(message.c_str(), message.size()); | 275 WriteAndFlush(message.c_str(), message.size()); |
259 std::string expected_contents("1234789"); | 276 std::string expected_contents("1234789"); |
260 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), | 277 VerifyStreamRead(expected_contents.c_str(), expected_contents.size(), |
261 dir_path_); | 278 dir_path_); |
262 } | 279 } |
263 | 280 |
264 // Tests that writing and reading to a stream with capacity greater than 4MB | 281 // Tests that writing and reading to a stream with capacity greater than 4MB |
265 // behaves correctly. | 282 // behaves correctly. |
266 TEST_F(CallSessionFileRotatingStreamTest, WriteAndReadLarge) { | 283 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadLarge) { |
267 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024); | 284 Init("CallSessionFileRotatingStreamTestWriteAndReadLarge", 6 * 1024 * 1024); |
268 | 285 |
269 ASSERT_TRUE(stream_->Open()); | 286 ASSERT_TRUE(stream_->Open()); |
270 const size_t buffer_size = 1024 * 1024; | 287 const size_t buffer_size = 1024 * 1024; |
271 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 288 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
272 for (int i = 0; i < 8; i++) { | 289 for (int i = 0; i < 8; i++) { |
273 memset(buffer.get(), i, buffer_size); | 290 memset(buffer.get(), i, buffer_size); |
274 EXPECT_EQ(SR_SUCCESS, | 291 EXPECT_EQ(SR_SUCCESS, |
275 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); | 292 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); |
276 } | 293 } |
277 | 294 |
278 stream_.reset(new CallSessionFileRotatingStream(dir_path_)); | 295 stream_.reset(new CallSessionFileRotatingStream(dir_path_)); |
279 ASSERT_TRUE(stream_->Open()); | 296 ASSERT_TRUE(stream_->Open()); |
280 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); | 297 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); |
281 int expected_vals[] = {0, 1, 2, 6, 7}; | 298 int expected_vals[] = {0, 1, 2, 6, 7}; |
282 for (size_t i = 0; i < arraysize(expected_vals); ++i) { | 299 for (size_t i = 0; i < arraysize(expected_vals); ++i) { |
283 memset(expected_buffer.get(), expected_vals[i], buffer_size); | 300 memset(expected_buffer.get(), expected_vals[i], buffer_size); |
284 EXPECT_EQ(SR_SUCCESS, | 301 EXPECT_EQ(SR_SUCCESS, |
285 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); | 302 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); |
286 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); | 303 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); |
287 } | 304 } |
288 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); | 305 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
289 } | 306 } |
290 | 307 |
291 // Tests that writing and reading to a stream where only the first file is | 308 // Tests that writing and reading to a stream where only the first file is |
292 // written to behaves correctly. | 309 // written to behaves correctly. |
293 TEST_F(CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) { | 310 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) { |
294 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf", | 311 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf", |
295 6 * 1024 * 1024); | 312 6 * 1024 * 1024); |
296 ASSERT_TRUE(stream_->Open()); | 313 ASSERT_TRUE(stream_->Open()); |
297 const size_t buffer_size = 1024 * 1024; | 314 const size_t buffer_size = 1024 * 1024; |
298 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 315 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
299 for (int i = 0; i < 2; i++) { | 316 for (int i = 0; i < 2; i++) { |
300 memset(buffer.get(), i, buffer_size); | 317 memset(buffer.get(), i, buffer_size); |
301 EXPECT_EQ(SR_SUCCESS, | 318 EXPECT_EQ(SR_SUCCESS, |
302 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); | 319 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); |
303 } | 320 } |
304 | 321 |
305 stream_.reset(new CallSessionFileRotatingStream(dir_path_)); | 322 stream_.reset(new CallSessionFileRotatingStream(dir_path_)); |
306 ASSERT_TRUE(stream_->Open()); | 323 ASSERT_TRUE(stream_->Open()); |
307 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); | 324 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); |
308 int expected_vals[] = {0, 1}; | 325 int expected_vals[] = {0, 1}; |
309 for (size_t i = 0; i < arraysize(expected_vals); ++i) { | 326 for (size_t i = 0; i < arraysize(expected_vals); ++i) { |
310 memset(expected_buffer.get(), expected_vals[i], buffer_size); | 327 memset(expected_buffer.get(), expected_vals[i], buffer_size); |
311 EXPECT_EQ(SR_SUCCESS, | 328 EXPECT_EQ(SR_SUCCESS, |
312 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); | 329 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); |
313 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); | 330 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); |
314 } | 331 } |
315 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); | 332 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); |
316 } | 333 } |
317 | 334 |
318 } // namespace rtc | 335 } // namespace rtc |
OLD | NEW |