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