code.vegaprotocol.io/vega@v0.79.0/wallet/api/interactor/parallel_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package interactor_test
    17  
    18  import (
    19  	"context"
    20  	"sync"
    21  	"testing"
    22  	"time"
    23  
    24  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    25  	"code.vegaprotocol.io/vega/wallet/api/interactor"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestParallelInteractor(t *testing.T) {
    32  	t.Run("Request wallet connection review succeeds", testRequestWalletConnectionReviewSucceeds)
    33  	t.Run("Request wallet selection review succeeds", testRequestWalletSelectionSucceeds)
    34  	t.Run("Request passphrase succeeds", testRequestPassphraseSucceeds)
    35  	t.Run("Request permissions review succeeds", testRequestPermissionsReviewSucceeds)
    36  	t.Run("Request transaction review for sending succeeds", testRequestTransactionReviewForSendingSucceeds)
    37  	t.Run("Request transaction review for signing succeeds", testRequestTransactionReviewForSigningSucceeds)
    38  	t.Run("Request transaction review for checking succeeds", testRequestTransactionReviewForCheckingSucceeds)
    39  }
    40  
    41  func testRequestWalletConnectionReviewSucceeds(t *testing.T) {
    42  	interactorCtx := context.Background()
    43  	interactionCtx := context.Background()
    44  	approval := vgrand.RandomStr(5)
    45  	traceID := vgrand.RandomStr(4)
    46  	hostname := vgrand.RandomStr(4)
    47  
    48  	outboundCh := make(chan interactor.Interaction)
    49  	defer close(outboundCh)
    50  
    51  	wg := sync.WaitGroup{}
    52  
    53  	wg.Add(1)
    54  	go func() {
    55  		receivedInteraction := <-outboundCh
    56  
    57  		assert.Equal(t, traceID, receivedInteraction.TraceID)
    58  		assert.Equal(t, interactor.RequestWalletConnectionReviewName, receivedInteraction.Name)
    59  		request, ok := receivedInteraction.Data.(interactor.RequestWalletConnectionReview)
    60  		require.True(t, ok)
    61  		assert.Equal(t, hostname, request.Hostname)
    62  		assert.Equal(t, uint8(1), request.StepNumber)
    63  
    64  		request.ResponseCh <- interactor.Interaction{
    65  			TraceID: traceID,
    66  			Name:    interactor.WalletConnectionDecisionName,
    67  			Data: interactor.WalletConnectionDecision{
    68  				ConnectionApproval: approval,
    69  			},
    70  		}
    71  
    72  		wg.Done()
    73  	}()
    74  
    75  	pInteractor := interactor.NewParallelInteractor(interactorCtx, outboundCh)
    76  	result, err := pInteractor.RequestWalletConnectionReview(interactionCtx, traceID, 1, hostname)
    77  
    78  	wg.Wait()
    79  
    80  	require.NoError(t, err)
    81  	assert.Equal(t, approval, result)
    82  }
    83  
    84  func testRequestWalletSelectionSucceeds(t *testing.T) {
    85  	interactorCtx := context.Background()
    86  	interactionCtx := context.Background()
    87  	selectedWallet := vgrand.RandomStr(5)
    88  	traceID := vgrand.RandomStr(4)
    89  	hostname := vgrand.RandomStr(4)
    90  	availableWallets := []string{
    91  		vgrand.RandomStr(4),
    92  		vgrand.RandomStr(4),
    93  	}
    94  
    95  	outboundCh := make(chan interactor.Interaction)
    96  	defer close(outboundCh)
    97  
    98  	wg := sync.WaitGroup{}
    99  
   100  	wg.Add(1)
   101  	go func() {
   102  		receivedInteraction := <-outboundCh
   103  
   104  		assert.Equal(t, traceID, receivedInteraction.TraceID)
   105  		assert.Equal(t, interactor.RequestWalletSelectionName, receivedInteraction.Name)
   106  		request, ok := receivedInteraction.Data.(interactor.RequestWalletSelection)
   107  		require.True(t, ok)
   108  		assert.Equal(t, hostname, request.Hostname)
   109  		assert.Equal(t, uint8(1), request.StepNumber)
   110  
   111  		request.ResponseCh <- interactor.Interaction{
   112  			TraceID: traceID,
   113  			Name:    interactor.SelectedWalletName,
   114  			Data: interactor.SelectedWallet{
   115  				Wallet: selectedWallet,
   116  			},
   117  		}
   118  
   119  		wg.Done()
   120  	}()
   121  
   122  	pInteractor := interactor.NewParallelInteractor(interactorCtx, outboundCh)
   123  	result, err := pInteractor.RequestWalletSelection(interactionCtx, traceID, 1, hostname, availableWallets)
   124  
   125  	wg.Wait()
   126  
   127  	require.NoError(t, err)
   128  	assert.Equal(t, selectedWallet, result)
   129  }
   130  
   131  func testRequestPassphraseSucceeds(t *testing.T) {
   132  	interactorCtx := context.Background()
   133  	interactionCtx := context.Background()
   134  	passphrase := vgrand.RandomStr(5)
   135  	traceID := vgrand.RandomStr(4)
   136  	wallet := vgrand.RandomStr(4)
   137  	reason := vgrand.RandomStr(4)
   138  
   139  	outboundCh := make(chan interactor.Interaction)
   140  	defer close(outboundCh)
   141  
   142  	wg := sync.WaitGroup{}
   143  
   144  	wg.Add(1)
   145  	go func() {
   146  		receivedInteraction := <-outboundCh
   147  
   148  		assert.Equal(t, traceID, receivedInteraction.TraceID)
   149  		assert.Equal(t, interactor.RequestPassphraseName, receivedInteraction.Name)
   150  		request, ok := receivedInteraction.Data.(interactor.RequestPassphrase)
   151  		require.True(t, ok)
   152  		assert.Equal(t, wallet, request.Wallet)
   153  		assert.Equal(t, reason, request.Reason)
   154  		assert.Equal(t, uint8(1), request.StepNumber)
   155  
   156  		request.ResponseCh <- interactor.Interaction{
   157  			TraceID: traceID,
   158  			Name:    interactor.EnteredPassphraseName,
   159  			Data: interactor.EnteredPassphrase{
   160  				Passphrase: passphrase,
   161  			},
   162  		}
   163  
   164  		wg.Done()
   165  	}()
   166  
   167  	pInteractor := interactor.NewParallelInteractor(interactorCtx, outboundCh)
   168  	result, err := pInteractor.RequestPassphrase(interactionCtx, traceID, 1, wallet, reason)
   169  
   170  	wg.Wait()
   171  
   172  	require.NoError(t, err)
   173  	assert.Equal(t, passphrase, result)
   174  }
   175  
   176  func testRequestPermissionsReviewSucceeds(t *testing.T) {
   177  	interactorCtx := context.Background()
   178  	interactionCtx := context.Background()
   179  	traceID := vgrand.RandomStr(4)
   180  	wallet := vgrand.RandomStr(4)
   181  	hostname := vgrand.RandomStr(4)
   182  	perms := map[string]string{
   183  		vgrand.RandomStr(4): vgrand.RandomStr(4),
   184  	}
   185  
   186  	outboundCh := make(chan interactor.Interaction)
   187  	defer close(outboundCh)
   188  
   189  	wg := sync.WaitGroup{}
   190  
   191  	wg.Add(1)
   192  	go func() {
   193  		receivedInteraction := <-outboundCh
   194  
   195  		assert.Equal(t, traceID, receivedInteraction.TraceID)
   196  		assert.Equal(t, interactor.RequestPermissionsReviewName, receivedInteraction.Name)
   197  		request, ok := receivedInteraction.Data.(interactor.RequestPermissionsReview)
   198  		require.True(t, ok)
   199  		assert.Equal(t, wallet, request.Wallet)
   200  		assert.Equal(t, hostname, request.Hostname)
   201  		assert.Equal(t, perms, request.Permissions)
   202  		assert.Equal(t, uint8(1), request.StepNumber)
   203  
   204  		request.ResponseCh <- interactor.Interaction{
   205  			TraceID: traceID,
   206  			Name:    interactor.DecisionName,
   207  			Data: interactor.Decision{
   208  				Approved: true,
   209  			},
   210  		}
   211  
   212  		wg.Done()
   213  	}()
   214  
   215  	pInteractor := interactor.NewParallelInteractor(interactorCtx, outboundCh)
   216  	result, err := pInteractor.RequestPermissionsReview(interactionCtx, traceID, 1, hostname, wallet, perms)
   217  
   218  	wg.Wait()
   219  
   220  	require.NoError(t, err)
   221  	assert.True(t, result)
   222  }
   223  
   224  func testRequestTransactionReviewForSendingSucceeds(t *testing.T) {
   225  	interactorCtx := context.Background()
   226  	interactionCtx := context.Background()
   227  	traceID := vgrand.RandomStr(4)
   228  	hostname := vgrand.RandomStr(4)
   229  	wallet := vgrand.RandomStr(4)
   230  	publicKey := vgrand.RandomStr(4)
   231  	receivedAt := time.Now()
   232  	transaction := vgrand.RandomStr(4)
   233  
   234  	outboundCh := make(chan interactor.Interaction)
   235  	defer close(outboundCh)
   236  
   237  	wg := sync.WaitGroup{}
   238  
   239  	wg.Add(1)
   240  	go func() {
   241  		receivedInteraction := <-outboundCh
   242  
   243  		assert.Equal(t, traceID, receivedInteraction.TraceID)
   244  		assert.Equal(t, interactor.RequestTransactionReviewForSendingName, receivedInteraction.Name)
   245  		request, ok := receivedInteraction.Data.(interactor.RequestTransactionReviewForSending)
   246  		require.True(t, ok)
   247  		assert.Equal(t, hostname, request.Hostname)
   248  		assert.Equal(t, wallet, request.Wallet)
   249  		assert.Equal(t, publicKey, request.PublicKey)
   250  		assert.Equal(t, transaction, request.Transaction)
   251  		assert.Equal(t, receivedAt, request.ReceivedAt)
   252  		assert.Equal(t, uint8(1), request.StepNumber)
   253  
   254  		request.ResponseCh <- interactor.Interaction{
   255  			TraceID: traceID,
   256  			Name:    interactor.DecisionName,
   257  			Data: interactor.Decision{
   258  				Approved: true,
   259  			},
   260  		}
   261  
   262  		wg.Done()
   263  	}()
   264  
   265  	pInteractor := interactor.NewParallelInteractor(interactorCtx, outboundCh)
   266  	result, err := pInteractor.RequestTransactionReviewForSending(interactionCtx, traceID, 1, hostname, wallet, publicKey, transaction, receivedAt)
   267  
   268  	wg.Wait()
   269  
   270  	require.NoError(t, err)
   271  	assert.True(t, result)
   272  }
   273  
   274  func testRequestTransactionReviewForSigningSucceeds(t *testing.T) {
   275  	interactorCtx := context.Background()
   276  	interactionCtx := context.Background()
   277  	traceID := vgrand.RandomStr(4)
   278  	hostname := vgrand.RandomStr(4)
   279  	wallet := vgrand.RandomStr(4)
   280  	publicKey := vgrand.RandomStr(4)
   281  	receivedAt := time.Now()
   282  	transaction := vgrand.RandomStr(4)
   283  
   284  	outboundCh := make(chan interactor.Interaction)
   285  	defer close(outboundCh)
   286  
   287  	wg := sync.WaitGroup{}
   288  
   289  	wg.Add(1)
   290  	go func() {
   291  		receivedInteraction := <-outboundCh
   292  
   293  		assert.Equal(t, traceID, receivedInteraction.TraceID)
   294  		assert.Equal(t, interactor.RequestTransactionReviewForSigningName, receivedInteraction.Name)
   295  		request, ok := receivedInteraction.Data.(interactor.RequestTransactionReviewForSigning)
   296  		require.True(t, ok)
   297  		assert.Equal(t, hostname, request.Hostname)
   298  		assert.Equal(t, wallet, request.Wallet)
   299  		assert.Equal(t, publicKey, request.PublicKey)
   300  		assert.Equal(t, transaction, request.Transaction)
   301  		assert.Equal(t, receivedAt, request.ReceivedAt)
   302  		assert.Equal(t, uint8(1), request.StepNumber)
   303  
   304  		request.ResponseCh <- interactor.Interaction{
   305  			TraceID: traceID,
   306  			Name:    interactor.DecisionName,
   307  			Data: interactor.Decision{
   308  				Approved: true,
   309  			},
   310  		}
   311  
   312  		wg.Done()
   313  	}()
   314  
   315  	pInteractor := interactor.NewParallelInteractor(interactorCtx, outboundCh)
   316  	result, err := pInteractor.RequestTransactionReviewForSigning(interactionCtx, traceID, 1, hostname, wallet, publicKey, transaction, receivedAt)
   317  
   318  	wg.Wait()
   319  
   320  	require.NoError(t, err)
   321  	assert.True(t, result)
   322  }
   323  
   324  func testRequestTransactionReviewForCheckingSucceeds(t *testing.T) {
   325  	interactorCtx := context.Background()
   326  	interactionCtx := context.Background()
   327  	traceID := vgrand.RandomStr(4)
   328  	hostname := vgrand.RandomStr(4)
   329  	wallet := vgrand.RandomStr(4)
   330  	publicKey := vgrand.RandomStr(4)
   331  	receivedAt := time.Now()
   332  	transaction := vgrand.RandomStr(4)
   333  
   334  	outboundCh := make(chan interactor.Interaction)
   335  	defer close(outboundCh)
   336  
   337  	wg := sync.WaitGroup{}
   338  
   339  	wg.Add(1)
   340  	go func() {
   341  		receivedInteraction := <-outboundCh
   342  
   343  		assert.Equal(t, traceID, receivedInteraction.TraceID)
   344  		assert.Equal(t, interactor.RequestTransactionReviewForCheckingName, receivedInteraction.Name)
   345  		request, ok := receivedInteraction.Data.(interactor.RequestTransactionReviewForChecking)
   346  		require.True(t, ok)
   347  		assert.Equal(t, hostname, request.Hostname)
   348  		assert.Equal(t, wallet, request.Wallet)
   349  		assert.Equal(t, publicKey, request.PublicKey)
   350  		assert.Equal(t, transaction, request.Transaction)
   351  		assert.Equal(t, receivedAt, request.ReceivedAt)
   352  		assert.Equal(t, uint8(1), request.StepNumber)
   353  
   354  		request.ResponseCh <- interactor.Interaction{
   355  			TraceID: traceID,
   356  			Name:    interactor.DecisionName,
   357  			Data: interactor.Decision{
   358  				Approved: true,
   359  			},
   360  		}
   361  
   362  		wg.Done()
   363  	}()
   364  
   365  	pInteractor := interactor.NewParallelInteractor(interactorCtx, outboundCh)
   366  	result, err := pInteractor.RequestTransactionReviewForChecking(interactionCtx, traceID, 1, hostname, wallet, publicKey, transaction, receivedAt)
   367  
   368  	wg.Wait()
   369  
   370  	require.NoError(t, err)
   371  	assert.True(t, result)
   372  }