Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: chrome/browser/metrics/metrics_log_serializer_unittest.cc

Issue 104493005: Update some uses of Value in chrome/browser to use the base:: namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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, 16 void SetLogText(const std::string& log_text,
17 MetricsLogManager::SerializedLog* log) { 17 MetricsLogManager::SerializedLog* log) {
18 std::string log_text_copy = log_text; 18 std::string log_text_copy = log_text;
19 log->SwapLogText(&log_text_copy); 19 log->SwapLogText(&log_text_copy);
20 } 20 }
21 21
22 } // namespace 22 } // namespace
23 23
24 // Store and retrieve empty list. 24 // Store and retrieve empty list.
25 TEST(MetricsLogSerializerTest, EmptyLogList) { 25 TEST(MetricsLogSerializerTest, EmptyLogList) {
26 ListValue list; 26 base::ListValue list;
27 std::vector<MetricsLogManager::SerializedLog> local_list; 27 std::vector<MetricsLogManager::SerializedLog> local_list;
28 28
29 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, 29 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
30 kLogByteLimit, &list); 30 kLogByteLimit, &list);
31 EXPECT_EQ(0U, list.GetSize()); 31 EXPECT_EQ(0U, list.GetSize());
32 32
33 local_list.clear(); // ReadLogsFromPrefList() expects empty |local_list|. 33 local_list.clear(); // ReadLogsFromPrefList() expects empty |local_list|.
34 EXPECT_EQ( 34 EXPECT_EQ(
35 MetricsLogSerializer::LIST_EMPTY, 35 MetricsLogSerializer::LIST_EMPTY,
36 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); 36 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
37 EXPECT_EQ(0U, local_list.size()); 37 EXPECT_EQ(0U, local_list.size());
38 } 38 }
39 39
40 // Store and retrieve a single log value. 40 // Store and retrieve a single log value.
41 TEST(MetricsLogSerializerTest, SingleElementLogList) { 41 TEST(MetricsLogSerializerTest, SingleElementLogList) {
42 ListValue list; 42 base::ListValue list;
43 43
44 std::vector<MetricsLogManager::SerializedLog> local_list(1); 44 std::vector<MetricsLogManager::SerializedLog> local_list(1);
45 SetLogText("Hello world!", &local_list[0]); 45 SetLogText("Hello world!", &local_list[0]);
46 46
47 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, 47 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
48 kLogByteLimit, &list); 48 kLogByteLimit, &list);
49 49
50 // |list| will now contain the following: 50 // |list| will now contain the following:
51 // [1, Base64Encode("Hello world!"), MD5("Hello world!")]. 51 // [1, Base64Encode("Hello world!"), MD5("Hello world!")].
52 ASSERT_EQ(3U, list.GetSize()); 52 ASSERT_EQ(3U, list.GetSize());
53 53
54 // Examine each element. 54 // Examine each element.
55 ListValue::const_iterator it = list.begin(); 55 base::ListValue::const_iterator it = list.begin();
56 int size = 0; 56 int size = 0;
57 (*it)->GetAsInteger(&size); 57 (*it)->GetAsInteger(&size);
58 EXPECT_EQ(1, size); 58 EXPECT_EQ(1, size);
59 59
60 ++it; 60 ++it;
61 std::string str; 61 std::string str;
62 (*it)->GetAsString(&str); // Base64 encoded "Hello world!" string. 62 (*it)->GetAsString(&str); // Base64 encoded "Hello world!" string.
63 std::string encoded; 63 std::string encoded;
64 base::Base64Encode("Hello world!", &encoded); 64 base::Base64Encode("Hello world!", &encoded);
65 EXPECT_TRUE(encoded == str); 65 EXPECT_TRUE(encoded == str);
66 66
67 ++it; 67 ++it;
68 (*it)->GetAsString(&str); // MD5 for encoded "Hello world!" string. 68 (*it)->GetAsString(&str); // MD5 for encoded "Hello world!" string.
69 EXPECT_TRUE(base::MD5String(encoded) == str); 69 EXPECT_TRUE(base::MD5String(encoded) == str);
70 70
71 ++it; 71 ++it;
72 EXPECT_TRUE(it == list.end()); // Reached end of list. 72 EXPECT_TRUE(it == list.end()); // Reached end of list.
73 73
74 local_list.clear(); 74 local_list.clear();
75 EXPECT_EQ( 75 EXPECT_EQ(
76 MetricsLogSerializer::RECALL_SUCCESS, 76 MetricsLogSerializer::RECALL_SUCCESS,
77 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); 77 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
78 EXPECT_EQ(1U, local_list.size()); 78 EXPECT_EQ(1U, local_list.size());
79 } 79 }
80 80
81 // 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
82 // bytes. 82 // bytes.
83 TEST(MetricsLogSerializerTest, LongButTinyLogList) { 83 TEST(MetricsLogSerializerTest, LongButTinyLogList) {
84 ListValue list; 84 base::ListValue list;
85 85
86 size_t log_count = kListLengthLimit * 5; 86 size_t log_count = kListLengthLimit * 5;
87 std::vector<MetricsLogManager::SerializedLog> local_list(log_count); 87 std::vector<MetricsLogManager::SerializedLog> local_list(log_count);
88 for (size_t i = 0; i < local_list.size(); ++i) 88 for (size_t i = 0; i < local_list.size(); ++i)
89 SetLogText("x", &local_list[i]); 89 SetLogText("x", &local_list[i]);
90 90
91 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, 91 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
92 kLogByteLimit, &list); 92 kLogByteLimit, &list);
93 std::vector<MetricsLogManager::SerializedLog> result_list; 93 std::vector<MetricsLogManager::SerializedLog> result_list;
94 EXPECT_EQ( 94 EXPECT_EQ(
95 MetricsLogSerializer::RECALL_SUCCESS, 95 MetricsLogSerializer::RECALL_SUCCESS,
96 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); 96 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list));
97 EXPECT_EQ(local_list.size(), result_list.size()); 97 EXPECT_EQ(local_list.size(), result_list.size());
98 98
99 EXPECT_TRUE(result_list.front().log_text().find("x") == 0); 99 EXPECT_TRUE(result_list.front().log_text().find("x") == 0);
100 } 100 }
101 101
102 // 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
103 // number of bytes until after passing the length limit. 103 // number of bytes until after passing the length limit.
104 TEST(MetricsLogSerializerTest, LongButSmallLogList) { 104 TEST(MetricsLogSerializerTest, LongButSmallLogList) {
105 ListValue list; 105 base::ListValue list;
106 106
107 size_t log_count = kListLengthLimit * 5; 107 size_t log_count = kListLengthLimit * 5;
108 // Make log_count logs each slightly larger than 108 // Make log_count logs each slightly larger than
109 // kLogByteLimit / (log_count - 2) 109 // kLogByteLimit / (log_count - 2)
110 // 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.
111 std::vector<MetricsLogManager::SerializedLog> local_list(log_count); 111 std::vector<MetricsLogManager::SerializedLog> local_list(log_count);
112 size_t log_size = (kLogByteLimit / (log_count - 2)) + 2; 112 size_t log_size = (kLogByteLimit / (log_count - 2)) + 2;
113 SetLogText("one", &local_list[0]); 113 SetLogText("one", &local_list[0]);
114 SetLogText("two", &local_list[1]); 114 SetLogText("two", &local_list[1]);
115 SetLogText("three", &local_list[2]); 115 SetLogText("three", &local_list[2]);
(...skipping 12 matching lines...) Expand all
128 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); 128 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list));
129 EXPECT_EQ(local_list.size() - 2, result_list.size()); 129 EXPECT_EQ(local_list.size() - 2, result_list.size());
130 130
131 EXPECT_TRUE(result_list.front().log_text().find("three") == 0); 131 EXPECT_TRUE(result_list.front().log_text().find("three") == 0);
132 EXPECT_TRUE(result_list.back().log_text().find("last") == 0); 132 EXPECT_TRUE(result_list.back().log_text().find("last") == 0);
133 } 133 }
134 134
135 // 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
136 // number of bytes. 136 // number of bytes.
137 TEST(MetricsLogSerializerTest, ShortButLargeLogList) { 137 TEST(MetricsLogSerializerTest, ShortButLargeLogList) {
138 ListValue list; 138 base::ListValue list;
139 139
140 std::vector<MetricsLogManager::SerializedLog> local_list(kListLengthLimit); 140 std::vector<MetricsLogManager::SerializedLog> local_list(kListLengthLimit);
141 // Make the total byte count about twice the minimum. 141 // Make the total byte count about twice the minimum.
142 size_t log_size = (kLogByteLimit / local_list.size()) * 2; 142 size_t log_size = (kLogByteLimit / local_list.size()) * 2;
143 for (size_t i = 0; i < local_list.size(); ++i) { 143 for (size_t i = 0; i < local_list.size(); ++i) {
144 std::string log_text = local_list[i].log_text(); 144 std::string log_text = local_list[i].log_text();
145 log_text.resize(log_size, ' '); 145 log_text.resize(log_size, ' ');
146 local_list[i].SwapLogText(&log_text); 146 local_list[i].SwapLogText(&log_text);
147 } 147 }
148 148
149 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, 149 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
150 kLogByteLimit, &list); 150 kLogByteLimit, &list);
151 std::vector<MetricsLogManager::SerializedLog> result_list; 151 std::vector<MetricsLogManager::SerializedLog> result_list;
152 EXPECT_EQ( 152 EXPECT_EQ(
153 MetricsLogSerializer::RECALL_SUCCESS, 153 MetricsLogSerializer::RECALL_SUCCESS,
154 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); 154 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list));
155 EXPECT_EQ(local_list.size(), result_list.size()); 155 EXPECT_EQ(local_list.size(), result_list.size());
156 } 156 }
157 157
158 // 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
159 // bytes. 159 // bytes.
160 TEST(MetricsLogSerializerTest, LongAndLargeLogList) { 160 TEST(MetricsLogSerializerTest, LongAndLargeLogList) {
161 ListValue list; 161 base::ListValue list;
162 162
163 // Include twice the max number of logs. 163 // Include twice the max number of logs.
164 std::vector<MetricsLogManager::SerializedLog> 164 std::vector<MetricsLogManager::SerializedLog>
165 local_list(kListLengthLimit * 2); 165 local_list(kListLengthLimit * 2);
166 // Make the total byte count about four times the minimum. 166 // Make the total byte count about four times the minimum.
167 size_t log_size = (kLogByteLimit / local_list.size()) * 4; 167 size_t log_size = (kLogByteLimit / local_list.size()) * 4;
168 SetLogText("First to keep", 168 SetLogText("First to keep",
169 &local_list[local_list.size() - kListLengthLimit]); 169 &local_list[local_list.size() - kListLengthLimit]);
170 for (size_t i = 0; i < local_list.size(); ++i) { 170 for (size_t i = 0; i < local_list.size(); ++i) {
171 std::string log_text = local_list[i].log_text(); 171 std::string log_text = local_list[i].log_text();
172 log_text.resize(log_size, ' '); 172 log_text.resize(log_size, ' ');
173 local_list[i].SwapLogText(&log_text); 173 local_list[i].SwapLogText(&log_text);
174 } 174 }
175 175
176 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, 176 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
177 kLogByteLimit, &list); 177 kLogByteLimit, &list);
178 std::vector<MetricsLogManager::SerializedLog> result_list; 178 std::vector<MetricsLogManager::SerializedLog> result_list;
179 EXPECT_EQ( 179 EXPECT_EQ(
180 MetricsLogSerializer::RECALL_SUCCESS, 180 MetricsLogSerializer::RECALL_SUCCESS,
181 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list)); 181 MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list));
182 // The max length should control the resulting size. 182 // The max length should control the resulting size.
183 EXPECT_EQ(kListLengthLimit, result_list.size()); 183 EXPECT_EQ(kListLengthLimit, result_list.size());
184 EXPECT_TRUE(result_list.front().log_text().find("First to keep") == 0); 184 EXPECT_TRUE(result_list.front().log_text().find("First to keep") == 0);
185 } 185 }
186 186
187 // Induce LIST_SIZE_TOO_SMALL corruption 187 // Induce LIST_SIZE_TOO_SMALL corruption
188 TEST(MetricsLogSerializerTest, SmallRecoveredListSize) { 188 TEST(MetricsLogSerializerTest, SmallRecoveredListSize) {
189 ListValue list; 189 base::ListValue list;
190 190
191 std::vector<MetricsLogManager::SerializedLog> local_list(1); 191 std::vector<MetricsLogManager::SerializedLog> local_list(1);
192 SetLogText("Hello world!", &local_list[0]); 192 SetLogText("Hello world!", &local_list[0]);
193 193
194 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, 194 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
195 kLogByteLimit, &list); 195 kLogByteLimit, &list);
196 EXPECT_EQ(3U, list.GetSize()); 196 EXPECT_EQ(3U, list.GetSize());
197 197
198 // Remove last element. 198 // Remove last element.
199 list.Remove(list.GetSize() - 1, NULL); 199 list.Remove(list.GetSize() - 1, NULL);
200 EXPECT_EQ(2U, list.GetSize()); 200 EXPECT_EQ(2U, list.GetSize());
201 201
202 local_list.clear(); 202 local_list.clear();
203 EXPECT_EQ( 203 EXPECT_EQ(
204 MetricsLogSerializer::LIST_SIZE_TOO_SMALL, 204 MetricsLogSerializer::LIST_SIZE_TOO_SMALL,
205 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); 205 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
206 } 206 }
207 207
208 // Remove size from the stored list. 208 // Remove size from the stored list.
209 TEST(MetricsLogSerializerTest, RemoveSizeFromLogList) { 209 TEST(MetricsLogSerializerTest, RemoveSizeFromLogList) {
210 ListValue list; 210 base::ListValue list;
211 211
212 std::vector<MetricsLogManager::SerializedLog> local_list(2); 212 std::vector<MetricsLogManager::SerializedLog> local_list(2);
213 SetLogText("one", &local_list[0]); 213 SetLogText("one", &local_list[0]);
214 SetLogText("two", &local_list[1]); 214 SetLogText("two", &local_list[1]);
215 EXPECT_EQ(2U, local_list.size()); 215 EXPECT_EQ(2U, local_list.size());
216 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, 216 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
217 kLogByteLimit, &list); 217 kLogByteLimit, &list);
218 EXPECT_EQ(4U, list.GetSize()); 218 EXPECT_EQ(4U, list.GetSize());
219 219
220 list.Remove(0, NULL); // Delete size (1st element). 220 list.Remove(0, NULL); // Delete size (1st element).
221 EXPECT_EQ(3U, list.GetSize()); 221 EXPECT_EQ(3U, list.GetSize());
222 222
223 local_list.clear(); 223 local_list.clear();
224 EXPECT_EQ( 224 EXPECT_EQ(
225 MetricsLogSerializer::LIST_SIZE_MISSING, 225 MetricsLogSerializer::LIST_SIZE_MISSING,
226 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); 226 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
227 } 227 }
228 228
229 // Corrupt size of stored list. 229 // Corrupt size of stored list.
230 TEST(MetricsLogSerializerTest, CorruptSizeOfLogList) { 230 TEST(MetricsLogSerializerTest, CorruptSizeOfLogList) {
231 ListValue list; 231 base::ListValue list;
232 232
233 std::vector<MetricsLogManager::SerializedLog> local_list(1); 233 std::vector<MetricsLogManager::SerializedLog> local_list(1);
234 SetLogText("Hello world!", &local_list[0]); 234 SetLogText("Hello world!", &local_list[0]);
235 235
236 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, 236 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
237 kLogByteLimit, &list); 237 kLogByteLimit, &list);
238 EXPECT_EQ(3U, list.GetSize()); 238 EXPECT_EQ(3U, list.GetSize());
239 239
240 // Change list size from 1 to 2. 240 // Change list size from 1 to 2.
241 EXPECT_TRUE(list.Set(0, Value::CreateIntegerValue(2))); 241 EXPECT_TRUE(list.Set(0, base::Value::CreateIntegerValue(2)));
242 EXPECT_EQ(3U, list.GetSize()); 242 EXPECT_EQ(3U, list.GetSize());
243 243
244 local_list.clear(); 244 local_list.clear();
245 EXPECT_EQ( 245 EXPECT_EQ(
246 MetricsLogSerializer::LIST_SIZE_CORRUPTION, 246 MetricsLogSerializer::LIST_SIZE_CORRUPTION,
247 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); 247 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
248 } 248 }
249 249
250 // Corrupt checksum of stored list. 250 // Corrupt checksum of stored list.
251 TEST(MetricsLogSerializerTest, CorruptChecksumOfLogList) { 251 TEST(MetricsLogSerializerTest, CorruptChecksumOfLogList) {
252 ListValue list; 252 base::ListValue list;
253 253
254 std::vector<MetricsLogManager::SerializedLog> local_list(1); 254 std::vector<MetricsLogManager::SerializedLog> local_list(1);
255 SetLogText("Hello world!", &local_list[0]); 255 SetLogText("Hello world!", &local_list[0]);
256 256
257 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit, 257 MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
258 kLogByteLimit, &list); 258 kLogByteLimit, &list);
259 EXPECT_EQ(3U, list.GetSize()); 259 EXPECT_EQ(3U, list.GetSize());
260 260
261 // Fetch checksum (last element) and change it. 261 // Fetch checksum (last element) and change it.
262 std::string checksum; 262 std::string checksum;
263 EXPECT_TRUE((*(list.end() - 1))->GetAsString(&checksum)); 263 EXPECT_TRUE((*(list.end() - 1))->GetAsString(&checksum));
264 checksum[0] = (checksum[0] == 'a') ? 'b' : 'a'; 264 checksum[0] = (checksum[0] == 'a') ? 'b' : 'a';
265 EXPECT_TRUE(list.Set(2, Value::CreateStringValue(checksum))); 265 EXPECT_TRUE(list.Set(2, base::Value::CreateStringValue(checksum)));
266 EXPECT_EQ(3U, list.GetSize()); 266 EXPECT_EQ(3U, list.GetSize());
267 267
268 local_list.clear(); 268 local_list.clear();
269 EXPECT_EQ( 269 EXPECT_EQ(
270 MetricsLogSerializer::CHECKSUM_CORRUPTION, 270 MetricsLogSerializer::CHECKSUM_CORRUPTION,
271 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); 271 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
272 } 272 }
OLDNEW
« no previous file with comments | « chrome/browser/metrics/metrics_log_serializer.cc ('k') | chrome/browser/metrics/metrics_log_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698