Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: services/preferences/tracked/registry_hash_store_contents_win_unittest.cc

Issue 2926453002: Fix a race condition in ~RegistryHashStoreContentsWin (Closed)
Patch Set: Add file I forgot to git add Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "services/preferences/tracked/registry_hash_store_contents_win.h" 5 #include "services/preferences/tracked/registry_hash_store_contents_win.h"
6 6
7 #include "base/files/scoped_temp_dir.h"
7 #include "base/strings/string16.h" 8 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
9 #include "base/test/test_reg_util_win.h" 10 #include "base/test/test_reg_util_win.h"
10 #include "base/values.h" 11 #include "base/values.h"
11 #include "base/win/registry.h" 12 #include "base/win/registry.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace { 15 namespace {
15 16
16 constexpr base::char16 kRegistryPath[] = L"Foo\\TestStore"; 17 constexpr base::char16 kRegistryPath[] = L"Foo\\TestStore";
17 constexpr base::char16 kStoreKey[] = L"test_store_key"; 18 constexpr base::char16 kStoreKey[] = L"test_store_key";
18 19
19 // Hex-encoded MACs are 64 characters long. 20 // Hex-encoded MACs are 64 characters long.
20 constexpr char kTestStringA[] = 21 constexpr char kTestStringA[] =
21 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 22 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
22 constexpr char kTestStringB[] = 23 constexpr char kTestStringB[] =
23 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; 24 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
24 25
25 constexpr char kAtomicPrefPath[] = "path1"; 26 constexpr char kAtomicPrefPath[] = "path1";
26 constexpr char kSplitPrefPath[] = "extension"; 27 constexpr char kSplitPrefPath[] = "extension";
27 28
28 class RegistryHashStoreContentsWinTest : public testing::Test { 29 class RegistryHashStoreContentsWinTest : public testing::Test {
29 protected: 30 protected:
30 RegistryHashStoreContentsWinTest() {} 31 RegistryHashStoreContentsWinTest() {}
31 32
32 void SetUp() override { 33 void SetUp() override {
33 ASSERT_NO_FATAL_FAILURE( 34 ASSERT_NO_FATAL_FAILURE(
34 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER)); 35 registry_override_manager_.OverrideRegistry(HKEY_CURRENT_USER));
35 36
36 contents.reset(new RegistryHashStoreContentsWin(kRegistryPath, kStoreKey)); 37 contents.reset(
38 new RegistryHashStoreContentsWin(kRegistryPath, kStoreKey, nullptr));
37 } 39 }
38 40
39 std::unique_ptr<HashStoreContents> contents; 41 std::unique_ptr<HashStoreContents> contents;
40 42
41 private: 43 private:
42 registry_util::RegistryOverrideManager registry_override_manager_; 44 registry_util::RegistryOverrideManager registry_override_manager_;
43 45
44 DISALLOW_COPY_AND_ASSIGN(RegistryHashStoreContentsWinTest); 46 DISALLOW_COPY_AND_ASSIGN(RegistryHashStoreContentsWinTest);
45 }; 47 };
46 48
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 contents->Reset(); 113 contents->Reset();
112 114
113 stored_mac.clear(); 115 stored_mac.clear();
114 EXPECT_FALSE(contents->GetMac(kAtomicPrefPath, &stored_mac)); 116 EXPECT_FALSE(contents->GetMac(kAtomicPrefPath, &stored_mac));
115 EXPECT_TRUE(stored_mac.empty()); 117 EXPECT_TRUE(stored_mac.empty());
116 118
117 split_macs.clear(); 119 split_macs.clear();
118 EXPECT_FALSE(contents->GetSplitMacs(kSplitPrefPath, &split_macs)); 120 EXPECT_FALSE(contents->GetSplitMacs(kSplitPrefPath, &split_macs));
119 EXPECT_EQ(0U, split_macs.size()); 121 EXPECT_EQ(0U, split_macs.size());
120 } 122 }
123
124 TEST(RegistryHashStoreContentsWinScopedTest, TestScopedDirsCleared) {
125 RegistryHashStoreContentsWin verifying_contents(registry_path, kStoreKey,
126 nullptr);
127 std::string stored_mac;
128
129 base::ScopedTempDir temp_dir;
130 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
131 const base::string16 registry_path =
132 temp_dir.GetPath().DirName().BaseName().LossyDisplayName();
133
134 scoped_refptr<TempScopedDirRegistryCleaner> temp_scoped_dir_cleaner =
135 new TempScopedDirRegistryCleaner();
gab 2017/06/12 15:52:16 MakeRefCounted<TempScopedDirRegistryCleaner>()
proberge 2017/06/12 18:46:02 Done. Just out of curiosity, is this by convention
gab 2017/06/12 18:52:24 We try to avoid "new" as much as possible these da
136 RegistryHashStoreContentsWin* contentsA = new RegistryHashStoreContentsWin(
gab 2017/06/12 15:52:16 Use unique_ptr, MakeUnique, and unique_ptr::reset(
proberge 2017/06/12 18:46:02 Done.
137 registry_path, kStoreKey, temp_scoped_dir_cleaner);
138 RegistryHashStoreContentsWin* contentsB = new RegistryHashStoreContentsWin(
139 registry_path, kStoreKey, temp_scoped_dir_cleaner);
140
141 contentsA.SetMac(kAtomicPrefPath, kTestStringA);
142 contentsB.SetMac(kAtomicPrefPath, kTestStringB);
143
144 temp_scoped_dir_cleaner = nullptr;
145 EXPECT_TRUE(verifying_contents.GetMac(kAtomicPrefPath, &stored_mac));
146 EXPECT_EQ(kTestStringB, stored_mac);
147
148 delete contentsB;
149 EXPECT_TRUE(verifying_contents.GetMac(kAtomicPrefPath, &stored_mac));
150 EXPECT_EQ(kTestStringB, stored_mac);
151
152 delete contentsA;
153 EXPECT_FALSE(verifying_contents.GetMac(kAtomicPrefPath, &stored_mac));
154 }
gab 2017/06/12 15:52:16 Also make a test with a Copy() that runs on anothe
proberge 2017/06/12 18:46:02 Done (not sure what you mean by "with a Copy()", h
gab 2017/06/12 18:52:24 I meant using HashStoreContents::MakeCopy() just l
proberge 2017/06/12 19:30:24 Ah, gotcha. Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698