github.com/koko1123/flow-go-1@v0.29.6/module/signature/type_encoder_test.go (about)

     1  package signature_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/koko1123/flow-go-1/model/encoding"
     9  	"github.com/koko1123/flow-go-1/module/signature"
    10  	"github.com/koko1123/flow-go-1/utils/unittest"
    11  )
    12  
    13  func TestEncodeDecodeStakingSig(t *testing.T) {
    14  	sig := unittest.SignatureFixture()
    15  	encoded := signature.EncodeSingleSig(encoding.SigTypeStaking, sig)
    16  	decodedType, decodedSig, err := signature.DecodeSingleSig(encoded)
    17  	require.NoError(t, err)
    18  	require.Equal(t, encoding.SigTypeStaking, decodedType)
    19  	require.Equal(t, sig, decodedSig)
    20  }
    21  
    22  func TestEncodeDecodeRandomBeaconSig(t *testing.T) {
    23  	sig := unittest.SignatureFixture()
    24  	encoded := signature.EncodeSingleSig(encoding.SigTypeRandomBeacon, sig)
    25  	decodedType, decodedSig, err := signature.DecodeSingleSig(encoded)
    26  	require.NoError(t, err)
    27  	require.Equal(t, encoding.SigTypeRandomBeacon, decodedType)
    28  	require.Equal(t, sig, decodedSig)
    29  }
    30  
    31  // encode with invalid sig type, then decode will fail
    32  func TestEncodeDecodeInvalidSig(t *testing.T) {
    33  	sig := unittest.SignatureFixture()
    34  
    35  	for i := int(encoding.SigTypeRandomBeacon) + 1; i < int(encoding.SigTypeRandomBeacon)+5; i++ {
    36  		sigType := encoding.SigType(i)
    37  		encoded := signature.EncodeSingleSig(sigType, sig)
    38  		_, _, err := signature.DecodeSingleSig(encoded)
    39  		require.Error(t, err)
    40  	}
    41  }
    42  
    43  func TestDecodeEmptySig(t *testing.T) {
    44  	_, _, err := signature.DecodeSingleSig([]byte{})
    45  	require.Error(t, err)
    46  }
    47  
    48  // encode two different sigs wth the same type, the encoded sig should be different
    49  func TestEncodeTwoSigsDifferent(t *testing.T) {
    50  	sigs := unittest.SignaturesFixture(2)
    51  	sig1, sig2 := sigs[0], sigs[1]
    52  	encodedSig1 := signature.EncodeSingleSig(encoding.SigTypeStaking, sig1)
    53  	encodedSig2 := signature.EncodeSingleSig(encoding.SigTypeStaking, sig2)
    54  	require.NotEqual(t, encodedSig1, encodedSig2)
    55  }
    56  
    57  // encode the same sig with the different type, the encoded sig should be different
    58  func TestEncodeSameSigWithDifferentTypeShouldBeDifferen(t *testing.T) {
    59  	sig := unittest.SignatureFixture()
    60  	encodedAsStaking := signature.EncodeSingleSig(encoding.SigTypeStaking, sig)
    61  	encodedAsRandomBeacon := signature.EncodeSingleSig(encoding.SigTypeRandomBeacon, sig)
    62  	require.NotEqual(t, encodedAsStaking, encodedAsRandomBeacon)
    63  }