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

     1  // Copyright 2022 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  	"encoding/hex"
    18  	"testing"
    19  
    20  	"github.com/pingcap/tidb/parser/mysql"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  var foobarPwdSM3Hash, _ = hex.DecodeString("24412430303524031a69251c34295c4b35167c7f1e5a7b63091349536c72627066426a635061762e556e6c63533159414d7762317261324a5a3047756b4244664177434e3043")
    25  
    26  func TestSM3(t *testing.T) {
    27  	testCases := [][]string{
    28  		{"abc", "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0"},
    29  		{"abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", "debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732"},
    30  	}
    31  	var expect []byte
    32  
    33  	for _, testCase := range testCases {
    34  		text := testCase[0]
    35  		expect, _ = hex.DecodeString(testCase[1])
    36  		result := Sm3Hash([]byte(text))
    37  		require.Equal(t, expect, result)
    38  	}
    39  }
    40  
    41  func TestCheckSM3PasswordGood(t *testing.T) {
    42  	pwd := "foobar"
    43  	r, err := CheckHashingPassword(foobarPwdSM3Hash, pwd, mysql.AuthTiDBSM3Password)
    44  	require.NoError(t, err)
    45  	require.True(t, r)
    46  }
    47  
    48  func TestCheckSM3PasswordBad(t *testing.T) {
    49  	pwd := "not_foobar"
    50  	pwhash, _ := hex.DecodeString("24412430303524031a69251c34295c4b35167c7f1e5a7b6309134956387565426743446d3643446176712f6c4b63323667346e48624872776f39512e4342416a693656676f2f")
    51  	r, err := CheckHashingPassword(pwhash, pwd, mysql.AuthTiDBSM3Password)
    52  	require.NoError(t, err)
    53  	require.False(t, r)
    54  }
    55  
    56  func TestCheckSM3PasswordShort(t *testing.T) {
    57  	pwd := "not_foobar"
    58  	pwhash, _ := hex.DecodeString("aaaaaaaa")
    59  	_, err := CheckHashingPassword(pwhash, pwd, mysql.AuthTiDBSM3Password)
    60  	require.Error(t, err)
    61  }
    62  
    63  func TestCheckSM3PasswordDigestTypeIncompatible(t *testing.T) {
    64  	pwd := "not_foobar"
    65  	pwhash, _ := hex.DecodeString("24432430303524031A69251C34295C4B35167C7F1E5A7B63091349503974624D34504B5A424679354856336868686F52485A736E4A733368786E427575516C73446469496537")
    66  	_, err := CheckHashingPassword(pwhash, pwd, mysql.AuthTiDBSM3Password)
    67  	require.Error(t, err)
    68  }
    69  
    70  func TestCheckSM3PasswordIterationsInvalid(t *testing.T) {
    71  	pwd := "not_foobar"
    72  	pwhash, _ := hex.DecodeString("24412430304724031A69251C34295C4B35167C7F1E5A7B63091349503974624D34504B5A424679354856336868686F52485A736E4A733368786E427575516C73446469496537")
    73  	_, err := CheckHashingPassword(pwhash, pwd, mysql.AuthTiDBSM3Password)
    74  	require.Error(t, err)
    75  }
    76  
    77  func TestNewSM3Password(t *testing.T) {
    78  	pwd := "testpwd"
    79  	pwhash := NewHashPassword(pwd, mysql.AuthTiDBSM3Password)
    80  	r, err := CheckHashingPassword([]byte(pwhash), pwd, mysql.AuthTiDBSM3Password)
    81  	require.NoError(t, err)
    82  	require.True(t, r)
    83  
    84  	for r := range pwhash {
    85  		require.Less(t, pwhash[r], uint8(128))
    86  		require.NotEqual(t, pwhash[r], 0)  // NUL
    87  		require.NotEqual(t, pwhash[r], 36) // '$'
    88  	}
    89  }
    90  
    91  func BenchmarkSM3Password(b *testing.B) {
    92  	for i := 0; i < b.N; i++ {
    93  		m, err := CheckHashingPassword(foobarPwdSM3Hash, "foobar", mysql.AuthTiDBSM3Password)
    94  		require.Nil(b, err)
    95  		require.True(b, m)
    96  	}
    97  }