github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_unfreeze_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 TestUnitTokenUnfreezeTransactionValidate(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 accountID, err := AccountIDFromString("0.0.123-esxsf") 46 require.NoError(t, err) 47 tokenID, err := TokenIDFromString("0.0.123-esxsf") 48 require.NoError(t, err) 49 50 tokenUnfreeze := NewTokenUnfreezeTransaction(). 51 SetTokenID(tokenID). 52 SetAccountID(accountID) 53 54 err = tokenUnfreeze.validateNetworkOnIDs(client) 55 require.NoError(t, err) 56 } 57 58 func TestUnitTokenUnfreezeTransactionValidateWrong(t *testing.T) { 59 t.Parallel() 60 61 client, err := _NewMockClient() 62 client.SetLedgerID(*NewLedgerIDTestnet()) 63 require.NoError(t, err) 64 client.SetAutoValidateChecksums(true) 65 accountID, err := AccountIDFromString("0.0.123-rmkykd") 66 require.NoError(t, err) 67 tokenID, err := TokenIDFromString("0.0.123-rmkykd") 68 require.NoError(t, err) 69 70 tokenUnfreeze := NewTokenUnfreezeTransaction(). 71 SetTokenID(tokenID). 72 SetAccountID(accountID) 73 74 err = tokenUnfreeze.validateNetworkOnIDs(client) 75 assert.Error(t, err) 76 if err != nil { 77 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 78 } 79 } 80 81 func TestUnitTokenUnfreezeTransactionGet(t *testing.T) { 82 t.Parallel() 83 84 tokenID := TokenID{Token: 7} 85 86 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 87 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 88 89 transaction, err := NewTokenUnfreezeTransaction(). 90 SetTransactionID(transactionID). 91 SetNodeAccountIDs(nodeAccountID). 92 SetTokenID(tokenID). 93 SetAccountID(AccountID{Account: 7}). 94 SetMaxTransactionFee(NewHbar(10)). 95 SetTransactionMemo(""). 96 SetTransactionValidDuration(60 * time.Second). 97 SetRegenerateTransactionID(false). 98 Freeze() 99 require.NoError(t, err) 100 101 transaction.GetTransactionID() 102 transaction.GetNodeAccountIDs() 103 104 _, err = transaction.GetTransactionHash() 105 require.NoError(t, err) 106 107 transaction.GetTokenID() 108 transaction.GetAccountID() 109 transaction.GetMaxTransactionFee() 110 transaction.GetTransactionMemo() 111 transaction.GetRegenerateTransactionID() 112 _, err = transaction.GetSignatures() 113 require.NoError(t, err) 114 transaction.GetRegenerateTransactionID() 115 transaction.GetMaxTransactionFee() 116 transaction.GetRegenerateTransactionID() 117 } 118 119 func TestUnitTokenUnfreezeTransactionNothingSet(t *testing.T) { 120 t.Parallel() 121 122 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 123 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 124 125 transaction, err := NewTokenUnfreezeTransaction(). 126 SetTransactionID(transactionID). 127 SetNodeAccountIDs(nodeAccountID). 128 Freeze() 129 require.NoError(t, err) 130 131 transaction.GetTransactionID() 132 transaction.GetNodeAccountIDs() 133 134 _, err = transaction.GetTransactionHash() 135 require.NoError(t, err) 136 137 transaction.GetTokenID() 138 transaction.GetAccountID() 139 transaction.GetMaxTransactionFee() 140 transaction.GetTransactionMemo() 141 transaction.GetRegenerateTransactionID() 142 _, err = transaction.GetSignatures() 143 require.NoError(t, err) 144 transaction.GetRegenerateTransactionID() 145 transaction.GetMaxTransactionFee() 146 transaction.GetRegenerateTransactionID() 147 } 148 149 func TestUnitTokenUnfreezeTransactionCoverage(t *testing.T) { 150 t.Parallel() 151 152 checksum := "dmqui" 153 grpc := time.Second * 30 154 token := TokenID{Token: 3, checksum: &checksum} 155 account := AccountID{Account: 3, checksum: &checksum} 156 nodeAccountID := []AccountID{{Account: 10}} 157 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 158 159 newKey, err := PrivateKeyGenerateEd25519() 160 require.NoError(t, err) 161 162 client, err := _NewMockClient() 163 client.SetLedgerID(*NewLedgerIDTestnet()) 164 require.NoError(t, err) 165 client.SetAutoValidateChecksums(true) 166 167 transaction, err := NewTokenUnfreezeTransaction(). 168 SetTransactionID(transactionID). 169 SetNodeAccountIDs(nodeAccountID). 170 SetTokenID(token). 171 SetAccountID(account). 172 SetGrpcDeadline(&grpc). 173 SetMaxTransactionFee(NewHbar(3)). 174 SetMaxRetry(3). 175 SetMaxBackoff(time.Second * 30). 176 SetMinBackoff(time.Second * 10). 177 SetTransactionMemo("no"). 178 SetTransactionValidDuration(time.Second * 30). 179 SetRegenerateTransactionID(false). 180 Freeze() 181 require.NoError(t, err) 182 183 err = transaction.validateNetworkOnIDs(client) 184 require.NoError(t, err) 185 _, err = transaction.Schedule() 186 require.NoError(t, err) 187 transaction.GetTransactionID() 188 transaction.GetNodeAccountIDs() 189 transaction.GetMaxRetry() 190 transaction.GetMaxTransactionFee() 191 transaction.GetMaxBackoff() 192 transaction.GetMinBackoff() 193 transaction.GetRegenerateTransactionID() 194 byt, err := transaction.ToBytes() 195 require.NoError(t, err) 196 txFromBytes, err := TransactionFromBytes(byt) 197 require.NoError(t, err) 198 sig, err := newKey.SignTransaction(&transaction.Transaction) 199 require.NoError(t, err) 200 201 _, err = transaction.GetTransactionHash() 202 require.NoError(t, err) 203 transaction.GetMaxTransactionFee() 204 transaction.GetTransactionMemo() 205 transaction.GetRegenerateTransactionID() 206 transaction.GetTokenID() 207 transaction.GetAccountID() 208 _, err = transaction.GetSignatures() 209 require.NoError(t, err) 210 transaction.getName() 211 switch b := txFromBytes.(type) { 212 case TokenUnfreezeTransaction: 213 b.AddSignature(newKey.PublicKey(), sig) 214 } 215 } 216 217 func TestUnitTokenUnfreezeTransactionMock(t *testing.T) { 218 t.Parallel() 219 220 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98") 221 require.NoError(t, err) 222 223 call := func(request *services.Transaction) *services.TransactionResponse { 224 require.NotEmpty(t, request.SignedTransactionBytes) 225 signedTransaction := services.SignedTransaction{} 226 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 227 228 require.NotEmpty(t, signedTransaction.BodyBytes) 229 transactionBody := services.TransactionBody{} 230 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 231 232 require.NotNil(t, transactionBody.TransactionID) 233 transactionId := transactionBody.TransactionID.String() 234 require.NotEqual(t, "", transactionId) 235 236 sigMap := signedTransaction.GetSigMap() 237 require.NotNil(t, sigMap) 238 239 return &services.TransactionResponse{ 240 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 241 } 242 } 243 responses := [][]interface{}{{ 244 call, 245 }} 246 247 client, server := NewMockClientAndServer(responses) 248 defer server.Close() 249 250 checksum := "dmqui" 251 token := TokenID{Token: 3, checksum: &checksum} 252 account := AccountID{Account: 3, checksum: &checksum} 253 254 freez, err := NewTokenUnfreezeTransaction(). 255 SetNodeAccountIDs([]AccountID{{Account: 3}}). 256 SetTokenID(token). 257 SetAccountID(account). 258 FreezeWith(client) 259 require.NoError(t, err) 260 261 _, err = freez.Sign(newKey).Execute(client) 262 require.NoError(t, err) 263 }