Debian Codegraph @

unittest/gunit/xplugin/xpl/message_builder_t.cc

raw symbols SCIP
1 /* Copyright (c) 2016, 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 <gtest/gtest.h>
25 #include <stddef.h>
26
27 #include <cstdint>
28
29 #include "plugin/x/protocol/encoders/encoding_xmessages.h"
30 #include "plugin/x/src/ngs/protocol/column_info_builder.h"
31 #include "plugin/x/src/ngs/protocol/protocol_protobuf.h"
32 #include "unittest/gunit/xplugin/xpl/protobuf_message.h"
33
34 namespace xpl {
35
36 namespace test {
37
38 template <typename T>
39 class Message_builder_encode_resultset : public ::testing::Test {
40 public:
41 std::string get_data_from_buffer() const {
42 std::string result;
43
44 auto page = m_buffer.m_front;
45
46 while (page && page->get_used_bytes()) {
47 const std::string data_on_page(
48 reinterpret_cast<char *>(page->m_begin_data), page->get_used_bytes());
49
50 result += data_on_page;
51 page = page->m_next_page;
52 }
53
54 return result;
55 }
56
57 ngs::Memory_block_pool m_memory_block_pool{{10, k_minimum_page_size}};
58 protocol::Encoding_pool m_pool{0, &m_memory_block_pool};
59 protocol::Encoding_buffer m_buffer{&m_pool};
60 protocol::XMessage_encoder m_encoder{&m_buffer};
61 };
62
63 template <typename ResultsetT, Mysqlx::ServerMessages::Type MessageId>
64 struct Resultset_pair_type {
65 using ResultsetType = ResultsetT;
66 static constexpr uint32_t message_id = MessageId;
67 };
68
69 using Resultset_types = ::testing::Types<
70 Resultset_pair_type<Mysqlx::Sql::StmtExecuteOk, Mysqlx::ServerMessages::OK>,
71 Resultset_pair_type<Mysqlx::Resultset::FetchDone,
72 Mysqlx::ServerMessages::RESULTSET_FETCH_DONE>,
73 Resultset_pair_type<
74 Mysqlx::Resultset::FetchDoneMoreResultsets,
75 Mysqlx::ServerMessages::RESULTSET_FETCH_DONE_MORE_RESULTSETS>,
76 Resultset_pair_type<
77 Mysqlx::Resultset::FetchDoneMoreOutParams,
78 Mysqlx::ServerMessages::RESULTSET_FETCH_DONE_MORE_OUT_PARAMS>,
79 Resultset_pair_type<Mysqlx::Resultset::FetchSuspended,
80 Mysqlx::ServerMessages::RESULTSET_FETCH_SUSPENDED>>;
81
82 TYPED_TEST_SUITE(Message_builder_encode_resultset, Resultset_types);
83
84 TYPED_TEST(Message_builder_encode_resultset, encode_resultset) {
85 this->m_encoder.template empty_xmessage<TypeParam::message_id>();
86
87 std::unique_ptr<typename TypeParam::ResultsetType> msg(
88 message_with_header_from_buffer<typename TypeParam::ResultsetType>(
89 this->get_data_from_buffer()));
90
91 ASSERT_TRUE(nullptr != msg);
92 ASSERT_TRUE(msg->IsInitialized());
93 }
94
95 namespace details {
96
97 const char k_first_character = 'a';
98 const char k_last_character = 'z';
99 const int k_character_distance = k_first_character - k_last_character;
100
101 std::string generate_long_string(const int length) {
102 std::string result(length, ' ');
103 int gen_state = 0;
104
105 std::generate(result.begin(), result.end(), [&gen_state]() {
106 return (gen_state++ % k_character_distance) + k_first_character;
107 });
108
109 return result;
110 }
111
112 } // namespace details
113
114 class Message_builder : public Message_builder_encode_resultset<void> {};
115
116 TEST_F(Message_builder, encode_compact_metadata) {
117 const uint64_t COLLATION = 1u;
118 const auto TYPE = Mysqlx::Resultset::ColumnMetaData::SINT;
119 const int DECIMALS = 3;
120 const uint32_t FLAGS = 0xabcdu;
121 const uint32_t LENGTH = 20u;
122 const uint32_t CONTENT_TYPE = 7u;
123
124 ::ngs::Column_info_builder column_info;
125
126 column_info.set_collation(COLLATION);
127 column_info.set_decimals(DECIMALS);
128 column_info.set_flags(FLAGS);
129 column_info.set_length(LENGTH);
130 column_info.set_type(TYPE);
131 column_info.set_content_type(CONTENT_TYPE);
132 m_encoder.encode_metadata(column_info.get());
133
134 std::string data = get_data_from_buffer();
135 std::unique_ptr<Mysqlx::Resultset::ColumnMetaData> msg(
136 message_with_header_from_buffer<Mysqlx::Resultset::ColumnMetaData>(data));
137
138 ASSERT_TRUE(nullptr != msg);
139
140 ASSERT_TRUE(msg->has_collation());
141 ASSERT_EQ(COLLATION, msg->collation());
142 ASSERT_TRUE(msg->has_type());
143 ASSERT_EQ(TYPE, msg->type());
144 ASSERT_TRUE(msg->has_fractional_digits());
145 ASSERT_EQ(DECIMALS, msg->fractional_digits());
146 ASSERT_TRUE(msg->has_flags());
147 ASSERT_EQ(FLAGS, msg->flags());
148 ASSERT_TRUE(msg->has_length());
149 ASSERT_EQ(LENGTH, msg->length());
150 ASSERT_TRUE(msg->has_content_type());
151 ASSERT_EQ(CONTENT_TYPE, msg->content_type());
152
153 ASSERT_FALSE(msg->has_catalog());
154 ASSERT_FALSE(msg->has_name());
155 ASSERT_FALSE(msg->has_original_name());
156 ASSERT_FALSE(msg->has_original_table());
157 ASSERT_FALSE(msg->has_schema());
158 ASSERT_FALSE(msg->has_table());
159 }
160
161 TEST_F(Message_builder, encode_full_metadata) {
162 const uint64_t COLLATION = 2u;
163 const auto TYPE = Mysqlx::Resultset::ColumnMetaData::BYTES;
164 const int DECIMALS = 4;
165 const uint32_t FLAGS = 0x89abu;
166 const uint32_t LENGTH = 0u;
167 const uint32_t CONTENT_TYPE = 1u;
168 const std::string CATALOG = "CATALOG_NAME";
169 const std::string TABLE_NAME = "TABLE_NAME";
170 const std::string ORG_TABLE_NAME = "ORG_TABLE_NAME";
171 const std::string SCHEMA = "SCHEMA_NAME";
172 const std::string COLUM_NAME = "COLUMN_NAME";
173 const std::string ORG_COLUM_NAME = "ORG_COLUMN_NAME";
174
175 ::ngs::Column_info_builder column_info;
176
177 column_info.set_non_compact_data(
178 CATALOG.c_str(), COLUM_NAME.c_str(), TABLE_NAME.c_str(), SCHEMA.c_str(),
179 ORG_COLUM_NAME.c_str(), ORG_TABLE_NAME.c_str());
180 column_info.set_collation(COLLATION);
181 column_info.set_decimals(DECIMALS);
182 column_info.set_flags(FLAGS);
183 column_info.set_length(LENGTH);
184 column_info.set_type(TYPE);
185 column_info.set_content_type(CONTENT_TYPE);
186
187 m_encoder.encode_metadata(column_info.get());
188
189 std::string data = get_data_from_buffer();
190 std::unique_ptr<Mysqlx::Resultset::ColumnMetaData> msg(
191 message_with_header_from_buffer<Mysqlx::Resultset::ColumnMetaData>(data));
192
193 ASSERT_TRUE(nullptr != msg);
194
195 ASSERT_TRUE(msg->has_collation());
196 ASSERT_EQ(COLLATION, msg->collation());
197 ASSERT_TRUE(msg->has_type());
198 ASSERT_EQ(TYPE, msg->type());
199 ASSERT_TRUE(msg->has_fractional_digits());
200 ASSERT_EQ(DECIMALS, msg->fractional_digits());
201 ASSERT_TRUE(msg->has_flags());
202 ASSERT_EQ(FLAGS, msg->flags());
203 ASSERT_TRUE(msg->has_length());
204 ASSERT_EQ(LENGTH, msg->length());
205 ASSERT_TRUE(msg->has_content_type());
206 ASSERT_EQ(CONTENT_TYPE, msg->content_type());
207 ASSERT_TRUE(msg->has_catalog());
208 ASSERT_EQ(CATALOG, msg->catalog());
209 ASSERT_TRUE(msg->has_name());
210 ASSERT_EQ(COLUM_NAME, msg->name());
211 ASSERT_TRUE(msg->has_original_name());
212 ASSERT_EQ(ORG_COLUM_NAME, msg->original_name());
213 ASSERT_TRUE(msg->has_original_table());
214 ASSERT_EQ(ORG_TABLE_NAME, msg->original_table());
215 ASSERT_TRUE(msg->has_schema());
216 ASSERT_EQ(SCHEMA, msg->schema());
217 ASSERT_TRUE(msg->has_table());
218 ASSERT_EQ(TABLE_NAME, msg->table());
219 }
220
221 TEST_F(Message_builder, encode_notice_with_text) {
222 const std::string k_expected_string = details::generate_long_string(1000);
223
224 m_encoder.encode_notice_text_message(k_expected_string);
225
226 std::string data = get_data_from_buffer();
227 std::unique_ptr<Mysqlx::Notice::Frame> msg(
228 message_with_header_from_buffer<Mysqlx::Notice::Frame>(data));
229
230 std::unique_ptr<Mysqlx::Notice::SessionStateChanged> state_changed(
231 message_from_buffer<Mysqlx::Notice::SessionStateChanged>(msg->payload()));
232
233 ASSERT_EQ(Mysqlx::Notice::Frame_Type_SESSION_STATE_CHANGED, msg->type());
234 ASSERT_EQ(Mysqlx::Notice::Frame_Scope_LOCAL, msg->scope());
235 ASSERT_EQ(Mysqlx::Notice::SessionStateChanged_Parameter_PRODUCED_MESSAGE,
236 state_changed->param());
237
238 ASSERT_EQ(1, state_changed->value_size());
239 ASSERT_EQ(Mysqlx::Datatypes::Scalar_Type::Scalar_Type_V_STRING,
240 state_changed->value(0).type());
241 ASSERT_EQ(k_expected_string, state_changed->value(0).v_string().value());
242 }
243
244 TEST_F(Message_builder, encode_notice_frame) {
245 const uint32_t TYPE = 2;
246 const int SCOPE = Mysqlx::Notice::Frame_Scope_GLOBAL;
247 const std::string DATA = "\0\0\1\12\12aaa\0";
248
249 m_encoder.encode_notice(TYPE, SCOPE, DATA);
250
251 std::unique_ptr<Mysqlx::Notice::Frame> msg(
252 message_with_header_from_buffer<Mysqlx::Notice::Frame>(
253 get_data_from_buffer()));
254
255 ASSERT_TRUE(nullptr != msg);
256
257 ASSERT_TRUE(msg->has_type());
258 ASSERT_EQ(TYPE, msg->type());
259 ASSERT_TRUE(msg->has_scope());
260 ASSERT_EQ(SCOPE, msg->scope());
261 ASSERT_TRUE(msg->has_payload());
262 ASSERT_EQ(DATA, msg->payload());
263 }
264
265 TEST_F(Message_builder, encode_notice_rows_affected) {
266 const uint64_t ROWS_AFFECTED = 10001u;
267
268 m_encoder.encode_notice_rows_affected(ROWS_AFFECTED);
269
270 std::unique_ptr<Mysqlx::Notice::Frame> msg(
271 message_with_header_from_buffer<Mysqlx::Notice::Frame>(
272 get_data_from_buffer()));
273
274 ASSERT_TRUE(nullptr != msg);
275
276 ASSERT_TRUE(msg->has_type());
277 ASSERT_EQ(3, msg->type()); /*Mysqlx::Notice::SessionStateChanged*/
278 ASSERT_TRUE(msg->has_scope());
279 ASSERT_EQ(Mysqlx::Notice::Frame_Scope_LOCAL, msg->scope());
280 ASSERT_TRUE(msg->has_payload());
281
282 Mysqlx::Notice::SessionStateChanged change;
283 change.ParseFromString(msg->payload());
284
285 ASSERT_EQ(Mysqlx::Notice::SessionStateChanged::ROWS_AFFECTED, change.param());
286 ASSERT_EQ(Mysqlx::Datatypes::Scalar::V_UINT, change.value(0).type());
287 ASSERT_EQ(ROWS_AFFECTED, change.value(0).v_unsigned_int());
288 }
289
290 } // namespace test
291
292 } // namespace xpl