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 |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 void CleanupLogDirectory(const FileRotatingStream& stream) { | 25 void CleanupLogDirectory(const FileRotatingStream& stream) { |
26 for (size_t i = 0; i < stream.GetNumFiles(); ++i) { | 26 for (size_t i = 0; i < stream.GetNumFiles(); ++i) { |
27 // Ignore return value, not all files are expected to exist. | 27 // Ignore return value, not all files are expected to exist. |
28 webrtc::test::RemoveFile(stream.GetFilePath(i)); | 28 webrtc::test::RemoveFile(stream.GetFilePath(i)); |
29 } | 29 } |
30 } | 30 } |
31 | 31 |
| 32 // Read the log files produced by a FileRotatingStream. |
| 33 rtc::Optional<std::string> ReadLogDirectory(const std::string& dir_path, |
| 34 const char* file_prefix) { |
| 35 FileStream stream; |
| 36 |
| 37 std::string file_name(file_prefix); |
| 38 file_name.append("_0"); |
| 39 |
| 40 // Find number of digits. |
| 41 size_t kMaxNumDigits = 5; |
| 42 size_t num_digits; |
| 43 for (num_digits = 1;; num_digits++) { |
| 44 if (num_digits > kMaxNumDigits) { |
| 45 return rtc::Optional<std::string>(); |
| 46 } |
| 47 rtc::Pathname file_path(dir_path, file_name); |
| 48 if (stream.Open(file_path.pathname(), "r", nullptr)) { |
| 49 break; |
| 50 } |
| 51 num_digits++; |
| 52 file_name.push_back('0'); |
| 53 } |
| 54 |
| 55 std::string data; |
| 56 int file_index = 0; |
| 57 |
| 58 while (true) { |
| 59 size_t pos = 0; |
| 60 |
| 61 while (true) { |
| 62 const size_t kBufSize = 8192; |
| 63 char buf[kBufSize]; |
| 64 size_t size_read; |
| 65 switch (stream.Read(buf, kBufSize, &size_read, nullptr)) { |
| 66 case SR_BLOCK: |
| 67 // Wanted to put an RTC_NOTREACHED() here, but then some |
| 68 // builds produce warnigns on missing FALLTHROUGH(). And if |
| 69 // that is added, other builds produce warnings about |
| 70 // FALLTHROUGH() being used in unreachable code. |
| 71 case SR_ERROR: |
| 72 return rtc::Optional<std::string>(); |
| 73 case SR_EOS: |
| 74 goto next_file; |
| 75 case SR_SUCCESS: |
| 76 data.insert(pos, buf, size_read); |
| 77 pos += size_read; |
| 78 } |
| 79 } |
| 80 next_file: // Open next file. |
| 81 std::ostringstream ss; |
| 82 ss << file_prefix << "_"; |
| 83 ss << std::setw(num_digits) << std::setfill('0') << ++file_index; |
| 84 |
| 85 rtc::Pathname file_path(dir_path, ss.str()); |
| 86 stream.Close(); |
| 87 if (!stream.Open(file_path.pathname(), "r", nullptr)) { |
| 88 break; |
| 89 } |
| 90 } |
| 91 return rtc::Optional<std::string>(std::move(data)); |
| 92 } |
| 93 |
32 } // namespace | 94 } // namespace |
33 | 95 |
34 #if defined (WEBRTC_ANDROID) | 96 #if defined (WEBRTC_ANDROID) |
35 // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. | 97 // Fails on Android: https://bugs.chromium.org/p/webrtc/issues/detail?id=4364. |
36 #define MAYBE_FileRotatingStreamTest DISABLED_FileRotatingStreamTest | 98 #define MAYBE_FileRotatingStreamTest DISABLED_FileRotatingStreamTest |
37 #else | 99 #else |
38 #define MAYBE_FileRotatingStreamTest FileRotatingStreamTest | 100 #define MAYBE_FileRotatingStreamTest FileRotatingStreamTest |
39 #endif | 101 #endif |
40 | 102 |
41 class MAYBE_FileRotatingStreamTest : public ::testing::Test { | 103 class MAYBE_FileRotatingStreamTest : public ::testing::Test { |
(...skipping 29 matching lines...) Expand all Loading... |
71 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); | 133 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); |
72 EXPECT_TRUE(stream_->Flush()); | 134 EXPECT_TRUE(stream_->Flush()); |
73 } | 135 } |
74 | 136 |
75 // Checks that the stream reads in the expected contents and then returns an | 137 // Checks that the stream reads in the expected contents and then returns an |
76 // end of stream result. | 138 // end of stream result. |
77 void VerifyStreamRead(const char* expected_contents, | 139 void VerifyStreamRead(const char* expected_contents, |
78 const size_t expected_length, | 140 const size_t expected_length, |
79 const std::string& dir_path, | 141 const std::string& dir_path, |
80 const char* file_prefix) { | 142 const char* file_prefix) { |
81 std::unique_ptr<FileRotatingStream> stream; | 143 rtc::Optional<std::string> contents = |
82 stream.reset(new FileRotatingStream(dir_path, file_prefix)); | 144 ReadLogDirectory(dir_path, file_prefix); |
83 ASSERT_TRUE(stream->Open()); | 145 EXPECT_TRUE(contents); |
84 size_t read = 0; | 146 EXPECT_EQ(contents->size(), expected_length); |
85 size_t stream_size = 0; | 147 EXPECT_EQ(0, memcmp(contents->data(), expected_contents, |
86 EXPECT_TRUE(stream->GetSize(&stream_size)); | 148 std::min(expected_length, contents->size()))); |
87 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); | |
88 EXPECT_EQ(SR_SUCCESS, | |
89 stream->ReadAll(buffer.get(), expected_length, &read, nullptr)); | |
90 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); | |
91 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); | |
92 EXPECT_EQ(stream_size, read); | |
93 } | 149 } |
94 | 150 |
95 void VerifyFileContents(const char* expected_contents, | 151 void VerifyFileContents(const char* expected_contents, |
96 const size_t expected_length, | 152 const size_t expected_length, |
97 const std::string& file_path) { | 153 const std::string& file_path) { |
98 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); | 154 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); |
99 FileStream stream; | 155 FileStream stream; |
100 ASSERT_TRUE(stream.Open(file_path, "r", nullptr)); | 156 ASSERT_TRUE(stream.Open(file_path, "r", nullptr)); |
101 EXPECT_EQ(rtc::SR_SUCCESS, | 157 size_t size_read = 0; |
102 stream.ReadAll(buffer.get(), expected_length, nullptr, nullptr)); | 158 EXPECT_EQ(rtc::SR_SUCCESS, stream.ReadAll(buffer.get(), expected_length, |
103 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); | 159 &size_read, nullptr)); |
| 160 EXPECT_EQ(size_read, expected_length); |
| 161 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), |
| 162 std::min(expected_length, size_read))); |
104 size_t file_size = 0; | 163 size_t file_size = 0; |
105 EXPECT_TRUE(stream.GetSize(&file_size)); | 164 EXPECT_TRUE(stream.GetSize(&file_size)); |
106 EXPECT_EQ(file_size, expected_length); | 165 EXPECT_EQ(file_size, expected_length); |
107 } | 166 } |
108 | 167 |
109 std::unique_ptr<FileRotatingStream> stream_; | 168 std::unique_ptr<FileRotatingStream> stream_; |
110 std::string dir_path_; | 169 std::string dir_path_; |
111 }; | 170 }; |
112 | 171 |
113 const char* MAYBE_FileRotatingStreamTest::kFilePrefix = | 172 const char* MAYBE_FileRotatingStreamTest::kFilePrefix = |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 void WriteAndFlush(const void* data, const size_t data_len) { | 296 void WriteAndFlush(const void* data, const size_t data_len) { |
238 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); | 297 EXPECT_EQ(SR_SUCCESS, stream_->WriteAll(data, data_len, nullptr, nullptr)); |
239 EXPECT_TRUE(stream_->Flush()); | 298 EXPECT_TRUE(stream_->Flush()); |
240 } | 299 } |
241 | 300 |
242 // Checks that the stream reads in the expected contents and then returns an | 301 // Checks that the stream reads in the expected contents and then returns an |
243 // end of stream result. | 302 // end of stream result. |
244 void VerifyStreamRead(const char* expected_contents, | 303 void VerifyStreamRead(const char* expected_contents, |
245 const size_t expected_length, | 304 const size_t expected_length, |
246 const std::string& dir_path) { | 305 const std::string& dir_path) { |
247 std::unique_ptr<CallSessionFileRotatingStream> stream( | 306 rtc::Optional<std::string> contents = |
248 new CallSessionFileRotatingStream(dir_path)); | 307 ReadLogDirectory(dir_path, "webrtc_log"); |
249 ASSERT_TRUE(stream->Open()); | 308 EXPECT_TRUE(contents); |
250 size_t read = 0; | 309 EXPECT_EQ(contents->size(), expected_length); |
251 size_t stream_size = 0; | 310 EXPECT_EQ(0, memcmp(contents->data(), expected_contents, |
252 EXPECT_TRUE(stream->GetSize(&stream_size)); | 311 std::min(expected_length, contents->size()))); |
253 std::unique_ptr<uint8_t[]> buffer(new uint8_t[expected_length]); | |
254 EXPECT_EQ(SR_SUCCESS, | |
255 stream->ReadAll(buffer.get(), expected_length, &read, nullptr)); | |
256 EXPECT_EQ(0, memcmp(expected_contents, buffer.get(), expected_length)); | |
257 EXPECT_EQ(SR_EOS, stream->ReadAll(buffer.get(), 1, nullptr, nullptr)); | |
258 EXPECT_EQ(stream_size, read); | |
259 } | 312 } |
260 | 313 |
261 std::unique_ptr<CallSessionFileRotatingStream> stream_; | 314 std::unique_ptr<CallSessionFileRotatingStream> stream_; |
262 std::string dir_path_; | 315 std::string dir_path_; |
263 }; | 316 }; |
264 | 317 |
265 // Tests that writing and reading to a stream with the smallest possible | 318 // Tests that writing and reading to a stream with the smallest possible |
266 // capacity works. | 319 // capacity works. |
267 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) { | 320 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadSmallest) { |
268 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4); | 321 Init("CallSessionFileRotatingStreamTestWriteAndReadSmallest", 4); |
(...skipping 26 matching lines...) Expand all Loading... |
295 | 348 |
296 ASSERT_TRUE(stream_->Open()); | 349 ASSERT_TRUE(stream_->Open()); |
297 const size_t buffer_size = 1024 * 1024; | 350 const size_t buffer_size = 1024 * 1024; |
298 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 351 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
299 for (int i = 0; i < 8; i++) { | 352 for (int i = 0; i < 8; i++) { |
300 memset(buffer.get(), i, buffer_size); | 353 memset(buffer.get(), i, buffer_size); |
301 EXPECT_EQ(SR_SUCCESS, | 354 EXPECT_EQ(SR_SUCCESS, |
302 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); | 355 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); |
303 } | 356 } |
304 | 357 |
305 stream_.reset(new CallSessionFileRotatingStream(dir_path_)); | 358 rtc::Optional<std::string> contents = |
306 ASSERT_TRUE(stream_->Open()); | 359 ReadLogDirectory(dir_path_, "webrtc_log"); |
307 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); | |
308 int expected_vals[] = {0, 1, 2, 6, 7}; | 360 int expected_vals[] = {0, 1, 2, 6, 7}; |
| 361 ASSERT_EQ(contents->size(), buffer_size * arraysize(expected_vals)); |
| 362 |
309 for (size_t i = 0; i < arraysize(expected_vals); ++i) { | 363 for (size_t i = 0; i < arraysize(expected_vals); ++i) { |
310 memset(expected_buffer.get(), expected_vals[i], buffer_size); | 364 const char* block = contents->data() + i * buffer_size; |
311 EXPECT_EQ(SR_SUCCESS, | 365 bool match = true; |
312 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); | 366 for (size_t j = 0; j < buffer_size; j++) { |
313 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); | 367 if (block[j] != expected_vals[i]) { |
| 368 match = false; |
| 369 break; |
| 370 } |
| 371 } |
| 372 // EXPECT call at end of loop, to limit the number of messages on failure. |
| 373 EXPECT_TRUE(match); |
314 } | 374 } |
315 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); | |
316 } | 375 } |
317 | 376 |
318 // Tests that writing and reading to a stream where only the first file is | 377 // Tests that writing and reading to a stream where only the first file is |
319 // written to behaves correctly. | 378 // written to behaves correctly. |
320 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) { | 379 TEST_F(MAYBE_CallSessionFileRotatingStreamTest, WriteAndReadFirstHalf) { |
321 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf", | 380 Init("CallSessionFileRotatingStreamTestWriteAndReadFirstHalf", |
322 6 * 1024 * 1024); | 381 6 * 1024 * 1024); |
323 ASSERT_TRUE(stream_->Open()); | 382 ASSERT_TRUE(stream_->Open()); |
324 const size_t buffer_size = 1024 * 1024; | 383 const size_t buffer_size = 1024 * 1024; |
325 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); | 384 std::unique_ptr<uint8_t[]> buffer(new uint8_t[buffer_size]); |
326 for (int i = 0; i < 2; i++) { | 385 for (int i = 0; i < 2; i++) { |
327 memset(buffer.get(), i, buffer_size); | 386 memset(buffer.get(), i, buffer_size); |
328 EXPECT_EQ(SR_SUCCESS, | 387 EXPECT_EQ(SR_SUCCESS, |
329 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); | 388 stream_->WriteAll(buffer.get(), buffer_size, nullptr, nullptr)); |
330 } | 389 } |
331 | 390 |
332 stream_.reset(new CallSessionFileRotatingStream(dir_path_)); | 391 rtc::Optional<std::string> contents = |
333 ASSERT_TRUE(stream_->Open()); | 392 ReadLogDirectory(dir_path_, "webrtc_log"); |
334 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); | 393 std::unique_ptr<uint8_t[]> expected_buffer(new uint8_t[buffer_size]); |
335 int expected_vals[] = {0, 1}; | 394 int expected_vals[] = {0, 1}; |
| 395 ASSERT_EQ(contents->size(), buffer_size * arraysize(expected_vals)); |
| 396 |
336 for (size_t i = 0; i < arraysize(expected_vals); ++i) { | 397 for (size_t i = 0; i < arraysize(expected_vals); ++i) { |
337 memset(expected_buffer.get(), expected_vals[i], buffer_size); | 398 const char* block = contents->data() + i * buffer_size; |
338 EXPECT_EQ(SR_SUCCESS, | 399 bool match = true; |
339 stream_->ReadAll(buffer.get(), buffer_size, nullptr, nullptr)); | 400 for (size_t j = 0; j < buffer_size; j++) { |
340 EXPECT_EQ(0, memcmp(buffer.get(), expected_buffer.get(), buffer_size)); | 401 if (block[j] != expected_vals[i]) { |
| 402 match = false; |
| 403 break; |
| 404 } |
| 405 } |
| 406 // EXPECT call at end of loop, to limit the number of messages on failure. |
| 407 EXPECT_TRUE(match); |
341 } | 408 } |
342 EXPECT_EQ(SR_EOS, stream_->ReadAll(buffer.get(), 1, nullptr, nullptr)); | |
343 } | 409 } |
344 | 410 |
345 } // namespace rtc | 411 } // namespace rtc |
OLD | NEW |