| 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 | #include <components/keyrings/common/json_data/json_writer.h> |
| 28 | |
| 29 | #include <gtest/gtest.h> |
| 30 | |
| 31 | namespace json_writer_unittest { |
| 32 | |
| 33 | class KeyringCommonJsonWriter_test : public ::testing::Test {}; |
| 34 | |
| 35 | using keyring_common::data::Data; |
| 36 | using keyring_common::json_data::Json_data_extension; |
| 37 | using keyring_common::json_data::Json_reader; |
| 38 | using keyring_common::json_data::Json_writer; |
| 39 | using keyring_common::meta::Metadata; |
| 40 | |
| 41 | TEST_F(KeyringCommonJsonWriter_test, JsonWriterTest) { |
| 42 | Json_writer json_writer; |
| 43 | Json_data_extension json_data_extension; |
| 44 | { |
| 45 | Metadata metadata("key1", "foo@bar"); |
| 46 | Data data("abcdefghijklmnop", "AES"); |
| 47 | ASSERT_FALSE(json_writer.add_element(metadata, data, json_data_extension)); |
| 48 | } |
| 49 | { |
| 50 | Metadata metadata("key1", "bar@foo"); |
| 51 | Data data("qrstuvwx", "RSA"); |
| 52 | ASSERT_FALSE(json_writer.add_element(metadata, data, json_data_extension)); |
| 53 | } |
| 54 | { |
| 55 | Metadata metadata("master_key", ""); |
| 56 | Data data("yzabcdefghijklmn", "AES"); |
| 57 | ASSERT_FALSE(json_writer.add_element(metadata, data, json_data_extension)); |
| 58 | } |
| 59 | |
| 60 | Json_reader json_reader(json_writer.to_string()); |
| 61 | ASSERT_TRUE(json_reader.valid()); |
| 62 | |
| 63 | Json_writer json_writer_2(json_writer.to_string()); |
| 64 | ASSERT_TRUE(json_writer_2.valid()); |
| 65 | ASSERT_TRUE(json_writer_2.num_elements() == 3); |
| 66 | { |
| 67 | Metadata metadata("key1", "bar@foo"); |
| 68 | ASSERT_FALSE(json_writer_2.remove_element(metadata, json_data_extension)); |
| 69 | ASSERT_TRUE(json_writer_2.remove_element(metadata, json_data_extension)); |
| 70 | } |
| 71 | ASSERT_TRUE(json_writer_2.num_elements() == 2); |
| 72 | { |
| 73 | Metadata metadata("key1", "bar@foo"); |
| 74 | Data data("qrstuvwx", "RSA"); |
| 75 | ASSERT_FALSE( |
| 76 | json_writer_2.add_element(metadata, data, json_data_extension)); |
| 77 | ASSERT_FALSE( |
| 78 | json_writer_2.add_element(metadata, data, json_data_extension)); |
| 79 | ASSERT_TRUE(json_writer_2.num_elements() == 4); |
| 80 | ASSERT_FALSE(json_writer_2.remove_element(metadata, json_data_extension)); |
| 81 | ASSERT_TRUE(json_writer_2.num_elements() == 2); |
| 82 | } |
| 83 | |
| 84 | Json_reader json_reader_2(json_writer_2.to_string()); |
| 85 | ASSERT_TRUE(json_reader_2.valid()); |
| 86 | } |
| 87 | |
| 88 | } // namespace json_writer_unittest |