• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 <windows.h>
6 
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/win/registry.h"
10 #include "chrome/installer/util/registry_key_backup.h"
11 #include "chrome/installer/util/registry_test_data.h"
12 #include "chrome/installer/util/work_item.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 using base::win::RegKey;
16 
17 class RegistryKeyBackupTest : public testing::Test {
18  protected:
TearDownTestCase()19   static void TearDownTestCase() {
20     logging::CloseLogFile();
21   }
22 
SetUp()23   virtual void SetUp() {
24     ASSERT_TRUE(test_data_.Initialize(HKEY_CURRENT_USER, L"SOFTWARE\\TmpTmp"));
25     destination_path_.assign(test_data_.base_path()).append(L"\\Destination");
26   }
27 
28   RegistryTestData test_data_;
29   std::wstring destination_path_;
30 };
31 
32 // Test that writing an uninitialized backup does nothing.
TEST_F(RegistryKeyBackupTest,Uninitialized)33 TEST_F(RegistryKeyBackupTest, Uninitialized) {
34   RegistryKeyBackup backup;
35 
36   EXPECT_TRUE(backup.WriteTo(test_data_.root_key(), destination_path_.c_str()));
37   EXPECT_FALSE(RegKey(test_data_.root_key(), destination_path_.c_str(),
38                       KEY_READ).Valid());
39 }
40 
41 // Test that initializing a backup with a non-existent key works, and that
42 // writing it back out does nothing.
TEST_F(RegistryKeyBackupTest,MissingKey)43 TEST_F(RegistryKeyBackupTest, MissingKey) {
44   std::wstring non_existent_key_path(test_data_.base_path() + L"\\NoKeyHere");
45   RegistryKeyBackup backup;
46 
47   EXPECT_TRUE(backup.Initialize(test_data_.root_key(),
48                                 non_existent_key_path.c_str()));
49   EXPECT_TRUE(backup.WriteTo(test_data_.root_key(), destination_path_.c_str()));
50   EXPECT_FALSE(RegKey(test_data_.root_key(), destination_path_.c_str(),
51                       KEY_READ).Valid());
52 }
53 
54 // Test that reading some data then writing it out does the right thing.
TEST_F(RegistryKeyBackupTest,ReadWrite)55 TEST_F(RegistryKeyBackupTest, ReadWrite) {
56   RegistryKeyBackup backup;
57 
58   EXPECT_TRUE(backup.Initialize(test_data_.root_key(),
59                                 test_data_.non_empty_key_path().c_str()));
60   EXPECT_TRUE(backup.WriteTo(test_data_.root_key(), destination_path_.c_str()));
61   test_data_.ExpectMatchesNonEmptyKey(test_data_.root_key(),
62                                       destination_path_.c_str());
63 }
64 
65 // Test that reading some data, swapping, then writing it out does the right
66 // thing.
TEST_F(RegistryKeyBackupTest,Swap)67 TEST_F(RegistryKeyBackupTest, Swap) {
68   RegistryKeyBackup backup;
69   RegistryKeyBackup other_backup;
70 
71   EXPECT_TRUE(backup.Initialize(test_data_.root_key(),
72                                 test_data_.non_empty_key_path().c_str()));
73   backup.swap(other_backup);
74   EXPECT_TRUE(other_backup.WriteTo(test_data_.root_key(),
75                                    destination_path_.c_str()));
76 
77   // Now make sure the one we started with is truly empty.
78   EXPECT_EQ(ERROR_SUCCESS,
79             RegKey(test_data_.root_key(), L"", KEY_QUERY_VALUE)
80                 .DeleteKey(destination_path_.c_str()));
81   EXPECT_TRUE(backup.WriteTo(test_data_.root_key(),
82                              destination_path_.c_str()));
83   EXPECT_FALSE(RegKey(test_data_.root_key(), destination_path_.c_str(),
84                       KEY_READ).Valid());
85 }
86