| 1 | /* |
| 2 | * Copyright (c) 2017, 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 <gtest/gtest.h> |
| 27 | #include <memory> |
| 28 | #include <string> |
| 29 | |
| 30 | #include "plugin/x/client/context/xssl_config.h" |
| 31 | |
| 32 | namespace xcl { |
| 33 | namespace test { |
| 34 | |
| 35 | using ::testing::Test; |
| 36 | using ::testing::Values; |
| 37 | using ::testing::WithParamInterface; |
| 38 | |
| 39 | using Ssl_string_field = std::string Ssl_config::*; |
| 40 | using Ssl_mode_field = Ssl_config::Mode Ssl_config::*; |
| 41 | |
| 42 | class Xcl_ssl_config_tests : public Test { |
| 43 | public: |
| 44 | bool assert_mode_requires_ssl(const Ssl_config::Mode m) { |
| 45 | m_sut.reset(new Ssl_config()); |
| 46 | m_sut->m_mode = m; |
| 47 | |
| 48 | return m_sut->does_mode_requires_ssl(); |
| 49 | } |
| 50 | |
| 51 | template <typename Value_type, typename Method, typename Field> |
| 52 | bool assert_value(Field field, Method method, const Value_type value, |
| 53 | const bool reset = true) { |
| 54 | if (reset) m_sut.reset(new Ssl_config()); |
| 55 | auto sut = m_sut.get(); |
| 56 | sut->*field = value; |
| 57 | |
| 58 | return (sut->*method)(); |
| 59 | } |
| 60 | |
| 61 | std::unique_ptr<Ssl_config> m_sut{new Ssl_config()}; |
| 62 | |
| 63 | Ssl_mode_field mode = &Ssl_config::m_mode; |
| 64 | Ssl_string_field tls_version = &Ssl_config::m_tls_version; |
| 65 | Ssl_string_field key = &Ssl_config::m_key; |
| 66 | Ssl_string_field ca = &Ssl_config::m_ca; |
| 67 | Ssl_string_field ca_path = &Ssl_config::m_ca_path; |
| 68 | Ssl_string_field cert = &Ssl_config::m_cert; |
| 69 | Ssl_string_field cipher = &Ssl_config::m_cipher; |
| 70 | Ssl_string_field crl = &Ssl_config::m_crl; |
| 71 | Ssl_string_field crl_path = &Ssl_config::m_crl_path; |
| 72 | }; |
| 73 | |
| 74 | TEST_F(Xcl_ssl_config_tests, is_configured) { |
| 75 | auto is_conf = &Ssl_config::is_configured; |
| 76 | |
| 77 | ASSERT_TRUE(m_sut->is_configured()); |
| 78 | ASSERT_FALSE(assert_value(mode, is_conf, Ssl_config::Mode::Ssl_disabled)); |
| 79 | ASSERT_TRUE(assert_value(mode, is_conf, Ssl_config::Mode::Ssl_preferred)); |
| 80 | ASSERT_TRUE(assert_value(mode, is_conf, Ssl_config::Mode::Ssl_required)); |
| 81 | ASSERT_TRUE(assert_value(mode, is_conf, Ssl_config::Mode::Ssl_verify_ca)); |
| 82 | ASSERT_TRUE( |
| 83 | assert_value(mode, is_conf, Ssl_config::Mode::Ssl_verify_identity)); |
| 84 | } |
| 85 | |
| 86 | TEST_F(Xcl_ssl_config_tests, is_ca_configured) { |
| 87 | auto is_ca_conf = &Ssl_config::is_ca_configured; |
| 88 | |
| 89 | ASSERT_TRUE(m_sut->is_configured()); |
| 90 | ASSERT_FALSE(assert_value(mode, is_ca_conf, Ssl_config::Mode::Ssl_required)); |
| 91 | ASSERT_FALSE(assert_value(tls_version, is_ca_conf, "text value")); |
| 92 | ASSERT_FALSE(assert_value(key, is_ca_conf, "text value")); |
| 93 | ASSERT_TRUE(assert_value(ca, is_ca_conf, "text value")); |
| 94 | ASSERT_TRUE(assert_value(ca_path, is_ca_conf, "text value")); |
| 95 | ASSERT_FALSE(assert_value(cert, is_ca_conf, "text value")); |
| 96 | ASSERT_FALSE(assert_value(cipher, is_ca_conf, "text value")); |
| 97 | ASSERT_FALSE(assert_value(crl, is_ca_conf, "text value")); |
| 98 | ASSERT_FALSE(assert_value(crl_path, is_ca_conf, "text value")); |
| 99 | } |
| 100 | |
| 101 | TEST_F(Xcl_ssl_config_tests, requires_ssl) { |
| 102 | ASSERT_FALSE(assert_mode_requires_ssl(Ssl_config::Mode::Ssl_disabled)); |
| 103 | ASSERT_FALSE(assert_mode_requires_ssl(Ssl_config::Mode::Ssl_preferred)); |
| 104 | ASSERT_TRUE(assert_mode_requires_ssl(Ssl_config::Mode::Ssl_required)); |
| 105 | ASSERT_TRUE(assert_mode_requires_ssl(Ssl_config::Mode::Ssl_verify_ca)); |
| 106 | ASSERT_TRUE(assert_mode_requires_ssl(Ssl_config::Mode::Ssl_verify_identity)); |
| 107 | } |
| 108 | |
| 109 | TEST_F(Xcl_ssl_config_tests, requires_ca) { |
| 110 | auto requires_ca = &Ssl_config::does_mode_requires_ca; |
| 111 | |
| 112 | ASSERT_FALSE(assert_value(mode, requires_ca, Ssl_config::Mode::Ssl_disabled)); |
| 113 | ASSERT_FALSE( |
| 114 | assert_value(mode, requires_ca, Ssl_config::Mode::Ssl_preferred)); |
| 115 | ASSERT_FALSE(assert_value(mode, requires_ca, Ssl_config::Mode::Ssl_required)); |
| 116 | ASSERT_TRUE(assert_value(mode, requires_ca, Ssl_config::Mode::Ssl_verify_ca)); |
| 117 | ASSERT_TRUE( |
| 118 | assert_value(mode, requires_ca, Ssl_config::Mode::Ssl_verify_identity)); |
| 119 | } |
| 120 | |
| 121 | } // namespace test |
| 122 | } // namespace xcl |