Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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 class MetricsLogSerializerTest : public ::testing::Test { | |
| 13 }; | |
| 14 } | |
| 15 | 12 |
| 16 static const size_t kMaxLocalListSize = 3; | 13 const size_t kMaxLocalListSize = 3; |
| 14 | |
| 15 } // namespace | |
| 16 | |
| 17 class MetricsLogSerializerTest : public ::testing::Test { | |
|
jar (doing other things)
2012/02/23 01:59:18
The class definition should still be inside the an
Ilya Sherman
2012/02/24 02:10:06
Somebody... and unfortunately, I don't remember wh
| |
| 18 }; | |
| 17 | 19 |
| 18 // Store and retrieve empty list. | 20 // Store and retrieve empty list. |
| 19 TEST(MetricsLogSerializerTest, EmptyLogList) { | 21 TEST(MetricsLogSerializerTest, EmptyLogList) { |
| 20 ListValue list; | 22 ListValue list; |
| 21 std::vector<std::string> local_list; | 23 std::vector<std::string> local_list; |
| 22 | 24 |
| 23 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, | 25 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, |
| 24 &list); | 26 &list); |
| 25 EXPECT_EQ(0U, list.GetSize()); | 27 EXPECT_EQ(0U, list.GetSize()); |
| 26 | 28 |
| 27 local_list.clear(); // ReadLogsFromPrefList() expects empty |local_list|. | 29 local_list.clear(); // ReadLogsFromPrefList() expects empty |local_list|. |
| 28 EXPECT_EQ(MetricsLogSerializer::LIST_EMPTY, | 30 EXPECT_EQ( |
| 29 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 31 MetricsLogSerializer::LIST_EMPTY, |
| 32 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | |
| 30 EXPECT_EQ(0U, local_list.size()); | 33 EXPECT_EQ(0U, local_list.size()); |
| 31 } | 34 } |
| 32 | 35 |
| 33 // Store and retrieve a single log value. | 36 // Store and retrieve a single log value. |
| 34 TEST(MetricsLogSerializerTest, SingleElementLogList) { | 37 TEST(MetricsLogSerializerTest, SingleElementLogList) { |
| 35 ListValue list; | 38 ListValue list; |
| 36 std::vector<std::string> local_list; | 39 std::vector<std::string> local_list; |
| 37 | 40 |
| 38 local_list.push_back("Hello world!"); | 41 local_list.push_back("Hello world!"); |
| 39 EXPECT_EQ(1U, local_list.size()); | 42 EXPECT_EQ(1U, local_list.size()); |
| 40 | 43 |
| 41 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, | 44 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, |
| 42 &list); | 45 &list); |
| 43 | 46 |
| 44 // |list| will now contain the following: | 47 // |list| will now contain the following: |
| 45 // [1, Base64Encode("Hello world!"), MD5("Hello world!")]. | 48 // [1, Base64Encode("Hello world!"), MD5("Hello world!")]. |
| 46 EXPECT_EQ(3U, list.GetSize()); | 49 ASSERT_EQ(3U, list.GetSize()); |
| 47 | 50 |
| 48 // Examine each element. | 51 // Examine each element. |
| 49 ListValue::const_iterator it = list.begin(); | 52 ListValue::const_iterator it = list.begin(); |
| 50 int size = 0; | 53 int size = 0; |
| 51 (*it)->GetAsInteger(&size); | 54 (*it)->GetAsInteger(&size); |
| 52 EXPECT_EQ(1, size); | 55 EXPECT_EQ(1, size); |
| 53 | 56 |
| 54 ++it; | 57 ++it; |
| 55 std::string str; | 58 std::string str; |
| 56 (*it)->GetAsString(&str); // Base64 encoded "Hello world!" string. | 59 (*it)->GetAsString(&str); // Base64 encoded "Hello world!" string. |
| 57 std::string encoded; | 60 std::string encoded; |
| 58 base::Base64Encode("Hello world!", &encoded); | 61 base::Base64Encode("Hello world!", &encoded); |
| 59 EXPECT_TRUE(encoded == str); | 62 EXPECT_TRUE(encoded == str); |
| 60 | 63 |
| 61 ++it; | 64 ++it; |
| 62 (*it)->GetAsString(&str); // MD5 for encoded "Hello world!" string. | 65 (*it)->GetAsString(&str); // MD5 for encoded "Hello world!" string. |
| 63 EXPECT_TRUE(base::MD5String(encoded) == str); | 66 EXPECT_TRUE(base::MD5String(encoded) == str); |
| 64 | 67 |
| 65 ++it; | 68 ++it; |
| 66 EXPECT_TRUE(it == list.end()); // Reached end of list. | 69 EXPECT_TRUE(it == list.end()); // Reached end of list. |
| 67 | 70 |
| 68 local_list.clear(); | 71 local_list.clear(); |
| 69 EXPECT_EQ(MetricsLogSerializer::RECALL_SUCCESS, | 72 EXPECT_EQ( |
| 70 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 73 MetricsLogSerializer::RECALL_SUCCESS, |
| 74 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | |
| 71 EXPECT_EQ(1U, local_list.size()); | 75 EXPECT_EQ(1U, local_list.size()); |
| 72 } | 76 } |
| 73 | 77 |
| 74 // Store elements greater than the limit. | 78 // Store elements greater than the limit. |
| 75 TEST(MetricsLogSerializerTest, OverLimitLogList) { | 79 TEST(MetricsLogSerializerTest, OverLimitLogList) { |
| 76 ListValue list; | 80 ListValue list; |
| 77 std::vector<std::string> local_list; | 81 std::vector<std::string> local_list; |
| 78 | 82 |
| 79 local_list.push_back("one"); | 83 local_list.push_back("one"); |
| 80 local_list.push_back("two"); | 84 local_list.push_back("two"); |
| 81 local_list.push_back("three"); | 85 local_list.push_back("three"); |
| 82 local_list.push_back("four"); | 86 local_list.push_back("four"); |
| 83 EXPECT_EQ(4U, local_list.size()); | 87 EXPECT_EQ(4U, local_list.size()); |
| 84 | 88 |
| 85 std::string expected_first; | 89 std::string expected_first; |
| 86 base::Base64Encode(local_list[local_list.size() - kMaxLocalListSize], | 90 base::Base64Encode(local_list[local_list.size() - kMaxLocalListSize], |
| 87 &expected_first); | 91 &expected_first); |
| 88 std::string expected_last; | 92 std::string expected_last; |
| 89 base::Base64Encode(local_list[local_list.size() - 1], | 93 base::Base64Encode(local_list[local_list.size() - 1], |
| 90 &expected_last); | 94 &expected_last); |
| 91 | 95 |
| 92 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, | 96 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, |
| 93 &list); | 97 &list); |
| 94 EXPECT_EQ(kMaxLocalListSize + 2, list.GetSize()); | 98 EXPECT_EQ(kMaxLocalListSize + 2, list.GetSize()); |
| 95 | 99 |
| 96 std::string actual_first; | 100 std::string actual_first; |
| 97 EXPECT_TRUE((*(list.begin() + 1))->GetAsString(&actual_first)); | 101 EXPECT_TRUE((*(list.begin() + 1))->GetAsString(&actual_first)); |
| 98 EXPECT_TRUE(expected_first == actual_first); | 102 EXPECT_EQ(expected_first, actual_first); |
| 99 | 103 |
| 100 std::string actual_last; | 104 std::string actual_last; |
| 101 EXPECT_TRUE((*(list.end() - 2))->GetAsString(&actual_last)); | 105 EXPECT_TRUE((*(list.end() - 2))->GetAsString(&actual_last)); |
| 102 EXPECT_TRUE(expected_last == actual_last); | 106 EXPECT_EQ(expected_last, actual_last); |
| 103 | 107 |
| 104 local_list.clear(); | 108 local_list.clear(); |
| 105 EXPECT_EQ(MetricsLogSerializer::RECALL_SUCCESS, | 109 EXPECT_EQ( |
| 106 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 110 MetricsLogSerializer::RECALL_SUCCESS, |
| 111 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | |
| 107 EXPECT_EQ(kMaxLocalListSize, local_list.size()); | 112 EXPECT_EQ(kMaxLocalListSize, local_list.size()); |
| 108 } | 113 } |
| 109 | 114 |
| 110 // Induce LIST_SIZE_TOO_SMALL corruption | 115 // Induce LIST_SIZE_TOO_SMALL corruption |
| 111 TEST(MetricsLogSerializerTest, SmallRecoveredListSize) { | 116 TEST(MetricsLogSerializerTest, SmallRecoveredListSize) { |
| 112 ListValue list; | 117 ListValue list; |
| 113 std::vector<std::string> local_list; | 118 std::vector<std::string> local_list; |
| 114 | 119 |
| 115 local_list.push_back("Hello world!"); | 120 local_list.push_back("Hello world!"); |
| 116 EXPECT_EQ(1U, local_list.size()); | 121 EXPECT_EQ(1U, local_list.size()); |
| 117 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, | 122 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, |
| 118 &list); | 123 &list); |
| 119 EXPECT_EQ(3U, list.GetSize()); | 124 EXPECT_EQ(3U, list.GetSize()); |
| 120 | 125 |
| 121 // Remove last element. | 126 // Remove last element. |
| 122 list.Remove(list.GetSize() - 1, NULL); | 127 list.Remove(list.GetSize() - 1, NULL); |
| 123 EXPECT_EQ(2U, list.GetSize()); | 128 EXPECT_EQ(2U, list.GetSize()); |
| 124 | 129 |
| 125 local_list.clear(); | 130 local_list.clear(); |
| 126 EXPECT_EQ(MetricsLogSerializer::LIST_SIZE_TOO_SMALL, | 131 EXPECT_EQ( |
| 127 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 132 MetricsLogSerializer::LIST_SIZE_TOO_SMALL, |
| 133 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | |
| 128 } | 134 } |
| 129 | 135 |
| 130 // Remove size from the stored list. | 136 // Remove size from the stored list. |
| 131 TEST(MetricsLogSerializerTest, RemoveSizeFromLogList) { | 137 TEST(MetricsLogSerializerTest, RemoveSizeFromLogList) { |
| 132 ListValue list; | 138 ListValue list; |
| 133 std::vector<std::string> local_list; | 139 std::vector<std::string> local_list; |
| 134 | 140 |
| 135 local_list.push_back("one"); | 141 local_list.push_back("one"); |
| 136 local_list.push_back("two"); | 142 local_list.push_back("two"); |
| 137 EXPECT_EQ(2U, local_list.size()); | 143 EXPECT_EQ(2U, local_list.size()); |
| 138 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, | 144 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, |
| 139 &list); | 145 &list); |
| 140 EXPECT_EQ(4U, list.GetSize()); | 146 EXPECT_EQ(4U, list.GetSize()); |
| 141 | 147 |
| 142 list.Remove(0, NULL); // Delete size (1st element). | 148 list.Remove(0, NULL); // Delete size (1st element). |
| 143 EXPECT_EQ(3U, list.GetSize()); | 149 EXPECT_EQ(3U, list.GetSize()); |
| 144 | 150 |
| 145 local_list.clear(); | 151 local_list.clear(); |
| 146 EXPECT_EQ(MetricsLogSerializer::LIST_SIZE_MISSING, | 152 EXPECT_EQ( |
| 147 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 153 MetricsLogSerializer::LIST_SIZE_MISSING, |
| 154 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | |
| 148 } | 155 } |
| 149 | 156 |
| 150 // Corrupt size of stored list. | 157 // Corrupt size of stored list. |
| 151 TEST(MetricsLogSerializerTest, CorruptSizeOfLogList) { | 158 TEST(MetricsLogSerializerTest, CorruptSizeOfLogList) { |
| 152 ListValue list; | 159 ListValue list; |
| 153 std::vector<std::string> local_list; | 160 std::vector<std::string> local_list; |
| 154 | 161 |
| 155 local_list.push_back("Hello world!"); | 162 local_list.push_back("Hello world!"); |
| 156 EXPECT_EQ(1U, local_list.size()); | 163 EXPECT_EQ(1U, local_list.size()); |
| 157 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, | 164 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, |
| 158 &list); | 165 &list); |
| 159 EXPECT_EQ(3U, list.GetSize()); | 166 EXPECT_EQ(3U, list.GetSize()); |
| 160 | 167 |
| 161 // Change list size from 1 to 2. | 168 // Change list size from 1 to 2. |
| 162 EXPECT_TRUE(list.Set(0, Value::CreateIntegerValue(2))); | 169 EXPECT_TRUE(list.Set(0, Value::CreateIntegerValue(2))); |
| 163 EXPECT_EQ(3U, list.GetSize()); | 170 EXPECT_EQ(3U, list.GetSize()); |
| 164 | 171 |
| 165 local_list.clear(); | 172 local_list.clear(); |
| 166 EXPECT_EQ(MetricsLogSerializer::LIST_SIZE_CORRUPTION, | 173 EXPECT_EQ( |
| 167 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 174 MetricsLogSerializer::LIST_SIZE_CORRUPTION, |
| 175 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | |
| 168 } | 176 } |
| 169 | 177 |
| 170 // Corrupt checksum of stored list. | 178 // Corrupt checksum of stored list. |
| 171 TEST(MetricsLogSerializerTest, CorruptChecksumOfLogList) { | 179 TEST(MetricsLogSerializerTest, CorruptChecksumOfLogList) { |
| 172 ListValue list; | 180 ListValue list; |
| 173 std::vector<std::string> local_list; | 181 std::vector<std::string> local_list; |
| 174 | 182 |
| 175 local_list.clear(); | 183 local_list.clear(); |
| 176 local_list.push_back("Hello world!"); | 184 local_list.push_back("Hello world!"); |
| 177 EXPECT_EQ(1U, local_list.size()); | 185 EXPECT_EQ(1U, local_list.size()); |
| 178 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, | 186 MetricsLogSerializer::WriteLogsToPrefList(local_list, kMaxLocalListSize, |
| 179 &list); | 187 &list); |
| 180 EXPECT_EQ(3U, list.GetSize()); | 188 EXPECT_EQ(3U, list.GetSize()); |
| 181 | 189 |
| 182 // Fetch checksum (last element) and change it. | 190 // Fetch checksum (last element) and change it. |
| 183 std::string checksum; | 191 std::string checksum; |
| 184 EXPECT_TRUE((*(list.end() - 1))->GetAsString(&checksum)); | 192 EXPECT_TRUE((*(list.end() - 1))->GetAsString(&checksum)); |
| 185 checksum[0] = (checksum[0] == 'a') ? 'b' : 'a'; | 193 checksum[0] = (checksum[0] == 'a') ? 'b' : 'a'; |
| 186 EXPECT_TRUE(list.Set(2, Value::CreateStringValue(checksum))); | 194 EXPECT_TRUE(list.Set(2, Value::CreateStringValue(checksum))); |
| 187 EXPECT_EQ(3U, list.GetSize()); | 195 EXPECT_EQ(3U, list.GetSize()); |
| 188 | 196 |
| 189 local_list.clear(); | 197 local_list.clear(); |
| 190 EXPECT_EQ(MetricsLogSerializer::CHECKSUM_CORRUPTION, | 198 EXPECT_EQ( |
| 191 MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list)); | 199 MetricsLogSerializer::CHECKSUM_CORRUPTION, |
| 200 MetricsLogSerializer::ReadLogsFromPrefList(list, true, &local_list)); | |
| 192 } | 201 } |
| OLD | NEW |