| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/base64.h" | 5 #include "base/base64.h" |
| 6 #include "base/md5.h" | 6 #include "base/md5.h" |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/metrics/metrics_log_serializer.h" | 8 #include "chrome/browser/metrics/metrics_log_serializer.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 typedef MetricsLogManager::SerializedLog SerializedLog; | 11 typedef MetricsLogManager::SerializedLog SerializedLog; |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 const size_t kMaxLocalListSize = 3; | 15 const size_t kMaxLogByteTotal = 1000; |
| 16 | 16 |
| 17 } // namespace | 17 } // namespace |
| 18 | 18 |
| 19 class MetricsLogSerializerTest : public ::testing::Test { | 19 class MetricsLogSerializerTest : public ::testing::Test { |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 // Store and retrieve empty list. | 22 // Store and retrieve empty list. |
| 23 TEST(MetricsLogSerializerTest, EmptyLogList) { | 23 TEST(MetricsLogSerializerTest, EmptyLogList) { |
| 24 ListValue list; | 24 ListValue list; |
| 25 std::vector<SerializedLog> local_list; | 25 std::vector<SerializedLog> local_list; |
| 26 | 26 |
| 27 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLocalListSize, | 27 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLogByteTotal, |
| 28 &list); | 28 &list); |
| 29 EXPECT_EQ(0U, list.GetSize()); | 29 EXPECT_EQ(0U, list.GetSize()); |
| 30 | 30 |
| 31 local_list.clear(); // ReadLogsFromPrefList() expects empty |local_list|. | 31 local_list.clear(); // ReadLogsFromPrefList() expects empty |local_list|. |
| 32 EXPECT_EQ( | 32 EXPECT_EQ( |
| 33 MetricsLogSerializer::LIST_EMPTY, | 33 MetricsLogSerializer::LIST_EMPTY, |
| 34 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | 34 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); |
| 35 EXPECT_EQ(0U, local_list.size()); | 35 EXPECT_EQ(0U, local_list.size()); |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Store and retrieve a single log value. | 38 // Store and retrieve a single log value. |
| 39 TEST(MetricsLogSerializerTest, SingleElementLogList) { | 39 TEST(MetricsLogSerializerTest, SingleElementLogList) { |
| 40 ListValue list; | 40 ListValue list; |
| 41 | 41 |
| 42 std::vector<SerializedLog> local_list(1); | 42 std::vector<SerializedLog> local_list(1); |
| 43 local_list[0].xml = "Hello world!"; | 43 local_list[0].xml = "Hello world!"; |
| 44 | 44 |
| 45 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLocalListSize, | 45 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLogByteTotal, |
| 46 &list); | 46 &list); |
| 47 | 47 |
| 48 // |list| will now contain the following: | 48 // |list| will now contain the following: |
| 49 // [1, Base64Encode("Hello world!"), MD5("Hello world!")]. | 49 // [1, Base64Encode("Hello world!"), MD5("Hello world!")]. |
| 50 ASSERT_EQ(3U, list.GetSize()); | 50 ASSERT_EQ(3U, list.GetSize()); |
| 51 | 51 |
| 52 // Examine each element. | 52 // Examine each element. |
| 53 ListValue::const_iterator it = list.begin(); | 53 ListValue::const_iterator it = list.begin(); |
| 54 int size = 0; | 54 int size = 0; |
| 55 (*it)->GetAsInteger(&size); | 55 (*it)->GetAsInteger(&size); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 69 ++it; | 69 ++it; |
| 70 EXPECT_TRUE(it == list.end()); // Reached end of list. | 70 EXPECT_TRUE(it == list.end()); // Reached end of list. |
| 71 | 71 |
| 72 local_list.clear(); | 72 local_list.clear(); |
| 73 EXPECT_EQ( | 73 EXPECT_EQ( |
| 74 MetricsLogSerializer::RECALL_SUCCESS, | 74 MetricsLogSerializer::RECALL_SUCCESS, |
| 75 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | 75 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); |
| 76 EXPECT_EQ(1U, local_list.size()); | 76 EXPECT_EQ(1U, local_list.size()); |
| 77 } | 77 } |
| 78 | 78 |
| 79 // Store elements greater than the limit. | 79 // Store a set of logs larger than the limit. |
| 80 TEST(MetricsLogSerializerTest, OverLimitLogList) { | 80 TEST(MetricsLogSerializerTest, OverLimitLogList) { |
| 81 ListValue list; | 81 ListValue list; |
| 82 | 82 |
| 83 // Make log_count logs each slightly larger that TotalByteLimit / log_count |
| 84 // so that the oldest (first) log won't fit. |
| 83 std::vector<SerializedLog> local_list(4); | 85 std::vector<SerializedLog> local_list(4); |
| 84 local_list[0].proto = "one"; | 86 size_t log_size = (kMaxLogByteTotal / local_list.size()) + 2; |
| 85 local_list[1].proto = "two"; | 87 local_list[0].xml = "one"; |
| 86 local_list[2].proto = "three"; | 88 local_list[1].xml = "two"; |
| 87 local_list[3].proto = "four"; | 89 local_list[2].xml = "three"; |
| 90 local_list[3].xml = "four"; |
| 91 for (size_t i = 0; i < local_list.size(); ++i) { |
| 92 local_list[i].xml.resize(log_size, ' '); |
| 93 } |
| 88 | 94 |
| 89 std::string expected_first; | 95 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLogByteTotal, |
| 90 base::Base64Encode(local_list[local_list.size() - kMaxLocalListSize].proto, | 96 &list); |
| 91 &expected_first); | 97 std::vector<SerializedLog> result_list; |
| 92 std::string expected_last; | |
| 93 base::Base64Encode(local_list[local_list.size() - 1].proto, | |
| 94 &expected_last); | |
| 95 | |
| 96 MetricsLogSerializer::WriteLogsToPrefList(local_list, false, | |
| 97 kMaxLocalListSize, &list); | |
| 98 EXPECT_EQ(kMaxLocalListSize + 2, list.GetSize()); | |
| 99 | |
| 100 std::string actual_first; | |
| 101 EXPECT_TRUE((*(list.begin() + 1))->GetAsString(&actual_first)); | |
| 102 EXPECT_EQ(expected_first, actual_first); | |
| 103 | |
| 104 std::string actual_last; | |
| 105 EXPECT_TRUE((*(list.end() - 2))->GetAsString(&actual_last)); | |
| 106 EXPECT_EQ(expected_last, actual_last); | |
| 107 | |
| 108 local_list.clear(); | |
| 109 EXPECT_EQ( | 98 EXPECT_EQ( |
| 110 MetricsLogSerializer::RECALL_SUCCESS, | 99 MetricsLogSerializer::RECALL_SUCCESS, |
| 111 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | 100 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &result_list)); |
| 112 EXPECT_EQ(kMaxLocalListSize, local_list.size()); | 101 EXPECT_EQ(local_list.size() - 1, result_list.size()); |
| 102 |
| 103 EXPECT_TRUE(result_list.front().xml.find("two") == 0); |
| 104 EXPECT_TRUE(result_list.back().xml.find("four") == 0); |
| 105 } |
| 106 |
| 107 // Store a log larger than the limit. |
| 108 TEST(MetricsLogSerializerTest, OverLimitIndividualLog) { |
| 109 ListValue list; |
| 110 |
| 111 std::vector<SerializedLog> local_list(4); |
| 112 local_list[0].xml = "one"; |
| 113 local_list[1].xml = "two"; |
| 114 local_list[2].xml = "three"; |
| 115 local_list[2].xml.resize(kMaxLogByteTotal + 1, ' '); |
| 116 local_list[3].xml = "four"; |
| 117 |
| 118 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLogByteTotal, |
| 119 &list); |
| 120 std::vector<SerializedLog> result_list; |
| 121 EXPECT_EQ( |
| 122 MetricsLogSerializer::RECALL_SUCCESS, |
| 123 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &result_list)); |
| 124 EXPECT_EQ(local_list.size() - 1, result_list.size()); |
| 125 |
| 126 for (size_t i = 0; i < result_list.size(); ++i) { |
| 127 EXPECT_LT(result_list[i].xml.size(), kMaxLogByteTotal); |
| 128 } |
| 129 } |
| 130 |
| 131 // Store a set of logs each of which is larger than the limit. |
| 132 TEST(MetricsLogSerializerTest, OverLimitAllLogs) { |
| 133 ListValue list; |
| 134 |
| 135 std::vector<SerializedLog> local_list(2); |
| 136 size_t log_size = kMaxLogByteTotal + 1; |
| 137 for (size_t i = 0; i < local_list.size(); ++i) { |
| 138 local_list[i].xml.resize(log_size, ' '); |
| 139 } |
| 140 |
| 141 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLogByteTotal, |
| 142 &list); |
| 143 std::vector<SerializedLog> result_list; |
| 144 EXPECT_EQ( |
| 145 MetricsLogSerializer::LIST_EMPTY, |
| 146 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &result_list)); |
| 147 EXPECT_EQ(0U, result_list.size()); |
| 113 } | 148 } |
| 114 | 149 |
| 115 // Induce LIST_SIZE_TOO_SMALL corruption | 150 // Induce LIST_SIZE_TOO_SMALL corruption |
| 116 TEST(MetricsLogSerializerTest, SmallRecoveredListSize) { | 151 TEST(MetricsLogSerializerTest, SmallRecoveredListSize) { |
| 117 ListValue list; | 152 ListValue list; |
| 118 | 153 |
| 119 std::vector<SerializedLog> local_list(1); | 154 std::vector<SerializedLog> local_list(1); |
| 120 local_list[0].xml = "Hello world!"; | 155 local_list[0].xml = "Hello world!"; |
| 121 | 156 |
| 122 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLocalListSize, | 157 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLogByteTotal, |
| 123 &list); | 158 &list); |
| 124 EXPECT_EQ(3U, list.GetSize()); | 159 EXPECT_EQ(3U, list.GetSize()); |
| 125 | 160 |
| 126 // Remove last element. | 161 // Remove last element. |
| 127 list.Remove(list.GetSize() - 1, NULL); | 162 list.Remove(list.GetSize() - 1, NULL); |
| 128 EXPECT_EQ(2U, list.GetSize()); | 163 EXPECT_EQ(2U, list.GetSize()); |
| 129 | 164 |
| 130 local_list.clear(); | 165 local_list.clear(); |
| 131 EXPECT_EQ( | 166 EXPECT_EQ( |
| 132 MetricsLogSerializer::LIST_SIZE_TOO_SMALL, | 167 MetricsLogSerializer::LIST_SIZE_TOO_SMALL, |
| 133 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | 168 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); |
| 134 } | 169 } |
| 135 | 170 |
| 136 // Remove size from the stored list. | 171 // Remove size from the stored list. |
| 137 TEST(MetricsLogSerializerTest, RemoveSizeFromLogList) { | 172 TEST(MetricsLogSerializerTest, RemoveSizeFromLogList) { |
| 138 ListValue list; | 173 ListValue list; |
| 139 | 174 |
| 140 std::vector<SerializedLog> local_list(2); | 175 std::vector<SerializedLog> local_list(2); |
| 141 local_list[0].xml = "one"; | 176 local_list[0].xml = "one"; |
| 142 local_list[1].xml = "two"; | 177 local_list[1].xml = "two"; |
| 143 EXPECT_EQ(2U, local_list.size()); | 178 EXPECT_EQ(2U, local_list.size()); |
| 144 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLocalListSize, | 179 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLogByteTotal, |
| 145 &list); | 180 &list); |
| 146 EXPECT_EQ(4U, list.GetSize()); | 181 EXPECT_EQ(4U, list.GetSize()); |
| 147 | 182 |
| 148 list.Remove(0, NULL); // Delete size (1st element). | 183 list.Remove(0, NULL); // Delete size (1st element). |
| 149 EXPECT_EQ(3U, list.GetSize()); | 184 EXPECT_EQ(3U, list.GetSize()); |
| 150 | 185 |
| 151 local_list.clear(); | 186 local_list.clear(); |
| 152 EXPECT_EQ( | 187 EXPECT_EQ( |
| 153 MetricsLogSerializer::LIST_SIZE_MISSING, | 188 MetricsLogSerializer::LIST_SIZE_MISSING, |
| 154 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | 189 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); |
| 155 } | 190 } |
| 156 | 191 |
| 157 // Corrupt size of stored list. | 192 // Corrupt size of stored list. |
| 158 TEST(MetricsLogSerializerTest, CorruptSizeOfLogList) { | 193 TEST(MetricsLogSerializerTest, CorruptSizeOfLogList) { |
| 159 ListValue list; | 194 ListValue list; |
| 160 | 195 |
| 161 std::vector<SerializedLog> local_list(1); | 196 std::vector<SerializedLog> local_list(1); |
| 162 local_list[0].xml = "Hello world!"; | 197 local_list[0].xml = "Hello world!"; |
| 163 | 198 |
| 164 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLocalListSize, | 199 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLogByteTotal, |
| 165 &list); | 200 &list); |
| 166 EXPECT_EQ(3U, list.GetSize()); | 201 EXPECT_EQ(3U, list.GetSize()); |
| 167 | 202 |
| 168 // Change list size from 1 to 2. | 203 // Change list size from 1 to 2. |
| 169 EXPECT_TRUE(list.Set(0, Value::CreateIntegerValue(2))); | 204 EXPECT_TRUE(list.Set(0, Value::CreateIntegerValue(2))); |
| 170 EXPECT_EQ(3U, list.GetSize()); | 205 EXPECT_EQ(3U, list.GetSize()); |
| 171 | 206 |
| 172 local_list.clear(); | 207 local_list.clear(); |
| 173 EXPECT_EQ( | 208 EXPECT_EQ( |
| 174 MetricsLogSerializer::LIST_SIZE_CORRUPTION, | 209 MetricsLogSerializer::LIST_SIZE_CORRUPTION, |
| 175 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | 210 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); |
| 176 } | 211 } |
| 177 | 212 |
| 178 // Corrupt checksum of stored list. | 213 // Corrupt checksum of stored list. |
| 179 TEST(MetricsLogSerializerTest, CorruptChecksumOfLogList) { | 214 TEST(MetricsLogSerializerTest, CorruptChecksumOfLogList) { |
| 180 ListValue list; | 215 ListValue list; |
| 181 | 216 |
| 182 std::vector<SerializedLog> local_list(1); | 217 std::vector<SerializedLog> local_list(1); |
| 183 local_list[0].xml = "Hello world!"; | 218 local_list[0].xml = "Hello world!"; |
| 184 | 219 |
| 185 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLocalListSize, | 220 MetricsLogSerializer::WriteLogsToPrefList(local_list, true, kMaxLogByteTotal, |
| 186 &list); | 221 &list); |
| 187 EXPECT_EQ(3U, list.GetSize()); | 222 EXPECT_EQ(3U, list.GetSize()); |
| 188 | 223 |
| 189 // Fetch checksum (last element) and change it. | 224 // Fetch checksum (last element) and change it. |
| 190 std::string checksum; | 225 std::string checksum; |
| 191 EXPECT_TRUE((*(list.end() - 1))->GetAsString(&checksum)); | 226 EXPECT_TRUE((*(list.end() - 1))->GetAsString(&checksum)); |
| 192 checksum[0] = (checksum[0] == 'a') ? 'b' : 'a'; | 227 checksum[0] = (checksum[0] == 'a') ? 'b' : 'a'; |
| 193 EXPECT_TRUE(list.Set(2, Value::CreateStringValue(checksum))); | 228 EXPECT_TRUE(list.Set(2, Value::CreateStringValue(checksum))); |
| 194 EXPECT_EQ(3U, list.GetSize()); | 229 EXPECT_EQ(3U, list.GetSize()); |
| 195 | 230 |
| 196 local_list.clear(); | 231 local_list.clear(); |
| 197 EXPECT_EQ( | 232 EXPECT_EQ( |
| 198 MetricsLogSerializer::CHECKSUM_CORRUPTION, | 233 MetricsLogSerializer::CHECKSUM_CORRUPTION, |
| 199 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | 234 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); |
| 200 } | 235 } |
| OLD | NEW |