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 namespace { | 11 namespace { |
12 | 12 |
13 const size_t kListLengthLimit = 3; | 13 const size_t kListLengthLimit = 3; |
14 const size_t kLogByteLimit = 1000; | 14 const size_t kLogByteLimit = 1000; |
15 | 15 |
| 16 void SetLogText(const std::string& log_text, |
| 17 MetricsLogManager::SerializedLog* log) { |
| 18 std::string log_text_copy = log_text; |
| 19 log->SwapLogText(&log_text_copy); |
| 20 } |
| 21 |
16 } // namespace | 22 } // namespace |
17 | 23 |
18 class MetricsLogSerializerTest : public ::testing::Test { | |
19 }; | |
20 | |
21 // Store and retrieve empty list. | 24 // Store and retrieve empty list. |
22 TEST(MetricsLogSerializerTest, EmptyLogList) { | 25 TEST(MetricsLogSerializerTest, EmptyLogList) { |
23 ListValue list; | 26 ListValue list; |
24 std::vector<std::string> local_list; | 27 std::vector<MetricsLogManager::SerializedLog> local_list; |
25 | 28 |
26 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, | 29 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, |
27 kLogByteLimit, &list); | 30 kLogByteLimit, &list); |
28 EXPECT_EQ(0U, list.GetSize()); | 31 EXPECT_EQ(0U, list.GetSize()); |
29 | 32 |
30 local_list.clear(); // ReadLogsFromPrefList() expects empty |local_list|. | 33 local_list.clear(); // ReadLogsFromPrefList() expects empty |local_list|. |
31 EXPECT_EQ( | 34 EXPECT_EQ( |
32 MetricsLogSerializer::LIST_EMPTY, | 35 MetricsLogSerializer::LIST_EMPTY, |
33 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 36 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); |
34 EXPECT_EQ(0U, local_list.size()); | 37 EXPECT_EQ(0U, local_list.size()); |
35 } | 38 } |
36 | 39 |
37 // Store and retrieve a single log value. | 40 // Store and retrieve a single log value. |
38 TEST(MetricsLogSerializerTest, SingleElementLogList) { | 41 TEST(MetricsLogSerializerTest, SingleElementLogList) { |
39 ListValue list; | 42 ListValue list; |
40 | 43 |
41 std::vector<std::string> local_list(1); | 44 std::vector<MetricsLogManager::SerializedLog> local_list(1); |
42 local_list[0] = "Hello world!"; | 45 SetLogText("Hello world!", &local_list[0]); |
43 | 46 |
44 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, | 47 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, |
45 kLogByteLimit, &list); | 48 kLogByteLimit, &list); |
46 | 49 |
47 // |list| will now contain the following: | 50 // |list| will now contain the following: |
48 // [1, Base64Encode("Hello world!"), MD5("Hello world!")]. | 51 // [1, Base64Encode("Hello world!"), MD5("Hello world!")]. |
49 ASSERT_EQ(3U, list.GetSize()); | 52 ASSERT_EQ(3U, list.GetSize()); |
50 | 53 |
51 // Examine each element. | 54 // Examine each element. |
52 ListValue::const_iterator it = list.begin(); | 55 ListValue::const_iterator it = list.begin(); |
(...skipping 21 matching lines...) Expand all Loading... |
74 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 77 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); |
75 EXPECT_EQ(1U, local_list.size()); | 78 EXPECT_EQ(1U, local_list.size()); |
76 } | 79 } |
77 | 80 |
78 // Store a set of logs over the length limit, but smaller than the min number of | 81 // Store a set of logs over the length limit, but smaller than the min number of |
79 // bytes. | 82 // bytes. |
80 TEST(MetricsLogSerializerTest, LongButTinyLogList) { | 83 TEST(MetricsLogSerializerTest, LongButTinyLogList) { |
81 ListValue list; | 84 ListValue list; |
82 | 85 |
83 size_t log_count = kListLengthLimit * 5; | 86 size_t log_count = kListLengthLimit * 5; |
84 std::vector<std::string> local_list(log_count); | 87 std::vector<MetricsLogManager::SerializedLog> local_list(log_count); |
85 for (size_t i = 0; i < local_list.size(); ++i) { | 88 for (size_t i = 0; i < local_list.size(); ++i) |
86 local_list[0] = "x"; | 89 SetLogText("x", &local_list[i]); |
87 } | |
88 | 90 |
89 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, | 91 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, |
90 kLogByteLimit, &list); | 92 kLogByteLimit, &list); |
91 std::vector<std::string> result_list; | 93 std::vector<MetricsLogManager::SerializedLog> result_list; |
92 EXPECT_EQ( | 94 EXPECT_EQ( |
93 MetricsLogSerializer::RECALL_SUCCESS, | 95 MetricsLogSerializer::RECALL_SUCCESS, |
94 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); | 96 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); |
95 EXPECT_EQ(local_list.size(), result_list.size()); | 97 EXPECT_EQ(local_list.size(), result_list.size()); |
96 | 98 |
97 EXPECT_TRUE(result_list.front().find("x") == 0); | 99 EXPECT_TRUE(result_list.front().log_text().find("x") == 0); |
98 } | 100 } |
99 | 101 |
100 // Store a set of logs over the length limit, but that doesn't reach the minimum | 102 // Store a set of logs over the length limit, but that doesn't reach the minimum |
101 // number of bytes until after passing the length limit. | 103 // number of bytes until after passing the length limit. |
102 TEST(MetricsLogSerializerTest, LongButSmallLogList) { | 104 TEST(MetricsLogSerializerTest, LongButSmallLogList) { |
103 ListValue list; | 105 ListValue list; |
104 | 106 |
105 size_t log_count = kListLengthLimit * 5; | 107 size_t log_count = kListLengthLimit * 5; |
106 // Make log_count logs each slightly larger than | 108 // Make log_count logs each slightly larger than |
107 // kLogByteLimit / (log_count - 2) | 109 // kLogByteLimit / (log_count - 2) |
108 // so that the minimum is reached before the oldest (first) two logs. | 110 // so that the minimum is reached before the oldest (first) two logs. |
109 std::vector<std::string> local_list(log_count); | 111 std::vector<MetricsLogManager::SerializedLog> local_list(log_count); |
110 size_t log_size = (kLogByteLimit / (log_count - 2)) + 2; | 112 size_t log_size = (kLogByteLimit / (log_count - 2)) + 2; |
111 local_list[0] = "one"; | 113 SetLogText("one", &local_list[0]); |
112 local_list[1] = "two"; | 114 SetLogText("two", &local_list[1]); |
113 local_list[2] = "three"; | 115 SetLogText("three", &local_list[2]); |
114 local_list[log_count - 1] = "last"; | 116 SetLogText("last", &local_list[log_count - 1]); |
115 for (size_t i = 0; i < local_list.size(); ++i) { | 117 for (size_t i = 0; i < local_list.size(); ++i) { |
116 local_list[i].resize(log_size, ' '); | 118 std::string log_text = local_list[i].log_text(); |
| 119 log_text.resize(log_size, ' '); |
| 120 local_list[i].SwapLogText(&log_text); |
117 } | 121 } |
118 | 122 |
119 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, | 123 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, |
120 kLogByteLimit, &list); | 124 kLogByteLimit, &list); |
121 std::vector<std::string> result_list; | 125 std::vector<MetricsLogManager::SerializedLog> result_list; |
122 EXPECT_EQ( | 126 EXPECT_EQ( |
123 MetricsLogSerializer::RECALL_SUCCESS, | 127 MetricsLogSerializer::RECALL_SUCCESS, |
124 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); | 128 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); |
125 EXPECT_EQ(local_list.size() - 2, result_list.size()); | 129 EXPECT_EQ(local_list.size() - 2, result_list.size()); |
126 | 130 |
127 EXPECT_TRUE(result_list.front().find("three") == 0); | 131 EXPECT_TRUE(result_list.front().log_text().find("three") == 0); |
128 EXPECT_TRUE(result_list.back().find("last") == 0); | 132 EXPECT_TRUE(result_list.back().log_text().find("last") == 0); |
129 } | 133 } |
130 | 134 |
131 // Store a set of logs within the length limit, but well over the minimum | 135 // Store a set of logs within the length limit, but well over the minimum |
132 // number of bytes. | 136 // number of bytes. |
133 TEST(MetricsLogSerializerTest, ShortButLargeLogList) { | 137 TEST(MetricsLogSerializerTest, ShortButLargeLogList) { |
134 ListValue list; | 138 ListValue list; |
135 | 139 |
136 std::vector<std::string> local_list(kListLengthLimit); | 140 std::vector<MetricsLogManager::SerializedLog> local_list(kListLengthLimit); |
137 // Make the total byte count about twice the minimum. | 141 // Make the total byte count about twice the minimum. |
138 size_t log_size = (kLogByteLimit / local_list.size()) * 2; | 142 size_t log_size = (kLogByteLimit / local_list.size()) * 2; |
139 for (size_t i = 0; i < local_list.size(); ++i) { | 143 for (size_t i = 0; i < local_list.size(); ++i) { |
140 local_list[i].resize(log_size, ' '); | 144 std::string log_text = local_list[i].log_text(); |
| 145 log_text.resize(log_size, ' '); |
| 146 local_list[i].SwapLogText(&log_text); |
141 } | 147 } |
142 | 148 |
143 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, | 149 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, |
144 kLogByteLimit, &list); | 150 kLogByteLimit, &list); |
145 std::vector<std::string> result_list; | 151 std::vector<MetricsLogManager::SerializedLog> result_list; |
146 EXPECT_EQ( | 152 EXPECT_EQ( |
147 MetricsLogSerializer::RECALL_SUCCESS, | 153 MetricsLogSerializer::RECALL_SUCCESS, |
148 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); | 154 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); |
149 EXPECT_EQ(local_list.size(), result_list.size()); | 155 EXPECT_EQ(local_list.size(), result_list.size()); |
150 } | 156 } |
151 | 157 |
152 // Store a set of logs over the length limit, and over the minimum number of | 158 // Store a set of logs over the length limit, and over the minimum number of |
153 // bytes. | 159 // bytes. |
154 TEST(MetricsLogSerializerTest, LongAndLargeLogList) { | 160 TEST(MetricsLogSerializerTest, LongAndLargeLogList) { |
155 ListValue list; | 161 ListValue list; |
156 | 162 |
157 // Include twice the max number of logs. | 163 // Include twice the max number of logs. |
158 std::vector<std::string> local_list(kListLengthLimit * 2); | 164 std::vector<MetricsLogManager::SerializedLog> |
| 165 local_list(kListLengthLimit * 2); |
159 // Make the total byte count about four times the minimum. | 166 // Make the total byte count about four times the minimum. |
160 size_t log_size = (kLogByteLimit / local_list.size()) * 4; | 167 size_t log_size = (kLogByteLimit / local_list.size()) * 4; |
161 local_list[local_list.size() - kListLengthLimit] = "First to keep"; | 168 SetLogText("First to keep", |
| 169 &local_list[local_list.size() - kListLengthLimit]); |
162 for (size_t i = 0; i < local_list.size(); ++i) { | 170 for (size_t i = 0; i < local_list.size(); ++i) { |
163 local_list[i].resize(log_size, ' '); | 171 std::string log_text = local_list[i].log_text(); |
| 172 log_text.resize(log_size, ' '); |
| 173 local_list[i].SwapLogText(&log_text); |
164 } | 174 } |
165 | 175 |
166 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, | 176 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, |
167 kLogByteLimit, &list); | 177 kLogByteLimit, &list); |
168 std::vector<std::string> result_list; | 178 std::vector<MetricsLogManager::SerializedLog> result_list; |
169 EXPECT_EQ( | 179 EXPECT_EQ( |
170 MetricsLogSerializer::RECALL_SUCCESS, | 180 MetricsLogSerializer::RECALL_SUCCESS, |
171 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); | 181 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); |
172 // The max length should control the resulting size. | 182 // The max length should control the resulting size. |
173 EXPECT_EQ(kListLengthLimit, result_list.size()); | 183 EXPECT_EQ(kListLengthLimit, result_list.size()); |
174 EXPECT_TRUE(result_list.front().find("First to keep") == 0); | 184 EXPECT_TRUE(result_list.front().log_text().find("First to keep") == 0); |
175 } | 185 } |
176 | 186 |
177 // Induce LIST_SIZE_TOO_SMALL corruption | 187 // Induce LIST_SIZE_TOO_SMALL corruption |
178 TEST(MetricsLogSerializerTest, SmallRecoveredListSize) { | 188 TEST(MetricsLogSerializerTest, SmallRecoveredListSize) { |
179 ListValue list; | 189 ListValue list; |
180 | 190 |
181 std::vector<std::string> local_list(1); | 191 std::vector<MetricsLogManager::SerializedLog> local_list(1); |
182 local_list[0] = "Hello world!"; | 192 SetLogText("Hello world!", &local_list[0]); |
183 | 193 |
184 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, | 194 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, |
185 kLogByteLimit, &list); | 195 kLogByteLimit, &list); |
186 EXPECT_EQ(3U, list.GetSize()); | 196 EXPECT_EQ(3U, list.GetSize()); |
187 | 197 |
188 // Remove last element. | 198 // Remove last element. |
189 list.Remove(list.GetSize() - 1, NULL); | 199 list.Remove(list.GetSize() - 1, NULL); |
190 EXPECT_EQ(2U, list.GetSize()); | 200 EXPECT_EQ(2U, list.GetSize()); |
191 | 201 |
192 local_list.clear(); | 202 local_list.clear(); |
193 EXPECT_EQ( | 203 EXPECT_EQ( |
194 MetricsLogSerializer::LIST_SIZE_TOO_SMALL, | 204 MetricsLogSerializer::LIST_SIZE_TOO_SMALL, |
195 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 205 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); |
196 } | 206 } |
197 | 207 |
198 // Remove size from the stored list. | 208 // Remove size from the stored list. |
199 TEST(MetricsLogSerializerTest, RemoveSizeFromLogList) { | 209 TEST(MetricsLogSerializerTest, RemoveSizeFromLogList) { |
200 ListValue list; | 210 ListValue list; |
201 | 211 |
202 std::vector<std::string> local_list(2); | 212 std::vector<MetricsLogManager::SerializedLog> local_list(2); |
203 local_list[0] = "one"; | 213 SetLogText("one", &local_list[0]); |
204 local_list[1] = "two"; | 214 SetLogText("two", &local_list[1]); |
205 EXPECT_EQ(2U, local_list.size()); | 215 EXPECT_EQ(2U, local_list.size()); |
206 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, | 216 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, |
207 kLogByteLimit, &list); | 217 kLogByteLimit, &list); |
208 EXPECT_EQ(4U, list.GetSize()); | 218 EXPECT_EQ(4U, list.GetSize()); |
209 | 219 |
210 list.Remove(0, NULL); // Delete size (1st element). | 220 list.Remove(0, NULL); // Delete size (1st element). |
211 EXPECT_EQ(3U, list.GetSize()); | 221 EXPECT_EQ(3U, list.GetSize()); |
212 | 222 |
213 local_list.clear(); | 223 local_list.clear(); |
214 EXPECT_EQ( | 224 EXPECT_EQ( |
215 MetricsLogSerializer::LIST_SIZE_MISSING, | 225 MetricsLogSerializer::LIST_SIZE_MISSING, |
216 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 226 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); |
217 } | 227 } |
218 | 228 |
219 // Corrupt size of stored list. | 229 // Corrupt size of stored list. |
220 TEST(MetricsLogSerializerTest, CorruptSizeOfLogList) { | 230 TEST(MetricsLogSerializerTest, CorruptSizeOfLogList) { |
221 ListValue list; | 231 ListValue list; |
222 | 232 |
223 std::vector<std::string> local_list(1); | 233 std::vector<MetricsLogManager::SerializedLog> local_list(1); |
224 local_list[0] = "Hello world!"; | 234 SetLogText("Hello world!", &local_list[0]); |
225 | 235 |
226 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, | 236 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, |
227 kLogByteLimit, &list); | 237 kLogByteLimit, &list); |
228 EXPECT_EQ(3U, list.GetSize()); | 238 EXPECT_EQ(3U, list.GetSize()); |
229 | 239 |
230 // Change list size from 1 to 2. | 240 // Change list size from 1 to 2. |
231 EXPECT_TRUE(list.Set(0, Value::CreateIntegerValue(2))); | 241 EXPECT_TRUE(list.Set(0, Value::CreateIntegerValue(2))); |
232 EXPECT_EQ(3U, list.GetSize()); | 242 EXPECT_EQ(3U, list.GetSize()); |
233 | 243 |
234 local_list.clear(); | 244 local_list.clear(); |
235 EXPECT_EQ( | 245 EXPECT_EQ( |
236 MetricsLogSerializer::LIST_SIZE_CORRUPTION, | 246 MetricsLogSerializer::LIST_SIZE_CORRUPTION, |
237 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 247 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); |
238 } | 248 } |
239 | 249 |
240 // Corrupt checksum of stored list. | 250 // Corrupt checksum of stored list. |
241 TEST(MetricsLogSerializerTest, CorruptChecksumOfLogList) { | 251 TEST(MetricsLogSerializerTest, CorruptChecksumOfLogList) { |
242 ListValue list; | 252 ListValue list; |
243 | 253 |
244 std::vector<std::string> local_list(1); | 254 std::vector<MetricsLogManager::SerializedLog> local_list(1); |
245 local_list[0] = "Hello world!"; | 255 SetLogText("Hello world!", &local_list[0]); |
246 | 256 |
247 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, | 257 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, |
248 kLogByteLimit, &list); | 258 kLogByteLimit, &list); |
249 EXPECT_EQ(3U, list.GetSize()); | 259 EXPECT_EQ(3U, list.GetSize()); |
250 | 260 |
251 // Fetch checksum (last element) and change it. | 261 // Fetch checksum (last element) and change it. |
252 std::string checksum; | 262 std::string checksum; |
253 EXPECT_TRUE((*(list.end() - 1))->GetAsString(&checksum)); | 263 EXPECT_TRUE((*(list.end() - 1))->GetAsString(&checksum)); |
254 checksum[0] = (checksum[0] == 'a') ? 'b' : 'a'; | 264 checksum[0] = (checksum[0] == 'a') ? 'b' : 'a'; |
255 EXPECT_TRUE(list.Set(2, Value::CreateStringValue(checksum))); | 265 EXPECT_TRUE(list.Set(2, Value::CreateStringValue(checksum))); |
256 EXPECT_EQ(3U, list.GetSize()); | 266 EXPECT_EQ(3U, list.GetSize()); |
257 | 267 |
258 local_list.clear(); | 268 local_list.clear(); |
259 EXPECT_EQ( | 269 EXPECT_EQ( |
260 MetricsLogSerializer::CHECKSUM_CORRUPTION, | 270 MetricsLogSerializer::CHECKSUM_CORRUPTION, |
261 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 271 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); |
262 } | 272 } |
OLD | NEW |