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