summaryrefslogtreecommitdiffstats
path: root/chrome/browser/profile_resetter/triggered_profile_resetter_win_unittest.cc
blob: 8ff5ef7da8eaef95ac7f6871673b1ecf6dc9d174 (plain)
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
// Copyright 2015 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 "chrome/browser/profile_resetter/triggered_profile_resetter.h"

#include <stdint.h>

#include "base/bit_cast.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/test/test_reg_util_win.h"
#include "base/win/registry.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/profile_resetter/triggered_profile_resetter_factory.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "components/variations/entropy_provider.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::win::RegKey;

class TriggeredProfileResetterTest : public testing::Test {
 protected:
  void SetUp() override {
    override_manager_.OverrideRegistry(HKEY_CURRENT_USER);

    // Activate the triggered reset field trial for these tests.
    field_trial_list_.reset(
        new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo")));
    base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
        "TriggeredResetFieldTrial", "On");
    trial->group();
  }

  content::TestBrowserThreadBundle thread_bundle_;
  TestingProfile profile_;

  void SetRegTimestampAndToolName(const base::string16& toolname,
                                  FILETIME* file_time) {
    RegKey trigger_key(HKEY_CURRENT_USER, kTriggeredResetRegistryPath,
                       KEY_ALL_ACCESS);
    ASSERT_TRUE(trigger_key.Valid());
    FILETIME ft;
    ::GetSystemTimeAsFileTime(&ft);
    ASSERT_TRUE(trigger_key.WriteValue(kTriggeredResetTimestamp, &ft,
                                       sizeof(ft), REG_QWORD) == ERROR_SUCCESS);
    ASSERT_TRUE(trigger_key.WriteValue(kTriggeredResetToolName,
                                       toolname.c_str()) == ERROR_SUCCESS);
    if (file_time)
      *file_time = ft;
  }

 private:
  registry_util::RegistryOverrideManager override_manager_;
  scoped_ptr<base::FieldTrialList> field_trial_list_;
};

TEST_F(TriggeredProfileResetterTest, HasResetTriggerAndClear) {
  SetRegTimestampAndToolName(base::string16(), nullptr);
  TriggeredProfileResetter triggered_profile_resetter(&profile_);
  triggered_profile_resetter.Activate();
  EXPECT_TRUE(triggered_profile_resetter.HasResetTrigger());
  triggered_profile_resetter.ClearResetTrigger();
  EXPECT_FALSE(triggered_profile_resetter.HasResetTrigger());
}

TEST_F(TriggeredProfileResetterTest, HasDuplicateResetTrigger) {
  FILETIME ft = {};
  SetRegTimestampAndToolName(base::string16(), &ft);
  profile_.GetPrefs()->SetInt64(prefs::kLastProfileResetTimestamp,
                                bit_cast<int64_t, FILETIME>(ft));

  TriggeredProfileResetter triggered_profile_resetter(&profile_);
  triggered_profile_resetter.Activate();
  EXPECT_FALSE(triggered_profile_resetter.HasResetTrigger());
}

TEST_F(TriggeredProfileResetterTest, HasToolName) {
  const wchar_t kToolName[] = L"ToolyMcTool";
  SetRegTimestampAndToolName(kToolName, nullptr);
  TriggeredProfileResetter triggered_profile_resetter(&profile_);
  triggered_profile_resetter.Activate();
  EXPECT_TRUE(triggered_profile_resetter.HasResetTrigger());
  EXPECT_STREQ(kToolName,
               triggered_profile_resetter.GetResetToolName().c_str());
}

TEST_F(TriggeredProfileResetterTest, HasLongToolName) {
  const wchar_t kLongToolName[] =
      L"ToolMcToolToolMcToolToolMcToolToolMcToolToolMcToolToolMcToolToolMcTool"
      L"ToolMcToolToolMcToolToolMcToolThisIsTheToolThatNeverEndsYesItGoesOnAnd"
      L"OnMyFriend";
  const wchar_t kExpectedToolName[] =
      L"ToolMcToolToolMcToolToolMcToolToolMcToolToolMcToolToolMcToolToolMcTool"
      L"ToolMcToolToolMcToolToolMcTool";
  SetRegTimestampAndToolName(kLongToolName, nullptr);
  TriggeredProfileResetter triggered_profile_resetter(&profile_);
  triggered_profile_resetter.Activate();
  EXPECT_TRUE(triggered_profile_resetter.HasResetTrigger());
  EXPECT_STREQ(kExpectedToolName,
               triggered_profile_resetter.GetResetToolName().c_str());
}