| 1 | /* |
| 2 | Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License, version 2.0, |
| 6 | as published by the Free Software Foundation. |
| 7 | |
| 8 | This program is designed to work with certain software (including |
| 9 | but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | as designated in a particular file or component or in included license |
| 11 | documentation. The authors of MySQL hereby grant you an additional |
| 12 | permission to link the program and your derivative works with the |
| 13 | separately licensed software that they have either included with |
| 14 | the program or referenced in the documentation. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License, version 2.0, for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | #include <gmock/gmock-matchers.h> |
| 27 | #include <gtest/gtest.h> |
| 28 | #include <plugin/keyring/converter.h> |
| 29 | #include <algorithm> |
| 30 | #include <map> |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
| 34 | namespace keyring_converter_unittest { |
| 35 | |
| 36 | using namespace keyring; |
| 37 | using ::testing::StrEq; |
| 38 | using Arch = Converter::Arch; |
| 39 | |
| 40 | class Converter_test : public ::testing::Test {}; |
| 41 | |
| 42 | class KeyData { |
| 43 | public: |
| 44 | KeyData(const char *id, const char *type, const char *user_id, |
| 45 | const char *data) |
| 46 | : id(id), type(type), user_id(user_id), data(data) {} |
| 47 | |
| 48 | std::string serialize(Arch arch) const { |
| 49 | std::string output; |
| 50 | |
| 51 | size_t width = Converter::get_width(arch); |
| 52 | |
| 53 | // key data lengths |
| 54 | std::vector<size_t> lengths = {id.length(), type.length(), user_id.length(), |
| 55 | data.length()}; |
| 56 | |
| 57 | // append lengths |
| 58 | char src[8] = {0}; |
| 59 | char dst[8] = {0}; |
| 60 | for (auto const &length : lengths) { |
| 61 | memcpy(src, &length, sizeof(size_t)); |
| 62 | Converter::convert(src, dst, Converter::get_native_arch(), arch); |
| 63 | output.append(dst, width); |
| 64 | } |
| 65 | |
| 66 | // append data |
| 67 | output += id + type + user_id + data; |
| 68 | |
| 69 | // calculate total length and append padding |
| 70 | size_t total = width + output.length(); |
| 71 | size_t padding = (width - total % width) % width; |
| 72 | output.append(padding, '\0'); |
| 73 | total += padding; |
| 74 | |
| 75 | // convert total |
| 76 | memcpy(src, &total, sizeof(size_t)); |
| 77 | Converter::convert(src, dst, Converter::get_native_arch(), arch); |
| 78 | output = std::string(dst, width) + output; |
| 79 | |
| 80 | return output; |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | std::string id, type, user_id, data; |
| 85 | }; |
| 86 | |
| 87 | /** |
| 88 | tests conversion between various width/endianess binary representations |
| 89 | */ |
| 90 | TEST_F(Converter_test, LengthConversion) { |
| 91 | struct Conversion { |
| 92 | Arch src, dst; |
| 93 | const std::string source, expected; |
| 94 | }; |
| 95 | |
| 96 | // list of valid conversions and expected results |
| 97 | Conversion valid_tasks[] = { |
| 98 | |
| 99 | // no conversion |
| 100 | {Arch::LE_32, Arch::LE_32, "\x12\x34\x56\x78", "\x12\x34\x56\x78"}, |
| 101 | {Arch::BE_32, Arch::BE_32, "\x12\x34\x56\x78", "\x12\x34\x56\x78"}, |
| 102 | { |
| 103 | Arch::LE_64, |
| 104 | Arch::LE_64, |
| 105 | "\x12\x34\x56\x78\x12\x34\x56\x78", |
| 106 | "\x12\x34\x56\x78\x12\x34\x56\x78", |
| 107 | }, |
| 108 | { |
| 109 | Arch::BE_64, |
| 110 | Arch::BE_64, |
| 111 | "\x12\x34\x56\x78\x12\x34\x56\x78", |
| 112 | "\x12\x34\x56\x78\x12\x34\x56\x78", |
| 113 | } |
| 114 | |
| 115 | // 32 bit endianess conversions |
| 116 | , |
| 117 | {Arch::LE_32, Arch::BE_32, "\x12\x34\x56\x78", "\x78\x56\x34\x12"}, |
| 118 | {Arch::BE_32, Arch::LE_32, "\x12\x34\x56\x78", "\x78\x56\x34\x12"} |
| 119 | |
| 120 | // 64 bit endianess conversions |
| 121 | , |
| 122 | {Arch::LE_64, Arch::BE_64, "\x12\x34\x56\x78\x12\x34\x56\x78", |
| 123 | "\x78\x56\x34\x12\x78\x56\x34\x12"}, |
| 124 | {Arch::BE_64, Arch::LE_64, "\x12\x34\x56\x78\x12\x34\x56\x78", |
| 125 | "\x78\x56\x34\x12\x78\x56\x34\x12"} |
| 126 | |
| 127 | // growth conversions |
| 128 | , |
| 129 | {Arch::LE_32, |
| 130 | Arch::LE_64, |
| 131 | "\x12\x34\x56\x78", |
| 132 | {"\x12\x34\x56\x78\x00\x00\x00\x00", 8}}, |
| 133 | {Arch::BE_32, Arch::LE_64, "\x12\x34\x56\x78", |
| 134 | "\x00\x00\x00\x00\x78\x56\x34\x12"}, |
| 135 | {Arch::LE_32, Arch::BE_64, "\x12\x34\x56\x78", |
| 136 | "\x00\x00\x00\x00\x78\x56\x34\x12"}, |
| 137 | {Arch::BE_32, Arch::BE_64, "\x12\x34\x56\x78", |
| 138 | "\x00\x00\x00\x00\x12\x34\x56\x78"} |
| 139 | |
| 140 | // shrink conversions |
| 141 | , |
| 142 | {Arch::LE_64, |
| 143 | Arch::LE_32, |
| 144 | {"\x12\x34\x56\x78\x00\x00\x00\x00", 8}, |
| 145 | "\x12\x34\x56\x78"}, |
| 146 | {Arch::LE_64, |
| 147 | Arch::BE_32, |
| 148 | {"\x12\x34\x56\x78\x00\x00\x00\x00", 8}, |
| 149 | "\x78\x56\x34\x12"}, |
| 150 | {Arch::BE_64, |
| 151 | Arch::LE_32, |
| 152 | {"\x00\x00\x00\x00\x12\x34\x56\x78", 8}, |
| 153 | "\x78\x56\x34\x12"}, |
| 154 | {Arch::BE_64, |
| 155 | Arch::BE_32, |
| 156 | {"\x00\x00\x00\x00\x12\x34\x56\x78", 8}, |
| 157 | "\x12\x34\x56\x78"}}; |
| 158 | |
| 159 | char result[8] = {0}; |
| 160 | for (auto &task : valid_tasks) { |
| 161 | // convert task |
| 162 | auto length = |
| 163 | Converter::convert(task.source.c_str(), result, task.src, task.dst); |
| 164 | |
| 165 | // there should be no errors |
| 166 | EXPECT_NE(length, 0); |
| 167 | |
| 168 | // result should match expected value |
| 169 | EXPECT_EQ(strncmp(result, task.expected.c_str(), task.expected.length()), |
| 170 | 0); |
| 171 | } |
| 172 | |
| 173 | // list of invalid conversions |
| 174 | Conversion invalid_tasks[] = { |
| 175 | // shrink conversions with values > 32bit |
| 176 | {Arch::LE_64, Arch::LE_32, {"\x12\x34\x56\x78\x90\x00\x00\x00", 8}, ""}, |
| 177 | {Arch::LE_64, Arch::BE_32, {"\x12\x34\x56\x78\x00\x90\x00\x00", 8}, ""}, |
| 178 | {Arch::BE_64, Arch::LE_32, {"\x00\x90\x00\x00\x12\x34\x56\x78", 8}, ""}, |
| 179 | {Arch::BE_64, Arch::BE_32, {"\x90\x00\x00\x00\x12\x34\x56\x78", 8}, ""}}; |
| 180 | |
| 181 | // each invalid task should cause errors |
| 182 | for (auto &task : invalid_tasks) |
| 183 | EXPECT_EQ( |
| 184 | Converter::convert(task.source.c_str(), result, task.src, task.dst), 0); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | tests conversion of key buffer examples |
| 189 | */ |
| 190 | TEST_F(Converter_test, KeyDataConversion) { |
| 191 | const std::vector<KeyData> keys = { |
| 192 | {"Key1", "AES", "Module1", "Key1DataString"}, |
| 193 | {"Key2", "RSA", "Module2", "Key2DataStringLonger"}, |
| 194 | {"Key3", "DES", "Module3", "Key3DataStringLongest"}}; |
| 195 | |
| 196 | const std::vector<Arch> order = {Arch::LE_32, Arch::BE_32, Arch::LE_64, |
| 197 | Arch::BE_64}; |
| 198 | |
| 199 | // generate keyring file representations |
| 200 | std::map<Arch, std::string> files; |
| 201 | for (auto const &arch : order) |
| 202 | for (auto const &key : keys) files[arch] += key.serialize(arch); |
| 203 | |
| 204 | // verify conversions |
| 205 | std::string result; |
| 206 | for (auto const &arch1 : order) |
| 207 | for (auto const &arch2 : order) { |
| 208 | // at least one arch has to be native |
| 209 | if (arch1 != Converter::get_native_arch() && |
| 210 | arch2 != Converter::get_native_arch()) |
| 211 | continue; |
| 212 | |
| 213 | // conversion has to be free of errors |
| 214 | EXPECT_FALSE(Converter::convert_data( |
| 215 | files[arch1].c_str(), files[arch1].length(), arch1, arch2, result)); |
| 216 | |
| 217 | // conversion result has to match destination format |
| 218 | EXPECT_EQ(result, files[arch2]); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | } // namespace keyring_converter_unittest |