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 "chrome/installer/util/registry_test_data.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/win/registry.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 using base::win::RegKey;
     12 
     13 RegistryTestData::RegistryTestData()
     14     : root_key_(NULL) {
     15 }
     16 
     17 RegistryTestData::~RegistryTestData() {
     18   Reset();
     19 }
     20 
     21 // static
     22 bool RegistryTestData::DeleteKey(HKEY root_key, const wchar_t* path) {
     23   LONG result = ERROR_SUCCESS;
     24   if (root_key != NULL && path != NULL && *path != L'\0') {
     25     result = RegKey(root_key, L"", KEY_QUERY_VALUE).DeleteKey(path);
     26     if (result == ERROR_FILE_NOT_FOUND) {
     27       result = ERROR_SUCCESS;
     28     } else {
     29       DLOG_IF(DFATAL, result != ERROR_SUCCESS)
     30           << "Failed to delete registry key " << path << ", result: " << result;
     31     }
     32   }
     33   return result == ERROR_SUCCESS;
     34 }
     35 
     36 void RegistryTestData::Reset() {
     37   // Clean up behind a previous use (ignore failures).
     38   DeleteKey(root_key_, base_path_.c_str());
     39 
     40   // Forget state.
     41   root_key_ = NULL;
     42   base_path_.clear();
     43   empty_key_path_.clear();
     44   non_empty_key_path_.clear();
     45 }
     46 
     47 bool RegistryTestData::Initialize(HKEY root_key, const wchar_t* base_path) {
     48   LONG result = ERROR_SUCCESS;
     49 
     50   Reset();
     51 
     52   // Take over the new registry location.
     53   if (DeleteKey(root_key, base_path)) {
     54     // Take on the new values.
     55     root_key_ = root_key;
     56     base_path_.assign(base_path);
     57 
     58     // Create our data.
     59     empty_key_path_.assign(base_path_).append(L"\\EmptyKey");
     60     non_empty_key_path_.assign(base_path_).append(L"\\NonEmptyKey");
     61 
     62     RegKey key;
     63 
     64     result = key.Create(root_key_, empty_key_path_.c_str(), KEY_QUERY_VALUE);
     65     if (result == ERROR_SUCCESS)
     66       result = key.Create(root_key_, non_empty_key_path_.c_str(), KEY_WRITE);
     67     if (result == ERROR_SUCCESS)
     68       result = key.WriteValue(NULL, non_empty_key_path_.c_str());
     69     if (result == ERROR_SUCCESS)
     70       result = key.CreateKey(L"SubKey", KEY_WRITE);
     71     if (result == ERROR_SUCCESS)
     72       result = key.WriteValue(L"SomeValue", 1UL);
     73     DLOG_IF(DFATAL, result != ERROR_SUCCESS)
     74         << "Failed to create test registry data based at " << base_path_
     75         << ", result: " << result;
     76   } else {
     77     result = ERROR_INVALID_PARAMETER;
     78   }
     79 
     80   return result == ERROR_SUCCESS;
     81 }
     82 
     83 void RegistryTestData::ExpectMatchesNonEmptyKey(HKEY root_key,
     84                                                 const wchar_t* path) {
     85   RegKey key;
     86 
     87   EXPECT_EQ(ERROR_SUCCESS, key.Open(root_key, path, KEY_READ));
     88   std::wstring str_value;
     89   EXPECT_EQ(ERROR_SUCCESS, key.ReadValue(NULL, &str_value));
     90   EXPECT_EQ(non_empty_key_path_, str_value);
     91   EXPECT_EQ(ERROR_SUCCESS, key.OpenKey(L"Subkey", KEY_READ));
     92   DWORD dw_value = 0;
     93   EXPECT_EQ(ERROR_SUCCESS, key.ReadValueDW(L"SomeValue", &dw_value));
     94   EXPECT_EQ(1UL, dw_value);
     95 }
     96 
     97 // static
     98 void RegistryTestData::ExpectEmptyKey(HKEY root_key, const wchar_t* path) {
     99   DWORD num_subkeys = 0;
    100   DWORD num_values = 0;
    101   RegKey key;
    102   EXPECT_EQ(ERROR_SUCCESS, key.Open(root_key, path, KEY_READ));
    103   EXPECT_EQ(ERROR_SUCCESS,
    104             RegQueryInfoKey(key.Handle(), NULL, NULL, NULL, &num_subkeys,
    105                             NULL, NULL, &num_values, NULL, NULL, NULL, NULL));
    106   EXPECT_EQ(0UL, num_subkeys);
    107   EXPECT_EQ(0UL, num_values);
    108 }
    109