Debian Codegraph @

unittest/gunit/dd_properties-t.cc

raw symbols SCIP
1 /* Copyright (c) 2013, 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 "my_inttypes.h"
28 #include "sql/dd/impl/properties_impl.h"
29 #include "sql/dd/properties.h"
30 #include "unittest/gunit/test_utils.h"
31
32 namespace dd_properties_unittest {
33
34 /**
35 The fixture for testing the dd::Properties and Properties_impl classes.
36 A fresh instance of this class will be created for each of the
37 TEST_F functions below.
38 The functions SetUp(), TearDown(), SetUpTestCase(), TearDownTestCase() are
39 inherited from ::testing::Test (google naming style differs from MySQL).
40 */
41
42 class PropertiesTest : public ::testing::Test {
43 protected:
44 PropertiesTest() = default;
45
46 void SetUp() override { m_props = new dd::Properties_impl(); }
47
48 void TearDown() override { delete m_props; }
49
50 static void SetUpTestCase() {}
51
52 static void TearDownTestCase() {}
53
54 dd::Properties *m_props;
55
56 private:
57 PropertiesTest(PropertiesTest const &) = delete;
58 PropertiesTest &operator=(PropertiesTest const &) = delete;
59 };
60
61 static const dd::String_type value(const dd::Properties &p,
62 const dd::String_type &key) {
63 dd::String_type val;
64 (void)p.get(key, &val);
65 return val;
66 }
67
68 // Tests that valid option parsing is handled
69 TEST_F(PropertiesTest, ValidStringParsing) {
70 dd::Properties *p = dd::Properties_impl::parse_properties("a=b;b=c");
71 EXPECT_TRUE(value(*p, "a") == "b");
72 EXPECT_TRUE(value(*p, "b") == "c");
73 EXPECT_TRUE(p->raw_string() == "a=b;b=c;");
74 delete p;
75
76 p = dd::Properties_impl::parse_properties("a=b;b=c;");
77 EXPECT_TRUE(value(*p, "a") == "b");
78 EXPECT_TRUE(value(*p, "b") == "c");
79 EXPECT_TRUE(p->raw_string() == "a=b;b=c;");
80 delete p;
81
82 p = dd::Properties_impl::parse_properties("\\=a=\\;b;b\\==\\=c");
83 EXPECT_TRUE(value(*p, "=a") == ";b");
84 EXPECT_TRUE(value(*p, "b=") == "=c");
85 EXPECT_TRUE(p->raw_string() == "\\=a=\\;b;b\\==\\=c;");
86 delete p;
87
88 p = dd::Properties_impl::parse_properties("");
89 EXPECT_TRUE(p != nullptr);
90 EXPECT_FALSE(p->exists(""));
91 delete p;
92
93 p = dd::Properties_impl::parse_properties("a=;");
94 EXPECT_TRUE(p != nullptr);
95 EXPECT_TRUE(value(*p, "a") == "");
96 delete p;
97 }
98
99 // Tests that option parsing errors are handled
100 TEST_F(PropertiesTest, FailingStringParsing) {
101 dd::Properties *p = dd::Properties_impl::parse_properties("a");
102 EXPECT_TRUE(p == nullptr);
103 delete p;
104
105 p = dd::Properties_impl::parse_properties("a");
106 EXPECT_TRUE(p == nullptr);
107 delete p;
108
109 p = dd::Properties_impl::parse_properties(";");
110 EXPECT_TRUE(p == nullptr);
111 delete p;
112
113 p = dd::Properties_impl::parse_properties("a\\=b");
114 EXPECT_TRUE(p == nullptr);
115 delete p;
116
117 p = dd::Properties_impl::parse_properties("=");
118 EXPECT_TRUE(p == nullptr);
119 delete p;
120
121 p = dd::Properties_impl::parse_properties("=a");
122 EXPECT_TRUE(p == nullptr);
123 delete p;
124 }
125
126 // Tests empty value behavior
127 TEST_F(PropertiesTest, EmptyValue) {
128 dd::Properties *p = dd::Properties_impl::parse_properties("k=;");
129 dd::String_type string_value("");
130
131 EXPECT_TRUE(value(*p, "k") == "");
132 EXPECT_TRUE(!p->get("k", &string_value) && string_value == "");
133 delete p;
134 }
135
136 // Tests utf-8 behavior
137 TEST_F(PropertiesTest, UTF8) {
138 const dd::String_type EUR("\xE2\x82\xAC");
139 const dd::String_type CNY("\xe5\x85\x83");
140 const dd::String_type JPY("\xe5\x86\x86");
141 const dd::String_type GBP("\xc2\xa3");
142 const dd::String_type USD("\x24");
143 const dd::String_type CNT("\xC2\xA2");
144
145 // Parse a utf-8 string and add more key=value pairs
146 dd::Properties *p = dd::Properties_impl::parse_properties("EUR=" + EUR);
147 p->set("CNY", CNY);
148 p->set("JPY", JPY);
149 p->set("GBP", GBP);
150 p->set("USD", USD);
151
152 p->set("1/100 " + EUR, "Cent, but not " + CNT);
153 p->set("1/100 " + GBP, "Pence, whatever that symbol is");
154 p->set("1/100 " + CNY, "Who knows, but not " + JPY + " for sure");
155 p->set("1/100 " + USD, "Half of my 2 " + CNT + "s worth");
156
157 EXPECT_TRUE(value(*p, "EUR") == EUR);
158 EXPECT_TRUE(value(*p, "1/100 " + value(*p, "EUR")) == "Cent, but not " + CNT);
159
160 EXPECT_TRUE(value(*p, "CNY") == CNY);
161 EXPECT_TRUE(value(*p, "1/100 " + value(*p, "CNY")) ==
162 "Who knows, but not " + JPY + " for sure");
163
164 EXPECT_TRUE(value(*p, "JPY") == JPY);
165
166 EXPECT_TRUE(value(*p, "GBP") == GBP);
167 EXPECT_TRUE(value(*p, "1/100 " + value(*p, "GBP")) ==
168 "Pence, whatever that symbol is");
169
170 EXPECT_TRUE(value(*p, "USD") == USD);
171 EXPECT_TRUE(value(*p, "1/100 " + value(*p, "USD")) ==
172 "Half of my 2 " + CNT + "s worth");
173 delete p;
174 }
175
176 // Tests setting and getting options
177 TEST_F(PropertiesTest, SetGetStrings) {
178 dd::Properties *p = new dd::Properties_impl();
179 dd::String_type str("");
180
181 p->set("a", "b");
182 EXPECT_TRUE(value(*p, "a") == "b");
183 EXPECT_TRUE(!p->get("a", &str) && str == "b");
184
185 p->set("b=", "c;");
186 EXPECT_TRUE(value(*p, "b=") == "c;");
187 EXPECT_TRUE(!p->get("b=", &str) && str == "c;");
188
189 p->set("d\\=", "e\\;");
190 EXPECT_TRUE(value(*p, "d\\=") == "e\\;");
191 EXPECT_TRUE(!p->get("d\\=", &str) && str == "e\\;");
192
193 p->set(";f", "=g");
194 EXPECT_TRUE(value(*p, ";f") == "=g");
195 EXPECT_TRUE(!p->get(";f", &str) && str == "=g");
196
197 p->set("\\;h", "\\=i");
198 EXPECT_TRUE(value(*p, "\\;h") == "\\=i");
199 EXPECT_TRUE(!p->get("\\;h", &str) && str == "\\=i");
200
201 // Key "" is illegal and will not be added
202 p->set("", "");
203
204 #if !defined(NDEBUG)
205 EXPECT_DEATH_IF_SUPPORTED(p->get("", &str), ".*UTC - mysqld got.*");
206 #else
207 EXPECT_TRUE(p->get("", &str));
208 #endif
209
210 EXPECT_FALSE(p->exists(""));
211 EXPECT_TRUE(p->remove(""));
212
213 EXPECT_TRUE(p->raw_string() ==
214 "\\;f=\\=g;\\\\\\;h=\\\\\\=i;a=b;b\\==c\\;;d\\\\\\==e\\\\\\;;");
215
216 // Create another object with the raw string as input
217 dd::Properties *p_copy =
218 dd::Properties_impl::parse_properties(p->raw_string());
219 // Verify same values
220 EXPECT_TRUE(value(*p, "a") == value(*p_copy, "a"));
221 EXPECT_TRUE(value(*p, "b=") == value(*p_copy, "b="));
222 EXPECT_TRUE(value(*p, "d\\=") == value(*p_copy, "d\\="));
223 EXPECT_TRUE(value(*p, ";f") == value(*p_copy, ";f"));
224 EXPECT_TRUE(value(*p, "\\;h") == value(*p_copy, "\\;h"));
225
226 EXPECT_TRUE(p->raw_string() == p_copy->raw_string());
227
228 delete p;
229 delete p_copy;
230 }
231
232 // Tests valid setting and getting of int and bool options
233 TEST_F(PropertiesTest, ValidSetGetIntBool) {
234 int64 val1_int64 = 0;
235 int64 val2_int64 = 0;
236 uint64 val1_uint64 = 0;
237 uint64 val2_uint64 = 0;
238 int32 val1_int32 = 0;
239 int32 val2_int32 = 0;
240 uint32 val1_uint32 = 0;
241 uint32 val2_uint32 = 0;
242
243 const char *MAX_INT64_STR = "9223372036854775807";
244 const int64 MAX_INT64 = std::numeric_limits<int64>::max();
245 const char *MIN_INT64_STR = "-9223372036854775808";
246 const int64 MIN_INT64 = std::numeric_limits<int64>::min();
247
248 const char *MAX_UINT64_STR = "18446744073709551615";
249 const uint64 MAX_UINT64 = std::numeric_limits<uint64>::max();
250 const char *MIN_UINT64_STR = "0";
251 const uint64 MIN_UINT64 = 0;
252
253 const char *MAX_INT32_STR = "2147483647";
254 const int32 MAX_INT32 = std::numeric_limits<int32>::max();
255 const char *MIN_INT32_STR = "-2147483648";
256 const int32 MIN_INT32 = std::numeric_limits<int32>::min();
257
258 const char *MAX_UINT32_STR = "4294967295";
259 const uint32 MAX_UINT32 = std::numeric_limits<uint32>::max();
260 const char *MIN_UINT32_STR = "0";
261 const uint32 MIN_UINT32 = 0;
262
263 dd::Properties *p = new dd::Properties_impl();
264
265 // Set and get as strings and ints, int64
266 p->set("str_int64", MAX_INT64_STR);
267 p->set("num_int64", MAX_INT64);
268
269 EXPECT_TRUE(value(*p, "str_int64") == value(*p, "num_int64") &&
270 value(*p, "str_int64") == MAX_INT64_STR);
271 EXPECT_TRUE(!p->get("str_int64", &val1_int64) &&
272 !p->get("num_int64", &val2_int64));
273 EXPECT_TRUE(val1_int64 == val2_int64 && val1_int64 == MAX_INT64);
274
275 p->set("str_int64", MIN_INT64_STR);
276 p->set("num_int64", MIN_INT64);
277
278 EXPECT_TRUE(value(*p, "str_int64") == value(*p, "num_int64") &&
279 value(*p, "str_int64") == MIN_INT64_STR);
280 EXPECT_TRUE(!p->get("str_int64", &val1_int64) &&
281 !p->get("num_int64", &val2_int64));
282 EXPECT_TRUE(val1_int64 == val2_int64 && val1_int64 == MIN_INT64);
283
284 // Set and get as strings and ints, uint64
285 p->set("str_uint64", MAX_UINT64_STR);
286 p->set("num_uint64", MAX_UINT64);
287
288 EXPECT_TRUE(value(*p, "str_uint64") == value(*p, "num_uint64") &&
289 value(*p, "str_uint64") == MAX_UINT64_STR);
290 EXPECT_TRUE(!p->get("str_uint64", &val1_uint64) &&
291 !p->get("num_uint64", &val2_uint64));
292 EXPECT_TRUE(val1_uint64 == val2_uint64 && val1_uint64 == MAX_UINT64);
293
294 p->set("str_uint64", MIN_UINT64_STR);
295 p->set("num_uint64", MIN_UINT64);
296
297 EXPECT_TRUE(value(*p, "str_uint64") == value(*p, "num_uint64") &&
298 value(*p, "str_uint64") == MIN_UINT64_STR);
299 EXPECT_TRUE(!p->get("str_uint64", &val1_uint64) &&
300 !p->get("num_uint64", &val2_uint64));
301 EXPECT_TRUE(val1_uint64 == val2_uint64 && val1_uint64 == MIN_UINT64);
302
303 // Set and get as strings and ints, int32
304 p->set("str_int32", MAX_INT32_STR);
305 p->set("num_int32", MAX_INT32);
306
307 EXPECT_TRUE(value(*p, "str_int32") == value(*p, "num_int32") &&
308 value(*p, "str_int32") == MAX_INT32_STR);
309 EXPECT_TRUE(!p->get("str_int32", &val1_int32) &&
310 !p->get("num_int32", &val2_int32));
311 EXPECT_TRUE(val1_int32 == val2_int32 && val1_int32 == MAX_INT32);
312
313 p->set("str_int32", MIN_INT32_STR);
314 p->set("num_int32", MIN_INT32);
315
316 EXPECT_TRUE(value(*p, "str_int32") == value(*p, "num_int32") &&
317 value(*p, "str_int32") == MIN_INT32_STR);
318 EXPECT_TRUE(!p->get("str_int32", &val1_int32) &&
319 !p->get("num_int32", &val2_int32));
320 EXPECT_TRUE(val1_int32 == val2_int32 && val1_int32 == MIN_INT32);
321
322 // Set and get as strings and ints, uint32
323 p->set("str_uint32", MAX_UINT32_STR);
324 p->set("num_uint32", MAX_UINT32);
325
326 EXPECT_TRUE(value(*p, "str_uint32") == value(*p, "num_uint32") &&
327 value(*p, "str_uint32") == MAX_UINT32_STR);
328 EXPECT_TRUE(!p->get("str_uint32", &val1_uint32) &&
329 !p->get("num_uint32", &val2_uint32));
330 EXPECT_TRUE(val1_uint32 == val2_uint32 && val1_uint32 == MAX_UINT32);
331
332 p->set("str_uint32", MIN_UINT32_STR);
333 p->set("num_uint32", MIN_UINT32);
334
335 EXPECT_TRUE(value(*p, "str_uint32") == value(*p, "num_uint32") &&
336 value(*p, "str_uint32") == MIN_UINT32_STR);
337 EXPECT_TRUE(!p->get("str_uint32", &val1_uint32) &&
338 !p->get("num_uint32", &val2_uint32));
339 EXPECT_TRUE(val1_uint32 == val2_uint32 && val1_uint32 == MIN_UINT32);
340
341 // Set and get as strings and bool
342 bool maybe = false;
343
344 p->set("bool", true);
345 EXPECT_TRUE(value(*p, "bool") == "1");
346 EXPECT_TRUE(!p->get("bool", &maybe) && maybe == true);
347 EXPECT_TRUE(!p->get("bool", &val1_int64) && val1_int64 == 1);
348 EXPECT_TRUE(!p->get("bool", &val1_uint64) && val1_uint64 == 1);
349 EXPECT_TRUE(!p->get("bool", &val1_int32) && val1_int32 == 1);
350 EXPECT_TRUE(!p->get("bool", &val1_uint32) && val1_uint32 == 1);
351 p->set("bool", false);
352 EXPECT_TRUE(value(*p, "bool") == "0");
353 EXPECT_TRUE(!p->get("bool", &maybe) && maybe == false);
354 EXPECT_TRUE(!p->get("bool", &val1_int64) && val1_int64 == 0);
355 EXPECT_TRUE(!p->get("bool", &val1_uint64) && val1_uint64 == 0);
356 EXPECT_TRUE(!p->get("bool", &val1_int32) && val1_int32 == 0);
357 EXPECT_TRUE(!p->get("bool", &val1_uint32) && val1_uint32 == 0);
358 p->remove("bool");
359
360 p->set("str_int_bool", 0);
361 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == false);
362 p->set("str_int_bool", 1);
363 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == true);
364 p->set("str_int_bool", MAX_INT64);
365 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == true);
366 p->set("str_int_bool", MIN_INT64);
367 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == true);
368
369 p->set("str_int_bool", 0);
370 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == false);
371 p->set("str_int_bool", 1);
372 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == true);
373 p->set("str_int_bool", MAX_UINT64);
374 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == true);
375 p->set("str_int_bool", MIN_UINT64);
376 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == false);
377
378 p->set("str_int_bool", 0);
379 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == false);
380 p->set("str_int_bool", 1);
381 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == true);
382 p->set("str_int_bool", MAX_INT32);
383 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == true);
384 p->set("str_int_bool", MIN_INT32);
385 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == true);
386
387 p->set("str_int_bool", 0);
388 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == false);
389 p->set("str_int_bool", 1);
390 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == true);
391 p->set("str_int_bool", MAX_UINT32);
392 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == true);
393 p->set("str_int_bool", MIN_UINT32);
394 EXPECT_TRUE(!p->get("str_int_bool", &maybe) && maybe == false);
395
396 EXPECT_TRUE(p->raw_string() ==
397 "num_int32=-2147483648;num_int64=-9223372036854775808;num_uint32="
398 "0;num_uint64=0;str_int32=-2147483648;str_int64=-"
399 "9223372036854775808;str_int_bool=0;str_uint32=0;str_uint64=0;");
400
401 delete p;
402 }
403
404 #if !defined(NDEBUG)
405
406 // Tests invalid setting and getting of int and bool options
407 typedef PropertiesTest PropertiesTestDeathTest;
408
409 TEST_F(PropertiesTestDeathTest, FailingSetGetIntBool) {
410 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
411 int32 val_int32 = 0;
412 uint32 val_uint32 = 0;
413 int64 val_int64 = 0;
414 uint64 val_uint64 = 0;
415
416 const char *OFL_INT64_STR = "9223372036854775808";
417 const char *UFL_INT64_STR = "-9223372036854775809";
418
419 const char *OFL_UINT64_STR = "18446744073709551616";
420
421 const char *OFL_INT32_STR = "2147483648";
422 const char *UFL_INT32_STR = "-2147483649";
423
424 const char *OFL_UINT32_STR = "4294967296";
425
426 dd::Properties *p = new dd::Properties_impl();
427
428 p->set("num_int64", OFL_INT64_STR);
429 EXPECT_TRUE(value(*p, "num_int64") == OFL_INT64_STR);
430 EXPECT_DEATH_IF_SUPPORTED(p->get("num_int64", &val_int64),
431 ".*UTC - mysqld got.*");
432
433 p->set("num_int64", UFL_INT64_STR);
434 EXPECT_TRUE(value(*p, "num_int64") == UFL_INT64_STR);
435 EXPECT_DEATH_IF_SUPPORTED(p->get("num_int64", &val_int64),
436 ".*UTC - mysqld got.*");
437
438 p->set("num_uint64", OFL_UINT64_STR);
439 EXPECT_TRUE(value(*p, "num_uint64") == OFL_UINT64_STR);
440 EXPECT_DEATH_IF_SUPPORTED(p->get("num_uint64", &val_uint64),
441 ".*UTC - mysqld got.*");
442
443 p->set("num_int32", OFL_INT32_STR);
444 EXPECT_TRUE(value(*p, "num_int32") == OFL_INT32_STR);
445 EXPECT_DEATH_IF_SUPPORTED(p->get("num_int32", &val_int32),
446 ".*UTC - mysqld got.*");
447
448 p->set("num_int32", UFL_INT32_STR);
449 EXPECT_TRUE(value(*p, "num_int32") == UFL_INT32_STR);
450 EXPECT_DEATH_IF_SUPPORTED(p->get("num_int32", &val_int32),
451 ".*UTC - mysqld got.*");
452
453 p->set("num_uint32", OFL_UINT32_STR);
454 EXPECT_DEATH_IF_SUPPORTED(p->get("num_uint32", &val_uint32),
455 ".*UTC - mysqld got.*");
456
457 // The overflowed unit32 may be retrieved as 64 bit
458 EXPECT_TRUE(!p->get("num_uint32", &val_uint64) &&
459 !p->get("num_uint32", &val_int64));
460
461 // An overflowing 64 bit int should not be accepted as a bool
462 bool maybe = false;
463
464 p->set("bool", OFL_UINT64_STR);
465 EXPECT_TRUE(value(*p, "bool") == OFL_UINT64_STR);
466 EXPECT_DEATH_IF_SUPPORTED(p->get("bool", &val_uint64),
467 ".*UTC - mysqld got.*");
468 EXPECT_DEATH_IF_SUPPORTED(p->get("bool", &maybe), ".*UTC - mysqld got.*");
469
470 p->set("bool", UFL_INT64_STR);
471 EXPECT_TRUE(value(*p, "bool") == UFL_INT64_STR);
472 EXPECT_DEATH_IF_SUPPORTED(p->get("bool", &val_int64), ".*UTC - mysqld got.*");
473 EXPECT_DEATH_IF_SUPPORTED(p->get("bool", &maybe), ".*UTC - mysqld got.*");
474 p->remove("bool");
475
476 // Integers with empty keys and non existing keys.
477 p->set("", 0);
478 EXPECT_FALSE(p->exists(""));
479 EXPECT_DEATH_IF_SUPPORTED(p->get("non_existing", &val_int64),
480 ".*UTC - mysqld got.*");
481 EXPECT_DEATH_IF_SUPPORTED(p->get("", &val_int64), ".*UTC - mysqld got.*");
482 EXPECT_DEATH_IF_SUPPORTED(value(*p, ""), ".*UTC - mysqld got.*");
483 EXPECT_TRUE(p->remove(""));
484
485 EXPECT_TRUE(p->raw_string() ==
486 "num_int32=-2147483649;num_int64=-9223372036854775809;num_uint32="
487 "4294967296;num_uint64=18446744073709551616;");
488
489 delete p;
490 }
491
492 #endif // NDEBUG
493
494 // Tests the exists function
495 TEST_F(PropertiesTest, OptionsExist) {
496 dd::Properties *p = new dd::Properties_impl();
497 EXPECT_FALSE(p->exists(""));
498 EXPECT_FALSE(p->exists("a"));
499
500 p->set("", "");
501 EXPECT_FALSE(p->exists(""));
502
503 p->set("empty", "");
504 EXPECT_TRUE(p->exists("empty"));
505 EXPECT_TRUE(value(*p, "empty") == "");
506
507 p->set("a", "b");
508 EXPECT_TRUE(p->exists("a"));
509 EXPECT_TRUE(value(*p, "a") == "b");
510
511 // Create another object with the raw string as input
512 dd::Properties *p_copy =
513 dd::Properties_impl::parse_properties(p->raw_string());
514 EXPECT_TRUE(p->exists("empty"));
515 EXPECT_TRUE(value(*p, "empty") == "");
516
517 EXPECT_TRUE(p->exists("a"));
518 EXPECT_TRUE(value(*p, "a") == "b");
519
520 delete p;
521 delete p_copy;
522 }
523
524 // Tests replacing values
525 TEST_F(PropertiesTest, ReplaceValues) {
526 dd::Properties *p = new dd::Properties_impl();
527 EXPECT_FALSE(p->exists(""));
528 EXPECT_FALSE(p->exists("a"));
529
530 p->set("empty", "");
531 EXPECT_TRUE(p->exists("empty"));
532 EXPECT_TRUE(value(*p, "empty") == "");
533
534 p->set("empty too", "");
535 EXPECT_TRUE(p->exists("empty too"));
536 EXPECT_TRUE(value(*p, "empty too") == "");
537
538 p->set("empty", " ");
539 EXPECT_TRUE(p->exists("empty"));
540 EXPECT_TRUE(value(*p, "empty") == " ");
541
542 p->set("a", "b");
543 EXPECT_TRUE(p->exists("a"));
544 EXPECT_TRUE(value(*p, "a") == "b");
545
546 p->set("a", "b too");
547 EXPECT_TRUE(p->exists("a"));
548 EXPECT_TRUE(value(*p, "a") == "b too");
549
550 p->set("a", "");
551 EXPECT_TRUE(p->exists("a"));
552 EXPECT_TRUE(value(*p, "a") == "");
553
554 EXPECT_TRUE(p->raw_string() == "a=;empty= ;empty too=;");
555
556 delete p;
557 }
558
559 // Tests removing options
560 TEST_F(PropertiesTest, RemoveOptions) {
561 dd::Properties *p = new dd::Properties_impl();
562
563 EXPECT_FALSE(p->exists(""));
564 EXPECT_FALSE(p->exists("a"));
565
566 EXPECT_TRUE(p->remove(""));
567 EXPECT_TRUE(p->remove("a"));
568
569 p->set("a", "");
570 EXPECT_TRUE(p->exists("a"));
571 EXPECT_TRUE(value(*p, "a") == "");
572 EXPECT_FALSE(p->remove("a"));
573 EXPECT_FALSE(p->exists("a"));
574 EXPECT_TRUE(p->remove("a"));
575
576 p->set("a", "b");
577 EXPECT_TRUE(p->exists("a"));
578 EXPECT_TRUE(value(*p, "a") == "b");
579 EXPECT_FALSE(p->remove("a"));
580 EXPECT_FALSE(p->exists("a"));
581 EXPECT_TRUE(p->remove("a"));
582
583 EXPECT_TRUE(p->raw_string() == "");
584
585 delete p;
586 }
587
588 // Tests iterating over options
589 TEST_F(PropertiesTest, IterationSize) {
590 dd::Properties *p = new dd::Properties_impl();
591
592 EXPECT_TRUE(p->size() == 0);
593
594 p->set("a", "b");
595 EXPECT_TRUE(p->size() == 1);
596
597 p->set("a", "b too");
598 EXPECT_TRUE(p->size() == 1);
599
600 p->set("b", "c");
601 EXPECT_TRUE(p->size() == 2);
602
603 p->set("c", "d");
604 EXPECT_TRUE(p->size() == 3);
605
606 EXPECT_FALSE(p->remove("a"));
607 EXPECT_TRUE(p->size() == 2);
608 EXPECT_TRUE(p->remove("a"));
609 EXPECT_TRUE(p->size() == 2);
610
611 p->set("", "");
612 EXPECT_TRUE(p->size() == 2);
613
614 int i = 0;
615 for (dd::Properties::iterator it = p->begin(); it != p->end(); ++it, ++i)
616 if (it->first == "b")
617 EXPECT_TRUE(it->second == "c");
618 else if (it->first == "c")
619 EXPECT_TRUE(it->second == "d");
620 else
621 EXPECT_TRUE(false);
622
623 EXPECT_TRUE(i == 2);
624
625 EXPECT_FALSE(p->remove("b"));
626 EXPECT_FALSE(p->remove("c"));
627 EXPECT_TRUE(p->remove(""));
628
629 EXPECT_TRUE(p->size() == 0);
630
631 for (dd::Properties::iterator it [[maybe_unused]] = p->begin();
632 it != p->end(); ++it, ++i)
633 EXPECT_TRUE(false);
634
635 delete p;
636 }
637
638 // Tests that valid integer conversions are handled
639 TEST_F(PropertiesTest, ValidIntConversions) {
640 int32 val_int32 = 0;
641 uint32 val_uint32 = 0;
642 int64 val_int64 = 0;
643 uint64 val_uint64 = 0;
644
645 const char *MAX_INT64_STR = "9223372036854775807";
646 const int64 MAX_INT64 = std::numeric_limits<int64>::max();
647 const char *MIN_INT64_STR = "-9223372036854775808";
648 const int64 MIN_INT64 = std::numeric_limits<int64>::min();
649
650 const char *MAX_UINT64_STR = "18446744073709551615";
651 const uint64 MAX_UINT64 = std::numeric_limits<uint64>::max();
652 const char *MIN_UINT64_STR = "0";
653 const uint64 MIN_UINT64 = 0;
654
655 const char *MAX_INT32_STR = "2147483647";
656 const int32 MAX_INT32 = std::numeric_limits<int32>::max();
657 const char *MIN_INT32_STR = "-2147483648";
658 const int32 MIN_INT32 = std::numeric_limits<int32>::min();
659
660 const char *MAX_UINT32_STR = "4294967295";
661 const uint32 MAX_UINT32 = std::numeric_limits<uint32>::max();
662 const char *MIN_UINT32_STR = "0";
663 const uint32 MIN_UINT32 = 0;
664
665 dd::Properties *p = new dd::Properties_impl();
666
667 // ======================================
668 // Positive test cases
669 // ======================================
670 // TC# Operation Source Target
671 // -----+------------+-----------+-------
672 // PTC1 Convert +num int64
673 // PTC2 Convert -num int64
674 // PTC3 Convert 0 int64
675 // PTC4 Convert MAX_INT64 int64
676 // PTC5 Convert MIN_INT64 int64
677 // -----+------------+-----------+-------
678 // PTC6 Convert +num uint64
679 // PTC7 Convert 0 uint64
680 // PTC8 Convert MAX_UINT32 uint64
681 // PTC9 Convert MIN_UINT32 uint64
682 // -----+------------+-----------+-------
683 // PTC10 Convert +num int32
684 // PTC11 Convert -num int32
685 // PTC12 Convert 0 int32
686 // PTC13 Convert MAX_INT32 int32
687 // PTC14 Convert MIN_INT32 int32
688 // -----+------------+-----------+-------
689 // PTC15 Convert +num uint32
690 // PTC16 Convert 0 uint32
691 // PTC17 Convert MAX_UINT32 uint32
692 // PTC18 Convert MIN_UINT32 uint32
693 // ======================================
694
695 // PTC1
696 EXPECT_TRUE(!p->from_str("123", &val_int64) && val_int64 == 123);
697 // PTC2
698 EXPECT_TRUE(!p->from_str("-123", &val_int64) && val_int64 == -123);
699 // PTC3
700 EXPECT_TRUE(!p->from_str("0", &val_int64) && val_int64 == 0);
701 // PTC4
702 EXPECT_TRUE(!p->from_str(MAX_INT64_STR, &val_int64) &&
703 val_int64 == MAX_INT64);
704 // PTC5
705 EXPECT_TRUE(!p->from_str(MIN_INT64_STR, &val_int64) &&
706 val_int64 == MIN_INT64);
707
708 // PTC6
709 EXPECT_TRUE(!p->from_str("123", &val_uint64) && val_uint64 == 123);
710 // PTC7
711 EXPECT_TRUE(!p->from_str("0", &val_uint64) && val_uint64 == 0);
712 // PTC8
713 EXPECT_TRUE(!p->from_str(MAX_UINT64_STR, &val_uint64) &&
714 val_uint64 == MAX_UINT64);
715 // PTC9
716 EXPECT_TRUE(!p->from_str(MIN_UINT64_STR, &val_uint64) &&
717 val_uint64 == MIN_UINT64);
718
719 // PTC10
720 EXPECT_TRUE(!p->from_str("123", &val_int32) && val_int32 == 123);
721 // PTC11
722 EXPECT_TRUE(!p->from_str("-123", &val_int32) && val_int32 == -123);
723 // PTC12
724 EXPECT_TRUE(!p->from_str("0", &val_int32) && val_int32 == 0);
725 // PTC13
726 EXPECT_TRUE(!p->from_str(MAX_INT32_STR, &val_int32) &&
727 val_int32 == MAX_INT32);
728 // PTC14
729 EXPECT_TRUE(!p->from_str(MIN_INT32_STR, &val_int32) &&
730 val_int32 == MIN_INT32);
731
732 // PTC15
733 EXPECT_TRUE(!p->from_str("123", &val_uint32) && val_uint32 == 123);
734 // PTC16
735 EXPECT_TRUE(!p->from_str("0", &val_uint32) && val_uint32 == 0);
736 // PTC17
737 EXPECT_TRUE(!p->from_str(MAX_UINT32_STR, &val_uint32) &&
738 val_uint32 == MAX_UINT32);
739 // PTC18
740 EXPECT_TRUE(!p->from_str(MIN_UINT32_STR, &val_uint32) &&
741 val_uint32 == MIN_UINT32);
742
743 delete p;
744 }
745
746 // Tests that integer conversion errors are handled
747 TEST_F(PropertiesTest, FailingIntConversions) {
748 int32 val_int32 = 0;
749 uint32 val_uint32 = 0;
750 int64 val_int64 = 0;
751 uint64 val_uint64 = 0;
752
753 const char *OFL_INT64_STR = "9223372036854775808";
754 const char *UFL_INT64_STR = "-9223372036854775809";
755
756 const char *OFL_UINT64_STR = "18446744073709551616";
757
758 const char *OFL_INT32_STR = "2147483648";
759 const char *UFL_INT32_STR = "-2147483649";
760
761 const char *OFL_UINT32_STR = "4294967296";
762
763 dd::Properties *p = new dd::Properties_impl();
764
765 // ======================================
766 // Negative test cases
767 // ======================================
768 // TC# Operation Source Target
769 // -----+------------+-----------+-------
770 // NTC1 Overflow OFL_INT64 int64
771 // NTC2 Underflow UFL_INT64 int64
772 // NTC3 Malformed str int64
773
774 // NTC4 Overflow OFL_UINT64 uint64
775 // NTC5 Malformed str uint64
776 // NTC6 Sign -num uint64
777
778 // NTC7 Overflow OFL_INT32 int32
779 // NTC8 Underflow UFL_INT32 int32
780 // NTC9 Malformed str int32
781
782 // NTC10 Overflow OFL_UINT32 uint32
783 // NTC11 Malformed str uint32
784 // NTC12 Sign -num uint32
785 // ======================================
786
787 // NTC1
788 EXPECT_TRUE(p->from_str(OFL_INT64_STR, &val_int64));
789 // NTC2
790 EXPECT_TRUE(p->from_str(UFL_INT64_STR, &val_int64));
791 // NTC3
792 EXPECT_TRUE(p->from_str("abc", &val_int64));
793
794 // NTC4
795 EXPECT_TRUE(p->from_str(OFL_UINT64_STR, &val_uint64));
796 // NTC5
797 EXPECT_TRUE(p->from_str("abc", &val_uint64));
798 // NTC6
799 EXPECT_TRUE(p->from_str("-1", &val_uint64));
800
801 // NTC7
802 EXPECT_TRUE(p->from_str(OFL_INT32_STR, &val_int32));
803 // NTC8
804 EXPECT_TRUE(p->from_str(UFL_INT32_STR, &val_int32));
805 // NTC9
806 EXPECT_TRUE(p->from_str("abc", &val_int32));
807
808 // NTC10
809 EXPECT_TRUE(p->from_str(OFL_UINT32_STR, &val_uint32));
810 // NTC11
811 EXPECT_TRUE(p->from_str("abc", &val_uint32));
812 // NTC12
813 EXPECT_TRUE(p->from_str("-1", &val_uint32));
814
815 delete p;
816 }
817
818 // Tests that valid boolean conversions are handled
819 TEST_F(PropertiesTest, ValidBoolConversions) {
820 bool val = false;
821 dd::Properties *p = new dd::Properties_impl();
822
823 const char *MIN_INT64_STR = "-9223372036854775808";
824 const char *MAX_INT64_STR = "9223372036854775807";
825 const char *MAX_UINT64_STR = "18446744073709551615";
826 const char *OFL_INT64_STR = "9223372036854775808";
827
828 // =======================================
829 // Positive test cases
830 // =======================================
831 // TC# Operation Source Target
832 // -----+------------+-------------+------
833 // PTC1 Convert true bool
834 // PTC2 Convert 1 bool
835 // PTC3 Convert true bool
836 // PTC4 Convert 0 bool
837 // PTC5 Convert MIN_INT64_STR bool
838 // PTC6 Convert MAX_INT64_STR bool
839 // PTC7 Convert MAX_UINT64_STR bool
840 // PTC8 Convert OFL_INT64_STR bool
841 // =======================================
842
843 // PTC1
844 EXPECT_TRUE(!p->from_str("true", &val) && val == true);
845 // PTC2
846 EXPECT_TRUE(!p->from_str("1", &val) && val == true);
847 // PTC3
848 EXPECT_TRUE(!p->from_str("false", &val) && val == false);
849 // PTC4
850 EXPECT_TRUE(!p->from_str("0", &val) && val == false);
851 // PTC5
852 EXPECT_TRUE(!p->from_str(MIN_INT64_STR, &val) && val == true);
853 // PTC6
854 EXPECT_TRUE(!p->from_str(MAX_INT64_STR, &val) && val == true);
855 // PTC7
856 EXPECT_TRUE(!p->from_str(MAX_UINT64_STR, &val) && val == true);
857 // PTC8
858 EXPECT_TRUE(!p->from_str(OFL_INT64_STR, &val) && val == true);
859
860 delete p;
861 }
862
863 // Tests that boolean conversion errors are handled
864 TEST_F(PropertiesTest, FailingBoolConversions) {
865 bool val = false;
866 dd::Properties *p = new dd::Properties_impl();
867
868 const char *UFL_INT64_STR = "-9223372036854775809";
869 const char *OFL_UINT64_STR = "18446744073709551616";
870
871 // =======================================
872 // Negative test cases
873 // =======================================
874 // TC# Operation Source Target
875 // -----+------------+-------------+------
876 // NTC1 Convert TRUE bool
877 // NTC2 Convert FALSE bool
878 // NTC3 Convert <empty> bool
879 // NTC4 Convert ttrue bool
880 // NTC5 Convert truee bool
881 // NTC6 Convert ffalse bool
882 // NTC7 Convert falsee bool
883 // NTC8 Convert UFL_INT64_STR bool
884 // NTC9 Convert OFL_UINT64_STR bool
885 // =======================================
886
887 // NTC1
888 EXPECT_TRUE(p->from_str("TRUE", &val));
889 // NTC2
890 EXPECT_TRUE(p->from_str("FALSE", &val));
891 // NTC3
892 EXPECT_TRUE(p->from_str("", &val));
893 // NTC4
894 EXPECT_TRUE(p->from_str("ttrue", &val));
895 // NTC5
896 EXPECT_TRUE(p->from_str("truee", &val));
897 // NTC6
898 EXPECT_TRUE(p->from_str("ffalse", &val));
899 // NTC7
900 EXPECT_TRUE(p->from_str("falsee", &val));
901 // NTC8
902 EXPECT_TRUE(p->from_str(UFL_INT64_STR, &val));
903 // NTC9
904 EXPECT_TRUE(p->from_str(OFL_UINT64_STR, &val));
905
906 delete p;
907 }
908
909 // Invoke conversion functions through class name
910 TEST_F(PropertiesTest, StaticConversionMethods) {
911 int32 val_int32 = 0;
912 uint32 val_uint32 = 0;
913 int64 val_int64 = 0;
914 uint64 val_uint64 = 0;
915 bool maybe = false;
916
917 EXPECT_TRUE(!dd::Properties::from_str("-123", &val_int64) &&
918 val_int64 == -123);
919
920 EXPECT_TRUE(dd::Properties::from_str("-123", &val_uint64));
921 EXPECT_TRUE(!dd::Properties::from_str("0", &val_uint64) && val_uint64 == 0);
922
923 EXPECT_TRUE(!dd::Properties::from_str("-123", &val_int32) &&
924 val_int32 == -123);
925
926 EXPECT_TRUE(dd::Properties::from_str("-123", &val_uint32));
927 EXPECT_TRUE(!dd::Properties::from_str("0", &val_uint32) && val_uint32 == 0);
928
929 EXPECT_TRUE(!dd::Properties::from_str("true", &maybe) && maybe == true);
930 EXPECT_TRUE(!dd::Properties::from_str("1", &maybe) && maybe == true);
931 EXPECT_TRUE(!dd::Properties::from_str("false", &maybe) && maybe == false);
932 EXPECT_TRUE(!dd::Properties::from_str("0", &maybe) && maybe == false);
933
934 EXPECT_TRUE(dd::Properties::from_str("", &maybe));
935 }
936
937 // Test assignment operator for deep copy of Property objects
938 TEST_F(PropertiesTest, Assign) {
939 dd::Properties *p = new dd::Properties_impl();
940 p->set("a", 1);
941
942 // Assign to a different object
943 dd::Properties *p_copy = new dd::Properties_impl();
944 p_copy->insert_values(*p);
945
946 // The "a" key should be present with the same value in both objects
947 int32 val_p = 0;
948 int32 val_p_copy = 0;
949 EXPECT_TRUE(!p->get("a", &val_p) && val_p == 1 &&
950 !p_copy->get("a", &val_p_copy) && val_p_copy == 1);
951
952 // Changing the value in one object should not be reflected in the other
953 p->set("a", 2);
954 EXPECT_TRUE(!p->get("a", &val_p) && val_p == 2 &&
955 !p_copy->get("a", &val_p_copy) && val_p_copy == 1);
956 delete p_copy;
957 delete p;
958 }
959
960 TEST_F(PropertiesTest, ValidKeys) {
961 // Create a property object with valid keys.
962 dd::Properties_impl p({"a"});
963
964 EXPECT_FALSE(p.set("a", "1"));
965 #if defined(NDEBUG)
966 EXPECT_TRUE(p.set("b", "2"));
967 #else
968 EXPECT_DEATH_IF_SUPPORTED(p.set("b", "2"), ".*UTC - mysqld got.*");
969 #endif
970 EXPECT_TRUE(p.raw_string() == "a=1;");
971 // Adding a valid key makes it possible to set it.
972 p.add_valid_keys({"b"});
973 EXPECT_FALSE(p.set("b", "2"));
974 EXPECT_TRUE(p.raw_string() == "a=1;b=2;");
975
976 // Clearing the valid keys still gives us the
977 // same raw string.
978 p.clear_valid_keys();
979 EXPECT_TRUE(p.raw_string() == "a=1;b=2;");
980
981 // And we can get the keys present.
982 dd::String_type str;
983 EXPECT_TRUE(!p.get("b", &str) && str == "2");
984
985 #if !defined(NDEBUG)
986 // Then adding a valid key asserts in dbug, though, because
987 // we might then hide keys present.
988 EXPECT_DEATH_IF_SUPPORTED(p.add_valid_keys({"c"}), ".*UTC - mysqld got.*");
989 #endif
990
991 // Removing all key-values will
992 // allow us to add a valid key, though.
993 p.clear();
994 p.clear_valid_keys();
995 p.add_valid_keys({"c"});
996
997 // Adding another valid key is allowed even if there are
998 // key-values present as long as there is already
999 // a set of valid keys.
1000 EXPECT_FALSE(p.set("c", "3"));
1001 p.add_valid_keys({"c"});
1002
1003 // Copying values from a raw string will skip invalid keys.
1004 p.clear();
1005 p.add_valid_keys({"b"});
1006 p.insert_values("a=1;b=2;");
1007 p.clear_valid_keys();
1008 EXPECT_TRUE(p.raw_string() == "b=2;");
1009 EXPECT_TRUE(p.size() == 1);
1010
1011 // Copying values from another property object will skip keys
1012 // that are invalid.
1013 p.clear();
1014 p.add_valid_keys({"b", "c"});
1015 dd::Properties_impl p_other;
1016 EXPECT_FALSE(p_other.set("a", "1_other"));
1017 EXPECT_FALSE(p_other.set("b", "2_other"));
1018 EXPECT_FALSE(p_other.set("c", "3_other"));
1019 EXPECT_TRUE(p_other.raw_string() == "a=1_other;b=2_other;c=3_other;");
1020
1021 // Now, only "b" and "c" will be copied, since "a" is invalid in p.
1022 p.insert_values(p_other);
1023 p.clear_valid_keys();
1024 EXPECT_TRUE(p.raw_string() == "b=2_other;c=3_other;");
1025 }
1026
1027 } // namespace dd_properties_unittest