github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/file_update_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 "bytes" 28 "testing" 29 "time" 30 31 "github.com/hashgraph/hedera-protobufs-go/services" 32 protobuf "google.golang.org/protobuf/proto" 33 "google.golang.org/protobuf/types/known/wrapperspb" 34 35 "github.com/stretchr/testify/assert" 36 37 "github.com/stretchr/testify/require" 38 ) 39 40 func TestUnitFileUpdateTransactionValidate(t *testing.T) { 41 t.Parallel() 42 43 client, err := _NewMockClient() 44 client.SetLedgerID(*NewLedgerIDTestnet()) 45 require.NoError(t, err) 46 client.SetAutoValidateChecksums(true) 47 fileID, err := FileIDFromString("0.0.123-esxsf") 48 require.NoError(t, err) 49 50 fileUpdate := NewFileUpdateTransaction(). 51 SetFileID(fileID) 52 53 err = fileUpdate.validateNetworkOnIDs(client) 54 require.NoError(t, err) 55 } 56 57 func TestUnitFileUpdateTransactionValidateWrong(t *testing.T) { 58 t.Parallel() 59 60 client, err := _NewMockClient() 61 client.SetLedgerID(*NewLedgerIDTestnet()) 62 require.NoError(t, err) 63 client.SetAutoValidateChecksums(true) 64 fileID, err := FileIDFromString("0.0.123-rmkykd") 65 require.NoError(t, err) 66 67 fileUpdate := NewFileUpdateTransaction(). 68 SetFileID(fileID) 69 70 err = fileUpdate.validateNetworkOnIDs(client) 71 assert.Error(t, err) 72 if err != nil { 73 assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error()) 74 } 75 } 76 77 func TestUnitFileUpdateTransactionMock(t *testing.T) { 78 t.Parallel() 79 80 newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98") 81 require.NoError(t, err) 82 83 call := func(request *services.Transaction) *services.TransactionResponse { 84 require.NotEmpty(t, request.SignedTransactionBytes) 85 signedTransaction := services.SignedTransaction{} 86 _ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction) 87 88 require.NotEmpty(t, signedTransaction.BodyBytes) 89 transactionBody := services.TransactionBody{} 90 _ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody) 91 92 require.NotNil(t, transactionBody.TransactionID) 93 transactionId := transactionBody.TransactionID.String() 94 require.NotEqual(t, "", transactionId) 95 96 sigMap := signedTransaction.GetSigMap() 97 require.NotNil(t, sigMap) 98 99 key, _ := PrivateKeyFromStringEd25519("302e020100300506032b657004220420d45e1557156908c967804615af59a000be88c7aa7058bfcbe0f46b16c28f887d") 100 for _, sigPair := range sigMap.SigPair { 101 verified := false 102 103 switch k := sigPair.Signature.(type) { 104 case *services.SignaturePair_Ed25519: 105 pbTemp, _ := PublicKeyFromBytesEd25519(sigPair.PubKeyPrefix) 106 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.Ed25519) 107 case *services.SignaturePair_ECDSASecp256K1: 108 pbTemp, _ := PublicKeyFromBytesECDSA(sigPair.PubKeyPrefix) 109 verified = pbTemp.Verify(signedTransaction.BodyBytes, k.ECDSASecp256K1) 110 } 111 require.True(t, verified) 112 } 113 114 require.Equal(t, bytes.Compare(sigMap.SigPair[1].PubKeyPrefix, key.PublicKey().BytesRaw()), 0) 115 require.Equal(t, bytes.Compare(sigMap.SigPair[0].PubKeyPrefix, newKey.PublicKey().BytesRaw()), 0) 116 117 if bod, ok := transactionBody.Data.(*services.TransactionBody_FileUpdate); ok { 118 require.Equal(t, bod.FileUpdate.FileID.FileNum, int64(3)) 119 require.Equal(t, bytes.Compare(bod.FileUpdate.Contents, []byte{123}), 0) 120 require.Equal(t, bod.FileUpdate.Memo, &wrapperspb.StringValue{Value: "no memo"}) 121 } 122 123 return &services.TransactionResponse{ 124 NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK, 125 } 126 } 127 responses := [][]interface{}{{ 128 call, 129 }} 130 131 client, server := NewMockClientAndServer(responses) 132 defer server.Close() 133 134 freez, err := NewFileUpdateTransaction(). 135 SetFileID(FileID{File: 3}). 136 SetNodeAccountIDs([]AccountID{{Account: 3}}). 137 SetFileMemo("no memo"). 138 SetKeys(newKey). 139 SetContents([]byte{123}). 140 FreezeWith(client) 141 require.NoError(t, err) 142 143 _, err = freez.Sign(newKey).Execute(client) 144 require.NoError(t, err) 145 } 146 147 func TestUnitFileUpdateTransactionGet(t *testing.T) { 148 t.Parallel() 149 150 fileID := FileID{File: 7} 151 152 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 153 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 154 155 newKey, err := PrivateKeyGenerateEd25519() 156 157 transaction, err := NewFileUpdateTransaction(). 158 SetTransactionID(transactionID). 159 SetNodeAccountIDs(nodeAccountID). 160 SetKeys(newKey). 161 SetContents([]byte{}). 162 SetFileID(fileID). 163 SetFileMemo(""). 164 SetMaxTransactionFee(NewHbar(10)). 165 SetTransactionMemo(""). 166 SetTransactionValidDuration(60 * time.Second). 167 SetRegenerateTransactionID(false). 168 Freeze() 169 require.NoError(t, err) 170 171 transaction.GetTransactionID() 172 transaction.GetNodeAccountIDs() 173 174 _, err = transaction.GetTransactionHash() 175 require.NoError(t, err) 176 177 transaction.GetKeys() 178 transaction.GetMaxTransactionFee() 179 transaction.GetTransactionMemo() 180 transaction.GetRegenerateTransactionID() 181 _, err = transaction.GetSignatures() 182 require.NoError(t, err) 183 transaction.GetRegenerateTransactionID() 184 transaction.GetMaxTransactionFee() 185 transaction.GetContents() 186 transaction.GetRegenerateTransactionID() 187 transaction.GetFileID() 188 transaction.GetFileMemo() 189 } 190 191 func TestUnitFileUpdateTransactionSetNothing(t *testing.T) { 192 t.Parallel() 193 194 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 195 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 196 197 transaction, err := NewFileUpdateTransaction(). 198 SetTransactionID(transactionID). 199 SetNodeAccountIDs(nodeAccountID). 200 Freeze() 201 require.NoError(t, err) 202 203 transaction.GetTransactionID() 204 transaction.GetNodeAccountIDs() 205 206 _, err = transaction.GetTransactionHash() 207 require.NoError(t, err) 208 209 transaction.GetKeys() 210 transaction.GetMaxTransactionFee() 211 transaction.GetTransactionMemo() 212 transaction.GetRegenerateTransactionID() 213 _, err = transaction.GetSignatures() 214 require.NoError(t, err) 215 transaction.GetRegenerateTransactionID() 216 transaction.GetMaxTransactionFee() 217 transaction.GetContents() 218 transaction.GetRegenerateTransactionID() 219 transaction.GetFileID() 220 transaction.GetFileMemo() 221 } 222 223 func TestUnitFileUpdateTransactionProtoCheck(t *testing.T) { 224 t.Parallel() 225 226 fileID := FileID{File: 7} 227 nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}} 228 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 229 230 newKey, err := PrivateKeyGenerateEd25519() 231 require.NoError(t, err) 232 233 transaction, err := NewFileUpdateTransaction(). 234 SetTransactionID(transactionID). 235 SetNodeAccountIDs(nodeAccountID). 236 SetKeys(newKey). 237 SetContents([]byte{5, 6}). 238 SetFileID(fileID). 239 SetFileMemo(""). 240 SetMaxTransactionFee(NewHbar(10)). 241 SetTransactionMemo(""). 242 SetTransactionValidDuration(60 * time.Second). 243 SetRegenerateTransactionID(false). 244 Freeze() 245 require.NoError(t, err) 246 247 transaction.GetTransactionID() 248 transaction.GetNodeAccountIDs() 249 250 proto := transaction.build().GetFileUpdate() 251 require.Equal(t, proto.Keys.Keys[0].String(), newKey._ToProtoKey().String()) 252 require.Equal(t, proto.Contents, []byte{5, 6}) 253 require.Equal(t, proto.FileID.String(), fileID._ToProtobuf().String()) 254 } 255 256 func TestUnitFileUpdateTransactionCoverage(t *testing.T) { 257 t.Parallel() 258 259 checksum := "dmqui" 260 grpc := time.Second * 30 261 file := FileID{File: 3, checksum: &checksum} 262 nodeAccountID := []AccountID{{Account: 10}} 263 transactionID := TransactionIDGenerate(AccountID{Account: 324}) 264 265 newKey, err := PrivateKeyGenerateEd25519() 266 require.NoError(t, err) 267 268 client, err := _NewMockClient() 269 client.SetLedgerID(*NewLedgerIDTestnet()) 270 require.NoError(t, err) 271 client.SetAutoValidateChecksums(true) 272 273 transaction, err := NewFileUpdateTransaction(). 274 SetTransactionID(transactionID). 275 SetNodeAccountIDs(nodeAccountID). 276 SetKeys(newKey). 277 SetFileMemo("yes"). 278 SetExpirationTime(time.Unix(23, 32)). 279 SetContents([]byte{0}). 280 SetFileID(file). 281 SetGrpcDeadline(&grpc). 282 SetMaxTransactionFee(NewHbar(3)). 283 SetMaxRetry(3). 284 SetMaxBackoff(time.Second * 30). 285 SetMinBackoff(time.Second * 10). 286 SetTransactionMemo("no"). 287 SetTransactionValidDuration(time.Second * 30). 288 SetRegenerateTransactionID(false). 289 Freeze() 290 require.NoError(t, err) 291 292 transaction.validateNetworkOnIDs(client) 293 294 _, err = transaction.Schedule() 295 require.NoError(t, err) 296 transaction.GetTransactionID() 297 transaction.GetNodeAccountIDs() 298 transaction.GetMaxRetry() 299 transaction.GetMaxTransactionFee() 300 transaction.GetMaxBackoff() 301 transaction.GetMinBackoff() 302 transaction.GetRegenerateTransactionID() 303 byt, err := transaction.ToBytes() 304 require.NoError(t, err) 305 txFromBytes, err := TransactionFromBytes(byt) 306 require.NoError(t, err) 307 sig, err := newKey.SignTransaction(&transaction.Transaction) 308 require.NoError(t, err) 309 310 _, err = transaction.GetTransactionHash() 311 require.NoError(t, err) 312 transaction.GetMaxTransactionFee() 313 transaction.GetTransactionMemo() 314 transaction.GetRegenerateTransactionID() 315 transaction.GetKeys() 316 transaction.GetFileMemo() 317 transaction.GetExpirationTime() 318 transaction.GetContents() 319 transaction.GetExpirationTime() 320 transaction.GetFileID() 321 _, err = transaction.GetSignatures() 322 require.NoError(t, err) 323 transaction.getName() 324 switch b := txFromBytes.(type) { 325 case FileUpdateTransaction: 326 b.AddSignature(newKey.PublicKey(), sig) 327 } 328 }