forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
com_init_balancer_unittest.cc
151 lines (122 loc) · 5.11 KB
/
com_init_balancer_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/win/com_init_balancer.h"
#include <shlobj.h>
#include <wrl/client.h>
#include "base/test/gtest_util.h"
#include "base/win/com_init_util.h"
#include "base/win/scoped_com_initializer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace win {
using Microsoft::WRL::ComPtr;
TEST(TestComInitBalancer, BalancedPairsWithComBalancerEnabled) {
{
// Assert COM has initialized correctly.
ScopedCOMInitializer com_initializer(
ScopedCOMInitializer::Uninitialization::kBlockPremature);
ASSERT_TRUE(com_initializer.Succeeded());
// Create COM object successfully.
ComPtr<IUnknown> shell_link;
HRESULT hr = ::CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_ALL,
IID_PPV_ARGS(&shell_link));
EXPECT_TRUE(SUCCEEDED(hr));
}
// ScopedCOMInitializer has gone out of scope and COM has been uninitialized.
EXPECT_DCHECK_DEATH(AssertComInitialized());
}
TEST(TestComInitBalancer, UnbalancedPairsWithComBalancerEnabled) {
{
// Assert COM has initialized correctly.
ScopedCOMInitializer com_initializer(
ScopedCOMInitializer::Uninitialization::kBlockPremature);
ASSERT_TRUE(com_initializer.Succeeded());
// Attempt to prematurely uninitialize the COM library.
::CoUninitialize();
::CoUninitialize();
// Assert COM is still initialized.
AssertComInitialized();
// Create COM object successfully.
ComPtr<IUnknown> shell_link;
HRESULT hr = ::CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_ALL,
IID_PPV_ARGS(&shell_link));
EXPECT_TRUE(SUCCEEDED(hr));
}
// ScopedCOMInitializer has gone out of scope and COM has been uninitialized.
EXPECT_DCHECK_DEATH(AssertComInitialized());
}
TEST(TestComInitBalancer, BalancedPairsWithComBalancerDisabled) {
{
// Assert COM has initialized correctly.
ScopedCOMInitializer com_initializer(
ScopedCOMInitializer::Uninitialization::kAllow);
ASSERT_TRUE(com_initializer.Succeeded());
// Create COM object successfully.
ComPtr<IUnknown> shell_link;
HRESULT hr = ::CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_ALL,
IID_PPV_ARGS(&shell_link));
EXPECT_TRUE(SUCCEEDED(hr));
}
// ScopedCOMInitializer has gone out of scope and COM has been uninitialized.
EXPECT_DCHECK_DEATH(AssertComInitialized());
}
TEST(TestComInitBalancer, UnbalancedPairsWithComBalancerDisabled) {
// Assert COM has initialized correctly.
ScopedCOMInitializer com_initializer(
ScopedCOMInitializer::Uninitialization::kAllow);
ASSERT_TRUE(com_initializer.Succeeded());
// Attempt to prematurely uninitialize the COM library.
::CoUninitialize();
::CoUninitialize();
// Assert COM is not initialized.
EXPECT_DCHECK_DEATH(AssertComInitialized());
// Create COM object unsuccessfully.
ComPtr<IUnknown> shell_link;
HRESULT hr = ::CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_ALL,
IID_PPV_ARGS(&shell_link));
EXPECT_TRUE(FAILED(hr));
EXPECT_EQ(CO_E_NOTINITIALIZED, hr);
}
TEST(TestComInitBalancer, OneRegisteredSpyRefCount) {
ScopedCOMInitializer com_initializer(
ScopedCOMInitializer::Uninitialization::kBlockPremature);
ASSERT_TRUE(com_initializer.Succeeded());
// Reference count should be 1 after initialization.
EXPECT_EQ(DWORD(1), com_initializer.GetCOMBalancerReferenceCountForTesting());
// Attempt to prematurely uninitialize the COM library.
::CoUninitialize();
// Expect reference count to remain at 1.
EXPECT_EQ(DWORD(1), com_initializer.GetCOMBalancerReferenceCountForTesting());
}
TEST(TestComInitBalancer, ThreeRegisteredSpiesRefCount) {
ScopedCOMInitializer com_initializer_1(
ScopedCOMInitializer::Uninitialization::kBlockPremature);
ScopedCOMInitializer com_initializer_2(
ScopedCOMInitializer::Uninitialization::kBlockPremature);
ScopedCOMInitializer com_initializer_3(
ScopedCOMInitializer::Uninitialization::kBlockPremature);
ASSERT_TRUE(com_initializer_1.Succeeded());
ASSERT_TRUE(com_initializer_2.Succeeded());
ASSERT_TRUE(com_initializer_3.Succeeded());
// Reference count should be 3 after initialization.
EXPECT_EQ(DWORD(3),
com_initializer_1.GetCOMBalancerReferenceCountForTesting());
EXPECT_EQ(DWORD(3),
com_initializer_2.GetCOMBalancerReferenceCountForTesting());
EXPECT_EQ(DWORD(3),
com_initializer_3.GetCOMBalancerReferenceCountForTesting());
// Attempt to prematurely uninitialize the COM library.
::CoUninitialize(); // Reference count -> 2.
::CoUninitialize(); // Reference count -> 1.
::CoUninitialize();
// Expect reference count to remain at 1.
EXPECT_EQ(DWORD(1),
com_initializer_1.GetCOMBalancerReferenceCountForTesting());
EXPECT_EQ(DWORD(1),
com_initializer_2.GetCOMBalancerReferenceCountForTesting());
EXPECT_EQ(DWORD(1),
com_initializer_3.GetCOMBalancerReferenceCountForTesting());
}
} // namespace win
} // namespace base