| 1 | /* Copyright (c) 2022, 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 | |
| 25 | #include "sql/xa.h" |
| 26 | #include "sql/xa/xid_extract.h" |
| 27 | |
| 28 | #include <gmock/gmock.h> |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | namespace xid_extract_unittests { |
| 32 | |
| 33 | class XID_extract_test : public ::testing::Test { |
| 34 | protected: |
| 35 | XID_extract_test() = default; |
| 36 | virtual void SetUp() {} |
| 37 | virtual void TearDown() {} |
| 38 | }; |
| 39 | |
| 40 | static void expect_valid(xa::XID_extractor &tokenizer, |
| 41 | std::string const &to_validate, char const *gtrid, |
| 42 | size_t gtrid_len, char const *bqual, size_t bqual_len, |
| 43 | int64_t format_id) { |
| 44 | EXPECT_EQ(1, tokenizer.extract(to_validate)); |
| 45 | |
| 46 | if (tokenizer.size() != 0) { |
| 47 | XID valid; |
| 48 | valid.set(format_id, gtrid, gtrid_len, bqual, bqual_len); |
| 49 | std::ostringstream oss; |
| 50 | oss << valid << std::flush; |
| 51 | std::string valid_str = oss.str(); |
| 52 | oss.str(""); |
| 53 | oss << tokenizer[0] << std::flush; |
| 54 | std::string to_validate_str = oss.str(); |
| 55 | |
| 56 | EXPECT_EQ(valid, tokenizer[0]); |
| 57 | EXPECT_EQ(valid_str, to_validate_str); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | TEST_F(XID_extract_test, Input_Output_test) { |
| 62 | std::string extract_from{ |
| 63 | "X'feedaaaa0000',X'beefaaaa0000',2021 XA COMMIT X'32adfe873947' " |
| 64 | ",\tX'2aaddef767782001'\n,\n1; XA " |
| 65 | "ROLLBACK X'23' , X'12'\n,\t1;X'as X' " |
| 66 | "\n something something X'',X'',1 X'' got it"}; |
| 67 | |
| 68 | xa::XID_extractor tokenizer{extract_from}; |
| 69 | EXPECT_EQ(4, tokenizer.size()); |
| 70 | for (auto token : tokenizer) { |
| 71 | EXPECT_EQ(false, token.is_null()); |
| 72 | } |
| 73 | |
| 74 | std::string hex64; |
| 75 | std::string str64; |
| 76 | for (int i = 0; i < 64; ++i) { |
| 77 | str64 += '\0'; |
| 78 | hex64 += "00"; |
| 79 | } |
| 80 | |
| 81 | // normal xid |
| 82 | expect_valid(tokenizer, "X'aABb' , X'1234', 2", "\xaa\xbb", 2, "\x12\x34", 2, |
| 83 | 2); |
| 84 | // zero length strings |
| 85 | expect_valid(tokenizer, "X'',X'',2", "", 0, "", 0, 2); |
| 86 | // whitespace |
| 87 | expect_valid(tokenizer, " X'' , X'' , 1 ", "", 0, "", 0, 1); |
| 88 | // zero-valued bytes |
| 89 | expect_valid(tokenizer, "X'00', X'0000', 1", "\x00", 1, "\x00\x00", 2, 1); |
| 90 | // 64 byte length |
| 91 | expect_valid(tokenizer, "X'" + hex64 + "', X'" + hex64 + "', 1", str64.data(), |
| 92 | 64, str64.data(), 64, 1); |
| 93 | // formatid 0 |
| 94 | expect_valid(tokenizer, "X'', X'', 0", "", 0, "", 0, 0); |
| 95 | // formatid 2^31-1 |
| 96 | expect_valid(tokenizer, "X'', X'', 2147483647", "", 0, "", 0, |
| 97 | std::numeric_limits<int32_t>::max()); |
| 98 | |
| 99 | // xid_t::operator!= test |
| 100 | XID xid; |
| 101 | xid.set(2, "", 0, "", 0); |
| 102 | tokenizer.extract("X'',X'',1"); |
| 103 | EXPECT_TRUE(xid != tokenizer[0]); |
| 104 | |
| 105 | // ivalid formatid |
| 106 | EXPECT_EQ(0, tokenizer.extract("X'', X'', MYSQL")); |
| 107 | // out of range formatid |
| 108 | EXPECT_EQ(0, tokenizer.extract("X'', X'', -1")); |
| 109 | EXPECT_EQ(0, tokenizer.extract("X'', X'', -2")); |
| 110 | EXPECT_EQ(0, tokenizer.extract("X'', X'', 9223372036854775808")); |
| 111 | EXPECT_EQ(0, tokenizer.extract("X'', X'', 10000000000000000000")); |
| 112 | EXPECT_EQ(0, tokenizer.extract("X'', X'', 20000000000000000000")); |
| 113 | // whitespace in hex string |
| 114 | EXPECT_EQ(0, tokenizer.extract("X' a 1', X'', 1")); |
| 115 | EXPECT_EQ(0, tokenizer.extract("X' ', X'', 1")); |
| 116 | // whitespace between X and ' |
| 117 | EXPECT_EQ(0, tokenizer.extract("X 'a1', X'', 1")); |
| 118 | EXPECT_EQ(0, tokenizer.extract("X'a1', X '', 1")); |
| 119 | // garbage between strings |
| 120 | EXPECT_EQ(0, tokenizer.extract("XA COMMIT X'32' hello! ,X'32' ['world'],2'")); |
| 121 | // missing commas |
| 122 | EXPECT_EQ(0, tokenizer.extract("X'' X'' 1")); |
| 123 | // odd-length hex strings |
| 124 | EXPECT_EQ(0, tokenizer.extract("X'0', X'', 1")); |
| 125 | EXPECT_EQ(0, tokenizer.extract("X'', X'123', 1")); |
| 126 | // missing commas |
| 127 | EXPECT_EQ(0, tokenizer.extract("XA COMMIT X'32' X'32' 2")); |
| 128 | // garbage and extra commas between strings |
| 129 | EXPECT_EQ(0, tokenizer.extract("XA COMMIT X'32', X, X'32', 2")); |
| 130 | // garbage and extra non-hex strings between strings |
| 131 | EXPECT_EQ(0, tokenizer.extract("XA COMMIT X'32', 'foo', X'32', 2")); |
| 132 | // Too long string |
| 133 | EXPECT_EQ(0, tokenizer.extract("X'" + hex64 + "01', X'', 1")); |
| 134 | EXPECT_EQ(0, tokenizer.extract("X'', X'" + hex64 + "01', 1")); |
| 135 | EXPECT_EQ(0, tokenizer.extract("X'" + hex64 + hex64 + hex64 + hex64 + |
| 136 | "01', X'', 1")); |
| 137 | EXPECT_EQ(0, tokenizer.extract("X'', X'" + hex64 + hex64 + hex64 + hex64 + |
| 138 | "01', 1")); |
| 139 | } |
| 140 | |
| 141 | } // namespace xid_extract_unittests |