github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/random/random_test.go (about)

     1  package random
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestStringLength(t *testing.T) {
    11  	for i := 0; i < 100; i++ {
    12  		s := String(i)
    13  		assert.Equal(t, i, len(s))
    14  	}
    15  }
    16  
    17  func TestStringDuplicates(t *testing.T) {
    18  	seen := map[string]bool{}
    19  	for i := 0; i < 100; i++ {
    20  		s := String(8)
    21  		assert.False(t, seen[s])
    22  		assert.Equal(t, 8, len(s))
    23  		seen[s] = true
    24  	}
    25  }
    26  
    27  func TestPasswordLength(t *testing.T) {
    28  	for i := 0; i <= 128; i++ {
    29  		s, err := Password(i)
    30  		require.NoError(t, err)
    31  		// expected length is number of bytes rounded up
    32  		expected := i / 8
    33  		if i%8 != 0 {
    34  			expected++
    35  		}
    36  		// then converted to base 64
    37  		expected = (expected*8 + 5) / 6
    38  		assert.Equal(t, expected, len(s), i)
    39  	}
    40  }
    41  
    42  func TestPasswordDuplicates(t *testing.T) {
    43  	seen := map[string]bool{}
    44  	for i := 0; i < 100; i++ {
    45  		s, err := Password(64)
    46  		require.NoError(t, err)
    47  		assert.False(t, seen[s])
    48  		seen[s] = true
    49  	}
    50  }