github.com/Finschia/finschia-sdk@v0.48.1/crypto/keyring/signing_algorithms_test.go (about)

     1  package keyring
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/Finschia/finschia-sdk/crypto/hd"
    10  )
    11  
    12  func TestNewSigningAlgoByString(t *testing.T) {
    13  	tests := []struct {
    14  		name         string
    15  		algoStr      string
    16  		isSupported  bool
    17  		expectedAlgo SignatureAlgo
    18  		expectedErr  error
    19  	}{
    20  		{
    21  			"supported algorithm",
    22  			"secp256k1",
    23  			true,
    24  			hd.Secp256k1,
    25  			nil,
    26  		},
    27  		{
    28  			"not supported",
    29  			"notsupportedalgo",
    30  			false,
    31  			nil,
    32  			fmt.Errorf("provided algorithm \"notsupportedalgo\" is not supported"),
    33  		},
    34  	}
    35  
    36  	list := SigningAlgoList{hd.Secp256k1}
    37  	for _, tt := range tests {
    38  		tt := tt
    39  		t.Run(tt.name, func(t *testing.T) {
    40  			algorithm, err := NewSigningAlgoFromString(tt.algoStr, list)
    41  			if tt.isSupported {
    42  				require.Equal(t, hd.Secp256k1, algorithm)
    43  			} else {
    44  				require.EqualError(t, err, tt.expectedErr.Error())
    45  			}
    46  		})
    47  	}
    48  }
    49  
    50  func TestAltSigningAlgoList_Contains(t *testing.T) {
    51  	list := SigningAlgoList{hd.Secp256k1}
    52  
    53  	require.True(t, list.Contains(hd.Secp256k1))
    54  	require.False(t, list.Contains(notSupportedAlgo{}))
    55  }
    56  
    57  func TestAltSigningAlgoList_String(t *testing.T) {
    58  	list := SigningAlgoList{hd.Secp256k1, notSupportedAlgo{}}
    59  	require.Equal(t, fmt.Sprintf("%s,notSupported", string(hd.Secp256k1Type)), list.String())
    60  }
    61  
    62  type notSupportedAlgo struct{}
    63  
    64  func (n notSupportedAlgo) Name() hd.PubKeyType {
    65  	return "notSupported"
    66  }
    67  
    68  func (n notSupportedAlgo) Derive() hd.DeriveFn {
    69  	return hd.Secp256k1.Derive()
    70  }
    71  
    72  func (n notSupportedAlgo) Generate() hd.GenerateFn {
    73  	return hd.Secp256k1.Generate()
    74  }