Debian Codegraph @

unittest/gunit/xplugin/xpl/capabilities_configurator_t.cc

raw symbols SCIP
1 /* Copyright (c) 2015, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26
27 #include "my_config.h" // NOLINT(build/include_subdir)
28
29 #include "plugin/x/src/capabilities/configurator.h"
30 #include "plugin/x/src/ngs/memory.h"
31 #include "plugin/x/src/xpl_error.h"
32 #include "unittest/gunit/xplugin/xpl/mock/capability_handler.h"
33
34 const int NUMBER_OF_HANDLERS = 4;
35 const char *NAMES[NUMBER_OF_HANDLERS] = {"first", "second", "third", "fourth"};
36
37 using ::Mysqlx::Connection::Capabilities;
38 using ::Mysqlx::Connection::Capability;
39 using ::Mysqlx::Datatypes::Any;
40
41 namespace xpl {
42 namespace test {
43
44 using testing::_;
45 using testing::Ref;
46 using testing::Return;
47 using testing::StrictMock;
48 using testing::Test;
49
50 class CapabilitiesConfiguratorTestSuite : public testing::Test {
51 public:
52 using Mock_ptr = std::shared_ptr<StrictMock<mock::Capability_handler>>;
53
54 void SetUp() override {
55 for (int i = 0; i < NUMBER_OF_HANDLERS; ++i) {
56 mock_handlers.push_back(
57 std::make_shared<StrictMock<mock::Capability_handler>>());
58 }
59 std::for_each(std::begin(mock_handlers), std::end(mock_handlers),
60 expect_is_gettable<true>);
61
62 std::for_each(mock_handlers.begin(), mock_handlers.end(),
63 default_is_supported<false>);
64
65 std::vector<std::shared_ptr<iface::Capability_handler>> handlers(
66 mock_handlers.begin(), mock_handlers.end());
67
68 sut.reset(new Capabilities_configurator(handlers));
69 }
70
71 template <bool Result>
72 static void expect_is_supported(Mock_ptr mock) {
73 EXPECT_CALL(*mock, is_settable()).WillRepeatedly(Return(Result));
74 EXPECT_CALL(*mock, is_gettable()).WillRepeatedly(Return(Result));
75 EXPECT_CALL(*mock, is_supported()).WillOnce(Return(Result));
76 }
77
78 template <bool Result>
79 static void default_is_supported(Mock_ptr mock) {
80 EXPECT_CALL(*mock, is_supported()).WillRepeatedly(Return(Result));
81 }
82
83 static void expect_get_capability(Mock_ptr mock) {
84 EXPECT_CALL(*mock, get_void(_));
85 }
86
87 static void expect_commit(Mock_ptr mock) {
88 EXPECT_CALL(*mock, commit_void());
89 }
90
91 template <bool Result>
92 static void expect_is_gettable(Mock_ptr mock) {
93 EXPECT_CALL(*mock, is_gettable()).WillRepeatedly(Return(Result));
94 }
95
96 void assert_get(std::vector<Mock_ptr> *supported_handlers) {
97 std::for_each(supported_handlers->begin(), supported_handlers->end(),
98 expect_is_supported<true>);
99 std::for_each(supported_handlers->begin(), supported_handlers->end(),
100 expect_get_name(NAMES));
101 std::for_each(supported_handlers->begin(), supported_handlers->end(),
102 expect_get_capability);
103
104 ngs::Memory_instrumented<Capabilities>::Unique_ptr cap(sut->get());
105
106 ASSERT_TRUE(nullptr != cap.get());
107 ASSERT_EQ(static_cast<int>(supported_handlers->size()),
108 cap->capabilities_size());
109
110 for (std::size_t i = 0; i < supported_handlers->size(); i++) {
111 ASSERT_EQ(NAMES[i], cap->capabilities(static_cast<int>(i)).name());
112 }
113 }
114
115 Capability &add_capability(Capabilities *caps, const std::size_t mock_index) {
116 Capability *cap = caps->add_capabilities();
117
118 cap->set_name(NAMES[mock_index]);
119 cap->mutable_value()->mutable_scalar()->set_v_signed_int(mock_index);
120
121 return *cap;
122 }
123
124 Capability &add_capability_and_expect_it(Capabilities *caps,
125 const std::size_t mock_index,
126 const ngs::Error_code &set_result) {
127 Capability &cap = add_capability(caps, mock_index);
128
129 EXPECT_CALL(*mock_handlers[mock_index], is_settable())
130 .WillRepeatedly(Return(true));
131 EXPECT_CALL(*mock_handlers[mock_index], set(Ref(cap.value())))
132 .WillRepeatedly(Return(set_result));
133
134 return cap;
135 }
136
137 struct expect_get_name {
138 template <std::size_t ELEMENTS>
139 explicit expect_get_name(const char *(&names)[ELEMENTS])
140 : m_names(names), m_elements(ELEMENTS), m_current(0) {}
141
142 void operator()(Mock_ptr mock) {
143 EXPECT_CALL(*mock, name()).WillOnce(Return(get_next_name()));
144 }
145
146 std::string get_next_name() {
147 std::string result = m_names[m_current++];
148
149 if (m_current >= m_elements) m_current = 0;
150
151 return result;
152 }
153
154 const char **m_names;
155 const std::size_t m_elements;
156 std::size_t m_current;
157 };
158
159 struct default_get_name : expect_get_name {
160 template <std::size_t ELEMENTS>
161 explicit default_get_name(const char *(&names)[ELEMENTS])
162 : expect_get_name(names) {}
163
164 void operator()(Mock_ptr &mock) {
165 EXPECT_CALL(*mock, name()).WillRepeatedly(Return(get_next_name()));
166 }
167 };
168
169 std::vector<Mock_ptr> mock_handlers;
170
171 std::unique_ptr<Capabilities_configurator> sut;
172 };
173
174 TEST_F(CapabilitiesConfiguratorTestSuite, get_doesNothing_whenEmpty) {
175 std::vector<Mock_ptr> empty;
176
177 assert_get(&empty);
178 }
179
180 TEST_F(CapabilitiesConfiguratorTestSuite, get_returnsAllCapabilities) {
181 assert_get(&mock_handlers);
182 }
183
184 TEST_F(CapabilitiesConfiguratorTestSuite, get_returnsOnlySupportedCaps) {
185 std::vector<Mock_ptr> supported_handlers;
186
187 supported_handlers.push_back(mock_handlers[0]);
188 supported_handlers.push_back(mock_handlers[NUMBER_OF_HANDLERS - 1]);
189
190 assert_get(&supported_handlers);
191 }
192
193 TEST_F(CapabilitiesConfiguratorTestSuite,
194 prepareSet_errorErrorAndCommitDoesNothing_whenOneUnknownCapability) {
195 std::unique_ptr<Capabilities> caps(new Capabilities());
196
197 Capability *cap = caps->add_capabilities();
198
199 cap->set_name("UNKNOWN");
200
201 std::for_each(mock_handlers.begin(), mock_handlers.end(),
202 default_get_name(NAMES));
203
204 ASSERT_EQ(ER_X_CAPABILITY_NOT_FOUND, sut->prepare_set(*caps).error);
205
206 sut->commit();
207 }
208
209 TEST_F(CapabilitiesConfiguratorTestSuite,
210 prepare_set_fail_when_duplicated_capabilities) {
211 std::unique_ptr<Capabilities> caps(new Capabilities());
212 std::vector<Mock_ptr> supported_handlers;
213
214 std::for_each(mock_handlers.begin(), mock_handlers.end(),
215 default_get_name(NAMES));
216
217 add_capability_and_expect_it(caps.get(), 0, {});
218 add_capability_and_expect_it(caps.get(), 0, {});
219 supported_handlers.push_back(mock_handlers[0]);
220
221 ASSERT_EQ(ER_X_DUPLICATED_CAPABILITIES, sut->prepare_set(*caps).error);
222
223 sut->commit();
224 }
225
226 TEST_F(CapabilitiesConfiguratorTestSuite,
227 prepareSet_success_whenAllRequestedCapsSucceded) {
228 std::unique_ptr<Capabilities> caps(new Capabilities());
229 std::vector<Mock_ptr> supported_handlers;
230
231 std::for_each(mock_handlers.begin(), mock_handlers.end(),
232 default_get_name(NAMES));
233
234 add_capability_and_expect_it(caps.get(), 0, {});
235 supported_handlers.push_back(mock_handlers[0]);
236
237 add_capability_and_expect_it(caps.get(), NUMBER_OF_HANDLERS - 1, {});
238 supported_handlers.push_back(mock_handlers[NUMBER_OF_HANDLERS - 1]);
239
240 ASSERT_FALSE(sut->prepare_set(*caps));
241
242 std::for_each(supported_handlers.begin(), supported_handlers.end(),
243 expect_commit);
244 sut->commit();
245 }
246
247 TEST_F(CapabilitiesConfiguratorTestSuite,
248 prepareSet_FailsAndCommitDoesNothing_whenAnyCapsFailsLast) {
249 std::unique_ptr<Capabilities> caps(new Capabilities());
250 std::vector<Mock_ptr> supported_handlers;
251
252 std::for_each(mock_handlers.begin(), mock_handlers.end(),
253 default_get_name(NAMES));
254
255 add_capability_and_expect_it(caps.get(), 0, {});
256 supported_handlers.push_back(mock_handlers[0]);
257
258 add_capability_and_expect_it(
259 caps.get(), NUMBER_OF_HANDLERS - 1,
260 ngs::Error(ER_X_CAPABILITIES_PREPARE_FAILED, "fail"));
261 supported_handlers.push_back(mock_handlers[NUMBER_OF_HANDLERS - 1]);
262
263 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, sut->prepare_set(*caps).error);
264
265 sut->commit();
266 }
267
268 TEST_F(CapabilitiesConfiguratorTestSuite,
269 prepareSet_FailsAndCommitDoesNothing_whenAnyCapsFailsFirst) {
270 std::unique_ptr<Capabilities> caps(new Capabilities());
271 std::vector<Mock_ptr> supported_handlers;
272
273 std::for_each(mock_handlers.begin(), mock_handlers.end(),
274 default_get_name(NAMES));
275
276 add_capability_and_expect_it(
277 caps.get(), 0, ngs::Error(ER_X_CAPABILITIES_PREPARE_FAILED, "fail"));
278 supported_handlers.push_back(mock_handlers[0]);
279
280 add_capability(caps.get(), NUMBER_OF_HANDLERS - 1);
281 supported_handlers.push_back(mock_handlers[NUMBER_OF_HANDLERS - 1]);
282
283 ASSERT_EQ(ER_X_CAPABILITIES_PREPARE_FAILED, sut->prepare_set(*caps).error);
284
285 sut->commit();
286 }
287
288 } // namespace test
289
290 } // namespace xpl