| 1 | /* Copyright (c) 2015, 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 "sql/sql_initialize.h" |
| 29 | |
| 30 | namespace initialize_password_unittest { |
| 31 | |
| 32 | static const char *null_s = nullptr; |
| 33 | |
| 34 | TEST(initialize_password, random_pwd_10chars) { |
| 35 | char pass[12]; |
| 36 | static const char *chars = g_allowed_pwd_chars; |
| 37 | |
| 38 | memset(pass, 0, sizeof(pass)); |
| 39 | bool failed = ::generate_password(&pass[1], 10); |
| 40 | pass[11] = 0; |
| 41 | |
| 42 | EXPECT_EQ(failed, false); |
| 43 | EXPECT_EQ(pass[0], 0); |
| 44 | for (char *ptr = &pass[1]; *ptr; ptr++) { |
| 45 | const char *s = strchr(chars, *ptr); |
| 46 | EXPECT_NE(s, null_s); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | TEST(initialize_password, random_pwd_0) { |
| 51 | char pass[11]; |
| 52 | |
| 53 | memset(pass, 0, sizeof(pass)); |
| 54 | bool failed = ::generate_password(&pass[1], 0); |
| 55 | EXPECT_EQ(failed, false); |
| 56 | |
| 57 | for (unsigned inx = 0; inx < sizeof(pass); inx++) EXPECT_EQ(pass[inx], 0); |
| 58 | } |
| 59 | |
| 60 | TEST(initialize_password, random_pwd_1) { |
| 61 | char pass[11]; |
| 62 | static const char *chars = g_allowed_pwd_chars; |
| 63 | |
| 64 | memset(pass, 0, sizeof(pass)); |
| 65 | bool failed = ::generate_password(&pass[1], 1); |
| 66 | EXPECT_EQ(failed, false); |
| 67 | |
| 68 | EXPECT_EQ(pass[0], 0); |
| 69 | |
| 70 | const char *s = strchr(chars, pass[1]); |
| 71 | EXPECT_NE(s, null_s); |
| 72 | |
| 73 | for (unsigned inx = 2; inx < sizeof(pass); inx++) EXPECT_EQ(pass[inx], 0); |
| 74 | } |
| 75 | |
| 76 | TEST(initialize_password, random_pwd_2) { |
| 77 | char pass[11]; |
| 78 | static const char *chars = g_allowed_pwd_chars; |
| 79 | unsigned inx; |
| 80 | |
| 81 | memset(pass, 0, sizeof(pass)); |
| 82 | bool failed = ::generate_password(&pass[1], 2); |
| 83 | |
| 84 | EXPECT_EQ(failed, false); |
| 85 | EXPECT_EQ(pass[0], 0); |
| 86 | |
| 87 | for (inx = 0; inx < 2; inx++) { |
| 88 | const char *s = strchr(chars, pass[1 + inx]); |
| 89 | EXPECT_NE(s, null_s); |
| 90 | } |
| 91 | |
| 92 | for (inx = 3; inx < sizeof(pass); inx++) EXPECT_EQ(pass[inx], 0); |
| 93 | } |
| 94 | |
| 95 | TEST(initialize_password, random_pwd_3) { |
| 96 | char pass[11]; |
| 97 | static const char *chars = g_allowed_pwd_chars; |
| 98 | unsigned inx; |
| 99 | |
| 100 | memset(pass, 0, sizeof(pass)); |
| 101 | bool failed = ::generate_password(&pass[1], 3); |
| 102 | EXPECT_EQ(failed, false); |
| 103 | |
| 104 | EXPECT_EQ(pass[0], 0); |
| 105 | |
| 106 | for (inx = 0; inx < 3; inx++) { |
| 107 | const char *s = strchr(chars, pass[1 + inx]); |
| 108 | EXPECT_NE(s, null_s); |
| 109 | } |
| 110 | |
| 111 | for (inx = 4; inx < sizeof(pass); inx++) EXPECT_EQ(pass[inx], 0); |
| 112 | } |
| 113 | |
| 114 | TEST(initialize_password, random_pwd_4) { |
| 115 | char pass[11]; |
| 116 | static const char *chars = g_allowed_pwd_chars; |
| 117 | unsigned inx; |
| 118 | |
| 119 | memset(pass, 0, sizeof(pass)); |
| 120 | bool failed = ::generate_password(&pass[1], 4); |
| 121 | EXPECT_EQ(failed, false); |
| 122 | |
| 123 | EXPECT_EQ(pass[0], 0); |
| 124 | |
| 125 | for (inx = 0; inx < 4; inx++) { |
| 126 | const char *s = strchr(chars, pass[1 + inx]); |
| 127 | EXPECT_NE(s, null_s); |
| 128 | } |
| 129 | |
| 130 | for (inx = 5; inx < sizeof(pass); inx++) EXPECT_EQ(pass[inx], 0); |
| 131 | } |
| 132 | |
| 133 | TEST(initialize_password, strong_pwd_10_chars) { |
| 134 | char pass[12]; |
| 135 | static const char *chars = g_allowed_pwd_chars; |
| 136 | static const char *low_chars = g_lower_case_chars; |
| 137 | static const char *up_chars = g_upper_case_chars; |
| 138 | static const char *sym_chars = g_special_chars; |
| 139 | static const char *num_chars = g_numeric_chars; |
| 140 | bool had_low = false, had_up = false, had_sym = false, had_num = false; |
| 141 | |
| 142 | memset(pass, 0, sizeof(pass)); |
| 143 | bool failed = ::generate_password(&pass[1], 10); |
| 144 | EXPECT_EQ(failed, false); |
| 145 | |
| 146 | EXPECT_EQ(pass[0], 0); |
| 147 | EXPECT_EQ(pass[11], 0); |
| 148 | for (char *ptr = &pass[1]; *ptr; ptr++) { |
| 149 | const char *s = strchr(chars, *ptr); |
| 150 | EXPECT_NE(s, null_s); |
| 151 | |
| 152 | if (!had_low && nullptr != strchr(low_chars, *ptr)) |
| 153 | had_low = true; |
| 154 | else if (!had_up && nullptr != strchr(up_chars, *ptr)) |
| 155 | had_up = true; |
| 156 | else if (!had_sym && nullptr != strchr(sym_chars, *ptr)) |
| 157 | had_sym = true; |
| 158 | else if (!had_num && nullptr != strchr(num_chars, *ptr)) |
| 159 | had_num = true; |
| 160 | } |
| 161 | |
| 162 | EXPECT_EQ(had_low, true); |
| 163 | EXPECT_EQ(had_up, true); |
| 164 | EXPECT_EQ(had_sym, true); |
| 165 | EXPECT_EQ(had_num, true); |
| 166 | } |
| 167 | |
| 168 | TEST(initialize_password, strong_pwd_4_chars) { |
| 169 | char pass[12]; |
| 170 | static const char *chars = g_allowed_pwd_chars; |
| 171 | static const char *low_chars = g_lower_case_chars; |
| 172 | static const char *up_chars = g_upper_case_chars; |
| 173 | static const char *sym_chars = g_special_chars; |
| 174 | static const char *num_chars = g_numeric_chars; |
| 175 | bool had_low = false, had_up = false, had_sym = false, had_num = false; |
| 176 | |
| 177 | memset(pass, 0, sizeof(pass)); |
| 178 | bool failed = ::generate_password(&pass[1], 4); |
| 179 | EXPECT_EQ(failed, false); |
| 180 | |
| 181 | EXPECT_EQ(pass[0], 0); |
| 182 | EXPECT_EQ(pass[5], 0); |
| 183 | for (char *ptr = &pass[1]; *ptr; ptr++) { |
| 184 | const char *s = strchr(chars, *ptr); |
| 185 | EXPECT_NE(s, null_s); |
| 186 | |
| 187 | if (!had_low && nullptr != strchr(low_chars, *ptr)) |
| 188 | had_low = true; |
| 189 | else if (!had_up && nullptr != strchr(up_chars, *ptr)) |
| 190 | had_up = true; |
| 191 | else if (!had_sym && nullptr != strchr(sym_chars, *ptr)) |
| 192 | had_sym = true; |
| 193 | else if (!had_num && nullptr != strchr(num_chars, *ptr)) |
| 194 | had_num = true; |
| 195 | } |
| 196 | |
| 197 | EXPECT_EQ(had_low, true); |
| 198 | EXPECT_EQ(had_up, true); |
| 199 | EXPECT_EQ(had_sym, true); |
| 200 | EXPECT_EQ(had_num, true); |
| 201 | } |
| 202 | |
| 203 | } // namespace initialize_password_unittest |