github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fs/config/crypt_internal_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func hashedKeyCompare(t *testing.T, a, b string, shouldMatch bool) {
    11  	err := SetConfigPassword(a)
    12  	require.NoError(t, err)
    13  	k1 := configKey
    14  
    15  	err = SetConfigPassword(b)
    16  	require.NoError(t, err)
    17  	k2 := configKey
    18  
    19  	if shouldMatch {
    20  		assert.Equal(t, k1, k2)
    21  	} else {
    22  		assert.NotEqual(t, k1, k2)
    23  	}
    24  }
    25  
    26  func TestPassword(t *testing.T) {
    27  	defer func() {
    28  		configKey = nil // reset password
    29  	}()
    30  	var err error
    31  	// Empty password should give error
    32  	err = SetConfigPassword("  \t  ")
    33  	require.Error(t, err)
    34  
    35  	// Test invalid utf8 sequence
    36  	err = SetConfigPassword(string([]byte{0xff, 0xfe, 0xfd}) + "abc")
    37  	require.Error(t, err)
    38  
    39  	// Simple check of wrong passwords
    40  	hashedKeyCompare(t, "mis", "match", false)
    41  
    42  	// Check that passwords match after unicode normalization
    43  	hashedKeyCompare(t, "ff\u0041\u030A", "ffÅ", true)
    44  
    45  	// Check that passwords preserves case
    46  	hashedKeyCompare(t, "abcdef", "ABCDEF", false)
    47  
    48  }