1 /* Copyright (c) 2014, 2021, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
22
23 #include "my_config.h"
24 #include "m_string.h"
25 #include <gtest/gtest.h>
26
27 #include "test_utils.h"
28
29 #include "sql_class.h"
30
31 namespace security_context_unittest
32 {
33
34 /*
35 Testing accessor functions of string type data members of class
36 Security_context.
37 */
TEST(Security_context,string_data_member)38 TEST(Security_context, string_data_member)
39 {
40 Security_context sctx;
41
42 // Case 1: Initialize Security context and check the values set.
43 EXPECT_EQ(sctx.user().length, (size_t)0);
44 EXPECT_EQ(sctx.host().length, (size_t)0);
45 EXPECT_EQ(sctx.ip().length, (size_t)0);
46 EXPECT_EQ(sctx.external_user().length, (size_t)0);
47 EXPECT_EQ(strcmp(sctx.host_or_ip().str, "connecting host"), 0);
48 EXPECT_EQ(sctx.priv_user().length, (size_t)0);
49 EXPECT_EQ(sctx.proxy_user().length, (size_t)0);
50 EXPECT_EQ(sctx.priv_host().length, (size_t)0);
51
52 // Case 2: Set the empty string to Securtiy context members and check values.
53 sctx.set_user_ptr("", 0);
54 sctx.set_host_ptr("", 0);
55 sctx.set_ip_ptr("", 0);
56 sctx.set_host_or_ip_ptr();
57 sctx.set_external_user_ptr("", 0);
58 sctx.assign_priv_user("", 0);
59 sctx.assign_proxy_user("", 0);
60 sctx.assign_priv_host("", 0);
61
62 EXPECT_EQ(sctx.user().length, (size_t)0);
63 EXPECT_EQ(sctx.host().length, (size_t)0);
64 EXPECT_EQ(sctx.ip().length, (size_t)0);
65 EXPECT_EQ(sctx.external_user().length, (size_t)0);
66 EXPECT_EQ(sctx.host_or_ip().length, (size_t)0);
67 EXPECT_EQ(sctx.priv_user().length, (size_t)0);
68 EXPECT_EQ(sctx.proxy_user().length, (size_t)0);
69 EXPECT_EQ(sctx.priv_host().length, (size_t)0);
70
71 // using method assign_xxxx();
72 sctx.assign_user("", 0);
73 sctx.assign_host("", 0);
74 sctx.assign_ip("", 0);
75 sctx.assign_external_user("", 0);
76
77 EXPECT_EQ(sctx.user().length, (size_t)0);
78 EXPECT_EQ(sctx.host().length, (size_t)0);
79 EXPECT_EQ(sctx.ip().length, (size_t)0);
80 EXPECT_EQ(sctx.external_user().length, (size_t)0);
81 EXPECT_EQ(sctx.host_or_ip().length, (size_t)0);
82 EXPECT_EQ(sctx.priv_user().length, (size_t)0);
83 EXPECT_EQ(sctx.proxy_user().length, (size_t)0);
84 EXPECT_EQ(sctx.priv_host().length, (size_t)0);
85
86 // Case 3: Set non-empty string to Securtiy context members and check values.
87 sctx.set_user_ptr(STRING_WITH_LEN("user_test"));
88 sctx.set_host_ptr(STRING_WITH_LEN("localhost"));
89 sctx.set_ip_ptr(STRING_WITH_LEN("127.0.0.1"));
90 sctx.set_host_or_ip_ptr();
91 sctx.set_external_user_ptr(STRING_WITH_LEN("ext_user_test"));
92 sctx.assign_priv_user(STRING_WITH_LEN("priv_user"));
93 sctx.assign_proxy_user(STRING_WITH_LEN("proxy_user"));
94 sctx.assign_priv_host(STRING_WITH_LEN("localhost"));
95
96 EXPECT_EQ(0, strcmp(sctx.user().str, "user_test"));
97 EXPECT_EQ(0, strcmp(sctx.host().str, "localhost"));
98 EXPECT_EQ(0, strcmp(sctx.ip().str, "127.0.0.1"));
99 EXPECT_EQ(0, strcmp(sctx.external_user().str, "ext_user_test"));
100 EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "localhost"));
101 EXPECT_EQ(0, strcmp(sctx.priv_user().str, "priv_user"));
102 EXPECT_EQ(0, strcmp(sctx.proxy_user().str, "proxy_user"));
103 EXPECT_EQ(0, strcmp(sctx.priv_host().str, "localhost"));
104
105 sctx.set_host_or_ip_ptr(sctx.ip().str, sctx.ip().length);
106 EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "127.0.0.1"));
107
108 // Case 4: Change members with non-empty string and check values.
109 sctx.set_user_ptr(STRING_WITH_LEN("user_test_1"));
110 sctx.set_host_ptr(STRING_WITH_LEN("localhost_1"));
111 sctx.set_ip_ptr(STRING_WITH_LEN("127.0.0.2"));
112 sctx.set_host_or_ip_ptr();
113 sctx.set_external_user_ptr(STRING_WITH_LEN("ext_user_test_1"));
114 sctx.assign_priv_user(STRING_WITH_LEN("priv_user_1"));
115 sctx.assign_proxy_user(STRING_WITH_LEN("proxy_user_1"));
116 sctx.assign_priv_host(STRING_WITH_LEN("localhost_1"));
117
118 EXPECT_EQ(0, strcmp(sctx.user().str, "user_test_1"));
119 EXPECT_EQ(0, strcmp(sctx.host().str, "localhost_1"));
120 EXPECT_EQ(0, strcmp(sctx.ip().str, "127.0.0.2"));
121 EXPECT_EQ(0, strcmp(sctx.external_user().str, "ext_user_test_1"));
122 EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "localhost_1"));
123 EXPECT_EQ(0, strcmp(sctx.priv_user().str, "priv_user_1"));
124 EXPECT_EQ(0, strcmp(sctx.proxy_user().str, "proxy_user_1"));
125 EXPECT_EQ(0, strcmp(sctx.priv_host().str, "localhost_1"));
126
127 // Case 5: Change members with non-empty string members with copy option.
128 sctx.assign_user(STRING_WITH_LEN("user_test"));
129 sctx.assign_host(STRING_WITH_LEN("localhost"));
130 sctx.assign_ip(STRING_WITH_LEN("127.0.0.1"));
131 sctx.set_host_or_ip_ptr();
132 sctx.assign_external_user(STRING_WITH_LEN("ext_user_test"));
133 sctx.assign_priv_user(STRING_WITH_LEN("priv_user"));
134 sctx.assign_proxy_user(STRING_WITH_LEN("proxy_user"));
135 sctx.assign_priv_host(STRING_WITH_LEN("localhost"));
136
137 EXPECT_EQ(0, strcmp(sctx.user().str, "user_test"));
138 EXPECT_EQ(0, strcmp(sctx.host().str, "localhost"));
139 EXPECT_EQ(0, strcmp(sctx.ip().str, "127.0.0.1"));
140 EXPECT_EQ(0, strcmp(sctx.external_user().str, "ext_user_test"));
141 EXPECT_EQ(0, strcmp(sctx.host_or_ip().str, "localhost"));
142 EXPECT_EQ(0, strcmp(sctx.priv_user().str, "priv_user"));
143 EXPECT_EQ(0, strcmp(sctx.proxy_user().str, "proxy_user"));
144 EXPECT_EQ(0, strcmp(sctx.priv_host().str, "localhost"));
145 }
146
147 }
148