github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/environment/account_key_updater_test.go (about) 1 package environment_test 2 3 import ( 4 "fmt" 5 "testing" 6 "unicode/utf8" 7 8 "github.com/fxamacker/cbor/v2" 9 "github.com/onflow/atree" 10 "github.com/onflow/cadence/runtime" 11 "github.com/onflow/cadence/runtime/sema" 12 "github.com/onflow/crypto" 13 "github.com/onflow/crypto/hash" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/require" 16 17 "github.com/onflow/flow-go/fvm/environment" 18 "github.com/onflow/flow-go/fvm/errors" 19 "github.com/onflow/flow-go/model/flow" 20 ) 21 22 func TestNewAccountKey_error_handling_produces_valid_utf8_and_sign_algo(t *testing.T) { 23 24 invalidSignAlgo := runtime.SignatureAlgorithm(254) 25 publicKey := &runtime.PublicKey{ 26 PublicKey: nil, 27 SignAlgo: invalidSignAlgo, 28 } 29 30 _, err := environment.NewAccountPublicKey( 31 publicKey, 32 sema.HashAlgorithmSHA2_384, 33 0, 34 0) 35 36 require.True(t, errors.IsValueError(err)) 37 38 require.Contains(t, err.Error(), fmt.Sprintf("%d", invalidSignAlgo)) 39 40 errorString := err.Error() 41 assert.True(t, utf8.ValidString(errorString)) 42 43 // check if they can encoded and decoded using CBOR 44 marshalledBytes, err := cbor.Marshal(errorString) 45 require.NoError(t, err) 46 47 var unmarshalledString string 48 49 err = cbor.Unmarshal(marshalledBytes, &unmarshalledString) 50 require.NoError(t, err) 51 52 require.Equal(t, errorString, unmarshalledString) 53 } 54 55 func TestNewAccountKey_error_handling_produces_valid_utf8_and_hash_algo(t *testing.T) { 56 57 publicKey := &runtime.PublicKey{ 58 PublicKey: nil, 59 SignAlgo: runtime.SignatureAlgorithmECDSA_P256, 60 } 61 62 invalidHashAlgo := sema.HashAlgorithm(112) 63 64 _, err := environment.NewAccountPublicKey(publicKey, invalidHashAlgo, 0, 0) 65 66 require.True(t, errors.IsValueError(err)) 67 68 require.Contains(t, err.Error(), fmt.Sprintf("%d", invalidHashAlgo)) 69 70 errorString := err.Error() 71 assert.True(t, utf8.ValidString(errorString)) 72 73 // check if they can encoded and decoded using CBOR 74 marshalledBytes, err := cbor.Marshal(errorString) 75 require.NoError(t, err) 76 77 var unmarshalledString string 78 79 err = cbor.Unmarshal(marshalledBytes, &unmarshalledString) 80 require.NoError(t, err) 81 82 require.Equal(t, errorString, unmarshalledString) 83 } 84 85 func TestNewAccountKey_error_handling_produces_valid_utf8(t *testing.T) { 86 87 publicKey := &runtime.PublicKey{ 88 PublicKey: []byte{0xc3, 0x28}, // some invalid UTF8 89 SignAlgo: runtime.SignatureAlgorithmECDSA_P256, 90 } 91 92 _, err := environment.NewAccountPublicKey( 93 publicKey, 94 runtime.HashAlgorithmSHA2_256, 95 0, 96 0) 97 98 require.True(t, errors.IsValueError(err)) 99 100 errorString := err.Error() 101 assert.True(t, utf8.ValidString(errorString)) 102 103 // check if they can encoded and decoded using CBOR 104 marshalledBytes, err := cbor.Marshal(errorString) 105 require.NoError(t, err) 106 107 var unmarshalledString string 108 109 err = cbor.Unmarshal(marshalledBytes, &unmarshalledString) 110 require.NoError(t, err) 111 112 require.Equal(t, errorString, unmarshalledString) 113 } 114 115 type FakePublicKey struct { 116 data []byte 117 } 118 119 var _ crypto.PublicKey = &FakePublicKey{} 120 121 func (f FakePublicKey) toAccountPublicKey() flow.AccountPublicKey { 122 return flow.AccountPublicKey{ 123 Index: 1, 124 PublicKey: f, 125 SignAlgo: crypto.ECDSASecp256k1, 126 HashAlgo: hash.SHA3_256, 127 SeqNumber: 0, 128 Weight: 1000, 129 Revoked: false, 130 } 131 } 132 133 func (f FakePublicKey) Encode() []byte { 134 return f.data 135 } 136 137 func (f FakePublicKey) Algorithm() crypto.SigningAlgorithm { return crypto.ECDSASecp256k1 } 138 func (f FakePublicKey) Size() int { return 0 } 139 func (f FakePublicKey) String() string { return "" } 140 func (f FakePublicKey) Verify(_ crypto.Signature, _ []byte, _ hash.Hasher) (bool, error) { 141 return false, nil 142 } 143 func (f FakePublicKey) EncodeCompressed() []byte { return nil } 144 func (f FakePublicKey) Equals(key crypto.PublicKey) bool { return false } 145 146 type FakeAccounts struct { 147 keyCount uint64 148 } 149 150 var _ environment.Accounts = &FakeAccounts{} 151 152 func (f FakeAccounts) Exists(address flow.Address) (bool, error) { return true, nil } 153 func (f FakeAccounts) Get(address flow.Address) (*flow.Account, error) { return &flow.Account{}, nil } 154 func (f FakeAccounts) GetPublicKeyCount(_ flow.Address) (uint64, error) { 155 return f.keyCount, nil 156 } 157 func (f FakeAccounts) AppendPublicKey(_ flow.Address, _ flow.AccountPublicKey) error { return nil } 158 func (f FakeAccounts) GetPublicKey(address flow.Address, keyIndex uint64) (flow.AccountPublicKey, error) { 159 if keyIndex >= f.keyCount { 160 return flow.AccountPublicKey{}, errors.NewAccountPublicKeyNotFoundError(address, keyIndex) 161 } 162 return FakePublicKey{}.toAccountPublicKey(), nil 163 } 164 165 func (f FakeAccounts) SetPublicKey(_ flow.Address, _ uint64, _ flow.AccountPublicKey) ([]byte, error) { 166 return nil, nil 167 } 168 func (f FakeAccounts) GetContractNames(_ flow.Address) ([]string, error) { return nil, nil } 169 func (f FakeAccounts) GetContract(_ string, _ flow.Address) ([]byte, error) { return nil, nil } 170 func (f FakeAccounts) ContractExists(_ string, _ flow.Address) (bool, error) { return false, nil } 171 func (f FakeAccounts) SetContract(_ string, _ flow.Address, _ []byte) error { return nil } 172 func (f FakeAccounts) DeleteContract(_ string, _ flow.Address) error { return nil } 173 func (f FakeAccounts) Create(_ []flow.AccountPublicKey, _ flow.Address) error { return nil } 174 func (f FakeAccounts) GetValue(_ flow.RegisterID) (flow.RegisterValue, error) { return nil, nil } 175 func (f FakeAccounts) GetStorageUsed(_ flow.Address) (uint64, error) { return 0, nil } 176 func (f FakeAccounts) SetValue(_ flow.RegisterID, _ []byte) error { return nil } 177 func (f FakeAccounts) AllocateSlabIndex(_ flow.Address) (atree.SlabIndex, error) { 178 return atree.SlabIndex{}, nil 179 } 180 func (f FakeAccounts) GenerateAccountLocalID(address flow.Address) (uint64, error) { 181 return 0, nil 182 }