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
3// found in the LICENSE file.
4
5#include "chrome/common/metrics/metrics_log_base.h"
6
7#include <string>
8
9#include "base/base64.h"
10#include "base/format_macros.h"
11#include "base/strings/stringprintf.h"
12#include "chrome/common/metrics/proto/chrome_user_metrics_extension.pb.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15TEST(MetricsLogBaseTest, EmptyRecord) {
16  MetricsLogBase log("totally bogus client ID", 137, "bogus version");
17  log.set_hardware_class("sample-class");
18  log.CloseLog();
19
20  std::string encoded;
21  log.GetEncodedLog(&encoded);
22
23  // A couple of fields are hard to mock, so these will be copied over directly
24  // for the expected output.
25  metrics::ChromeUserMetricsExtension parsed;
26  ASSERT_TRUE(parsed.ParseFromString(encoded));
27
28  metrics::ChromeUserMetricsExtension expected;
29  expected.set_client_id(5217101509553811875);  // Hashed bogus client ID
30  expected.set_session_id(137);
31  expected.mutable_system_profile()->set_build_timestamp(
32      parsed.system_profile().build_timestamp());
33  expected.mutable_system_profile()->set_app_version("bogus version");
34  expected.mutable_system_profile()->set_channel(
35      parsed.system_profile().channel());
36  expected.mutable_system_profile()->mutable_hardware()->set_hardware_class(
37      "sample-class");
38
39  EXPECT_EQ(expected.SerializeAsString(), encoded);
40}
41
42// Make sure our ID hashes are the same as what we see on the server side.
43TEST(MetricsLogBaseTest, Hashes) {
44  static const struct {
45    std::string input;
46    std::string output;
47  } cases[] = {
48    {"Back", "0x0557fa923dcee4d0"},
49    {"Forward", "0x67d2f6740a8eaebf"},
50    {"NewTab", "0x290eb683f96572f1"},
51  };
52
53  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
54    uint64 hash = MetricsLogBase::Hash(cases[i].input);
55    std::string hash_hex = base::StringPrintf("0x%016" PRIx64, hash);
56    EXPECT_EQ(cases[i].output, hash_hex);
57  }
58};
59