code.vegaprotocol.io/vega@v0.79.0/vegatools/checktx/check_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 checktx
    17  
    18  import (
    19  	"encoding/base64"
    20  	"os"
    21  	"path"
    22  	"testing"
    23  
    24  	v1 "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    25  
    26  	"github.com/golang/protobuf/jsonpb"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  const (
    31  	txVersion = 3
    32  )
    33  
    34  func createTestDataFile(t *testing.T, testFileDir string, fileName string, encodedTransaction string) {
    35  	t.Helper()
    36  	err := os.MkdirAll(testFileDir, 0o755)
    37  	assert.NoErrorf(t, err, "error occurred when attempting to make a directory for the valid test data")
    38  
    39  	filePath := path.Join(testFileDir, fileName)
    40  
    41  	err = os.WriteFile(filePath, []byte(encodedTransaction), 0o644)
    42  	assert.NoErrorf(t, err, "error when creating transaction.json file.\nerr: %v", err)
    43  }
    44  
    45  func clearTestData(t *testing.T, directory string) {
    46  	t.Helper()
    47  	err := os.RemoveAll(directory)
    48  	assert.NoErrorf(t, err, "error occurred when attempting to clean valid test data dir")
    49  }
    50  
    51  func TestCheckTransactionsInDirectoryThrowsNoErrAndReturnsAccurateMetrics(t *testing.T) {
    52  	testDir := t.TempDir()
    53  	defer clearTestData(t, testDir)
    54  	encodedTransaction, err := CreatedEncodedTransactionData()
    55  	assert.NoErrorf(t, err, "error occurred when attempting to create encoded test data")
    56  
    57  	createTestDataFile(t, testDir, "transaction1.txt", encodedTransaction)
    58  	createTestDataFile(t, testDir, "transaction2.txt", encodedTransaction)
    59  
    60  	resultData, err := CheckTransactionsInDirectory(testDir)
    61  	assert.NoErrorf(t, err, "expected no error to occur when analysing valid transactions. Err: %v", err)
    62  	assert.Equalf(t, 2, resultData.TransactionsAnalysed, "expected 2 transactions to have been analysed, instead there was %d", resultData.TransactionsAnalysed)
    63  	assert.Equalf(t, 2, resultData.TransactionsPassed, "expected 2 transactions to have passed, instead there was %d", resultData.TransactionsPassed)
    64  	assert.Equalf(t, 0, resultData.TransactionsFailed, "expected 0 transactions to have failed, instead there was %d", resultData.TransactionsFailed)
    65  }
    66  
    67  func TestCheckTransactionsInDirectoryThrowsErrIfFileContainsInvalidBase64Data(t *testing.T) {
    68  	testDir := t.TempDir()
    69  	defer clearTestData(t, testDir)
    70  	createTestDataFile(t, testDir, "transaction1.txt", "12345")
    71  
    72  	resultData, err := CheckTransactionsInDirectory(testDir)
    73  	assert.Errorf(t, err, "expected to exit CheckTransactionsInDirectory with an err when one of the files has invalid data, no error was thrown")
    74  	assert.Equalf(t, 0, resultData.TransactionsAnalysed, "expected 0 transactions to have been analysed, instead there was %d", resultData.TransactionsAnalysed)
    75  	assert.Equalf(t, 0, resultData.TransactionsPassed, "expected 0 transactions to have passed, instead there was %d", resultData.TransactionsPassed)
    76  	assert.Equalf(t, 0, resultData.TransactionsFailed, "expected 0 transactions to have failed, instead there was %d", resultData.TransactionsFailed)
    77  }
    78  
    79  func TestCheckTransactionsInDirectoryAccuratelyReportsFailures(t *testing.T) {
    80  	testDir := t.TempDir()
    81  	defer clearTestData(t, testDir)
    82  	encodedTransaction, err := CreatedEncodedTransactionData()
    83  	assert.NoErrorf(t, err, "error occurred when attempting to create encoded test data")
    84  
    85  	transactionForFailScenario, err := CreateTransaction()
    86  	assert.NoErrorf(t, err, "error occurred when attempting to create encoded test data")
    87  
    88  	marshaller := jsonpb.Marshaler{}
    89  	failScenarioJson, err := marshaller.MarshalToString(transactionForFailScenario)
    90  	assert.NoErrorf(t, err, "error occurred when attempting to marshal transaction json to string. Err: %v", err)
    91  	failScenarioNonProtoEncode := base64.StdEncoding.EncodeToString([]byte(failScenarioJson))
    92  
    93  	createTestDataFile(t, testDir, "transaction1.txt", encodedTransaction)
    94  	createTestDataFile(t, testDir, "transaction2.txt", failScenarioNonProtoEncode)
    95  
    96  	resultData, err := CheckTransactionsInDirectory(testDir)
    97  	assert.NoErrorf(t, err, "expected no error from CheckTransactionsInDirectory when analysing valid base64 encoded data. Err: %v", err)
    98  	assert.Equalf(t, 2, resultData.TransactionsAnalysed, "expected 2 transactions to have been analysed, instead there was %d", resultData.TransactionsAnalysed)
    99  	assert.Equalf(t, 1, resultData.TransactionsPassed, "expected 1 transactions to have passed, instead there was %d", resultData.TransactionsPassed)
   100  	assert.Equalf(t, 1, resultData.TransactionsFailed, "expected 1 transactions to have failed, instead there was %d", resultData.TransactionsFailed)
   101  }
   102  
   103  func TestMarshalAndEncodeTransaction(t *testing.T) {
   104  	encoded, err := marshalAndEncodeTransaction(&v1.Transaction{})
   105  	assert.NoError(t, err)
   106  	assertIsBase64Encoded(t, encoded)
   107  }
   108  
   109  func TestDecodeAndUnmarshalTransactionUnmarshalsEncodedData(t *testing.T) {
   110  	encodedTransaction, err := CreatedEncodedTransactionData()
   111  	assert.NoError(t, err)
   112  
   113  	unmarshalled, err := decodeAndUnmarshalTransaction(encodedTransaction)
   114  	assert.NoError(t, err)
   115  	assert.Equalf(t, v1.TxVersion(txVersion), unmarshalled.Version, "expected version to be set in the unmarshalled data")
   116  	assert.Equalf(t, TestAlgoName, unmarshalled.Signature.Algo, "algo to be set in the unmarshalled data")
   117  }
   118  
   119  func TestDecodeAndUnmarshalTransactionThrowsErrorWithInvalidData(t *testing.T) {
   120  	_, err := decodeAndUnmarshalTransaction("invalid")
   121  	assert.Error(t, err)
   122  }
   123  
   124  func assertIsBase64Encoded(t *testing.T, encodedStr string) {
   125  	t.Helper()
   126  	_, err := base64.StdEncoding.DecodeString(encodedStr)
   127  	assert.NoError(t, err, "Expected the string to be base64 encoded")
   128  }