| 1 | /* Copyright (c) 2014, 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 "my_config.h" |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "m_string.h" |
| 29 | #include "sql/sql_class.h" |
| 30 | #include "unittest/gunit/test_utils.h" |
| 31 | |
| 32 | namespace security_context_unittest { |
| 33 | |
| 34 | /* |
| 35 | Testing accessor functions of string type data members of class |
| 36 | Security_context. |
| 37 | */ |
| 38 | TEST(Security_context, string_data_member) { |
| 39 | Security_context sctx; |
| 40 | |
| 41 | // Case 1: Initialize Security context and check the values set. |
| 42 | EXPECT_EQ(sctx.user().length, (size_t)0); |
| 43 | EXPECT_EQ(sctx.host().length, (size_t)0); |
| 44 | EXPECT_EQ(sctx.ip().length, (size_t)0); |
| 45 | EXPECT_EQ(sctx.external_user().length, (size_t)0); |
| 46 | EXPECT_EQ(strcmp(sctx.host_or_ip().str, "connecting host"), 0); |
| 47 | EXPECT_EQ(sctx.priv_user().length, (size_t)0); |
| 48 | EXPECT_EQ(sctx.proxy_user().length, (size_t)0); |
| 49 | EXPECT_EQ(sctx.priv_host().length, (size_t)0); |
| 50 | |
| 51 | // Case 2: Set the empty string to Securtiy context members and check values. |
| 52 | sctx.set_user_ptr("", 0); |
| 53 | sctx.set_host_ptr("", 0); |
| 54 | sctx.set_ip_ptr("", 0); |
| 55 | sctx.set_host_or_ip_ptr(); |
| 56 | sctx.set_external_user_ptr("", 0); |
| 57 | sctx.assign_priv_user("", 0); |
| 58 | sctx.assign_proxy_user("", 0); |
| 59 | sctx.assign_priv_host("", 0); |
| 60 | |
| 61 | EXPECT_EQ(sctx.user().length, (size_t)0); |
| 62 | EXPECT_EQ(sctx.host().length, (size_t)0); |
| 63 | EXPECT_EQ(sctx.ip().length, (size_t)0); |
| 64 | EXPECT_EQ(sctx.external_user().length, (size_t)0); |
| 65 | EXPECT_EQ(sctx.host_or_ip().length, (size_t)0); |
| 66 | EXPECT_EQ(sctx.priv_user().length, (size_t)0); |
| 67 | EXPECT_EQ(sctx.proxy_user().length, (size_t)0); |
| 68 | EXPECT_EQ(sctx.priv_host().length, (size_t)0); |
| 69 | |
| 70 | // using method assign_xxxx(); |
| 71 | sctx.assign_user("", 0); |
| 72 | sctx.assign_host("", 0); |
| 73 | sctx.assign_ip("", 0); |
| 74 | sctx.assign_external_user("", 0); |
| 75 | |
| 76 | EXPECT_EQ(sctx.user().length, (size_t)0); |
| 77 | EXPECT_EQ(sctx.host().length, (size_t)0); |
| 78 | EXPECT_EQ(sctx.ip().length, (size_t)0); |
| 79 | EXPECT_EQ(sctx.external_user().length, (size_t)0); |
| 80 | EXPECT_EQ(sctx.host_or_ip().length, (size_t)0); |
| 81 | EXPECT_EQ(sctx.priv_user().length, (size_t)0); |
| 82 | EXPECT_EQ(sctx.proxy_user().length, (size_t)0); |
| 83 | EXPECT_EQ(sctx.priv_host().length, (size_t)0); |
| 84 | |
| 85 | // using method assign_host() but passing the nullptr to it |
| 86 | sctx.assign_host(nullptr, 0); |
| 87 | EXPECT_EQ(sctx.host().length, (size_t)0); |
| 88 | |
| 89 | // Case 3: Set non-empty string to Securtiy context members and check values. |
| 90 | sctx.set_user_ptr(STRING_WITH_LEN("user_test")); |
| 91 | sctx.set_host_ptr(STRING_WITH_LEN("localhost")); |
| 92 | sctx.set_ip_ptr(STRING_WITH_LEN("127.0.0.1")); |
| 93 | sctx.set_host_or_ip_ptr(); |
| 94 | sctx.set_external_user_ptr(STRING_WITH_LEN("ext_user_test")); |
| 95 | sctx.assign_priv_user(STRING_WITH_LEN("priv_user")); |
| 96 | sctx.assign_proxy_user(STRING_WITH_LEN("proxy_user")); |
| 97 | sctx.assign_priv_host(STRING_WITH_LEN("localhost")); |
| 98 | |
| 99 | EXPECT_EQ(0, strcmp(sctx.user().str, "user_test")); |
| 100 | EXPECT_EQ(0, strcmp(sctx.host().str, "localhost")); |
| 101 | EXPECT_EQ(0, strcmp(sctx.ip().str, "127.0.0.1")); |
| 102 | EXPECT_EQ(0, strcmp(sctx.external_user().str, "ext_user_test")); |
| 103 | EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "localhost")); |
| 104 | EXPECT_EQ(0, strcmp(sctx.priv_user().str, "priv_user")); |
| 105 | EXPECT_EQ(0, strcmp(sctx.proxy_user().str, "proxy_user")); |
| 106 | EXPECT_EQ(0, strcmp(sctx.priv_host().str, "localhost")); |
| 107 | |
| 108 | sctx.set_host_or_ip_ptr(sctx.ip().str, sctx.ip().length); |
| 109 | EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "127.0.0.1")); |
| 110 | |
| 111 | // Case 4: Change members with non-empty string and check values. |
| 112 | sctx.set_user_ptr(STRING_WITH_LEN("user_test_1")); |
| 113 | sctx.set_host_ptr(STRING_WITH_LEN("localhost_1")); |
| 114 | sctx.set_ip_ptr(STRING_WITH_LEN("127.0.0.2")); |
| 115 | sctx.set_host_or_ip_ptr(); |
| 116 | sctx.set_external_user_ptr(STRING_WITH_LEN("ext_user_test_1")); |
| 117 | sctx.assign_priv_user(STRING_WITH_LEN("priv_user_1")); |
| 118 | sctx.assign_proxy_user(STRING_WITH_LEN("proxy_user_1")); |
| 119 | sctx.assign_priv_host(STRING_WITH_LEN("localhost_1")); |
| 120 | |
| 121 | EXPECT_EQ(0, strcmp(sctx.user().str, "user_test_1")); |
| 122 | EXPECT_EQ(0, strcmp(sctx.host().str, "localhost_1")); |
| 123 | EXPECT_EQ(0, strcmp(sctx.ip().str, "127.0.0.2")); |
| 124 | EXPECT_EQ(0, strcmp(sctx.external_user().str, "ext_user_test_1")); |
| 125 | EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "localhost_1")); |
| 126 | EXPECT_EQ(0, strcmp(sctx.priv_user().str, "priv_user_1")); |
| 127 | EXPECT_EQ(0, strcmp(sctx.proxy_user().str, "proxy_user_1")); |
| 128 | EXPECT_EQ(0, strcmp(sctx.priv_host().str, "localhost_1")); |
| 129 | |
| 130 | // Case 5: Change members with non-empty string members with copy option. |
| 131 | sctx.assign_user(STRING_WITH_LEN("user_test")); |
| 132 | sctx.assign_host(STRING_WITH_LEN("localhost")); |
| 133 | sctx.assign_ip(STRING_WITH_LEN("127.0.0.1")); |
| 134 | sctx.set_host_or_ip_ptr(); |
| 135 | sctx.assign_external_user(STRING_WITH_LEN("ext_user_test")); |
| 136 | sctx.assign_priv_user(STRING_WITH_LEN("priv_user")); |
| 137 | sctx.assign_proxy_user(STRING_WITH_LEN("proxy_user")); |
| 138 | sctx.assign_priv_host(STRING_WITH_LEN("localhost")); |
| 139 | |
| 140 | EXPECT_EQ(0, strcmp(sctx.user().str, "user_test")); |
| 141 | EXPECT_EQ(0, strcmp(sctx.host().str, "localhost")); |
| 142 | EXPECT_EQ(0, strcmp(sctx.ip().str, "127.0.0.1")); |
| 143 | EXPECT_EQ(0, strcmp(sctx.external_user().str, "ext_user_test")); |
| 144 | EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "localhost")); |
| 145 | EXPECT_EQ(0, strcmp(sctx.priv_user().str, "priv_user")); |
| 146 | EXPECT_EQ(0, strcmp(sctx.proxy_user().str, "proxy_user")); |
| 147 | EXPECT_EQ(0, strcmp(sctx.priv_host().str, "localhost")); |
| 148 | } |
| 149 | |
| 150 | } // namespace security_context_unittest |