github.com/pingcap/tidb/parser@v0.0.0-20231013125129-93a834a6bf8d/auth/mysql_native_password_test.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package auth
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestEncodePassword(t *testing.T) {
    23  	pwd := "123"
    24  	require.Equal(t, "*23AE809DDACAF96AF0FD78ED04B6A265E05AA257", EncodePassword(pwd))
    25  	require.Equal(t, EncodePasswordBytes([]byte(pwd)), EncodePassword(pwd))
    26  }
    27  
    28  func TestDecodePassword(t *testing.T) {
    29  	x, err := DecodePassword(EncodePassword("123"))
    30  	require.NoError(t, err)
    31  	require.Equal(t, Sha1Hash(Sha1Hash([]byte("123"))), x)
    32  }
    33  
    34  func TestCheckScramble(t *testing.T) {
    35  	pwd := "abc"
    36  	salt := []byte{85, 92, 45, 22, 58, 79, 107, 6, 122, 125, 58, 80, 12, 90, 103, 32, 90, 10, 74, 82}
    37  	auth := []byte{24, 180, 183, 225, 166, 6, 81, 102, 70, 248, 199, 143, 91, 204, 169, 9, 161, 171, 203, 33}
    38  	encodepwd := EncodePassword(pwd)
    39  	hpwd, err := DecodePassword(encodepwd)
    40  	require.NoError(t, err)
    41  
    42  	res := CheckScrambledPassword(salt, hpwd, auth)
    43  	require.True(t, res)
    44  
    45  	// Do not panic for invalid input.
    46  	res = CheckScrambledPassword(salt, hpwd, []byte("xxyyzz"))
    47  	require.False(t, res)
    48  }