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