| 1 | /* Copyright (c) 2021, 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 <string> |
| 25 | |
| 26 | #include "components/keyrings/common/json_data/json_reader.h" |
| 27 | |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | namespace json_reader_unitest { |
| 31 | |
| 32 | class KeyringCommonJsonReader_test : public ::testing::Test {}; |
| 33 | |
| 34 | using keyring_common::json_data::Json_data_extension; |
| 35 | using keyring_common::json_data::Json_reader; |
| 36 | using keyring_common::json_data::output_vector; |
| 37 | using std::string; |
| 38 | |
| 39 | TEST_F(KeyringCommonJsonReader_test, JsonReaderTest) { |
| 40 | std::string data; |
| 41 | data.assign( |
| 42 | "{" |
| 43 | " \"version\": \"1.0\"," |
| 44 | " \"elements\": [" |
| 45 | " {" |
| 46 | " \"user\": \"foo@bar\"," |
| 47 | " \"data_id\": \"key1\"," |
| 48 | " \"data_type\": \"AES\"," |
| 49 | " \"data\": \"6162636465666768696a6b6c6d6e6f70\"," |
| 50 | " \"extension\": []" |
| 51 | " }," |
| 52 | " {" |
| 53 | " \"user\": \"bar@foo\"," |
| 54 | " \"data_id\": \"key1\"," |
| 55 | " \"data_type\": \"RSA\"," |
| 56 | " \"data\": \"7172737475767778\"," |
| 57 | " \"extension\": []" |
| 58 | " }," |
| 59 | " {" |
| 60 | " \"user\": \"\"," |
| 61 | " \"data_id\": \"master_key\"," |
| 62 | " \"data_type\": \"AES\"," |
| 63 | " \"data\": \"797a6162636465666768696a6b6c6d6e\"," |
| 64 | " \"extension\": []" |
| 65 | " }" |
| 66 | " ]" |
| 67 | "}"); |
| 68 | Json_reader json_reader(data); |
| 69 | ASSERT_TRUE(json_reader.valid()); |
| 70 | string expected_version("1.0"); |
| 71 | ASSERT_TRUE(json_reader.version() == expected_version); |
| 72 | ASSERT_TRUE(json_reader.num_elements() == 3); |
| 73 | |
| 74 | output_vector output; |
| 75 | ASSERT_FALSE(json_reader.get_elements(output)); |
| 76 | ASSERT_TRUE(output.size() == 3); |
| 77 | auto element = output[0].first; |
| 78 | |
| 79 | ASSERT_TRUE(element.first.owner_id() == "foo@bar"); |
| 80 | ASSERT_TRUE(element.first.key_id() == "key1"); |
| 81 | ASSERT_TRUE(element.second.data() == "abcdefghijklmnop"); |
| 82 | ASSERT_TRUE(element.second.type() == "AES"); |
| 83 | |
| 84 | auto ext = output[0].second.get(); |
| 85 | ASSERT_TRUE(ext->version() == "1.0"); |
| 86 | |
| 87 | element = output[1].first; |
| 88 | ASSERT_TRUE(element.first.owner_id() == "bar@foo"); |
| 89 | ASSERT_TRUE(element.first.key_id() == "key1"); |
| 90 | ASSERT_TRUE(element.second.data() == "qrstuvwx"); |
| 91 | ASSERT_TRUE(element.second.type() == "RSA"); |
| 92 | |
| 93 | ext = output[1].second.get(); |
| 94 | ASSERT_TRUE(ext->version() == "1.0"); |
| 95 | |
| 96 | element = output[2].first; |
| 97 | ASSERT_TRUE(element.first.owner_id() == ""); |
| 98 | ASSERT_TRUE(element.first.key_id() == "master_key"); |
| 99 | ASSERT_TRUE(element.second.data() == "yzabcdefghijklmn"); |
| 100 | ASSERT_TRUE(element.second.type() == "AES"); |
| 101 | |
| 102 | ext = output[2].second.get(); |
| 103 | ASSERT_TRUE(ext->version() == "1.0"); |
| 104 | } |
| 105 | |
| 106 | } // namespace json_reader_unitest |