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