Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/work_item.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 using base::win::RegKey; | |
| 15 | |
| 16 class RegistryKeyBackupTest : public testing::Test { | |
| 17 protected: | |
| 18 static const HKEY kRootKey; | |
| 19 | |
| 20 static void SetUpTestCase() { | |
|
erikwright (departed)
2011/09/15 18:38:57
Same questions apply about SetUpTestCase vs. SetUp
grt (UTC plus 2)
2011/09/16 17:45:45
Done.
| |
| 21 // Make some strings that we'll use. | |
| 22 temp_key_path_ = new std::wstring(L"SOFTWARE\\TmpTmp"); | |
| 23 dest_key_path_ = new std::wstring(*temp_key_path_ + L"\\Destination"); | |
| 24 non_empty_key_path_ = new std::wstring(*temp_key_path_ + L"\\NonEmptyKey"); | |
| 25 | |
| 26 // Put some data in the registry that the individual tests can use. | |
| 27 RegKey key; | |
| 28 ASSERT_EQ(ERROR_SUCCESS, | |
| 29 key.Create(kRootKey, non_empty_key_path_->c_str(), KEY_WRITE)); | |
| 30 ASSERT_EQ(ERROR_SUCCESS, | |
| 31 key.WriteValue(NULL, non_empty_key_path_->c_str())); | |
| 32 ASSERT_EQ(ERROR_SUCCESS, key.CreateKey(L"Subkey", KEY_WRITE)); | |
| 33 ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(L"SomeValue", 1U)); | |
| 34 } | |
| 35 | |
| 36 static void TearDownTestCase() { | |
| 37 // Clean up the temporary key | |
| 38 RegKey key(kRootKey, L"", KEY_QUERY_VALUE); | |
| 39 ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(temp_key_path_->c_str())); | |
| 40 | |
| 41 delete temp_key_path_; | |
| 42 temp_key_path_ = NULL; | |
| 43 delete dest_key_path_; | |
| 44 dest_key_path_ = NULL; | |
| 45 delete non_empty_key_path_; | |
| 46 non_empty_key_path_ = NULL; | |
| 47 | |
| 48 logging::CloseLogFile(); | |
| 49 } | |
| 50 | |
| 51 static std::wstring* temp_key_path_; | |
| 52 static std::wstring* dest_key_path_; | |
| 53 static std::wstring* non_empty_key_path_; | |
| 54 }; | |
| 55 | |
| 56 const HKEY RegistryKeyBackupTest::kRootKey = HKEY_CURRENT_USER; | |
| 57 std::wstring* RegistryKeyBackupTest::temp_key_path_ = NULL; | |
| 58 std::wstring* RegistryKeyBackupTest::dest_key_path_ = NULL; | |
| 59 std::wstring* RegistryKeyBackupTest::non_empty_key_path_ = NULL; | |
| 60 | |
| 61 // Test that initialzing a backup with an invalid RegKey returns false. | |
| 62 TEST_F(RegistryKeyBackupTest, InvalidKey) { | |
| 63 RegKey invalid; | |
| 64 RegistryKeyBackup backup; | |
| 65 | |
| 66 ASSERT_FALSE(invalid.Valid()); // Santiy check. | |
| 67 EXPECT_FALSE(backup.Initialize(invalid)); | |
| 68 } | |
| 69 | |
| 70 // Test that writing an empty backup to the registry doesn't modify anything. | |
| 71 TEST_F(RegistryKeyBackupTest, WriteEmptyBackup) { | |
| 72 RegKey dest; | |
| 73 RegistryKeyBackup backup; | |
| 74 | |
| 75 // |backup| is empty, so writing must not modify the registry. | |
| 76 EXPECT_EQ(ERROR_SUCCESS, | |
| 77 dest.Create(kRootKey, dest_key_path_->c_str(), | |
| 78 KEY_READ | KEY_WRITE)); | |
| 79 EXPECT_TRUE(backup.WriteTo(&dest)); | |
| 80 DWORD num_subkeys = 0; | |
| 81 DWORD num_values = 0; | |
| 82 EXPECT_EQ(ERROR_SUCCESS, | |
| 83 RegQueryInfoKey(dest.Handle(), NULL, NULL, NULL, &num_subkeys, | |
| 84 NULL, NULL, &num_values, NULL, NULL, NULL, NULL)); | |
| 85 EXPECT_EQ(0UL, num_subkeys); | |
| 86 EXPECT_EQ(0UL, num_values); | |
| 87 } | |
| 88 | |
| 89 // Test that reading some data then writing it out does the right thing. | |
| 90 TEST_F(RegistryKeyBackupTest, ReadWrite) { | |
| 91 RegKey key; | |
| 92 RegistryKeyBackup backup; | |
| 93 | |
| 94 EXPECT_EQ(ERROR_SUCCESS, | |
| 95 key.Open(kRootKey, non_empty_key_path_->c_str(), KEY_READ)); | |
| 96 EXPECT_TRUE(backup.Initialize(key)); | |
| 97 EXPECT_EQ(ERROR_SUCCESS, | |
| 98 key.Create(kRootKey, dest_key_path_->c_str(), | |
| 99 KEY_READ | KEY_WRITE)); | |
| 100 EXPECT_TRUE(backup.WriteTo(&key)); | |
| 101 std::wstring str_value; | |
| 102 EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(NULL, &str_value)); | |
| 103 EXPECT_EQ(*non_empty_key_path_, str_value); | |
| 104 EXPECT_EQ(ERROR_SUCCESS, key.OpenKey(L"Subkey", KEY_READ)); | |
| 105 DWORD dw_value = 0; | |
| 106 EXPECT_EQ(ERROR_SUCCESS, key.ReadValueDW(L"SomeValue", &dw_value)); | |
| 107 EXPECT_EQ(1U, dw_value); | |
| 108 } | |
| OLD | NEW |