github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_info_query_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  
    32  	"github.com/stretchr/testify/assert"
    33  
    34  	"github.com/stretchr/testify/require"
    35  )
    36  
    37  func TestUnitFileInfoQueryValidate(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	client, err := _NewMockClient()
    41  	client.SetLedgerID(*NewLedgerIDTestnet())
    42  	require.NoError(t, err)
    43  	client.SetAutoValidateChecksums(true)
    44  	fileID, err := FileIDFromString("0.0.123-esxsf")
    45  	require.NoError(t, err)
    46  
    47  	fileInfo := NewFileInfoQuery().
    48  		SetFileID(fileID)
    49  
    50  	err = fileInfo.validateNetworkOnIDs(client)
    51  	require.NoError(t, err)
    52  }
    53  
    54  func TestUnitFileInfoQueryValidateWrong(t *testing.T) {
    55  	t.Parallel()
    56  
    57  	client, err := _NewMockClient()
    58  	client.SetLedgerID(*NewLedgerIDTestnet())
    59  	require.NoError(t, err)
    60  	client.SetAutoValidateChecksums(true)
    61  	fileID, err := FileIDFromString("0.0.123-rmkykd")
    62  	require.NoError(t, err)
    63  
    64  	fileInfo := NewFileInfoQuery().
    65  		SetFileID(fileID)
    66  
    67  	err = fileInfo.validateNetworkOnIDs(client)
    68  	assert.Error(t, err)
    69  	if err != nil {
    70  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    71  	}
    72  }
    73  
    74  func TestUnitFileInfoQueryMock(t *testing.T) {
    75  	t.Parallel()
    76  
    77  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
    78  	require.NoError(t, err)
    79  	key := newKey.PublicKey().BytesRaw()
    80  
    81  	responses := [][]interface{}{{
    82  		&services.Response{
    83  			Response: &services.Response_FileGetInfo{
    84  				FileGetInfo: &services.FileGetInfoResponse{
    85  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
    86  				},
    87  			},
    88  		},
    89  		&services.Response{
    90  			Response: &services.Response_FileGetInfo{
    91  				FileGetInfo: &services.FileGetInfoResponse{
    92  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_COST_ANSWER, Cost: 2},
    93  				},
    94  			},
    95  		},
    96  		&services.Response{
    97  			Response: &services.Response_FileGetInfo{
    98  				FileGetInfo: &services.FileGetInfoResponse{
    99  					Header: &services.ResponseHeader{NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, ResponseType: services.ResponseType_ANSWER_ONLY, Cost: 2},
   100  					FileInfo: &services.FileGetInfoResponse_FileInfo{
   101  						FileID:         &services.FileID{FileNum: 3},
   102  						Size:           10,
   103  						ExpirationTime: nil,
   104  						Deleted:        false,
   105  						Keys: &services.KeyList{
   106  							Keys: []*services.Key{
   107  								{
   108  									Key: &services.Key_Ed25519{
   109  										Ed25519: key,
   110  									},
   111  								},
   112  							},
   113  						},
   114  						Memo:     "no memo",
   115  						LedgerId: []byte{0},
   116  					},
   117  				},
   118  			},
   119  		},
   120  	}}
   121  
   122  	client, server := NewMockClientAndServer(responses)
   123  	defer server.Close()
   124  
   125  	query := NewFileInfoQuery().
   126  		SetFileID(FileID{File: 3}).
   127  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   128  		SetMaxQueryPayment(NewHbar(1))
   129  
   130  	cost, err := query.GetCost(client)
   131  	require.NoError(t, err)
   132  	require.Equal(t, cost, HbarFromTinybar(2))
   133  
   134  	result, err := query.Execute(client)
   135  	require.NoError(t, err)
   136  
   137  	require.Equal(t, result.Keys.keys[0].String(), newKey.PublicKey().String())
   138  	require.Equal(t, result.FileMemo, "no memo")
   139  	require.Equal(t, result.IsDeleted, false)
   140  	require.True(t, result.LedgerID.IsMainnet())
   141  }
   142  
   143  func TestUnitFileInfoQueryGet(t *testing.T) {
   144  	t.Parallel()
   145  
   146  	checksum := "dmqui"
   147  	fileID := FileID{File: 3, checksum: &checksum}
   148  	deadline := time.Duration(time.Minute)
   149  	accountId := AccountID{Account: 123}
   150  	transactionID := TransactionIDGenerate(accountId)
   151  	query := NewFileInfoQuery().
   152  		SetFileID(fileID).
   153  		SetQueryPayment(NewHbar(2)).
   154  		SetMaxQueryPayment(NewHbar(1)).
   155  		SetQueryPayment(HbarFromTinybar(25)).
   156  		SetNodeAccountIDs([]AccountID{{Account: 10}, {Account: 11}, {Account: 12}}).
   157  		SetGrpcDeadline(&deadline).
   158  		SetMaxBackoff(1 * time.Minute).
   159  		SetMinBackoff(500 * time.Millisecond).
   160  		SetMaxRetry(5).
   161  		SetPaymentTransactionID(transactionID).
   162  		SetMaxQueryPayment(NewHbar(500))
   163  	client, err := _NewMockClient()
   164  	client.SetLedgerID(*NewLedgerIDTestnet())
   165  	require.NoError(t, err)
   166  	client.SetAutoValidateChecksums(true)
   167  	err = query.validateNetworkOnIDs(client)
   168  	require.NoError(t, err)
   169  	require.Equal(t, fileID, query.GetFileID())
   170  	require.Equal(t, []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}, query.GetNodeAccountIDs())
   171  	require.Equal(t, 500*time.Millisecond, query.GetMinBackoff())
   172  	require.Equal(t, 1*time.Minute, query.GetMaxBackoff())
   173  	require.Equal(t, 5, query.GetMaxRetryCount())
   174  	require.Equal(t, transactionID, query.GetPaymentTransactionID())
   175  	require.Equal(t, HbarFromTinybar(25), query.GetQueryPayment())
   176  	require.Equal(t, NewHbar(500), query.GetMaxQueryPayment())
   177  	require.Equal(t, &deadline, query.GetGrpcDeadline())
   178  }
   179  
   180  func TestUnitFileInfoQuerySetNothing(t *testing.T) {
   181  	t.Parallel()
   182  
   183  	balance := NewFileInfoQuery()
   184  
   185  	require.Equal(t, FileID{}, balance.GetFileID())
   186  	require.Equal(t, []AccountID{}, balance.GetNodeAccountIDs())
   187  	require.Equal(t, 250*time.Millisecond, balance.GetMinBackoff())
   188  	require.Equal(t, 8*time.Second, balance.GetMaxBackoff())
   189  	require.Equal(t, 10, balance.GetMaxRetryCount())
   190  	require.Equal(t, TransactionID{}, balance.GetPaymentTransactionID())
   191  	require.Equal(t, Hbar{}, balance.GetQueryPayment())
   192  	require.Equal(t, Hbar{}, balance.GetMaxQueryPayment())
   193  }