github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/client_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  	"bytes"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/rs/zerolog"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func TestUnitClientFromConfig(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	client, err := ClientFromConfig([]byte(testClientJSON))
    42  	require.NoError(t, err)
    43  
    44  	assert.NotNil(t, client)
    45  	assert.True(t, len(client.network.network) > 0)
    46  	assert.Nil(t, client.operator)
    47  }
    48  
    49  func TestUnitClientFromConfigWithOperator(t *testing.T) {
    50  	t.Parallel()
    51  
    52  	client, err := ClientFromConfig([]byte(testClientJSONWithOperator))
    53  	require.NoError(t, err)
    54  
    55  	assert.NotNil(t, client)
    56  
    57  	testOperatorKey, err := PrivateKeyFromString("302e020100300506032b657004220420db484b828e64b2d8f12ce3c0a0e93a0b8cce7af1bb8f39c97732394482538e10")
    58  	require.NoError(t, err)
    59  
    60  	assert.True(t, len(client.network.network) > 0)
    61  	assert.NotNil(t, client.operator)
    62  	assert.Equal(t, testOperatorKey.ed25519PrivateKey.keyData, client.operator.privateKey.ed25519PrivateKey.keyData)
    63  	assert.Equal(t, AccountID{Account: 3}.Account, client.operator.accountID.Account)
    64  }
    65  
    66  func TestUnitClientFromConfigWithoutMirrorNetwork(t *testing.T) {
    67  	t.Parallel()
    68  
    69  	client, err := ClientFromConfig([]byte(testClientJSONWithoutMirrorNetwork))
    70  	require.NoError(t, err)
    71  	assert.NotNil(t, client)
    72  
    73  	assert.True(t, len(client.network.network) > 0)
    74  	assert.True(t, len(client.GetMirrorNetwork()) == 0)
    75  }
    76  
    77  func TestUnitClientFromConfigWrongMirrorNetworkType(t *testing.T) {
    78  	t.Parallel()
    79  
    80  	_, err := ClientFromConfig([]byte(testClientJSONWrongTypeMirror))
    81  	assert.Error(t, err)
    82  	if err != nil {
    83  		assert.Equal(t, "mirrorNetwork is expected to be a string, an array of strings or nil", err.Error())
    84  	}
    85  }
    86  
    87  func TestUnitClientFromConfigWrongNetworkType(t *testing.T) {
    88  	t.Parallel()
    89  
    90  	_, err := ClientFromConfig([]byte(testClientJSONWrongTypeNetwork))
    91  	assert.Error(t, err)
    92  	if err != nil {
    93  		assert.Equal(t, "network is expected to be map of string to string, or string", err.Error())
    94  	}
    95  }
    96  
    97  func TestUnitClientFromConfigWrongAccountIDNetworkType(t *testing.T) {
    98  	_, err := ClientFromConfig([]byte(testClientJSONWrongAccountIDNetwork))
    99  	assert.Error(t, err)
   100  	if err != nil {
   101  		assert.Equal(t, "expected {shard}.{realm}.{num}", err.Error())
   102  	}
   103  }
   104  
   105  func TestUnitClientFromCorrectConfigFile(t *testing.T) {
   106  	t.Parallel()
   107  
   108  	client, err := ClientFromConfigFile("client-config-with-operator.json")
   109  	assert.NoError(t, err)
   110  	assert.NotNil(t, client)
   111  	assert.NotNil(t, client.operator)
   112  	assert.Equal(t, AccountID{Account: 3}.Account, client.operator.accountID.Account)
   113  	assert.Equal(t, "a608e2130a0a3cb34f86e757303c862bee353d9ab77ba4387ec084f881d420d4", client.operator.privateKey.StringRaw())
   114  }
   115  
   116  func TestUnitClientFromMissingConfigFile(t *testing.T) {
   117  	t.Parallel()
   118  
   119  	client, err := ClientFromConfigFile("missing.json")
   120  	assert.Error(t, err)
   121  	assert.Nil(t, client)
   122  }
   123  
   124  func TestUnitClientSetNetworkExtensive(t *testing.T) {
   125  	t.Parallel()
   126  
   127  	client, err := _NewMockClient()
   128  	require.NoError(t, err)
   129  	nodes := make(map[string]AccountID, 2)
   130  	nodes["0.testnet.hedera.com:50211"] = AccountID{0, 0, 3, nil, nil, nil}
   131  	nodes["1.testnet.hedera.com:50211"] = AccountID{0, 0, 4, nil, nil, nil}
   132  
   133  	err = client.SetNetwork(nodes)
   134  	require.NoError(t, err)
   135  	network := client.GetNetwork()
   136  	assert.Equal(t, 2, len(network))
   137  	assert.Equal(t, network["0.testnet.hedera.com:50211"], AccountID{0, 0, 3, nil, nil, nil})
   138  	assert.Equal(t, network["1.testnet.hedera.com:50211"], AccountID{0, 0, 4, nil, nil, nil})
   139  
   140  	nodes = make(map[string]AccountID, 2)
   141  	nodes["0.testnet.hedera.com:50211"] = AccountID{0, 0, 3, nil, nil, nil}
   142  	nodes["1.testnet.hedera.com:50211"] = AccountID{0, 0, 4, nil, nil, nil}
   143  	nodes["2.testnet.hedera.com:50211"] = AccountID{0, 0, 5, nil, nil, nil}
   144  
   145  	err = client.SetNetwork(nodes)
   146  	require.NoError(t, err)
   147  	network = client.GetNetwork()
   148  	assert.Equal(t, 3, len(network))
   149  	assert.Equal(t, network["0.testnet.hedera.com:50211"], AccountID{0, 0, 3, nil, nil, nil})
   150  	assert.Equal(t, network["1.testnet.hedera.com:50211"], AccountID{0, 0, 4, nil, nil, nil})
   151  	assert.Equal(t, network["2.testnet.hedera.com:50211"], AccountID{0, 0, 5, nil, nil, nil})
   152  
   153  	nodes = make(map[string]AccountID, 1)
   154  	nodes["2.testnet.hedera.com:50211"] = AccountID{0, 0, 5, nil, nil, nil}
   155  
   156  	err = client.SetNetwork(nodes)
   157  	require.NoError(t, err)
   158  	network = client.GetNetwork()
   159  	networkMirror := client.GetMirrorNetwork()
   160  	assert.Equal(t, 1, len(network))
   161  	assert.Equal(t, network["2.testnet.hedera.com:50211"], AccountID{0, 0, 5, nil, nil, nil})
   162  	// There is only one mirror address, no matter the transport security
   163  	assert.Equal(t, "nonexistent-mirror-testnet:443", networkMirror[0])
   164  
   165  	client.SetTransportSecurity(true)
   166  	client.SetCertificateVerification(true)
   167  	network = client.GetNetwork()
   168  	networkMirror = client.GetMirrorNetwork()
   169  	assert.Equal(t, network["2.testnet.hedera.com:50212"], AccountID{0, 0, 5, nil, nil, nil})
   170  	assert.Equal(t, "nonexistent-mirror-testnet:443", networkMirror[0])
   171  
   172  	err = client.Close()
   173  	require.NoError(t, err)
   174  }
   175  
   176  func TestUnitClientSetMirrorNetwork(t *testing.T) {
   177  	t.Parallel()
   178  
   179  	mirrorNetworkString := "testnet.mirrornode.hedera.com:443"
   180  	mirrorNetwork1String := "testnet1.mirrornode.hedera.com:443"
   181  	defaultNetwork := make([]string, 0)
   182  	defaultNetwork = append(defaultNetwork, mirrorNetworkString)
   183  	client, err := _NewMockClient()
   184  	require.NoError(t, err)
   185  	client.SetMirrorNetwork(defaultNetwork)
   186  
   187  	mirrorNetwork := client.GetMirrorNetwork()
   188  	assert.Equal(t, 1, len(mirrorNetwork))
   189  	assert.Equal(t, mirrorNetworkString, mirrorNetwork[0])
   190  
   191  	defaultNetworkWithExtraNode := make([]string, 0)
   192  	defaultNetworkWithExtraNode = append(defaultNetworkWithExtraNode, mirrorNetworkString)
   193  	defaultNetworkWithExtraNode = append(defaultNetworkWithExtraNode, mirrorNetwork1String)
   194  
   195  	client.SetMirrorNetwork(defaultNetworkWithExtraNode)
   196  	mirrorNetwork = client.GetMirrorNetwork()
   197  	assert.Equal(t, 2, len(mirrorNetwork))
   198  	require.True(t, contains(mirrorNetwork, mirrorNetworkString))
   199  	require.True(t, contains(mirrorNetwork, mirrorNetwork1String))
   200  
   201  	defaultNetwork = make([]string, 0)
   202  	defaultNetwork = append(defaultNetwork, mirrorNetwork1String)
   203  
   204  	client.SetMirrorNetwork(defaultNetwork)
   205  	mirrorNetwork = client.GetMirrorNetwork()
   206  	assert.Equal(t, 1, len(mirrorNetwork))
   207  	assert.Equal(t, mirrorNetwork1String, mirrorNetwork[0])
   208  
   209  	defaultNetwork = make([]string, 0)
   210  	defaultNetwork = append(defaultNetwork, mirrorNetworkString)
   211  
   212  	client.SetMirrorNetwork(defaultNetwork)
   213  	mirrorNetwork = client.GetMirrorNetwork()
   214  	assert.Equal(t, 1, len(mirrorNetwork))
   215  	assert.Equal(t, mirrorNetworkString, mirrorNetwork[0])
   216  
   217  	client.SetTransportSecurity(true)
   218  	mirrorNetwork = client.GetMirrorNetwork()
   219  	// SetTransportSecurity is deprecated, so the mirror node should not be updated
   220  	assert.Equal(t, mirrorNetworkString, mirrorNetwork[0])
   221  
   222  	err = client.Close()
   223  	require.NoError(t, err)
   224  }
   225  
   226  func contains(s []string, e string) bool {
   227  	for _, a := range s {
   228  		if a == e {
   229  			return true
   230  		}
   231  	}
   232  	return false
   233  }
   234  
   235  func TestUnitClientSetMultipleNetwork(t *testing.T) {
   236  	t.Parallel()
   237  
   238  	client, err := _NewMockClient()
   239  	client.SetLedgerID(*NewLedgerIDTestnet())
   240  	require.NoError(t, err)
   241  	nodes := make(map[string]AccountID, 8)
   242  	nodes["0.testnet.hedera.com:50211"] = AccountID{0, 0, 3, nil, nil, nil}
   243  	nodes["34.94.106.61:50211"] = AccountID{0, 0, 3, nil, nil, nil}
   244  	nodes["50.18.132.211:50211"] = AccountID{0, 0, 3, nil, nil, nil}
   245  	nodes["138.91.142.219:50211"] = AccountID{0, 0, 3, nil, nil, nil}
   246  
   247  	nodes["1.testnet.hedera.com:50211"] = AccountID{0, 0, 4, nil, nil, nil}
   248  	nodes["35.237.119.55:50211"] = AccountID{0, 0, 4, nil, nil, nil}
   249  	nodes["3.212.6.13:50211"] = AccountID{0, 0, 4, nil, nil, nil}
   250  	nodes["52.168.76.241:50211"] = AccountID{0, 0, 4, nil, nil, nil}
   251  
   252  	err = client.SetNetwork(nodes)
   253  	require.NoError(t, err)
   254  	net := client.GetNetwork()
   255  
   256  	if val, ok := net["0.testnet.hedera.com:50211"]; ok {
   257  		require.Equal(t, val.String(), "0.0.3")
   258  	}
   259  
   260  	if val, ok := net["1.testnet.hedera.com:50211"]; ok {
   261  		require.Equal(t, val.String(), "0.0.4")
   262  	}
   263  
   264  	if val, ok := net["50.18.132.211:50211"]; ok {
   265  		require.Equal(t, val.String(), "0.0.3")
   266  	}
   267  
   268  	if val, ok := net["3.212.6.13:50211"]; ok {
   269  		require.Equal(t, val.String(), "0.0.4")
   270  	}
   271  
   272  }
   273  
   274  func TestUnitClientLogger(t *testing.T) {
   275  	client := ClientForTestnet()
   276  
   277  	var buf bytes.Buffer
   278  	writer := zerolog.ConsoleWriter{Out: &buf, TimeFormat: time.RFC3339}
   279  
   280  	hederaLoger := NewLogger("test", LoggerLevelTrace)
   281  
   282  	l := zerolog.New(&writer)
   283  	hederaLoger.logger = &l
   284  
   285  	client.SetLogger(hederaLoger)
   286  	client.SetLogLevel(LoggerLevelInfo)
   287  
   288  	client.logger.Trace("trace message", "traceKey", "traceValue")
   289  	client.logger.Debug("debug message", "debugKey", "debugValue")
   290  	client.logger.Info("info message", "infoKey", "infoValue")
   291  	client.logger.Warn("warn message", "warnKey", "warnValue")
   292  	client.logger.Error("error message", "errorKey", "errorValue")
   293  
   294  	assert.NotContains(t, buf.String(), "trace message")
   295  	assert.NotContains(t, buf.String(), "debug message")
   296  	assert.Contains(t, buf.String(), "info message")
   297  	assert.Contains(t, buf.String(), "warn message")
   298  	assert.Contains(t, buf.String(), "error message")
   299  
   300  	buf.Reset()
   301  	client.SetLogLevel(LoggerLevelWarn)
   302  	client.logger.Trace("trace message", "traceKey", "traceValue")
   303  	client.logger.Debug("debug message", "debugKey", "debugValue")
   304  	client.logger.Info("info message", "infoKey", "infoValue")
   305  	client.logger.Warn("warn message", "warnKey", "warnValue")
   306  	client.logger.Error("error message", "errorKey", "errorValue")
   307  
   308  	assert.NotContains(t, buf.String(), "trace message")
   309  	assert.NotContains(t, buf.String(), "debug message")
   310  	assert.NotContains(t, buf.String(), "info message")
   311  	assert.Contains(t, buf.String(), "warn message")
   312  	assert.Contains(t, buf.String(), "error message")
   313  
   314  	buf.Reset()
   315  	client.SetLogLevel(LoggerLevelTrace)
   316  	client.logger.Trace("trace message", "traceKey", "traceValue")
   317  	client.logger.Debug("debug message", "debugKey", "debugValue")
   318  	client.logger.Info("info message", "infoKey", "infoValue")
   319  	client.logger.Warn("warn message", "warnKey", "warnValue")
   320  	client.logger.Error("error message", "errorKey", "errorValue")
   321  
   322  	assert.Contains(t, buf.String(), "trace message")
   323  	assert.Contains(t, buf.String(), "debug message")
   324  	assert.Contains(t, buf.String(), "info message")
   325  	assert.Contains(t, buf.String(), "warn message")
   326  	assert.Contains(t, buf.String(), "error message")
   327  
   328  	hl := client.GetLogger()
   329  	assert.Equal(t, hl, hederaLoger)
   330  }