github.com/Consensys/quorum@v21.1.0+incompatible/plugin/account/gateway_test.go (about)

     1  package account
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/ethereum/go-ethereum/accounts"
    10  	"github.com/ethereum/go-ethereum/common"
    11  	"github.com/ethereum/go-ethereum/plugin/account/internal/testutils"
    12  	"github.com/golang/mock/gomock"
    13  	"github.com/jpmorganchase/quorum-account-plugin-sdk-go/mock_proto"
    14  	"github.com/jpmorganchase/quorum-account-plugin-sdk-go/proto"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  var (
    20  	scheme = "scheme"
    21  
    22  	acct1 = accounts.Account{
    23  		Address: common.HexToAddress("0x4d6d744b6da435b5bbdde2526dc20e9a41cb72e5"),
    24  		URL:     accounts.URL{Scheme: scheme, Path: "acctUri1"},
    25  	}
    26  
    27  	acct2 = accounts.Account{
    28  		Address: common.HexToAddress("0x2332f90a329c2c55ba120b1449d36a144d1f9fe4"),
    29  		URL:     accounts.URL{Scheme: scheme, Path: "acctUri2"},
    30  	}
    31  )
    32  
    33  func TestPluginGateway_Status(t *testing.T) {
    34  	ctrl := gomock.NewController(t)
    35  	defer ctrl.Finish()
    36  
    37  	resp := &proto.StatusResponse{Status: "some status"}
    38  
    39  	wantReq := &proto.StatusRequest{}
    40  	wantStatus := resp.Status
    41  
    42  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
    43  	mockClient.
    44  		EXPECT().
    45  		Status(gomock.Any(), testutils.StatusRequestMatcher{R: wantReq}).
    46  		Return(resp, nil)
    47  
    48  	g := &service{client: mockClient}
    49  	got, err := g.Status(context.Background())
    50  
    51  	assert.NoError(t, err)
    52  	assert.Equal(t, wantStatus, got)
    53  }
    54  
    55  func TestPluginGateway_Open(t *testing.T) {
    56  	ctrl := gomock.NewController(t)
    57  	defer ctrl.Finish()
    58  
    59  	resp := &proto.OpenResponse{}
    60  
    61  	wantReq := &proto.OpenRequest{Passphrase: "pwd"}
    62  
    63  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
    64  	mockClient.
    65  		EXPECT().
    66  		Open(gomock.Any(), testutils.OpenRequestMatcher{R: wantReq}).
    67  		Return(resp, nil)
    68  
    69  	g := &service{client: mockClient}
    70  	err := g.Open(context.Background(), "pwd")
    71  
    72  	assert.NoError(t, err)
    73  }
    74  
    75  func TestPluginGateway_Close(t *testing.T) {
    76  	ctrl := gomock.NewController(t)
    77  	defer ctrl.Finish()
    78  
    79  	resp := &proto.CloseResponse{}
    80  
    81  	wantReq := &proto.CloseRequest{}
    82  
    83  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
    84  	mockClient.
    85  		EXPECT().
    86  		Close(gomock.Any(), testutils.CloseRequestMatcher{R: wantReq}).
    87  		Return(resp, nil)
    88  
    89  	g := &service{client: mockClient}
    90  	err := g.Close(context.Background())
    91  
    92  	assert.NoError(t, err)
    93  }
    94  
    95  func TestPluginGateway_Accounts(t *testing.T) {
    96  	ctrl := gomock.NewController(t)
    97  	defer ctrl.Finish()
    98  
    99  	resp := &proto.AccountsResponse{
   100  		Accounts: []*proto.Account{
   101  			{Address: acct1.Address.Bytes(), Url: acct1.URL.String()},
   102  			{Address: acct2.Address.Bytes(), Url: acct2.URL.String()},
   103  		},
   104  	}
   105  
   106  	wantReq := &proto.AccountsRequest{}
   107  	wantAccts := []accounts.Account{acct1, acct2}
   108  
   109  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
   110  	mockClient.
   111  		EXPECT().
   112  		Accounts(gomock.Any(), testutils.AccountsRequestMatcher{R: wantReq}).
   113  		Return(resp, nil)
   114  
   115  	g := &service{client: mockClient}
   116  	got := g.Accounts(context.Background())
   117  
   118  	assert.Equal(t, wantAccts, got)
   119  }
   120  
   121  func TestPluginGateway_Contains(t *testing.T) {
   122  	ctrl := gomock.NewController(t)
   123  	defer ctrl.Finish()
   124  
   125  	resp := &proto.ContainsResponse{IsContained: true}
   126  
   127  	wantReq := &proto.ContainsRequest{
   128  		Address: acct1.Address.Bytes(),
   129  	}
   130  
   131  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
   132  	mockClient.
   133  		EXPECT().
   134  		Contains(gomock.Any(), testutils.ContainsRequestMatcher{R: wantReq}).
   135  		Return(resp, nil)
   136  
   137  	g := &service{client: mockClient}
   138  	got := g.Contains(context.Background(), acct1)
   139  
   140  	assert.True(t, got)
   141  }
   142  
   143  func TestPluginGateway_Sign(t *testing.T) {
   144  	ctrl := gomock.NewController(t)
   145  	defer ctrl.Finish()
   146  
   147  	want := []byte("signed data")
   148  	resp := &proto.SignResponse{Sig: want}
   149  
   150  	toSign := []byte("to sign")
   151  	wantReq := &proto.SignRequest{
   152  		Address: acct1.Address.Bytes(),
   153  		ToSign:  toSign,
   154  	}
   155  
   156  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
   157  	mockClient.
   158  		EXPECT().
   159  		Sign(gomock.Any(), testutils.SignRequestMatcher{R: wantReq}).
   160  		Return(resp, nil)
   161  
   162  	g := &service{client: mockClient}
   163  	got, err := g.Sign(context.Background(), acct1, toSign)
   164  
   165  	assert.NoError(t, err)
   166  	assert.Equal(t, want, got)
   167  }
   168  
   169  func TestPluginGateway_UnlockAndSign(t *testing.T) {
   170  	ctrl := gomock.NewController(t)
   171  	defer ctrl.Finish()
   172  
   173  	want := []byte("signed data")
   174  	resp := &proto.SignResponse{Sig: want}
   175  
   176  	toSign := []byte("to sign")
   177  	wantReq := &proto.UnlockAndSignRequest{
   178  		Address:    acct1.Address.Bytes(),
   179  		ToSign:     toSign,
   180  		Passphrase: "pwd",
   181  	}
   182  
   183  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
   184  	mockClient.
   185  		EXPECT().
   186  		UnlockAndSign(gomock.Any(), testutils.UnlockAndSignRequestMatcher{R: wantReq}).
   187  		Return(resp, nil)
   188  
   189  	g := &service{client: mockClient}
   190  	got, err := g.UnlockAndSign(context.Background(), acct1, toSign, "pwd")
   191  
   192  	assert.NoError(t, err)
   193  	assert.Equal(t, want, got)
   194  }
   195  
   196  func TestPluginGateway_TimedUnlock(t *testing.T) {
   197  	ctrl := gomock.NewController(t)
   198  	defer ctrl.Finish()
   199  
   200  	resp := &proto.TimedUnlockResponse{}
   201  
   202  	pwd := "pwd"
   203  
   204  	wantReq := &proto.TimedUnlockRequest{
   205  		Address:  acct1.Address.Bytes(),
   206  		Password: pwd,
   207  		Duration: int64(1),
   208  	}
   209  
   210  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
   211  	mockClient.
   212  		EXPECT().
   213  		TimedUnlock(gomock.Any(), testutils.TimedUnlockRequestMatcher{R: wantReq}).
   214  		Return(resp, nil)
   215  
   216  	g := &service{client: mockClient}
   217  	err := g.TimedUnlock(context.Background(), acct1, pwd, time.Nanosecond)
   218  
   219  	assert.NoError(t, err)
   220  }
   221  
   222  func TestPluginGateway_Lock(t *testing.T) {
   223  	ctrl := gomock.NewController(t)
   224  	defer ctrl.Finish()
   225  
   226  	resp := &proto.LockResponse{}
   227  
   228  	wantReq := &proto.LockRequest{
   229  		Address: acct1.Address.Bytes(),
   230  	}
   231  
   232  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
   233  	mockClient.
   234  		EXPECT().
   235  		Lock(gomock.Any(), testutils.LockRequestMatcher{R: wantReq}).
   236  		Return(resp, nil)
   237  
   238  	g := &service{client: mockClient}
   239  	err := g.Lock(context.Background(), acct1)
   240  
   241  	assert.NoError(t, err)
   242  }
   243  
   244  func TestPluginGateway_NewAccount(t *testing.T) {
   245  	ctrl := gomock.NewController(t)
   246  	defer ctrl.Finish()
   247  
   248  	resp := &proto.NewAccountResponse{
   249  		Account: &proto.Account{
   250  			Address: acct1.Address.Bytes(),
   251  			Url:     acct1.URL.String(),
   252  		},
   253  	}
   254  
   255  	newAccountConfig := []byte("newacctconfig")
   256  
   257  	b, err := json.Marshal(newAccountConfig)
   258  	require.NoError(t, err)
   259  	wantReq := &proto.NewAccountRequest{
   260  		NewAccountConfig: b,
   261  	}
   262  
   263  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
   264  	mockClient.
   265  		EXPECT().
   266  		NewAccount(gomock.Any(), testutils.NewAccountRequestMatcher{R: wantReq}).
   267  		Return(resp, nil)
   268  
   269  	g := &service{client: mockClient}
   270  	gotAcct, err := g.NewAccount(context.Background(), newAccountConfig)
   271  
   272  	assert.Equal(t, acct1, gotAcct)
   273  	assert.NoError(t, err)
   274  }
   275  
   276  func TestPluginGateway_ImportRawKey(t *testing.T) {
   277  	ctrl := gomock.NewController(t)
   278  	defer ctrl.Finish()
   279  
   280  	resp := &proto.ImportRawKeyResponse{
   281  		Account: &proto.Account{
   282  			Address: acct1.Address.Bytes(),
   283  			Url:     acct1.URL.String(),
   284  		},
   285  	}
   286  
   287  	newAccountConfig := []byte("newacctconfig")
   288  	var rawKey = "1fe8f1ad4053326db20529257ac9401f2e6c769ef1d736b8c2f5aba5f787c72b"
   289  
   290  	b, err := json.Marshal(newAccountConfig)
   291  	require.NoError(t, err)
   292  	wantReq := &proto.ImportRawKeyRequest{
   293  		RawKey:           rawKey,
   294  		NewAccountConfig: b,
   295  	}
   296  
   297  	mockClient := mock_proto.NewMockAccountServiceClient(ctrl)
   298  	mockClient.
   299  		EXPECT().
   300  		ImportRawKey(gomock.Any(), testutils.ImportRawKeyRequestMatcher{R: wantReq}).
   301  		Return(resp, nil)
   302  
   303  	g := &service{client: mockClient}
   304  	gotAcct, err := g.ImportRawKey(context.Background(), rawKey, newAccountConfig)
   305  
   306  	assert.Equal(t, acct1, gotAcct)
   307  	assert.NoError(t, err)
   308  }
   309  
   310  func TestPluginGateway_ImportRawKey_InvalidRawKey(t *testing.T) {
   311  	ctrl := gomock.NewController(t)
   312  	defer ctrl.Finish()
   313  
   314  	newAccountConfig := []byte("newacctconfig")
   315  	var rawKey = "aaaaaa"
   316  
   317  	g := &service{}
   318  	_, err := g.ImportRawKey(context.Background(), rawKey, newAccountConfig)
   319  
   320  	require.EqualError(t, err, "invalid length, need 256 bits")
   321  }
   322  
   323  func Test_ToUrl(t *testing.T) {
   324  	strUrl := "http://myurl:8000"
   325  	want := accounts.URL{
   326  		Scheme: "http",
   327  		Path:   "myurl:8000",
   328  	}
   329  	got, err := ToUrl(strUrl)
   330  	require.NoError(t, err)
   331  	require.Equal(t, want, got)
   332  }
   333  
   334  func Test_ToUrl_Invalid(t *testing.T) {
   335  	strUrl := "://noscheme:8000"
   336  	_, err := ToUrl(strUrl)
   337  	require.Error(t, err)
   338  }