Home | History | Annotate | Download | only in win
      1 // Copyright (c) 2010 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 "base/win/registry.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 namespace base {
      9 namespace win {
     10 
     11 namespace {
     12 
     13 const wchar_t kRootKey[] = L"Base_Registry_Unittest";
     14 
     15 class RegistryTest : public testing::Test {
     16  public:
     17   RegistryTest() {}
     18 
     19  protected:
     20   virtual void SetUp() {
     21     // Create a temporary key.
     22     RegKey key(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS);
     23     key.DeleteKey(kRootKey);
     24     ASSERT_NE(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, kRootKey, KEY_READ));
     25     ASSERT_EQ(ERROR_SUCCESS, key.Create(HKEY_CURRENT_USER, kRootKey, KEY_READ));
     26   }
     27 
     28   virtual void TearDown() {
     29     // Clean up the temporary key.
     30     RegKey key(HKEY_CURRENT_USER, L"", KEY_SET_VALUE);
     31     ASSERT_EQ(ERROR_SUCCESS, key.DeleteKey(kRootKey));
     32   }
     33 
     34  private:
     35   DISALLOW_COPY_AND_ASSIGN(RegistryTest);
     36 };
     37 
     38 TEST_F(RegistryTest, ValueTest) {
     39   RegKey key;
     40 
     41   std::wstring foo_key(kRootKey);
     42   foo_key += L"\\Foo";
     43   ASSERT_EQ(ERROR_SUCCESS, key.Create(HKEY_CURRENT_USER, foo_key.c_str(),
     44                                       KEY_READ));
     45 
     46   {
     47     ASSERT_EQ(ERROR_SUCCESS, key.Open(HKEY_CURRENT_USER, foo_key.c_str(),
     48                                       KEY_READ | KEY_SET_VALUE));
     49     ASSERT_TRUE(key.Valid());
     50 
     51     const wchar_t* kStringValueName = L"StringValue";
     52     const wchar_t* kDWORDValueName = L"DWORDValue";
     53     const wchar_t* kInt64ValueName = L"Int64Value";
     54     const wchar_t* kStringData = L"string data";
     55     const DWORD kDWORDData = 0xdeadbabe;
     56     const int64 kInt64Data = 0xdeadbabedeadbabeLL;
     57 
     58     // Test value creation
     59     ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(kStringValueName, kStringData));
     60     ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(kDWORDValueName, kDWORDData));
     61     ASSERT_EQ(ERROR_SUCCESS, key.WriteValue(kInt64ValueName, &kInt64Data,
     62                                             sizeof(kInt64Data), REG_QWORD));
     63     EXPECT_EQ(3U, key.ValueCount());
     64     EXPECT_TRUE(key.ValueExists(kStringValueName));
     65     EXPECT_TRUE(key.ValueExists(kDWORDValueName));
     66     EXPECT_TRUE(key.ValueExists(kInt64ValueName));
     67 
     68     // Test Read
     69     std::wstring string_value;
     70     DWORD dword_value = 0;
     71     int64 int64_value = 0;
     72     ASSERT_EQ(ERROR_SUCCESS, key.ReadValue(kStringValueName, &string_value));
     73     ASSERT_EQ(ERROR_SUCCESS, key.ReadValueDW(kDWORDValueName, &dword_value));
     74     ASSERT_EQ(ERROR_SUCCESS, key.ReadInt64(kInt64ValueName, &int64_value));
     75     EXPECT_STREQ(kStringData, string_value.c_str());
     76     EXPECT_EQ(kDWORDData, dword_value);
     77     EXPECT_EQ(kInt64Data, int64_value);
     78 
     79     // Make sure out args are not touched if ReadValue fails
     80     const wchar_t* kNonExistent = L"NonExistent";
     81     ASSERT_NE(ERROR_SUCCESS, key.ReadValue(kNonExistent, &string_value));
     82     ASSERT_NE(ERROR_SUCCESS, key.ReadValueDW(kNonExistent, &dword_value));
     83     ASSERT_NE(ERROR_SUCCESS, key.ReadInt64(kNonExistent, &int64_value));
     84     EXPECT_STREQ(kStringData, string_value.c_str());
     85     EXPECT_EQ(kDWORDData, dword_value);
     86     EXPECT_EQ(kInt64Data, int64_value);
     87 
     88     // Test delete
     89     ASSERT_EQ(ERROR_SUCCESS, key.DeleteValue(kStringValueName));
     90     ASSERT_EQ(ERROR_SUCCESS, key.DeleteValue(kDWORDValueName));
     91     ASSERT_EQ(ERROR_SUCCESS, key.DeleteValue(kInt64ValueName));
     92     EXPECT_EQ(0U, key.ValueCount());
     93     EXPECT_FALSE(key.ValueExists(kStringValueName));
     94     EXPECT_FALSE(key.ValueExists(kDWORDValueName));
     95     EXPECT_FALSE(key.ValueExists(kInt64ValueName));
     96   }
     97 }
     98 
     99 }  // namespace
    100 
    101 }  // namespace win
    102 }  // namespace base
    103