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