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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
base / win / variant_conversions_unittest.cc [blame]
// 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/variant_conversions.h"
#include <stdint.h>
#include <wrl/client.h>
#include <wrl/implements.h>
#include <set>
#include <utility>
#include "base/containers/contains.h"
#include "base/win/dispatch_stub.h"
#include "base/win/scoped_bstr.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::win::internal::VariantConverter;
using base::win::test::DispatchStub;
namespace base {
namespace win {
namespace {
static constexpr VARTYPE kSupportedVartypes[] = {
VT_BOOL, VT_I1, VT_UI1, VT_I2, VT_UI2, VT_I4, VT_UI4, VT_I8,
VT_UI8, VT_R4, VT_R8, VT_DATE, VT_BSTR, VT_UNKNOWN, VT_DISPATCH};
template <VARTYPE ElementVartype>
static bool TestIsConvertibleTo(const std::set<VARTYPE>& allowed_vartypes) {
for (VARTYPE vartype : kSupportedVartypes) {
if (VariantConverter<ElementVartype>::IsConvertibleTo(vartype) !=
base::Contains(allowed_vartypes, vartype)) {
return false;
}
}
return true;
}
template <VARTYPE ElementVartype>
static bool TestIsConvertibleFrom(const std::set<VARTYPE>& allowed_vartypes) {
for (VARTYPE vartype : kSupportedVartypes) {
if (VariantConverter<ElementVartype>::IsConvertibleFrom(vartype) !=
base::Contains(allowed_vartypes, vartype)) {
return false;
}
}
return true;
}
} // namespace
TEST(VariantConverterTest, VariantTypeBool) {
VARIANT variant;
V_VT(&variant) = VT_BOOL;
VariantConverter<VT_BOOL>::RawSet(&variant, VARIANT_TRUE);
EXPECT_EQ(V_BOOL(&variant), VARIANT_TRUE);
EXPECT_EQ(VariantConverter<VT_BOOL>::RawGet(variant), VARIANT_TRUE);
const std::set<VARTYPE> allowed_vartypes = {VT_BOOL};
EXPECT_TRUE(TestIsConvertibleTo<VT_BOOL>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_BOOL>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeI1) {
VARIANT variant;
V_VT(&variant) = VT_I1;
VariantConverter<VT_I1>::RawSet(&variant, 34);
EXPECT_EQ(V_I1(&variant), 34);
EXPECT_EQ(VariantConverter<VT_I1>::RawGet(variant), 34);
const std::set<VARTYPE> allowed_vartypes = {VT_I1};
EXPECT_TRUE(TestIsConvertibleTo<VT_I1>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_I1>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeUI1) {
VARIANT variant;
V_VT(&variant) = VT_UI1;
VariantConverter<VT_UI1>::RawSet(&variant, 34U);
EXPECT_EQ(V_UI1(&variant), 34U);
EXPECT_EQ(VariantConverter<VT_UI1>::RawGet(variant), 34U);
const std::set<VARTYPE> allowed_vartypes = {VT_UI1};
EXPECT_TRUE(TestIsConvertibleTo<VT_UI1>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_UI1>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeI2) {
VARIANT variant;
V_VT(&variant) = VT_I2;
VariantConverter<VT_I2>::RawSet(&variant, 8738);
EXPECT_EQ(V_I2(&variant), 8738);
EXPECT_EQ(VariantConverter<VT_I2>::RawGet(variant), 8738);
const std::set<VARTYPE> allowed_vartypes = {VT_I2};
EXPECT_TRUE(TestIsConvertibleTo<VT_I2>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_I2>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeUI2) {
VARIANT variant;
V_VT(&variant) = VT_UI2;
VariantConverter<VT_UI2>::RawSet(&variant, 8738U);
EXPECT_EQ(V_UI2(&variant), 8738U);
EXPECT_EQ(VariantConverter<VT_UI2>::RawGet(variant), 8738U);
const std::set<VARTYPE> allowed_vartypes = {VT_UI2};
EXPECT_TRUE(TestIsConvertibleTo<VT_UI2>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_UI2>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeI4) {
VARIANT variant;
V_VT(&variant) = VT_I4;
VariantConverter<VT_I4>::RawSet(&variant, 572662306);
EXPECT_EQ(V_I4(&variant), 572662306);
EXPECT_EQ(VariantConverter<VT_I4>::RawGet(variant), 572662306);
const std::set<VARTYPE> allowed_vartypes = {VT_I4};
EXPECT_TRUE(TestIsConvertibleTo<VT_I4>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_I4>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeUI4) {
VARIANT variant;
V_VT(&variant) = VT_UI4;
VariantConverter<VT_UI4>::RawSet(&variant, 572662306U);
EXPECT_EQ(V_UI4(&variant), 572662306U);
EXPECT_EQ(VariantConverter<VT_UI4>::RawGet(variant), 572662306U);
const std::set<VARTYPE> allowed_vartypes = {VT_UI4};
EXPECT_TRUE(TestIsConvertibleTo<VT_UI4>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_UI4>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeI8) {
VARIANT variant;
V_VT(&variant) = VT_I8;
VariantConverter<VT_I8>::RawSet(&variant, 2459565876494606882);
EXPECT_EQ(V_I8(&variant), 2459565876494606882);
EXPECT_EQ(VariantConverter<VT_I8>::RawGet(variant), 2459565876494606882);
const std::set<VARTYPE> allowed_vartypes = {VT_I8};
EXPECT_TRUE(TestIsConvertibleTo<VT_I8>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_I8>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeUI8) {
VARIANT variant;
V_VT(&variant) = VT_UI8;
VariantConverter<VT_UI8>::RawSet(&variant, 2459565876494606882U);
EXPECT_EQ(V_UI8(&variant), 2459565876494606882U);
EXPECT_EQ(VariantConverter<VT_UI8>::RawGet(variant), 2459565876494606882U);
const std::set<VARTYPE> allowed_vartypes = {VT_UI8};
EXPECT_TRUE(TestIsConvertibleTo<VT_UI8>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_UI8>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeR4) {
VARIANT variant;
V_VT(&variant) = VT_R4;
VariantConverter<VT_R4>::RawSet(&variant, 3.14159f);
EXPECT_EQ(V_R4(&variant), 3.14159f);
EXPECT_EQ(VariantConverter<VT_R4>::RawGet(variant), 3.14159f);
const std::set<VARTYPE> allowed_vartypes = {VT_R4};
EXPECT_TRUE(TestIsConvertibleTo<VT_R4>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_R4>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeR8) {
VARIANT variant;
V_VT(&variant) = VT_R8;
VariantConverter<VT_R8>::RawSet(&variant, 3.14159);
EXPECT_EQ(V_R8(&variant), 3.14159);
EXPECT_EQ(VariantConverter<VT_R8>::RawGet(variant), 3.14159);
const std::set<VARTYPE> allowed_vartypes = {VT_R8};
EXPECT_TRUE(TestIsConvertibleTo<VT_R8>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_R8>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeDate) {
SYSTEMTIME sys_time;
::GetSystemTime(&sys_time);
DATE date;
::SystemTimeToVariantTime(&sys_time, &date);
VARIANT variant;
V_VT(&variant) = VT_DATE;
VariantConverter<VT_DATE>::RawSet(&variant, date);
EXPECT_EQ(V_DATE(&variant), date);
EXPECT_EQ(VariantConverter<VT_DATE>::RawGet(variant), date);
const std::set<VARTYPE> allowed_vartypes = {VT_DATE};
EXPECT_TRUE(TestIsConvertibleTo<VT_DATE>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_DATE>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeBstr) {
ScopedBstr scoped_bstr;
scoped_bstr.Allocate(L"some text");
VARIANT variant;
V_VT(&variant) = VT_BSTR;
VariantConverter<VT_BSTR>::RawSet(&variant, scoped_bstr.Get());
EXPECT_EQ(V_BSTR(&variant), scoped_bstr.Get());
EXPECT_EQ(VariantConverter<VT_BSTR>::RawGet(variant), scoped_bstr.Get());
const std::set<VARTYPE> allowed_vartypes = {VT_BSTR};
EXPECT_TRUE(TestIsConvertibleTo<VT_BSTR>(allowed_vartypes));
EXPECT_TRUE(TestIsConvertibleFrom<VT_BSTR>(allowed_vartypes));
}
TEST(VariantConverterTest, VariantTypeUnknown) {
Microsoft::WRL::ComPtr<IUnknown> unknown =
Microsoft::WRL::Make<DispatchStub>();
VARIANT variant;
V_VT(&variant) = VT_UNKNOWN;
VariantConverter<VT_UNKNOWN>::RawSet(&variant, unknown.Get());
EXPECT_EQ(V_UNKNOWN(&variant), unknown.Get());
EXPECT_EQ(VariantConverter<VT_UNKNOWN>::RawGet(variant), unknown.Get());
const std::set<VARTYPE> allow_convertible_to = {VT_UNKNOWN};
const std::set<VARTYPE> allow_convertible_from = {VT_UNKNOWN, VT_DISPATCH};
EXPECT_TRUE(TestIsConvertibleTo<VT_UNKNOWN>(allow_convertible_to));
EXPECT_TRUE(TestIsConvertibleFrom<VT_UNKNOWN>(allow_convertible_from));
}
TEST(VariantConverterTest, VariantTypeDispatch) {
Microsoft::WRL::ComPtr<IDispatch> dispatch =
Microsoft::WRL::Make<DispatchStub>();
VARIANT variant;
V_VT(&variant) = VT_DISPATCH;
VariantConverter<VT_DISPATCH>::RawSet(&variant, dispatch.Get());
EXPECT_EQ(V_DISPATCH(&variant), dispatch.Get());
EXPECT_EQ(VariantConverter<VT_DISPATCH>::RawGet(variant), dispatch.Get());
const std::set<VARTYPE> allow_convertible_to = {VT_UNKNOWN, VT_DISPATCH};
const std::set<VARTYPE> allow_convertible_from = {VT_DISPATCH};
EXPECT_TRUE(TestIsConvertibleTo<VT_DISPATCH>(allow_convertible_to));
EXPECT_TRUE(TestIsConvertibleFrom<VT_DISPATCH>(allow_convertible_from));
}
} // namespace win
} // namespace base