github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_delete_transaction_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/hashgraph/hedera-protobufs-go/services"
    31  	protobuf "google.golang.org/protobuf/proto"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func TestUnitFileDeleteTransactionValidate(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	client, err := _NewMockClient()
    42  	client.SetLedgerID(*NewLedgerIDTestnet())
    43  	require.NoError(t, err)
    44  	client.SetAutoValidateChecksums(true)
    45  	fileID, err := FileIDFromString("0.0.123-esxsf")
    46  	require.NoError(t, err)
    47  
    48  	fileDelete := NewFileDeleteTransaction().
    49  		SetFileID(fileID)
    50  
    51  	err = fileDelete.validateNetworkOnIDs(client)
    52  	require.NoError(t, err)
    53  }
    54  
    55  func TestUnitFileDeleteTransactionValidateWrong(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	client, err := _NewMockClient()
    59  	client.SetLedgerID(*NewLedgerIDTestnet())
    60  	require.NoError(t, err)
    61  	client.SetAutoValidateChecksums(true)
    62  	fileID, err := FileIDFromString("0.0.123-rmkykd")
    63  	require.NoError(t, err)
    64  
    65  	fileDelete := NewFileDeleteTransaction().
    66  		SetFileID(fileID)
    67  
    68  	err = fileDelete.validateNetworkOnIDs(client)
    69  	assert.Error(t, err)
    70  	if err != nil {
    71  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    72  	}
    73  }
    74  
    75  func TestUnitFileDeleteTransactionMock(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	call := func(request *services.Transaction) *services.TransactionResponse {
    79  		require.NotEmpty(t, request.SignedTransactionBytes)
    80  		signedTransaction := services.SignedTransaction{}
    81  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
    82  
    83  		require.NotEmpty(t, signedTransaction.BodyBytes)
    84  		transactionBody := services.TransactionBody{}
    85  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
    86  
    87  		require.NotNil(t, transactionBody.TransactionID)
    88  		transactionId := transactionBody.TransactionID.String()
    89  		require.NotEqual(t, "", transactionId)
    90  
    91  		sigMap := signedTransaction.GetSigMap()
    92  		require.NotNil(t, sigMap)
    93  
    94  		for _, sigPair := range sigMap.SigPair {
    95  			verified := false
    96  
    97  			switch k := sigPair.Signature.(type) {
    98  			case *services.SignaturePair_Ed25519:
    99  				pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix)
   100  				verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519)
   101  				key, _ := PrivateKeyFromStringEd25519("302e020100300506032b657004220420d45e1557156908c967804615af59a000be88c7aa7058bfcbe0f46b16c28f887d")
   102  				require.Equal(t, key.PublicKey().String(), pbTemp.String())
   103  			case *services.SignaturePair_ECDSASecp256K1:
   104  				pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix)
   105  				verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1)
   106  			}
   107  			require.True(t, verified)
   108  		}
   109  
   110  		if bod, ok := transactionBody.Data.(*services.TransactionBody_FileDelete); ok {
   111  			require.Equal(t, bod.FileDelete.FileID.FileNum, int64(3))
   112  		}
   113  
   114  		return &services.TransactionResponse{
   115  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   116  		}
   117  	}
   118  	responses := [][]interface{}{{
   119  		call,
   120  	}}
   121  
   122  	client, server := NewMockClientAndServer(responses)
   123  	defer server.Close()
   124  
   125  	_, err := NewFileDeleteTransaction().
   126  		SetFileID(FileID{File: 3}).
   127  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   128  		Execute(client)
   129  	require.NoError(t, err)
   130  }
   131  
   132  func TestUnitFileDeleteTransactionGet(t *testing.T) {
   133  	t.Parallel()
   134  
   135  	fileID := FileID{File: 7}
   136  
   137  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   138  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   139  
   140  	transaction, err := NewFileDeleteTransaction().
   141  		SetTransactionID(transactionID).
   142  		SetNodeAccountIDs(nodeAccountID).
   143  		SetFileID(fileID).
   144  		SetMaxTransactionFee(NewHbar(10)).
   145  		SetTransactionMemo("").
   146  		SetTransactionValidDuration(60 * time.Second).
   147  		SetRegenerateTransactionID(false).
   148  		Freeze()
   149  	require.NoError(t, err)
   150  
   151  	transaction.GetTransactionID()
   152  	transaction.GetNodeAccountIDs()
   153  
   154  	_, err = transaction.GetTransactionHash()
   155  	require.NoError(t, err)
   156  
   157  	transaction.GetFileID()
   158  	transaction.GetMaxTransactionFee()
   159  	transaction.GetTransactionMemo()
   160  	transaction.GetRegenerateTransactionID()
   161  	_, err = transaction.GetSignatures()
   162  	require.NoError(t, err)
   163  	transaction.GetRegenerateTransactionID()
   164  	transaction.GetMaxTransactionFee()
   165  	transaction.GetRegenerateTransactionID()
   166  }
   167  
   168  func TestUnitFileDeleteTransactionNothingSet(t *testing.T) {
   169  	t.Parallel()
   170  
   171  	fileID := FileID{File: 7}
   172  
   173  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   174  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   175  
   176  	transaction, err := NewFileDeleteTransaction().
   177  		SetTransactionID(transactionID).
   178  		SetNodeAccountIDs(nodeAccountID).
   179  		SetFileID(fileID).
   180  		SetMaxTransactionFee(NewHbar(10)).
   181  		SetTransactionMemo("").
   182  		SetTransactionValidDuration(60 * time.Second).
   183  		SetRegenerateTransactionID(false).
   184  		Freeze()
   185  	require.NoError(t, err)
   186  
   187  	transaction.GetTransactionID()
   188  	transaction.GetNodeAccountIDs()
   189  
   190  	_, err = transaction.GetTransactionHash()
   191  	require.NoError(t, err)
   192  
   193  	transaction.GetFileID()
   194  	transaction.GetMaxTransactionFee()
   195  	transaction.GetTransactionMemo()
   196  	transaction.GetRegenerateTransactionID()
   197  	_, err = transaction.GetSignatures()
   198  	require.NoError(t, err)
   199  	transaction.GetRegenerateTransactionID()
   200  	transaction.GetMaxTransactionFee()
   201  	transaction.GetRegenerateTransactionID()
   202  }
   203  
   204  func TestUnitFileDeleteTransactionCoverage(t *testing.T) {
   205  	t.Parallel()
   206  
   207  	checksum := "dmqui"
   208  	grpc := time.Second * 30
   209  	file := FileID{File: 3, checksum: &checksum}
   210  	nodeAccountID := []AccountID{{Account: 10}}
   211  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   212  
   213  	newKey, err := PrivateKeyGenerateEd25519()
   214  	require.NoError(t, err)
   215  
   216  	client, err := _NewMockClient()
   217  	client.SetLedgerID(*NewLedgerIDTestnet())
   218  	require.NoError(t, err)
   219  	client.SetAutoValidateChecksums(true)
   220  
   221  	transaction, err := NewFileDeleteTransaction().
   222  		SetTransactionID(transactionID).
   223  		SetNodeAccountIDs(nodeAccountID).
   224  		SetFileID(file).
   225  		SetGrpcDeadline(&grpc).
   226  		SetMaxTransactionFee(NewHbar(3)).
   227  		SetMaxRetry(3).
   228  		SetMaxBackoff(time.Second * 30).
   229  		SetMinBackoff(time.Second * 10).
   230  		SetTransactionMemo("no").
   231  		SetTransactionValidDuration(time.Second * 30).
   232  		SetRegenerateTransactionID(false).
   233  		Freeze()
   234  	require.NoError(t, err)
   235  
   236  	transaction.validateNetworkOnIDs(client)
   237  
   238  	_, err = transaction.Schedule()
   239  	require.NoError(t, err)
   240  	transaction.GetTransactionID()
   241  	transaction.GetNodeAccountIDs()
   242  	transaction.GetMaxRetry()
   243  	transaction.GetMaxTransactionFee()
   244  	transaction.GetMaxBackoff()
   245  	transaction.GetMinBackoff()
   246  	transaction.GetRegenerateTransactionID()
   247  	byt, err := transaction.ToBytes()
   248  	require.NoError(t, err)
   249  	txFromBytes, err := TransactionFromBytes(byt)
   250  	require.NoError(t, err)
   251  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   252  	require.NoError(t, err)
   253  
   254  	_, err = transaction.GetTransactionHash()
   255  	require.NoError(t, err)
   256  	transaction.GetMaxTransactionFee()
   257  	transaction.GetTransactionMemo()
   258  	transaction.GetRegenerateTransactionID()
   259  	transaction.GetFileID()
   260  	_, err = transaction.GetSignatures()
   261  	require.NoError(t, err)
   262  	transaction.getName()
   263  	switch b := txFromBytes.(type) {
   264  	case FileDeleteTransaction:
   265  		b.AddSignature(newKey.PublicKey(), sig)
   266  	}
   267  }