| 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 | /* See http://code.google.com/p/googletest/wiki/Primer */ |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "storage/innobase/include/univ.i" |
| 29 | #include "storage/innobase/include/ut0mem.h" |
| 30 | |
| 31 | namespace innodb_ut0mem_unittest { |
| 32 | |
| 33 | /* test ut_str_sql_format() */ |
| 34 | TEST(ut0mem, utstrsqlformat) { |
| 35 | const char *buf_initial = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
| 36 | ulint buf_initial_sz = strlen(buf_initial) + 1; |
| 37 | char buf[64]; |
| 38 | ulint ret; |
| 39 | struct { |
| 40 | const char *in; |
| 41 | ulint in_len; |
| 42 | ulint buf_size; |
| 43 | ulint ret_expected; |
| 44 | const char *buf_expected; |
| 45 | } test_data[] = { |
| 46 | {"abcd", 4, 0, 0, buf_initial}, |
| 47 | {"abcd", 4, 1, 1, ""}, |
| 48 | {"abcd", 4, 2, 1, ""}, |
| 49 | {"abcd", 0, 3, 3, "''"}, |
| 50 | {"abcd", 1, 3, 1, ""}, |
| 51 | {"abcd", 2, 3, 1, ""}, |
| 52 | {"abcd", 3, 3, 1, ""}, |
| 53 | {"abcd", 4, 3, 1, ""}, |
| 54 | {"abcd", 0, 4, 3, "''"}, |
| 55 | {"abcd", 1, 4, 4, "'a'"}, |
| 56 | {"abcd", 2, 4, 4, "'a'"}, |
| 57 | {"abcd", 3, 4, 4, "'a'"}, |
| 58 | {"abcd", 4, 4, 4, "'a'"}, |
| 59 | {"abcde", 5, 4, 4, "'a'"}, |
| 60 | {"'", 1, 4, 3, "''"}, |
| 61 | {"''", 2, 4, 3, "''"}, |
| 62 | {"a'", 2, 4, 4, "'a'"}, |
| 63 | {"'a", 2, 4, 3, "''"}, |
| 64 | {"ab", 2, 4, 4, "'a'"}, |
| 65 | {"abcdef", 0, 5, 3, "''"}, |
| 66 | {"abcdef", 1, 5, 4, "'a'"}, |
| 67 | {"abcdef", 2, 5, 5, "'ab'"}, |
| 68 | {"abcdef", 3, 5, 5, "'ab'"}, |
| 69 | {"abcdef", 4, 5, 5, "'ab'"}, |
| 70 | {"abcdef", 5, 5, 5, "'ab'"}, |
| 71 | {"abcdef", 6, 5, 5, "'ab'"}, |
| 72 | {"'", 1, 5, 5, "''''"}, |
| 73 | {"''", 2, 5, 5, "''''"}, |
| 74 | {"a'", 2, 5, 4, "'a'"}, |
| 75 | {"'a", 2, 5, 5, "''''"}, |
| 76 | {"ab", 2, 5, 5, "'ab'"}, |
| 77 | {"abc", 3, 5, 5, "'ab'"}, |
| 78 | {"ab", 2, 6, 5, "'ab'"}, |
| 79 | {"a'b'c", 5, 32, 10, "'a''b''c'"}, |
| 80 | {"a'b'c'", 6, 32, 12, "'a''b''c'''"}, |
| 81 | }; |
| 82 | |
| 83 | for (ulint i = 0; i < UT_ARR_SIZE(test_data); i++) { |
| 84 | memcpy(buf, buf_initial, buf_initial_sz); |
| 85 | |
| 86 | ret = ut_str_sql_format(test_data[i].in, test_data[i].in_len, buf, |
| 87 | test_data[i].buf_size); |
| 88 | |
| 89 | EXPECT_EQ(test_data[i].ret_expected, ret); |
| 90 | EXPECT_STREQ(test_data[i].buf_expected, buf); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace innodb_ut0mem_unittest |