| 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 <gtest/gtest.h> |
| 25 | #include <cstdio> /* std::remove */ |
| 26 | #include <fstream> |
| 27 | |
| 28 | #include "components/keyrings/common/config/config_reader.h" |
| 29 | |
| 30 | namespace config_reader_unitest { |
| 31 | |
| 32 | class KeyringCommonConfigReader_test : public ::testing::Test {}; |
| 33 | |
| 34 | using keyring_common::config::Config_reader; |
| 35 | |
| 36 | TEST_F(KeyringCommonConfigReader_test, Config_reader_test) { |
| 37 | std::string config_file_name("config_file"); |
| 38 | std::string config_data = |
| 39 | "{" |
| 40 | "\"config_1\": \"This is a string config.\"," |
| 41 | "\"config_2\": false," |
| 42 | "\"config_3\": 42" |
| 43 | "}"; |
| 44 | std::ofstream out_file(config_file_name.c_str()); |
| 45 | ASSERT_TRUE(out_file.is_open()); |
| 46 | out_file.write(config_data.c_str(), config_data.length()); |
| 47 | out_file.close(); |
| 48 | |
| 49 | Config_reader config_reader(config_file_name); |
| 50 | |
| 51 | std::string string_config; |
| 52 | ASSERT_FALSE( |
| 53 | config_reader.get_element<std::string>("config_1", string_config)); |
| 54 | ASSERT_EQ(string_config, "This is a string config."); |
| 55 | |
| 56 | bool bool_config = true; |
| 57 | ASSERT_FALSE(config_reader.get_element<bool>("config_2", bool_config)); |
| 58 | ASSERT_FALSE(bool_config); |
| 59 | |
| 60 | int int_config = 0; |
| 61 | ASSERT_FALSE(config_reader.get_element<int>("config_3", int_config)); |
| 62 | ASSERT_EQ(int_config, 42); |
| 63 | |
| 64 | int non_existing = 0; |
| 65 | ASSERT_TRUE( |
| 66 | config_reader.get_element<int>("config_nonexisting", non_existing)); |
| 67 | |
| 68 | std::remove(config_file_name.c_str()); |
| 69 | } |
| 70 | |
| 71 | } // namespace config_reader_unitest |