94
94
ERR_NO_CEXT = "C Extension not available"
95
95
96
96
97
+ def setUpModule () -> None :
98
+ with MySQLConnection (** tests .get_mysql_config ()) as cnx :
99
+ global NATIVE_PASSWORD_INSTALLED
100
+
101
+ user = {
102
+ "user" : "_bugs" ,
103
+ "host" : "%" ,
104
+ "auth_plugin" : "mysql_native_password" ,
105
+ "password" : "s3cr3t" ,
106
+ }
107
+ stmt = (
108
+ "CREATE USER IF NOT EXISTS '{user}'@'{host}' IDENTIFIED "
109
+ "WITH {auth_plugin} BY '{password}'"
110
+ ).format (** user )
111
+ with cnx .cursor () as cur :
112
+ try :
113
+ cur .execute ("DROP USER IF EXISTS '{user}'@'{host}'" .format (** user ))
114
+ cur .execute (stmt )
115
+ cur .execute ("DROP USER IF EXISTS '{user}'@'{host}'" .format (** user ))
116
+ NATIVE_PASSWORD_INSTALLED = True
117
+ except mysql .connector .errors .DatabaseError :
118
+ try :
119
+ cur .execute ("INSTALL COMPONENT 'file://mysql_native_password'" )
120
+ NATIVE_PASSWORD_INSTALLED = True
121
+ except mysql .connector .errors .DatabaseError :
122
+ NATIVE_PASSWORD_INSTALLED = False
123
+
124
+
97
125
class Bug437972Tests (tests .MySQLConnectorTests ):
98
126
def test_windows_tcp_connection (self ):
99
127
"""lp:437972 TCP connection to Windows"""
@@ -409,6 +437,9 @@ def test_bits(self):
409
437
self .cnx .close ()
410
438
411
439
440
+ @unittest .skipIf (
441
+ tests .MYSQL_VERSION >= (8 , 4 , 0 ), "mysql_native_password is deprecated"
442
+ )
412
443
class Bug519301 (tests .MySQLConnectorTests ):
413
444
"""lp:519301 Temporary connection failures with 2 exceptions"""
414
445
@@ -2464,29 +2495,6 @@ def test_ssl(self):
2464
2495
class BugOra16217765 (tests .MySQLConnectorTests ):
2465
2496
"""BUG#16217765: Fix authentication plugin support"""
2466
2497
2467
- users = {
2468
- "sha256user" : {
2469
- "username" : "sha256user" ,
2470
- "password" : "sha256P@ss" ,
2471
- "auth_plugin" : "sha256_password" ,
2472
- },
2473
- "nativeuser" : {
2474
- "username" : "nativeuser" ,
2475
- "password" : "nativeP@ss" ,
2476
- "auth_plugin" : "mysql_native_password" ,
2477
- },
2478
- "sha256user_np" : {
2479
- "username" : "sha256user_np" ,
2480
- "password" : "" ,
2481
- "auth_plugin" : "sha256_password" ,
2482
- },
2483
- "nativeuser_np" : {
2484
- "username" : "nativeuser_np" ,
2485
- "password" : "" ,
2486
- "auth_plugin" : "mysql_native_password" ,
2487
- },
2488
- }
2489
-
2490
2498
def _create_user (self , cnx , user , password , host , database , plugin ):
2491
2499
self ._drop_user (cnx , user , host )
2492
2500
create_user = "CREATE USER '{user}'@'{host}' IDENTIFIED WITH {plugin}"
@@ -2521,6 +2529,36 @@ def _drop_user(self, cnx, user, host):
2521
2529
pass
2522
2530
2523
2531
def setUp (self ):
2532
+ self .users = {
2533
+ "sha256user" : {
2534
+ "username" : "sha256user" ,
2535
+ "password" : "sha256P@ss" ,
2536
+ "auth_plugin" : "sha256_password" ,
2537
+ },
2538
+ "sha256user_np" : {
2539
+ "username" : "sha256user_np" ,
2540
+ "password" : "" ,
2541
+ "auth_plugin" : "sha256_password" ,
2542
+ },
2543
+ }
2544
+
2545
+ if NATIVE_PASSWORD_INSTALLED :
2546
+ self .users = {
2547
+ ** self .users ,
2548
+ ** {
2549
+ "nativeuser_np" : {
2550
+ "username" : "nativeuser_np" ,
2551
+ "password" : "" ,
2552
+ "auth_plugin" : "mysql_native_password" ,
2553
+ },
2554
+ "nativeuser" : {
2555
+ "username" : "nativeuser" ,
2556
+ "password" : "nativeP@ss" ,
2557
+ "auth_plugin" : "mysql_native_password" ,
2558
+ },
2559
+ },
2560
+ }
2561
+
2524
2562
self .errmsg = "AuthPlugin {0} failed: {1}"
2525
2563
config = tests .get_mysql_config ()
2526
2564
self .host = config ["host" ]
@@ -2633,6 +2671,9 @@ def test_sha256_nonssl(self):
2633
2671
),
2634
2672
)
2635
2673
def test_native (self ):
2674
+ if not NATIVE_PASSWORD_INSTALLED :
2675
+ self .skipTest ("mysql_native_password isn't loaded" )
2676
+
2636
2677
config = tests .get_mysql_config ()
2637
2678
config ["unix_socket" ] = None
2638
2679
@@ -6609,8 +6650,9 @@ def setUp(self):
6609
6650
if tests .MYSQL_VERSION < (8 , 0 , 0 ):
6610
6651
self .new_users = self .users [0 :4 ]
6611
6652
else :
6612
- self .new_users = self .users
6613
-
6653
+ self .new_users = (
6654
+ self .users if NATIVE_PASSWORD_INSTALLED else self .users [- 4 :]
6655
+ )
6614
6656
for new_user in self .new_users :
6615
6657
cnx .cmd_query ("DROP USER IF EXISTS '{user}'@'{host}'" .format (** new_user ))
6616
6658
@@ -6650,13 +6692,17 @@ def test_change_user(self):
6650
6692
(1 , 2 ),
6651
6693
(2 , 3 ),
6652
6694
(3 , 0 ),
6653
- (3 , 4 ),
6654
- (4 , 5 ),
6655
- (5 , 3 ),
6656
- (5 , 0 ),
6657
6695
]
6696
+ if NATIVE_PASSWORD_INSTALLED :
6697
+ test_cases += [
6698
+ (3 , 4 ),
6699
+ (4 , 5 ),
6700
+ (5 , 3 ),
6701
+ (5 , 0 ),
6702
+ ]
6703
+
6658
6704
for user1 , user2 in test_cases :
6659
- conn_args_user1 = self .users [user1 ].copy ()
6705
+ conn_args_user1 = self .new_users [user1 ].copy ()
6660
6706
try :
6661
6707
conn_args_user1 .pop ("auth_plugin" )
6662
6708
except :
@@ -6668,24 +6714,28 @@ def test_change_user(self):
6668
6714
cnx_test = self .cnx .__class__ (** conn_args_user1 )
6669
6715
cnx_test .cmd_query ("SELECT USER()" )
6670
6716
first_user = cnx_test .get_rows ()[0 ][0 ][0 ]
6671
- self .assertEqual ("{user}@{host}" .format (** self .users [user1 ]), first_user )
6717
+ self .assertEqual (
6718
+ "{user}@{host}" .format (** self .new_users [user1 ]), first_user
6719
+ )
6672
6720
6673
6721
cnx_test .cmd_change_user (
6674
- self .users [user2 ]["user" ],
6675
- self .users [user2 ]["password" ],
6676
- self .users [user2 ]["database" ],
6722
+ self .new_users [user2 ]["user" ],
6723
+ self .new_users [user2 ]["password" ],
6724
+ self .new_users [user2 ]["database" ],
6677
6725
)
6678
6726
cnx_test .cmd_query ("SELECT USER()" )
6679
6727
rows = cnx_test .get_rows ()
6680
6728
current_user = rows [0 ][0 ][0 ]
6681
6729
self .assertNotEqual (first_user , current_user )
6682
- self .assertEqual ("{user}@{host}" .format (** self .users [user2 ]), current_user )
6730
+ self .assertEqual (
6731
+ "{user}@{host}" .format (** self .new_users [user2 ]), current_user
6732
+ )
6683
6733
cnx_test .close ()
6684
6734
6685
6735
def tearDown (self ):
6686
6736
cnx = MySQLConnection (** self .connect_kwargs )
6687
6737
# cleanup users
6688
- for new_user in self .users :
6738
+ for new_user in self .new_users :
6689
6739
cnx .cmd_query ("DROP USER IF EXISTS '{user}'@'{host}'" .format (** new_user ))
6690
6740
6691
6741
0 commit comments