Home | History | Annotate | Download | only in util
      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:
     19   static void TearDownTestCase() {
     20     logging::CloseLogFile();
     21   }
     22 
     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.
     33 TEST_F(RegistryKeyBackupTest, Uninitialized) {
     34   RegistryKeyBackup backup;
     35 
     36   EXPECT_TRUE(backup.WriteTo(test_data_.root_key(),
     37                              destination_path_.c_str(),
     38                              WorkItem::kWow64Default));
     39   EXPECT_FALSE(RegKey(test_data_.root_key(), destination_path_.c_str(),
     40                       KEY_READ).Valid());
     41 }
     42 
     43 // Test that initializing a backup with a non-existent key works, and that
     44 // writing it back out does nothing.
     45 TEST_F(RegistryKeyBackupTest, MissingKey) {
     46   std::wstring non_existent_key_path(test_data_.base_path() + L"\\NoKeyHere");
     47   RegistryKeyBackup backup;
     48 
     49   EXPECT_TRUE(backup.Initialize(test_data_.root_key(),
     50                                 non_existent_key_path.c_str(),
     51                                 WorkItem::kWow64Default));
     52   EXPECT_TRUE(backup.WriteTo(test_data_.root_key(),
     53                              destination_path_.c_str(),
     54                              WorkItem::kWow64Default));
     55   EXPECT_FALSE(RegKey(test_data_.root_key(), destination_path_.c_str(),
     56                       KEY_READ).Valid());
     57 }
     58 
     59 // Test that reading some data then writing it out does the right thing.
     60 TEST_F(RegistryKeyBackupTest, ReadWrite) {
     61   RegistryKeyBackup backup;
     62 
     63   EXPECT_TRUE(backup.Initialize(test_data_.root_key(),
     64                                 test_data_.non_empty_key_path().c_str(),
     65                                 WorkItem::kWow64Default));
     66   EXPECT_TRUE(backup.WriteTo(test_data_.root_key(),
     67                              destination_path_.c_str(),
     68                              WorkItem::kWow64Default));
     69   test_data_.ExpectMatchesNonEmptyKey(test_data_.root_key(),
     70                                       destination_path_.c_str());
     71 }
     72 
     73 // Test that reading some data, swapping, then writing it out does the right
     74 // thing.
     75 TEST_F(RegistryKeyBackupTest, Swap) {
     76   RegistryKeyBackup backup;
     77   RegistryKeyBackup other_backup;
     78 
     79   EXPECT_TRUE(backup.Initialize(test_data_.root_key(),
     80                                 test_data_.non_empty_key_path().c_str(),
     81                                 WorkItem::kWow64Default));
     82   backup.swap(other_backup);
     83   EXPECT_TRUE(other_backup.WriteTo(test_data_.root_key(),
     84                                    destination_path_.c_str(),
     85                                    WorkItem::kWow64Default));
     86 
     87   // Now make sure the one we started with is truly empty.
     88   EXPECT_EQ(ERROR_SUCCESS,
     89             RegKey(test_data_.root_key(), L"", KEY_QUERY_VALUE)
     90                 .DeleteKey(destination_path_.c_str()));
     91   EXPECT_TRUE(backup.WriteTo(test_data_.root_key(),
     92                              destination_path_.c_str(),
     93                              WorkItem::kWow64Default));
     94   EXPECT_FALSE(RegKey(test_data_.root_key(), destination_path_.c_str(),
     95                       KEY_READ).Valid());
     96 }
     97