| 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 <cstdio> /* std::remove */ |
| 25 | |
| 26 | #include <components/keyrings/common/json_data/json_reader.h> |
| 27 | #include <components/keyrings/common/json_data/json_writer.h> |
| 28 | #include "components/keyrings/common/data_file/reader.h" |
| 29 | #include "components/keyrings/common/data_file/writer.h" |
| 30 | |
| 31 | #include <gtest/gtest.h> |
| 32 | |
| 33 | namespace json_file_operations_unittest { |
| 34 | |
| 35 | class KeyringCommonJsonFileOperations_test : public ::testing::Test {}; |
| 36 | |
| 37 | using keyring_common::data::Data; |
| 38 | using keyring_common::data_file::File_reader; |
| 39 | using keyring_common::data_file::File_writer; |
| 40 | using keyring_common::json_data::Json_data_extension; |
| 41 | using keyring_common::json_data::Json_reader; |
| 42 | using keyring_common::json_data::Json_writer; |
| 43 | using keyring_common::json_data::output_vector; |
| 44 | using keyring_common::meta::Metadata; |
| 45 | |
| 46 | TEST_F(KeyringCommonJsonFileOperations_test, JsonFileTests) { |
| 47 | /* Write JSON data */ |
| 48 | Json_writer json_writer; |
| 49 | Json_data_extension json_data_extension; |
| 50 | { |
| 51 | Metadata metadata("key1", "foo@bar"); |
| 52 | Data data("abcdefghijklmnop", "AES"); |
| 53 | ASSERT_FALSE(json_writer.add_element(metadata, data, json_data_extension)); |
| 54 | } |
| 55 | { |
| 56 | Metadata metadata("key1", "bar@foo"); |
| 57 | Data data("qrstuvwx", "RSA"); |
| 58 | ASSERT_FALSE(json_writer.add_element(metadata, data, json_data_extension)); |
| 59 | } |
| 60 | { |
| 61 | Metadata metadata("master_key", ""); |
| 62 | Data data("yzabcdefghijklmn", "AES"); |
| 63 | ASSERT_FALSE(json_writer.add_element(metadata, data, json_data_extension)); |
| 64 | } |
| 65 | |
| 66 | /* Write JSON data to file */ |
| 67 | std::string file_name("json_file_operations_test"); |
| 68 | std::string json_data = json_writer.to_string(); |
| 69 | ASSERT_TRUE(json_data.length() > 0); |
| 70 | |
| 71 | File_writer file_writer(file_name, json_data); |
| 72 | ASSERT_TRUE(file_writer.valid()); |
| 73 | |
| 74 | /* Read JSON data from file */ |
| 75 | std::string read_data; |
| 76 | File_reader file_reader(file_name, true, read_data); |
| 77 | ASSERT_TRUE(file_reader.valid()); |
| 78 | |
| 79 | ASSERT_TRUE(read_data == json_data); |
| 80 | |
| 81 | /* Move data to JSON document */ |
| 82 | Json_reader json_reader(read_data); |
| 83 | ASSERT_TRUE(json_reader.valid()); |
| 84 | |
| 85 | ASSERT_TRUE(json_reader.num_elements() == 3); |
| 86 | |
| 87 | output_vector output; |
| 88 | ASSERT_FALSE(json_reader.get_elements(output)); |
| 89 | ASSERT_TRUE(output.size() == 3); |
| 90 | |
| 91 | /* Validate retrieved data */ |
| 92 | auto element = output[0].first; |
| 93 | |
| 94 | ASSERT_TRUE(element.first.owner_id() == "foo@bar"); |
| 95 | ASSERT_TRUE(element.first.key_id() == "key1"); |
| 96 | ASSERT_TRUE(element.second.data() == "abcdefghijklmnop"); |
| 97 | ASSERT_TRUE(element.second.type() == "AES"); |
| 98 | |
| 99 | auto ext = output[0].second.get(); |
| 100 | ASSERT_TRUE(ext->version() == "1.0"); |
| 101 | |
| 102 | element = output[1].first; |
| 103 | ASSERT_TRUE(element.first.owner_id() == "bar@foo"); |
| 104 | ASSERT_TRUE(element.first.key_id() == "key1"); |
| 105 | ASSERT_TRUE(element.second.data() == "qrstuvwx"); |
| 106 | ASSERT_TRUE(element.second.type() == "RSA"); |
| 107 | |
| 108 | ext = output[1].second.get(); |
| 109 | ASSERT_TRUE(ext->version() == "1.0"); |
| 110 | |
| 111 | element = output[2].first; |
| 112 | ASSERT_TRUE(element.first.owner_id() == ""); |
| 113 | ASSERT_TRUE(element.first.key_id() == "master_key"); |
| 114 | ASSERT_TRUE(element.second.data() == "yzabcdefghijklmn"); |
| 115 | ASSERT_TRUE(element.second.type() == "AES"); |
| 116 | |
| 117 | ext = output[2].second.get(); |
| 118 | ASSERT_TRUE(ext->version() == "1.0"); |
| 119 | |
| 120 | ASSERT_TRUE(std::remove(file_name.c_str()) == 0); |
| 121 | } |
| 122 | |
| 123 | } // namespace json_file_operations_unittest |