| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/metrics/persisted_logs.h" | 5 #include "components/metrics/persisted_logs.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
| 12 #include "base/sha1.h" | 12 #include "base/sha1.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "components/prefs/pref_registry_simple.h" | 14 #include "components/prefs/pref_registry_simple.h" |
| 15 #include "components/prefs/scoped_user_pref_update.h" | 15 #include "components/prefs/scoped_user_pref_update.h" |
| 16 #include "components/prefs/testing_pref_service.h" | 16 #include "components/prefs/testing_pref_service.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "third_party/zlib/google/compression_utils.h" | 18 #include "third_party/zlib/google/compression_utils.h" |
| 19 | 19 |
| 20 namespace metrics { | 20 namespace metrics { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const char kTestPrefName[] = "TestPref"; | 24 const char kTestPrefName[] = "TestPref"; |
| 25 const char kTestOutdatedPrefName[] = "OutdatedTestPref"; |
| 25 const size_t kLogCountLimit = 3; | 26 const size_t kLogCountLimit = 3; |
| 26 const size_t kLogByteLimit = 1000; | 27 const size_t kLogByteLimit = 1000; |
| 27 | 28 |
| 28 // Compresses |log_data| and returns the result. | 29 // Compresses |log_data| and returns the result. |
| 29 std::string Compress(const std::string& log_data) { | 30 std::string Compress(const std::string& log_data) { |
| 30 std::string compressed_log_data; | 31 std::string compressed_log_data; |
| 31 EXPECT_TRUE(compression::GzipCompress(log_data, &compressed_log_data)); | 32 EXPECT_TRUE(compression::GzipCompress(log_data, &compressed_log_data)); |
| 32 return compressed_log_data; | 33 return compressed_log_data; |
| 33 } | 34 } |
| 34 | 35 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 56 protected: | 57 protected: |
| 57 TestingPrefServiceSimple prefs_; | 58 TestingPrefServiceSimple prefs_; |
| 58 | 59 |
| 59 private: | 60 private: |
| 60 DISALLOW_COPY_AND_ASSIGN(PersistedLogsTest); | 61 DISALLOW_COPY_AND_ASSIGN(PersistedLogsTest); |
| 61 }; | 62 }; |
| 62 | 63 |
| 63 class TestPersistedLogs : public PersistedLogs { | 64 class TestPersistedLogs : public PersistedLogs { |
| 64 public: | 65 public: |
| 65 TestPersistedLogs(PrefService* service, size_t min_log_bytes) | 66 TestPersistedLogs(PrefService* service, size_t min_log_bytes) |
| 66 : PersistedLogs(service, kTestPrefName, kLogCountLimit, min_log_bytes, | 67 : PersistedLogs(service, |
| 67 0) { | 68 kTestPrefName, |
| 68 } | 69 kTestOutdatedPrefName, |
| 70 kLogCountLimit, |
| 71 min_log_bytes, |
| 72 0) {} |
| 69 | 73 |
| 70 // Stages and removes the next log, while testing it's value. | 74 // Stages and removes the next log, while testing it's value. |
| 71 void ExpectNextLog(const std::string& expected_log) { | 75 void ExpectNextLog(const std::string& expected_log) { |
| 72 StageLog(); | 76 StageLog(); |
| 73 EXPECT_EQ(staged_log(), Compress(expected_log)); | 77 EXPECT_EQ(staged_log(), Compress(expected_log)); |
| 74 DiscardStagedLog(); | 78 DiscardStagedLog(); |
| 75 } | 79 } |
| 76 | 80 |
| 77 private: | 81 private: |
| 78 DISALLOW_COPY_AND_ASSIGN(TestPersistedLogs); | 82 DISALLOW_COPY_AND_ASSIGN(TestPersistedLogs); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 104 EXPECT_EQ(PersistedLogs::RECALL_SUCCESS, | 108 EXPECT_EQ(PersistedLogs::RECALL_SUCCESS, |
| 105 result_persisted_logs.DeserializeLogs()); | 109 result_persisted_logs.DeserializeLogs()); |
| 106 EXPECT_EQ(1U, result_persisted_logs.size()); | 110 EXPECT_EQ(1U, result_persisted_logs.size()); |
| 107 | 111 |
| 108 // Verify that the result log matches the initial log. | 112 // Verify that the result log matches the initial log. |
| 109 persisted_logs.StageLog(); | 113 persisted_logs.StageLog(); |
| 110 result_persisted_logs.StageLog(); | 114 result_persisted_logs.StageLog(); |
| 111 EXPECT_EQ(persisted_logs.staged_log(), result_persisted_logs.staged_log()); | 115 EXPECT_EQ(persisted_logs.staged_log(), result_persisted_logs.staged_log()); |
| 112 EXPECT_EQ(persisted_logs.staged_log_hash(), | 116 EXPECT_EQ(persisted_logs.staged_log_hash(), |
| 113 result_persisted_logs.staged_log_hash()); | 117 result_persisted_logs.staged_log_hash()); |
| 118 EXPECT_EQ(persisted_logs.staged_log_timestamp(), |
| 119 result_persisted_logs.staged_log_timestamp()); |
| 114 } | 120 } |
| 115 | 121 |
| 116 // Store a set of logs over the length limit, but smaller than the min number of | 122 // Store a set of logs over the length limit, but smaller than the min number of |
| 117 // bytes. | 123 // bytes. |
| 118 TEST_F(PersistedLogsTest, LongButTinyLogList) { | 124 TEST_F(PersistedLogsTest, LongButTinyLogList) { |
| 119 TestPersistedLogs persisted_logs(&prefs_, kLogByteLimit); | 125 TestPersistedLogs persisted_logs(&prefs_, kLogByteLimit); |
| 120 | 126 |
| 121 size_t log_count = kLogCountLimit * 5; | 127 size_t log_count = kLogCountLimit * 5; |
| 122 for (size_t i = 0; i < log_count; ++i) | 128 for (size_t i = 0; i < log_count; ++i) |
| 123 persisted_logs.StoreLog("x"); | 129 persisted_logs.StoreLog("x"); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 | 286 |
| 281 TestPersistedLogs persisted_logs(&prefs_, kLogByteLimit); | 287 TestPersistedLogs persisted_logs(&prefs_, kLogByteLimit); |
| 282 persisted_logs.StoreLog(kFooText); | 288 persisted_logs.StoreLog(kFooText); |
| 283 persisted_logs.StageLog(); | 289 persisted_logs.StageLog(); |
| 284 | 290 |
| 285 EXPECT_EQ(Compress(kFooText), persisted_logs.staged_log()); | 291 EXPECT_EQ(Compress(kFooText), persisted_logs.staged_log()); |
| 286 EXPECT_EQ(foo_hash, persisted_logs.staged_log_hash()); | 292 EXPECT_EQ(foo_hash, persisted_logs.staged_log_hash()); |
| 287 } | 293 } |
| 288 | 294 |
| 289 } // namespace metrics | 295 } // namespace metrics |
| OLD | NEW |