Debian Codegraph @

unittest/gunit/xplugin/xpl/encoder_validator.h

raw symbols SCIP
1 /* Copyright (c) 2019, 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 #ifndef UNITTEST_GUNIT_XPLUGIN_XPL_ENCODER_VALIDATOR_H_
25 #define UNITTEST_GUNIT_XPLUGIN_XPL_ENCODER_VALIDATOR_H_
26
27 #include <gtest/gtest.h>
28 #include <stddef.h>
29 #include <string>
30
31 #include "plugin/x/protocol/encoders/encoding_xprotocol.h"
32
33 namespace protocol {
34
35 namespace test {
36
37 class Encoder_validator {
38 private:
39 std::string get_location() {
40 if (m_message_started) {
41 return "(field: " + std::to_string(m_field_number) + ")";
42 }
43 return "(operation-index:" +
44 std::to_string(m_field_number + m_raw_data_number) + ")";
45 }
46
47 void check_size(const char *info) {
48 if (m_buffer_left < 0)
49 FAIL() << "Buffer underflow at " << info << get_location()
50 << ", please increase the buffer in begin_xmessage or "
51 "ensure_buffer_size call";
52 }
53
54 public:
55 void summarize_buffer(const char *info) {
56 if (m_buffer_left > 0 && !m_allow_not_used_data) {
57 FAIL() << "Buffer was not filled to its boundaries at " << info
58 << ", not used space: " << m_buffer_left;
59 }
60
61 if (m_buffer_left < 0) {
62 FAIL() << "Buffer is too small at " << info
63 << ", we need additional: " << -m_buffer_left
64 << " bytes, in total: " << -m_buffer_left + m_buffer_size;
65 }
66 }
67
68 using Position = XProtocol_encoder::Position;
69
70 template <uint32_t t>
71 using Field_delimiter = XProtocol_encoder::Field_delimiter<t>;
72
73 void configure_allow_bigger_buffers(const bool allow_bigger_buffers) {
74 m_allow_not_used_data = allow_bigger_buffers;
75 }
76
77 template <uint32_t size>
78 void ensure_buffer_size() {
79 m_buffer_size = m_buffer_left = size;
80 }
81
82 template <uint32_t id, uint32_t needed_size>
83 Position begin_xmessage() {
84 if (m_message_started) {
85 ADD_FAILURE() << "Message already started";
86 return {};
87 }
88
89 m_buffer_size = m_buffer_left = needed_size;
90 m_buffer_left -= k_xmsg_header_size;
91
92 return {};
93 }
94
95 template <uint32_t id, uint32_t needed_size>
96 void begin_xmessage(Position *position) {
97 *position = begin_xmessage<id, needed_size>();
98 }
99
100 void end_xmessage(const Position &) {
101 if (m_message_ended) {
102 FAIL() << "Message already finished";
103 }
104
105 summarize_buffer("end_xmessage");
106 }
107
108 void abort_xmessage(const Position &position) { end_xmessage(position); }
109
110 void encode_fixed_uint32(const uint32_t) {
111 ++m_raw_data_number;
112 m_buffer_left -= k_fixed32_size;
113
114 check_size("encode_raw_data_fixed_uint32");
115 }
116
117 void encode_fixed_uint64(const uint64_t) {
118 ++m_raw_data_number;
119 m_buffer_left -= k_fixed64_size;
120
121 check_size("encode_raw_data_fixed_uint64");
122 }
123
124 void encode_fixedvar16_uint32(const uint32_t) {
125 ++m_raw_data_number;
126 m_buffer_left -= k_fixed16_size;
127
128 check_size("encode_raw_data_fixedvar16_uint32");
129 }
130
131 void encode_fixedvar8_uint8(const uint8_t) {
132 ++m_raw_data_number;
133 m_buffer_left -= k_fixed8_size;
134
135 check_size("encode_raw_data_fixedvar8_uint8");
136 }
137
138 void encode_var_uint32(const uint32_t) {
139 ++m_raw_data_number;
140 m_buffer_left -= (k_varint32_size);
141
142 check_size("encode_raw_data_uint32");
143 }
144
145 void encode_var_uint64(const uint64_t) {
146 ++m_raw_data_number;
147 m_buffer_left -= (k_varint64_size);
148
149 check_size("encode_raw_data_uint64");
150 }
151
152 void encode_var_sint64(const int64_t) {
153 ++m_raw_data_number;
154 m_buffer_left -= (k_varint64_size);
155
156 check_size("encode_raw_data_sint64");
157 }
158
159 template <uint64_t value>
160 void encode_const_var_uint() {
161 ++m_raw_data_number;
162 m_buffer_left -= (k_varint64_size);
163
164 check_size("encode_raw_data_uint64_template");
165 }
166
167 template <uint32_t field_id>
168 void encode_field_delimited_header() {
169 ++m_field_number;
170 m_buffer_left -= (k_varint_field_prefix_size);
171
172 check_size("encode_field_delimited");
173 }
174
175 template <uint32_t field_id>
176 void encode_field_enum(const int32_t) {
177 ++m_field_number;
178 m_buffer_left -= (k_varint_field_prefix_size + k_varint32_size);
179
180 check_size("encode_field_enum");
181 }
182
183 template <uint32_t field_id>
184 void encode_optional_field_var_uint64(const uint64_t *) {
185 ++m_field_number;
186 m_buffer_left -= (k_varint_field_prefix_size + k_varint64_size);
187
188 check_size("encode_optional_field_var_uint64");
189 }
190
191 template <uint32_t field_id>
192 void encode_optional_field_var_uint32(const uint32_t *) {
193 ++m_field_number;
194 m_buffer_left -= (k_varint_field_prefix_size + k_varint32_size);
195
196 check_size("encode_optional_field_var_uint32");
197 }
198
199 template <uint32_t field_id>
200 void encode_field_string(const std::string &) {
201 ++m_field_number;
202 summarize_buffer("encode_field_string");
203 m_buffer_left = 0;
204 }
205
206 template <uint32_t field_id, uint64_t value>
207 void encode_field_const_var_uint() {
208 ++m_field_number;
209 m_buffer_left -= (k_varint_field_prefix_size + k_varint64_size);
210
211 check_size("encode_field_const_var_uint");
212 }
213
214 template <uint32_t field_id, int64_t value>
215 void encode_field_const_enum() {
216 ++m_field_number;
217 m_buffer_left -= (k_varint_field_prefix_size + k_varint64_size);
218
219 check_size("encode_field_const_enum");
220 }
221
222 template <uint32_t field_id>
223 void encode_field_var_uint32(const uint32_t) {
224 ++m_field_number;
225 m_buffer_left -= (k_varint_field_prefix_size + k_varint64_size);
226
227 check_size("k_varint32_size");
228 }
229
230 template <uint32_t id, uint32_t delimiter_length = 1>
231 Field_delimiter<delimiter_length> begin_delimited_field() {
232 ++m_field_number;
233 m_buffer_left -= (k_varint_field_prefix_size + k_varint64_size);
234
235 check_size("begin_delimited_field");
236
237 return {};
238 }
239
240 template <uint32_t delimiter_length>
241 void end_delimited_field(const Field_delimiter<delimiter_length> &) {
242 // check_size("end_delimited_field");
243 }
244
245 template <uint32_t field_id>
246 void encode_field_var_uint64(const uint64_t) {
247 ++m_field_number;
248 m_buffer_left -= (k_varint_field_prefix_size + k_varint64_size);
249
250 check_size("encode_field_var_uint64");
251 }
252
253 void encode_raw(const uint8_t *, uint32_t) { m_buffer_left = 0; }
254
255 template <uint32_t id>
256 void empty_xmessage() {
257 if (m_message_started) {
258 FAIL() << "Message already started";
259 }
260
261 if (m_message_ended) {
262 FAIL() << "Message already ended";
263 }
264
265 m_message_started = true;
266 m_message_ended = true;
267 check_size("empty_xmessage");
268 }
269
270 private:
271 const int k_varint_field_prefix_size = 10;
272 const int k_varint32_size = 5;
273 const int k_varint64_size = 10;
274 const int k_fixed8_size = 1;
275 const int k_fixed16_size = 2;
276 const int k_fixed32_size = 4;
277 const int k_fixed64_size = 8;
278 const int k_xmsg_header_size = 5;
279
280 bool m_message_started = false;
281 bool m_message_ended = false;
282 int64_t m_buffer_left = 0;
283 int64_t m_buffer_size = 0;
284 int m_field_number = 0;
285 int m_raw_data_number = 0;
286 bool m_allow_not_used_data = false;
287 };
288
289 } // namespace test
290
291 } // namespace protocol
292
293 #endif // UNITTEST_GUNIT_XPLUGIN_XPL_ENCODER_VALIDATOR_H_