github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/environment/account_key_reader_test.go (about) 1 package environment_test 2 3 import ( 4 "testing" 5 "testing/quick" 6 7 "github.com/onflow/cadence/runtime/common" 8 testMock "github.com/stretchr/testify/mock" 9 "github.com/stretchr/testify/require" 10 11 "github.com/onflow/crypto" 12 "github.com/onflow/crypto/hash" 13 14 "github.com/onflow/flow-go/fvm/environment" 15 "github.com/onflow/flow-go/fvm/environment/mock" 16 "github.com/onflow/flow-go/fvm/tracing" 17 ) 18 19 func newDummyAccountKeyReader( 20 t *testing.T, 21 keyCount uint64, 22 ) environment.AccountKeyReader { 23 tracer := tracing.NewTracerSpan() 24 meter := mock.NewMeter(t) 25 meter.On("MeterComputation", testMock.Anything, testMock.Anything).Return(nil) 26 accounts := &FakeAccounts{keyCount: keyCount} 27 return environment.NewAccountKeyReader(tracer, meter, accounts) 28 } 29 30 func bytesToAddress(bytes ...uint8) common.Address { 31 return common.MustBytesToAddress(bytes) 32 } 33 34 func TestKeyConversionValidAlgorithms(t *testing.T) { 35 t.Parallel() 36 37 t.Run("invalid hash algo", func(t *testing.T) { 38 t.Parallel() 39 40 accountKey := FakePublicKey{}.toAccountPublicKey() 41 accountKey.HashAlgo = hash.UnknownHashingAlgorithm 42 43 rtKey, err := environment.FlowToRuntimeAccountKey(accountKey) 44 require.Error(t, err) 45 require.Nil(t, rtKey) 46 }) 47 48 t.Run("invalid sign algo", func(t *testing.T) { 49 t.Parallel() 50 51 accountKey := FakePublicKey{}.toAccountPublicKey() 52 accountKey.SignAlgo = crypto.UnknownSigningAlgorithm 53 54 rtKey, err := environment.FlowToRuntimeAccountKey(accountKey) 55 require.Error(t, err) 56 require.Nil(t, rtKey) 57 }) 58 59 t.Run("valid key", func(t *testing.T) { 60 t.Parallel() 61 62 accountKey := FakePublicKey{}.toAccountPublicKey() 63 64 rtKey, err := environment.FlowToRuntimeAccountKey(accountKey) 65 require.NoError(t, err) 66 require.NotNil(t, rtKey) 67 68 require.Equal(t, accountKey.PublicKey.Encode(), rtKey.PublicKey.PublicKey) 69 }) 70 } 71 func TestAccountKeyReader_get_valid_key(t *testing.T) { 72 t.Parallel() 73 address := bytesToAddress(1, 2, 3, 4) 74 75 res, err := newDummyAccountKeyReader(t, 10).GetAccountKey(address, 0) 76 require.NoError(t, err) 77 78 expected, err := environment.FlowToRuntimeAccountKey( 79 FakePublicKey{}.toAccountPublicKey(), 80 ) 81 82 require.NoError(t, err) 83 84 require.Equal(t, expected, res) 85 } 86 87 func TestAccountKeyReader_get_out_of_range(t *testing.T) { 88 t.Parallel() 89 address := bytesToAddress(1, 2, 3, 4) 90 91 res, err := newDummyAccountKeyReader(t, 0).GetAccountKey(address, 1000) 92 // GetAccountKey should distinguish between an invalid index, and issues like failing to fetch a key from storage 93 require.Nil(t, err) 94 require.Nil(t, res) 95 } 96 97 func TestAccountKeyReader_get_key_count(t *testing.T) { 98 t.Parallel() 99 address := bytesToAddress(1, 2, 3, 4) 100 101 identity := func(n uint64) (uint64, error) { return n, nil } 102 prop := func(n uint64) (uint64, error) { 103 return newDummyAccountKeyReader(t, n).AccountKeysCount(address) 104 } 105 106 if err := quick.CheckEqual(identity, prop, nil); err != nil { 107 t.Error(err) 108 } 109 }