Chromium Code Reviews| Index: extensions/browser/api/cast_channel/logger_unittest.cc |
| diff --git a/extensions/browser/api/cast_channel/logger_unittest.cc b/extensions/browser/api/cast_channel/logger_unittest.cc |
| index d1ec5fcdbbf2d676c92814e2eb21fdd5e3a7b7bc..6ce8801218d039e732c6af16d42e2dd5eabc9438 100644 |
| --- a/extensions/browser/api/cast_channel/logger_unittest.cc |
| +++ b/extensions/browser/api/cast_channel/logger_unittest.cc |
| @@ -6,6 +6,7 @@ |
| #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
| #include "extensions/browser/api/cast_channel/logger.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/zlib/zlib.h" |
| namespace extensions { |
| namespace core_api { |
| @@ -25,6 +26,52 @@ class CastChannelLoggerTest : public testing::Test { |
| base::TimeTicks())) {} |
| virtual ~CastChannelLoggerTest() {} |
| + bool Uncompress(const char* input, int length, std::string* output) { |
| + z_stream stream = {0}; |
| + |
| + stream.next_in = reinterpret_cast<uint8*>(const_cast<char*>(input)); |
| + stream.avail_in = length; |
| + stream.next_out = reinterpret_cast<uint8*>(&(*output)[0]); |
| + stream.avail_out = output->size(); |
| + |
| + bool success = false; |
| + while (stream.avail_in > 0 && stream.avail_out > 0) { |
| + // 16 is added to read in gzip format. |
| + int result = inflateInit2(&stream, MAX_WBITS + 16); |
| + DCHECK_EQ(Z_OK, result); |
| + |
| + result = inflate(&stream, Z_FINISH); |
| + success = (result == Z_STREAM_END); |
| + if (!success) { |
| + DVLOG(2) << "inflate() failed. Result: " << result; |
| + break; |
| + } |
| + |
| + result = inflateEnd(&stream); |
| + DCHECK(result == Z_OK); |
| + } |
| + |
| + if (stream.avail_in == 0) { |
| + success = true; |
| + output->resize(output->size() - stream.avail_out); |
| + } |
| + return success; |
| + } |
| + |
| + bool GetLog(Log* log) { |
| + int length = 0; |
| + scoped_ptr<char[]> output = logger_->GetLogs(&length); |
| + if (!output.get()) |
| + return false; |
| + |
| + // 100kb should be enough for test purposes. |
| + std::string uncompressed(100000, 0); |
| + if (!Uncompress(output.get(), length, &uncompressed)) |
| + return false; |
| + |
| + return log->ParseFromString(uncompressed); |
| + } |
| + |
| protected: |
| base::SimpleTestTickClock* clock_; |
| scoped_refptr<Logger> logger_; |
| @@ -54,13 +101,8 @@ TEST_F(CastChannelLoggerTest, BasicLogging) { |
| "Parsing failed", AuthResult::ERROR_NSS_CERT_PARSING_FAILED, 4); |
| logger_->LogSocketChallengeReplyEvent(2, auth_result); |
| - std::string output; |
| - bool success = logger_->LogToString(&output); |
| - ASSERT_TRUE(success); |
| - |
| Log log; |
| - success = log.ParseFromString(output); |
| - ASSERT_TRUE(success); |
| + ASSERT_TRUE(GetLog(&log)); |
|
mark a. foltz
2014/08/13 22:00:24
I might write this as
scoped_ptr<Log> log(GetLog(
imcheng
2014/08/13 23:58:14
Done.
|
| ASSERT_EQ(2, log.aggregated_socket_event_size()); |
| { |
| @@ -127,13 +169,8 @@ TEST_F(CastChannelLoggerTest, TooManySockets) { |
| logger_->LogSocketEvent(i, EventType::CAST_SOCKET_CREATED); |
| } |
| - std::string output; |
| - bool success = logger_->LogToString(&output); |
| - ASSERT_TRUE(success); |
| - |
| Log log; |
| - success = log.ParseFromString(output); |
| - ASSERT_TRUE(success); |
| + ASSERT_TRUE(GetLog(&log)); |
| ASSERT_EQ(kMaxSocketsToLog, log.aggregated_socket_event_size()); |
| EXPECT_EQ(5, log.num_evicted_aggregated_socket_events()); |
| @@ -150,13 +187,8 @@ TEST_F(CastChannelLoggerTest, TooManyEvents) { |
| clock_->Advance(base::TimeDelta::FromMicroseconds(1)); |
| } |
| - std::string output; |
| - bool success = logger_->LogToString(&output); |
| - ASSERT_TRUE(success); |
| - |
| Log log; |
| - success = log.ParseFromString(output); |
| - ASSERT_TRUE(success); |
| + ASSERT_TRUE(GetLog(&log)); |
| ASSERT_EQ(1, log.aggregated_socket_event_size()); |
| EXPECT_EQ(0, log.num_evicted_aggregated_socket_events()); |
| @@ -171,22 +203,14 @@ TEST_F(CastChannelLoggerTest, TooManyEvents) { |
| TEST_F(CastChannelLoggerTest, Reset) { |
| logger_->LogSocketEvent(1, EventType::CAST_SOCKET_CREATED); |
| - std::string output; |
| - bool success = logger_->LogToString(&output); |
| - ASSERT_TRUE(success); |
| - |
| Log log; |
| - success = log.ParseFromString(output); |
| - ASSERT_TRUE(success); |
| + ASSERT_TRUE(GetLog(&log)); |
| EXPECT_EQ(1, log.aggregated_socket_event_size()); |
| logger_->Reset(); |
| - success = logger_->LogToString(&output); |
| - ASSERT_TRUE(success); |
| - success = log.ParseFromString(output); |
| - ASSERT_TRUE(success); |
| + ASSERT_TRUE(GetLog(&log)); |
| EXPECT_EQ(0, log.aggregated_socket_event_size()); |
| } |