github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/request/signature_test.go (about)

     1  package request
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/onflow/flow-go/engine/access/rest/models"
    11  	"github.com/onflow/flow-go/engine/access/rest/util"
    12  	"github.com/onflow/flow-go/model/flow"
    13  )
    14  
    15  func TestSignature_InvalidParse(t *testing.T) {
    16  	var signature Signature
    17  
    18  	tests := []struct {
    19  		in  string
    20  		err string
    21  	}{
    22  		{"s", "invalid encoding"},
    23  		{"", "missing value"},
    24  	}
    25  
    26  	for _, test := range tests {
    27  		err := signature.Parse(test.in)
    28  		assert.EqualError(t, err, test.err)
    29  	}
    30  
    31  }
    32  
    33  func TestSignature_ValidParse(t *testing.T) {
    34  	var signature Signature
    35  	err := signature.Parse("Mzg0MTQ5ODg4ZTg4MjRmYjMyNzM4MmM2ZWQ4ZjNjZjk1ODRlNTNlMzk4NGNhMDAxZmZjMjgwNzM4NmM0MzY3NTYxNmYwMTAwMTMzNDVkNjhmNzZkMmQ5YTBkYmI1MDA0MmEzOWRlOThlYzAzNTJjYTBkZWY3YjBlNjQ0YWJjOTQ=")
    36  	assert.NoError(t, err)
    37  	// todo test values
    38  }
    39  
    40  func TestTransactionSignature_ValidParse(t *testing.T) {
    41  	var txSignature TransactionSignature
    42  	addr := "01cf0e2f2f715450"
    43  	sig := "c83665f5212fad065cd27d370ef80e5fbdd20cd57411af5c76076a15dced05ac6e6d9afa88cd7337bf9c869f6785ecc1c568ca593a99dfeec14e024c0cd78289"
    44  	sigHex, _ := hex.DecodeString(sig)
    45  	encodedSig := util.ToBase64(sigHex)
    46  	err := txSignature.Parse(addr, "0", encodedSig, flow.Localnet.Chain())
    47  
    48  	assert.NoError(t, err)
    49  	assert.Equal(t, addr, txSignature.Address.String())
    50  	assert.Equal(t, 0, txSignature.SignerIndex)
    51  	assert.Equal(t, uint64(0), txSignature.KeyIndex)
    52  	assert.Equal(t, sig, fmt.Sprintf("%x", txSignature.Signature))
    53  }
    54  
    55  func TestTransactionSignatures_ValidParse(t *testing.T) {
    56  	tests := []struct {
    57  		inAddresses []string
    58  		inSigs      []string
    59  	}{
    60  		{[]string{"01cf0e2f2f715450"}, []string{"c83665f5212fad065cd27d370ef80e5fbdd20cd57411af5c76076a15dced05ac6e6d9afa88cd7337bf9c869f6785ecc1c568ca593a99dfeec14e024c0cd78289"}},
    61  		{[]string{"ee82856bf20e2aa6", "e03daebed8ca0615"}, []string{"223665f5212fad065cd27d370ef80e5fbdd20cd57411af5c76076a15dced05ac6e6d9afa88cd7337bf9c869f6785ecc1c568ca593a99dfeec14e024c0cd78289", "5553665f5212fad065cd27d370ef80e5fbdd20cd57411af5c76076a15dced05ac6e6d9afa88cd7337bf9c869f6785ecc1c568ca593a99dfeec14e024c0cd7822"}},
    62  	}
    63  
    64  	var txSigantures TransactionSignatures
    65  	chain := flow.Localnet.Chain()
    66  	for _, test := range tests {
    67  		sigs := make([]models.TransactionSignature, len(test.inAddresses))
    68  		for i, a := range test.inAddresses {
    69  			sigHex, _ := hex.DecodeString(test.inSigs[i])
    70  			encodedSig := util.ToBase64(sigHex)
    71  			sigs[i].Signature = encodedSig
    72  			sigs[i].KeyIndex = "0"
    73  			sigs[i].Address = a
    74  		}
    75  
    76  		err := txSigantures.Parse(sigs, chain)
    77  		assert.NoError(t, err)
    78  
    79  		assert.Equal(t, len(txSigantures), len(sigs))
    80  		for i, sig := range sigs {
    81  			assert.Equal(t, sig.Address, txSigantures[i].Address.String())
    82  			assert.Equal(t, 0, txSigantures[i].SignerIndex)
    83  			assert.Equal(t, uint64(0), txSigantures[i].KeyIndex)
    84  			assert.Equal(t, test.inSigs[i], fmt.Sprintf("%x", txSigantures[i].Signature))
    85  		}
    86  	}
    87  }