github.com/MetalBlockchain/metalgo@v1.11.9/utils/password/password_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package password
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestSufficientlyStrong(t *testing.T) {
    14  	tests := []struct {
    15  		password string
    16  		expected Strength
    17  	}{
    18  		{
    19  			password: "",
    20  			expected: VeryWeak,
    21  		},
    22  		{
    23  			password: "a",
    24  			expected: VeryWeak,
    25  		},
    26  		{
    27  			password: "password",
    28  			expected: VeryWeak,
    29  		},
    30  		{
    31  			password: "thisisareallylongandpresumablyverystrongpassword",
    32  			expected: VeryStrong,
    33  		},
    34  	}
    35  	for _, test := range tests {
    36  		t.Run(fmt.Sprintf("%s-%d", test.password, test.expected), func(t *testing.T) {
    37  			require.True(t, SufficientlyStrong(test.password, test.expected))
    38  		})
    39  	}
    40  }
    41  
    42  func TestIsValid(t *testing.T) {
    43  	tests := []struct {
    44  		password    string
    45  		expected    Strength
    46  		expectedErr error
    47  	}{
    48  		{
    49  			password:    "",
    50  			expected:    VeryWeak,
    51  			expectedErr: ErrEmptyPassword,
    52  		},
    53  		{
    54  			password: "a",
    55  			expected: VeryWeak,
    56  		},
    57  		{
    58  			password: "password",
    59  			expected: VeryWeak,
    60  		},
    61  		{
    62  			password: "thisisareallylongandpresumablyverystrongpassword",
    63  			expected: VeryStrong,
    64  		},
    65  		{
    66  			password: string(make([]byte, maxPassLen)),
    67  			expected: VeryWeak,
    68  		},
    69  		{
    70  			password:    string(make([]byte, maxPassLen+1)),
    71  			expected:    VeryWeak,
    72  			expectedErr: ErrPassMaxLength,
    73  		},
    74  		{
    75  			password:    "password",
    76  			expected:    Weak,
    77  			expectedErr: ErrWeakPassword,
    78  		},
    79  	}
    80  	for _, test := range tests {
    81  		t.Run(fmt.Sprintf("%s-%d", test.password, test.expected), func(t *testing.T) {
    82  			err := IsValid(test.password, test.expected)
    83  			require.ErrorIs(t, err, test.expectedErr)
    84  		})
    85  	}
    86  }