github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/mysql/db_helper_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 mysql
    15  
    16  import (
    17  	"encoding/base64"
    18  	"net/url"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestDecodePassword(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	tests := []struct {
    28  		name       string
    29  		password   string
    30  		want       string
    31  		needEncode bool
    32  		needEscape bool
    33  	}{
    34  		{
    35  			name:     "case1",
    36  			password: "123456",
    37  			want:     "123456",
    38  		},
    39  		{
    40  			name:       "case2",
    41  			password:   "asdeer112",
    42  			want:       "asdeer112",
    43  			needEncode: true,
    44  		},
    45  		{
    46  			name:       "case3",
    47  			password:   "asdeer112!@#&",
    48  			want:       "asdeer112!@#&",
    49  			needEscape: true,
    50  		},
    51  		{
    52  			name:       "case4",
    53  			password:   "!@#12312//",
    54  			want:       "!@#12312//",
    55  			needEncode: true,
    56  			needEscape: true,
    57  		},
    58  	}
    59  	for _, c := range tests {
    60  		var err error
    61  		if c.needEscape {
    62  			c.password = url.QueryEscape(c.password)
    63  		}
    64  		if c.needEncode {
    65  			c.password = base64.StdEncoding.EncodeToString([]byte(c.password))
    66  			tem, err := base64.StdEncoding.DecodeString(c.password)
    67  			c.password = string(tem)
    68  			require.NoError(t, err, c.name)
    69  		}
    70  		if c.needEscape {
    71  			c.password, err = url.QueryUnescape(c.password)
    72  			require.NoError(t, err, c.name)
    73  		}
    74  		require.Equal(t, c.want, c.password)
    75  	}
    76  }