This source file includes following definitions.
- SetLogText
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#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"
namespace {
const size_t kListLengthLimit = 3;
const size_t kLogByteLimit = 1000;
void SetLogText(const std::string& log_text,
MetricsLogManager::SerializedLog* log) {
std::string log_text_copy = log_text;
log->SwapLogText(&log_text_copy);
}
}
TEST(MetricsLogSerializerTest, EmptyLogList) {
base::ListValue list;
std::vector<MetricsLogManager::SerializedLog> local_list;
MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
kLogByteLimit, &list);
EXPECT_EQ(0U, list.GetSize());
local_list.clear();
EXPECT_EQ(
MetricsLogSerializer::LIST_EMPTY,
MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
EXPECT_EQ(0U, local_list.size());
}
TEST(MetricsLogSerializerTest, SingleElementLogList) {
base::ListValue list;
std::vector<MetricsLogManager::SerializedLog> local_list(1);
SetLogText("Hello world!", &local_list[0]);
MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
kLogByteLimit, &list);
ASSERT_EQ(3U, list.GetSize());
base::ListValue::const_iterator it = list.begin();
int size = 0;
(*it)->GetAsInteger(&size);
EXPECT_EQ(1, size);
++it;
std::string str;
(*it)->GetAsString(&str);
std::string encoded;
base::Base64Encode("Hello world!", &encoded);
EXPECT_TRUE(encoded == str);
++it;
(*it)->GetAsString(&str);
EXPECT_TRUE(base::MD5String(encoded) == str);
++it;
EXPECT_TRUE(it == list.end());
local_list.clear();
EXPECT_EQ(
MetricsLogSerializer::RECALL_SUCCESS,
MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
EXPECT_EQ(1U, local_list.size());
}
TEST(MetricsLogSerializerTest, LongButTinyLogList) {
base::ListValue list;
size_t log_count = kListLengthLimit * 5;
std::vector<MetricsLogManager::SerializedLog> local_list(log_count);
for (size_t i = 0; i < local_list.size(); ++i)
SetLogText("x", &local_list[i]);
MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
kLogByteLimit, &list);
std::vector<MetricsLogManager::SerializedLog> result_list;
EXPECT_EQ(
MetricsLogSerializer::RECALL_SUCCESS,
MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list));
EXPECT_EQ(local_list.size(), result_list.size());
EXPECT_TRUE(result_list.front().log_text().find("x") == 0);
}
TEST(MetricsLogSerializerTest, LongButSmallLogList) {
base::ListValue list;
size_t log_count = kListLengthLimit * 5;
std::vector<MetricsLogManager::SerializedLog> local_list(log_count);
size_t log_size = (kLogByteLimit / (log_count - 2)) + 2;
SetLogText("one", &local_list[0]);
SetLogText("two", &local_list[1]);
SetLogText("three", &local_list[2]);
SetLogText("last", &local_list[log_count - 1]);
for (size_t i = 0; i < local_list.size(); ++i) {
std::string log_text = local_list[i].log_text();
log_text.resize(log_size, ' ');
local_list[i].SwapLogText(&log_text);
}
MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
kLogByteLimit, &list);
std::vector<MetricsLogManager::SerializedLog> result_list;
EXPECT_EQ(
MetricsLogSerializer::RECALL_SUCCESS,
MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list));
EXPECT_EQ(local_list.size() - 2, result_list.size());
EXPECT_TRUE(result_list.front().log_text().find("three") == 0);
EXPECT_TRUE(result_list.back().log_text().find("last") == 0);
}
TEST(MetricsLogSerializerTest, ShortButLargeLogList) {
base::ListValue list;
std::vector<MetricsLogManager::SerializedLog> local_list(kListLengthLimit);
size_t log_size = (kLogByteLimit / local_list.size()) * 2;
for (size_t i = 0; i < local_list.size(); ++i) {
std::string log_text = local_list[i].log_text();
log_text.resize(log_size, ' ');
local_list[i].SwapLogText(&log_text);
}
MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
kLogByteLimit, &list);
std::vector<MetricsLogManager::SerializedLog> result_list;
EXPECT_EQ(
MetricsLogSerializer::RECALL_SUCCESS,
MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list));
EXPECT_EQ(local_list.size(), result_list.size());
}
TEST(MetricsLogSerializerTest, LongAndLargeLogList) {
base::ListValue list;
std::vector<MetricsLogManager::SerializedLog>
local_list(kListLengthLimit * 2);
size_t log_size = (kLogByteLimit / local_list.size()) * 4;
SetLogText("First to keep",
&local_list[local_list.size() - kListLengthLimit]);
for (size_t i = 0; i < local_list.size(); ++i) {
std::string log_text = local_list[i].log_text();
log_text.resize(log_size, ' ');
local_list[i].SwapLogText(&log_text);
}
MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
kLogByteLimit, &list);
std::vector<MetricsLogManager::SerializedLog> result_list;
EXPECT_EQ(
MetricsLogSerializer::RECALL_SUCCESS,
MetricsLogSerializer::ReadLogsFromPrefList(list, &result_list));
EXPECT_EQ(kListLengthLimit, result_list.size());
EXPECT_TRUE(result_list.front().log_text().find("First to keep") == 0);
}
TEST(MetricsLogSerializerTest, SmallRecoveredListSize) {
base::ListValue list;
std::vector<MetricsLogManager::SerializedLog> local_list(1);
SetLogText("Hello world!", &local_list[0]);
MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
kLogByteLimit, &list);
EXPECT_EQ(3U, list.GetSize());
list.Remove(list.GetSize() - 1, NULL);
EXPECT_EQ(2U, list.GetSize());
local_list.clear();
EXPECT_EQ(
MetricsLogSerializer::LIST_SIZE_TOO_SMALL,
MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
}
TEST(MetricsLogSerializerTest, RemoveSizeFromLogList) {
base::ListValue list;
std::vector<MetricsLogManager::SerializedLog> local_list(2);
SetLogText("one", &local_list[0]);
SetLogText("two", &local_list[1]);
EXPECT_EQ(2U, local_list.size());
MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
kLogByteLimit, &list);
EXPECT_EQ(4U, list.GetSize());
list.Remove(0, NULL);
EXPECT_EQ(3U, list.GetSize());
local_list.clear();
EXPECT_EQ(
MetricsLogSerializer::LIST_SIZE_MISSING,
MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
}
TEST(MetricsLogSerializerTest, CorruptSizeOfLogList) {
base::ListValue list;
std::vector<MetricsLogManager::SerializedLog> local_list(1);
SetLogText("Hello world!", &local_list[0]);
MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
kLogByteLimit, &list);
EXPECT_EQ(3U, list.GetSize());
EXPECT_TRUE(list.Set(0, base::Value::CreateIntegerValue(2)));
EXPECT_EQ(3U, list.GetSize());
local_list.clear();
EXPECT_EQ(
MetricsLogSerializer::LIST_SIZE_CORRUPTION,
MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
}
TEST(MetricsLogSerializerTest, CorruptChecksumOfLogList) {
base::ListValue list;
std::vector<MetricsLogManager::SerializedLog> local_list(1);
SetLogText("Hello world!", &local_list[0]);
MetricsLogSerializer::WriteLogsToPrefList(local_list, kListLengthLimit,
kLogByteLimit, &list);
EXPECT_EQ(3U, list.GetSize());
std::string checksum;
EXPECT_TRUE((*(list.end() - 1))->GetAsString(&checksum));
checksum[0] = (checksum[0] == 'a') ? 'b' : 'a';
EXPECT_TRUE(list.Set(2, base::Value::CreateStringValue(checksum)));
EXPECT_EQ(3U, list.GetSize());
local_list.clear();
EXPECT_EQ(
MetricsLogSerializer::CHECKSUM_CORRUPTION,
MetricsLogSerializer::ReadLogsFromPrefList(list, &local_list));
}