github.com/turingchain2020/turingchain@v1.1.21/rpc/grpchandler_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rpc 6 7 import ( 8 "encoding/hex" 9 "fmt" 10 "testing" 11 12 "strings" 13 14 "github.com/turingchain2020/turingchain/client/mocks" 15 "github.com/turingchain2020/turingchain/common" 16 "github.com/turingchain2020/turingchain/types" 17 pb "github.com/turingchain2020/turingchain/types" 18 "github.com/golang/protobuf/proto" 19 "github.com/stretchr/testify/assert" 20 "github.com/stretchr/testify/mock" 21 "golang.org/x/net/context" 22 "google.golang.org/grpc/peer" 23 ) 24 25 var ( 26 g Grpc 27 qapi *mocks.QueueProtocolAPI 28 ) 29 30 // Addr is an autogenerated mock type for the Addr type 31 type Addr struct { 32 mock.Mock 33 } 34 35 // Network provides a mock function with given fields: 36 func (_m *Addr) Network() string { 37 ret := _m.Called() 38 39 var r0 string 40 if rf, ok := ret.Get(0).(func() string); ok { 41 r0 = rf() 42 } else { 43 r0 = ret.Get(0).(string) 44 } 45 46 return r0 47 } 48 49 // String provides a mock function with given fields: 50 func (_m *Addr) String() string { 51 ret := _m.Called() 52 53 var r0 string 54 if rf, ok := ret.Get(0).(func() string); ok { 55 r0 = rf() 56 } else { 57 r0 = ret.Get(0).(string) 58 } 59 60 return r0 61 } 62 63 func init() { 64 //addr := "192.168.1.1" 65 //remoteIpWhitelist[addr] = true 66 //grpcFuncWhitelist["*"] = true 67 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 68 Init(cfg) 69 qapi = new(mocks.QueueProtocolAPI) 70 qapi.On("GetConfig", mock.Anything).Return(cfg) 71 g.cli.QueueProtocolAPI = qapi 72 } 73 74 func getOkCtx() context.Context { 75 addr := new(Addr) 76 addr.On("String").Return("192.168.1.1") 77 78 ctx := context.Background() 79 pr := &peer.Peer{ 80 Addr: addr, 81 AuthInfo: nil, 82 } 83 ctx = peer.NewContext(ctx, pr) 84 return ctx 85 } 86 87 func testSendTransactionOk(t *testing.T) { 88 89 var in *types.Transaction 90 reply := &types.Reply{IsOk: true, Msg: nil} 91 qapi.On("SendTx", in).Return(reply, nil) 92 93 reply, err := g.SendTransaction(getOkCtx(), in) 94 assert.Nil(t, err, "the error should be nil") 95 assert.Equal(t, true, reply.IsOk, "reply should be ok") 96 } 97 98 func TestGrpc_SendTransactionSync(t *testing.T) { 99 var tx types.Transaction 100 reply := &types.Reply{IsOk: true, Msg: tx.Hash()} 101 mockAPI := new(mocks.QueueProtocolAPI) 102 mockAPI.On("SendTx", mock.Anything).Return(reply, nil) 103 mockAPI.On("QueryTx", mock.Anything).Return(&types.TransactionDetail{}, nil) 104 105 g := Grpc{} 106 g.cli.QueueProtocolAPI = mockAPI 107 reply, err := g.SendTransactionSync(getOkCtx(), &tx) 108 assert.Nil(t, err, "the error should be nil") 109 assert.Equal(t, true, reply.IsOk, "reply should be ok") 110 assert.Equal(t, tx.Hash(), reply.Msg) 111 } 112 113 func TestSendTransaction(t *testing.T) { 114 testSendTransactionOk(t) 115 } 116 117 func testVersionOK(t *testing.T) { 118 reply := &types.VersionInfo{Turingchain: "6.0.2"} 119 qapi.On("Version").Return(reply, nil) 120 data, err := g.Version(getOkCtx(), nil) 121 assert.Nil(t, err, "the error should be nil") 122 assert.Equal(t, "6.0.2", data.Turingchain, "reply should be ok") 123 } 124 125 func TestVersion(t *testing.T) { 126 testVersionOK(t) 127 } 128 129 func testGetMemPoolOK(t *testing.T) { 130 var in *types.ReqGetMempool 131 qapi.On("GetMempool", in).Return(nil, nil) 132 data, err := g.GetMemPool(getOkCtx(), nil) 133 assert.Nil(t, err, "the error should be nil") 134 assert.Nil(t, data) 135 } 136 137 func Test_GetMemPool(t *testing.T) { 138 testGetMemPoolOK(t) 139 } 140 141 func testGetLastMemPoolOK(t *testing.T) { 142 qapi.On("GetLastMempool").Return(nil, nil) 143 data, err := g.GetLastMemPool(getOkCtx(), nil) 144 assert.Nil(t, err, "the error should be nil") 145 assert.Nil(t, data) 146 } 147 148 func TestGetLastMemPool(t *testing.T) { 149 testGetLastMemPoolOK(t) 150 } 151 152 func testGetProperFeeOK(t *testing.T) { 153 var in *types.ReqProperFee 154 qapi.On("GetProperFee", in).Return(&types.ReplyProperFee{ProperFee: 1000000}, nil) 155 data, err := g.GetProperFee(getOkCtx(), in) 156 assert.Nil(t, err, "the error should be nil") 157 assert.Equal(t, int64(1000000), data.ProperFee) 158 } 159 160 func TestGetProperFee(t *testing.T) { 161 testGetProperFeeOK(t) 162 } 163 164 func testQueryChainError(t *testing.T) { 165 var in *pb.ChainExecutor 166 167 qapi.On("QueryChain", in).Return(nil, fmt.Errorf("error")).Once() 168 _, err := g.QueryChain(getOkCtx(), in) 169 assert.EqualError(t, err, "error", "return error") 170 } 171 172 func testQueryChainOK(t *testing.T) { 173 var in *pb.ChainExecutor 174 var msg types.Message 175 var req types.ReqString 176 req.Data = "msg" 177 msg = &req 178 qapi.On("QueryChain", in).Return(msg, nil).Once() 179 data, err := g.QueryChain(getOkCtx(), nil) 180 assert.Nil(t, err, "the error should be nil") 181 assert.Equal(t, true, data.IsOk, "reply should be ok") 182 var decodemsg types.ReqString 183 pb.Decode(data.Msg, &decodemsg) 184 assert.Equal(t, req.Data, decodemsg.Data) 185 } 186 187 func TestQueryChain(t *testing.T) { 188 testQueryChainError(t) 189 testQueryChainOK(t) 190 } 191 192 func testGetPeerInfoOK(t *testing.T) { 193 qapi.On("PeerInfo", mock.Anything).Return(nil, nil) 194 data, err := g.GetPeerInfo(getOkCtx(), &types.P2PGetPeerReq{}) 195 assert.Nil(t, err, "the error should be nil") 196 assert.Nil(t, data) 197 } 198 199 func TestGetPeerInfo(t *testing.T) { 200 testGetPeerInfoOK(t) 201 } 202 203 func testNetInfoOK(t *testing.T) { 204 qapi.On("GetNetInfo", mock.Anything).Return(nil, nil) 205 data, err := g.NetInfo(getOkCtx(), &types.P2PGetNetInfoReq{}) 206 assert.Nil(t, err, "the error should be nil") 207 assert.Nil(t, data) 208 } 209 210 func TestNetInfo(t *testing.T) { 211 testNetInfoOK(t) 212 } 213 214 func testGetAccountsOK(t *testing.T) { 215 qapi.On("ExecWalletFunc", "wallet", "WalletGetAccountList", mock.Anything).Return(&types.WalletAccounts{}, nil) 216 _, err := g.GetAccounts(getOkCtx(), nil) 217 assert.Nil(t, err, "the error should be nil") 218 } 219 220 func TestGetAccount(t *testing.T) { 221 qapi.On("ExecWalletFunc", "wallet", "WalletGetAccount", mock.Anything).Return(&types.WalletAccount{}, nil) 222 _, err := g.GetAccount(getOkCtx(), nil) 223 assert.Nil(t, err, "the error should be nil") 224 } 225 func TestGetAccounts(t *testing.T) { 226 testGetAccountsOK(t) 227 } 228 229 func testNewAccountOK(t *testing.T) { 230 var in *pb.ReqNewAccount 231 qapi.On("ExecWalletFunc", "wallet", "NewAccount", in).Return(&types.WalletAccount{}, nil) 232 _, err := g.NewAccount(getOkCtx(), nil) 233 assert.Nil(t, err, "the error should be nil") 234 } 235 236 func TestNewAccount(t *testing.T) { 237 testNewAccountOK(t) 238 } 239 240 func testWalletTransactionListOK(t *testing.T) { 241 var in *pb.ReqWalletTransactionList 242 qapi.On("ExecWalletFunc", "wallet", "WalletTransactionList", in).Return(&pb.WalletTxDetails{}, nil) 243 _, err := g.WalletTransactionList(getOkCtx(), nil) 244 assert.Nil(t, err, "the error should be nil") 245 } 246 247 func TestWalletTransactionList(t *testing.T) { 248 testWalletTransactionListOK(t) 249 } 250 251 func testImportPrivKeyOK(t *testing.T) { 252 var in *pb.ReqWalletImportPrivkey 253 qapi.On("ExecWalletFunc", "wallet", "WalletImportPrivkey", in).Return(&pb.WalletAccount{}, nil) 254 _, err := g.ImportPrivkey(getOkCtx(), nil) 255 assert.Nil(t, err, "the error should be nil") 256 } 257 258 func TestImportPrivKey(t *testing.T) { 259 testImportPrivKeyOK(t) 260 } 261 262 func testSendToAddressOK(t *testing.T) { 263 var in *pb.ReqWalletSendToAddress 264 qapi.On("ExecWalletFunc", "wallet", "WalletSendToAddress", in).Return(&pb.ReplyHash{}, nil) 265 _, err := g.SendToAddress(getOkCtx(), nil) 266 assert.Nil(t, err, "the error should be nil") 267 } 268 269 func TestSendToAddress(t *testing.T) { 270 testSendToAddressOK(t) 271 } 272 273 func testSetTxFeeOK(t *testing.T) { 274 var in *pb.ReqWalletSetFee 275 qapi.On("ExecWalletFunc", "wallet", "WalletSetFee", in).Return(&pb.Reply{}, nil) 276 _, err := g.SetTxFee(getOkCtx(), nil) 277 assert.Nil(t, err, "the error should be nil") 278 } 279 280 func TestSetTxFee(t *testing.T) { 281 testSetTxFeeOK(t) 282 } 283 284 func testSetLablOK(t *testing.T) { 285 var in *pb.ReqWalletSetLabel 286 qapi.On("ExecWalletFunc", "wallet", "WalletSetLabel", in).Return(&pb.WalletAccount{}, nil) 287 _, err := g.SetLabl(getOkCtx(), nil) 288 assert.Nil(t, err, "the error should be nil") 289 } 290 291 func TestSetLabl(t *testing.T) { 292 testSetLablOK(t) 293 } 294 295 func testMergeBalanceOK(t *testing.T) { 296 var in *pb.ReqWalletMergeBalance 297 qapi.On("ExecWalletFunc", "wallet", "WalletMergeBalance", in).Return(&pb.ReplyHashes{}, nil) 298 _, err := g.MergeBalance(getOkCtx(), nil) 299 assert.Nil(t, err, "the error should be nil") 300 } 301 302 func TestMergeBalance(t *testing.T) { 303 testMergeBalanceOK(t) 304 } 305 306 func testSetPasswdOK(t *testing.T) { 307 var in *pb.ReqWalletSetPasswd 308 qapi.On("ExecWalletFunc", "wallet", "WalletSetPasswd", in).Return(&pb.Reply{}, nil) 309 _, err := g.SetPasswd(getOkCtx(), nil) 310 assert.Nil(t, err, "the error should be nil") 311 } 312 313 func TestSetPasswd(t *testing.T) { 314 testSetPasswdOK(t) 315 } 316 317 func testLockOK(t *testing.T) { 318 var in *pb.ReqNil 319 qapi.On("ExecWalletFunc", "wallet", "WalletLock", in).Return(&pb.Reply{}, nil) 320 _, err := g.Lock(getOkCtx(), nil) 321 assert.Nil(t, err, "the error should be nil") 322 } 323 324 func TestLock(t *testing.T) { 325 testLockOK(t) 326 } 327 328 func testUnLockOK(t *testing.T) { 329 var in *pb.WalletUnLock 330 qapi.On("ExecWalletFunc", "wallet", "WalletUnLock", in).Return(&pb.Reply{}, nil) 331 _, err := g.UnLock(getOkCtx(), nil) 332 assert.Nil(t, err, "the error should be nil") 333 } 334 335 func TestUnLock(t *testing.T) { 336 testUnLockOK(t) 337 } 338 339 func testGenSeedOK(t *testing.T) { 340 var in *pb.GenSeedLang 341 qapi.On("ExecWalletFunc", "wallet", "GenSeed", in).Return(&pb.ReplySeed{}, nil) 342 _, err := g.GenSeed(getOkCtx(), nil) 343 assert.Nil(t, err, "the error should be nil") 344 } 345 346 func TestGenSeed(t *testing.T) { 347 testGenSeedOK(t) 348 } 349 350 func testGetSeedOK(t *testing.T) { 351 var in *pb.GetSeedByPw 352 qapi.On("ExecWalletFunc", "wallet", "GetSeed", in).Return(&pb.ReplySeed{}, nil) 353 _, err := g.GetSeed(getOkCtx(), nil) 354 assert.Nil(t, err, "the error should be nil") 355 } 356 357 func TestGetSeed(t *testing.T) { 358 testGetSeedOK(t) 359 } 360 361 func testSaveSeedOK(t *testing.T) { 362 var in *pb.SaveSeedByPw 363 qapi.On("ExecWalletFunc", "wallet", "SaveSeed", in).Return(&pb.Reply{}, nil) 364 _, err := g.SaveSeed(getOkCtx(), nil) 365 assert.Nil(t, err, "the error should be nil") 366 } 367 368 func TestSaveSeed(t *testing.T) { 369 testSaveSeedOK(t) 370 } 371 372 func testGetWalletStatusOK(t *testing.T) { 373 var in *pb.ReqNil 374 qapi.On("ExecWalletFunc", "wallet", "GetWalletStatus", in).Return(&pb.WalletStatus{}, nil) 375 _, err := g.GetWalletStatus(getOkCtx(), nil) 376 assert.Nil(t, err, "the error should be nil") 377 } 378 379 func TestGetWalletStatus(t *testing.T) { 380 testGetWalletStatusOK(t) 381 } 382 383 func testDumpPrivkeyOK(t *testing.T) { 384 var in *pb.ReqString 385 qapi.On("ExecWalletFunc", "wallet", "DumpPrivkey", in).Return(&pb.ReplyString{}, nil) 386 _, err := g.DumpPrivkey(getOkCtx(), nil) 387 assert.Nil(t, err, "the error should be nil") 388 } 389 390 func TestDumpPrivkey(t *testing.T) { 391 testDumpPrivkeyOK(t) 392 } 393 394 func testDumpPrivkeysFileOK(t *testing.T) { 395 var in *pb.ReqPrivkeysFile 396 qapi.On("ExecWalletFunc", "wallet", "DumpPrivkeysFile", in).Return(&pb.Reply{}, nil) 397 _, err := g.DumpPrivkeysFile(getOkCtx(), nil) 398 assert.Nil(t, err, "the error should be nil") 399 } 400 401 func TestDumpPrivkeysFile(t *testing.T) { 402 testDumpPrivkeysFileOK(t) 403 } 404 405 func testImportPrivkeysFileOK(t *testing.T) { 406 var in *pb.ReqPrivkeysFile 407 qapi.On("ExecWalletFunc", "wallet", "ImportPrivkeysFile", in).Return(&pb.Reply{}, nil) 408 _, err := g.ImportPrivkeysFile(getOkCtx(), nil) 409 assert.Nil(t, err, "the error should be nil") 410 } 411 412 func TestImportPrivkeysFile(t *testing.T) { 413 testImportPrivkeysFileOK(t) 414 } 415 416 func testGetBlocksError(t *testing.T) { 417 var in = pb.ReqBlocks{IsDetail: true} 418 qapi.On("GetBlocks", &in).Return(nil, fmt.Errorf("error")).Once() 419 _, err := g.GetBlocks(getOkCtx(), &in) 420 assert.EqualError(t, err, "error", "the error should be error") 421 } 422 423 func testGetBlocksOK(t *testing.T) { 424 var in = pb.ReqBlocks{IsDetail: true} 425 var details types.BlockDetails 426 427 var block = &types.Block{Version: 1} 428 var detail = &types.BlockDetail{Block: block} 429 details.Items = append(details.Items, detail) 430 431 qapi.On("GetBlocks", &in).Return(&details, nil).Once() 432 data, err := g.GetBlocks(getOkCtx(), &in) 433 assert.Nil(t, err, "the error should be nil") 434 assert.Equal(t, true, data.IsOk) 435 436 var details2 types.BlockDetails 437 pb.Decode(data.Msg, &details2) 438 if !proto.Equal(&details, &details2) { 439 assert.Equal(t, details, details2) 440 } 441 } 442 443 func TestGetBlocks(t *testing.T) { 444 testGetBlocksError(t) 445 testGetBlocksOK(t) 446 } 447 448 func testGetHexTxByHashError(t *testing.T) { 449 var in *pb.ReqHash 450 451 qapi.On("QueryTx", in).Return(nil, fmt.Errorf("error")).Once() 452 _, err := g.GetHexTxByHash(getOkCtx(), in) 453 assert.EqualError(t, err, "error", "the error should be error") 454 } 455 456 func testGetHexTxByHashOK(t *testing.T) { 457 var in *pb.ReqHash 458 tx := &types.Transaction{Fee: 1} 459 var td = &types.TransactionDetail{Tx: tx} 460 var tdNil = &types.TransactionDetail{Tx: nil} 461 462 encodetx := common.ToHex(pb.Encode(tx)) 463 464 qapi.On("QueryTx", in).Return(tdNil, nil).Once() 465 data, err := g.GetHexTxByHash(getOkCtx(), in) 466 assert.Nil(t, err, "the error should be nil") 467 assert.Equal(t, "", data.Tx) 468 469 qapi.On("QueryTx", in).Return(td, nil).Once() 470 data, err = g.GetHexTxByHash(getOkCtx(), in) 471 assert.Nil(t, err, "the error should be nil") 472 assert.Equal(t, encodetx, data.Tx) 473 } 474 475 func TestGetHexTxByHash(t *testing.T) { 476 testGetHexTxByHashError(t) 477 testGetHexTxByHashOK(t) 478 } 479 480 func testGetTransactionByAddrOK(t *testing.T) { 481 var in *pb.ReqAddr 482 qapi.On("GetTransactionByAddr", in).Return(nil, nil) 483 data, err := g.GetTransactionByAddr(getOkCtx(), in) 484 assert.Nil(t, err, "the error should be nil") 485 assert.Nil(t, data) 486 } 487 488 func TestGetTransactionByAddr(t *testing.T) { 489 testGetTransactionByAddrOK(t) 490 } 491 492 func testGetTransactionByHashesOK(t *testing.T) { 493 var in *pb.ReqHashes 494 qapi.On("GetTransactionByHash", in).Return(nil, nil) 495 data, err := g.GetTransactionByHashes(getOkCtx(), in) 496 assert.Nil(t, err, "the error should be nil") 497 assert.Nil(t, data) 498 } 499 500 func TestGetTransactionByHashes(t *testing.T) { 501 testGetTransactionByHashesOK(t) 502 } 503 504 func testGetHeadersOK(t *testing.T) { 505 var in *pb.ReqBlocks 506 qapi.On("GetHeaders", in).Return(nil, nil) 507 data, err := g.GetHeaders(getOkCtx(), in) 508 assert.Nil(t, err, "the error should be nil") 509 assert.Nil(t, data) 510 } 511 512 func TestGetHeaders(t *testing.T) { 513 testGetHeadersOK(t) 514 } 515 516 func testGetBlockOverviewOK(t *testing.T) { 517 var in *pb.ReqHash 518 qapi.On("GetBlockOverview", in).Return(nil, nil) 519 data, err := g.GetBlockOverview(getOkCtx(), in) 520 assert.Nil(t, err, "the error should be nil") 521 assert.Nil(t, data) 522 } 523 524 func TestGetBlockOverview(t *testing.T) { 525 testGetBlockOverviewOK(t) 526 } 527 528 func testGetBlockHashOK(t *testing.T) { 529 var in *pb.ReqInt 530 qapi.On("GetBlockHash", in).Return(nil, nil) 531 data, err := g.GetBlockHash(getOkCtx(), in) 532 assert.Nil(t, err, "the error should be nil") 533 assert.Nil(t, data) 534 } 535 536 func TestGetBlockHash(t *testing.T) { 537 testGetBlockHashOK(t) 538 } 539 540 func testIsSyncOK(t *testing.T) { 541 var in *pb.ReqNil 542 qapi.On("IsSync").Return(nil, nil) 543 data, err := g.IsSync(getOkCtx(), in) 544 assert.Nil(t, err, "the error should be nil") 545 assert.Nil(t, data) 546 } 547 548 func TestIsSync(t *testing.T) { 549 testIsSyncOK(t) 550 } 551 552 func testIsNtpClockSyncOK(t *testing.T) { 553 var in *pb.ReqNil 554 qapi.On("IsNtpClockSync").Return(nil, nil) 555 data, err := g.IsNtpClockSync(getOkCtx(), in) 556 assert.Nil(t, err, "the error should be nil") 557 assert.Nil(t, data) 558 } 559 560 func TestIsNtpClockSync(t *testing.T) { 561 testIsNtpClockSyncOK(t) 562 } 563 564 func testGetLastHeaderOK(t *testing.T) { 565 var in *pb.ReqNil 566 qapi.On("GetLastHeader").Return(nil, nil) 567 data, err := g.GetLastHeader(getOkCtx(), in) 568 assert.Nil(t, err, "the error should be nil") 569 assert.Nil(t, data) 570 } 571 572 func TestGetLastHeader(t *testing.T) { 573 testGetLastHeaderOK(t) 574 } 575 576 func testQueryTransactionOK(t *testing.T) { 577 var in *pb.ReqHash 578 qapi.On("QueryTx", in).Return(nil, nil) 579 data, err := g.QueryTransaction(getOkCtx(), in) 580 assert.Nil(t, err, "the error should be nil") 581 assert.Nil(t, data) 582 } 583 584 func TestQueryTransaction(t *testing.T) { 585 testQueryTransactionOK(t) 586 } 587 588 func TestReWriteRawTx(t *testing.T) { 589 txHex1 := "0a05636f696e73122c18010a281080c2d72f222131477444795771577233553637656a7663776d333867396e7a6e7a434b58434b7120a08d0630a696c0b3f78dd9ec083a2131477444795771577233553637656a7663776d333867396e7a6e7a434b58434b71" 590 in := &types.ReWriteRawTx{ 591 Tx: txHex1, 592 Fee: 29977777777, 593 Expire: "130s", 594 To: "aabbccdd", 595 Index: 0, 596 } 597 598 data, err := g.ReWriteRawTx(getOkCtx(), in) 599 assert.Nil(t, err) 600 assert.NotNil(t, data.Data) 601 rtTx := hex.EncodeToString(data.Data) 602 assert.NotEqual(t, txHex1, rtTx) 603 604 tx := &types.Transaction{} 605 err = types.Decode(data.Data, tx) 606 assert.Nil(t, err) 607 assert.Equal(t, tx.Fee, in.Fee) 608 assert.Equal(t, in.To, tx.To) 609 } 610 611 func TestGrpc_CreateNoBalanceTransaction(t *testing.T) { 612 _, err := g.CreateNoBalanceTransaction(getOkCtx(), &pb.NoBalanceTx{}) 613 assert.NoError(t, err) 614 } 615 616 func TestGrpc_CreateNoBalanceTxs(t *testing.T) { 617 _, err := g.CreateNoBalanceTxs(getOkCtx(), &pb.NoBalanceTxs{TxHexs: []string{"0a05746f6b656e12413804223d0a0443434e5910a09c011a0d74657374207472616e73666572222231333559774e715367694551787577586650626d526d48325935334564673864343820a08d0630969a9fe6c4b9c7ba5d3a2231333559774e715367694551787577586650626d526d483259353345646738643438", "0a05746f6b656e12413804223d0a0443434e5910b0ea011a0d74657374207472616e73666572222231333559774e715367694551787577586650626d526d48325935334564673864343820a08d0630bca0a2dbc0f182e06f3a2231333559774e715367694551787577586650626d526d483259353345646738643438"}}) 618 assert.NoError(t, err) 619 } 620 621 func TestGrpc_CreateRawTransaction(t *testing.T) { 622 _, err := g.CreateRawTransaction(getOkCtx(), &pb.CreateTx{}) 623 assert.NoError(t, err) 624 } 625 626 func TestGrpc_CreateTransaction(t *testing.T) { 627 _, err := g.CreateTransaction(getOkCtx(), &pb.CreateTxIn{Execer: []byte("coins")}) 628 assert.Equal(t, err, types.ErrActionNotSupport) 629 } 630 631 func TestGrpc_CreateRawTxGroup(t *testing.T) { 632 _, err := g.CreateRawTxGroup(getOkCtx(), &pb.CreateTransactionGroup{}) 633 assert.Equal(t, types.ErrTxGroupCountLessThanTwo, err) 634 } 635 636 func TestGrpc_GetAddrOverview(t *testing.T) { 637 _, err := g.GetAddrOverview(getOkCtx(), &types.ReqAddr{}) 638 assert.Equal(t, err, types.ErrInvalidAddress) 639 } 640 641 func TestGrpc_GetBalance(t *testing.T) { 642 qapi.On("StoreGet", mock.Anything).Return(nil, types.ErrInvalidParam) 643 _, err := g.GetBalance(getOkCtx(), &types.ReqBalance{}) 644 assert.Equal(t, err, types.ErrInvalidParam) 645 } 646 647 func TestGrpc_GetAllExecBalance(t *testing.T) { 648 _, err := g.GetAllExecBalance(getOkCtx(), &pb.ReqAllExecBalance{}) 649 assert.Equal(t, err, types.ErrInvalidAddress) 650 } 651 652 func TestGrpc_QueryConsensus(t *testing.T) { 653 qapi.On("QueryConsensus", mock.Anything).Return(&types.ReqString{Data: "test"}, nil) 654 _, err := g.QueryConsensus(getOkCtx(), &pb.ChainExecutor{}) 655 assert.NoError(t, err) 656 } 657 658 func TestGrpc_ExecWallet(t *testing.T) { 659 qapi.On("ExecWallet", mock.Anything).Return(&types.ReqString{Data: "test"}, nil) 660 _, err := g.ExecWallet(getOkCtx(), &pb.ChainExecutor{}) 661 assert.NoError(t, err) 662 } 663 664 func TestGrpc_GetLastBlockSequence(t *testing.T) { 665 qapi.On("GetLastBlockSequence", mock.Anything).Return(nil, nil) 666 _, err := g.GetLastBlockSequence(getOkCtx(), &types.ReqNil{}) 667 assert.NoError(t, err) 668 } 669 670 func TestGrpc_GetBlockByHashes(t *testing.T) { 671 qapi.On("GetBlockByHashes", mock.Anything, mock.Anything, mock.Anything).Return(nil, nil) 672 _, err := g.GetBlockByHashes(getOkCtx(), &types.ReqHashes{}) 673 assert.NoError(t, err) 674 } 675 676 func TestGrpc_GetSequenceByHash(t *testing.T) { 677 qapi.On("GetSequenceByHash", mock.Anything, mock.Anything, mock.Anything).Return(nil, nil) 678 _, err := g.GetSequenceByHash(getOkCtx(), &pb.ReqHash{}) 679 assert.NoError(t, err) 680 } 681 682 func TestGrpc_SignRawTx(t *testing.T) { 683 qapi.On("ExecWalletFunc", "wallet", "SignRawTx", mock.Anything).Return(&pb.ReplySignRawTx{}, nil) 684 _, err := g.SignRawTx(getOkCtx(), &types.ReqSignRawTx{}) 685 assert.NoError(t, err) 686 } 687 688 func TestGrpc_QueryRandNum(t *testing.T) { 689 qapi.On("Query", mock.Anything, mock.Anything, mock.Anything).Return(&pb.ReplyHash{Hash: []byte("test")}, nil) 690 _, err := g.QueryRandNum(getOkCtx(), &pb.ReqRandHash{}) 691 assert.NoError(t, err) 692 } 693 694 func TestGrpc_GetFork(t *testing.T) { 695 types.RegFork("para", func(cfg *types.TuringchainConfig) { 696 cfg.SetDappFork("para", "fork100", 100) 697 }) 698 699 str := types.GetDefaultCfgstring() 700 newstr := strings.Replace(str, "Title=\"local\"", "Title=\"turingchain\"", 1) 701 cfg := types.NewTuringchainConfig(newstr) 702 Init(cfg) 703 api := new(mocks.QueueProtocolAPI) 704 api.On("GetConfig", mock.Anything).Return(cfg) 705 grpc := Grpc{} 706 grpc.cli.QueueProtocolAPI = api 707 val, err := grpc.GetFork(getOkCtx(), &pb.ReqKey{Key: []byte("para-fork100")}) 708 assert.NoError(t, err) 709 assert.Equal(t, int64(100), val.Data) 710 711 cfg1 := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 712 Init(cfg1) 713 api1 := new(mocks.QueueProtocolAPI) 714 api1.On("GetConfig", mock.Anything).Return(cfg1) 715 grpc1 := Grpc{} 716 grpc1.cli.QueueProtocolAPI = api1 717 val, err = grpc1.GetFork(getOkCtx(), &pb.ReqKey{Key: []byte("ForkBlockHash")}) 718 assert.NoError(t, err) 719 assert.Equal(t, int64(1), val.Data) 720 } 721 722 func TestGrpc_LoadParaTxByTitle(t *testing.T) { 723 qapi.On("LoadParaTxByTitle", mock.Anything, mock.Anything, mock.Anything).Return(nil, nil) 724 _, err := g.LoadParaTxByTitle(getOkCtx(), &pb.ReqHeightByTitle{}) 725 assert.NoError(t, err) 726 } 727 728 func TestGrpc_GetParaTxByHeight(t *testing.T) { 729 qapi.On("GetParaTxByHeight", mock.Anything, mock.Anything, mock.Anything).Return(nil, nil) 730 _, err := g.GetParaTxByHeight(getOkCtx(), &pb.ReqParaTxByHeight{}) 731 assert.NoError(t, err) 732 } 733 734 func TestGrpc_GetServerTime(t *testing.T) { 735 qapi.On("GetServer", mock.Anything, mock.Anything, mock.Anything).Return(nil, nil) 736 _, err := g.GetServerTime(getOkCtx(), nil) 737 assert.NoError(t, err) 738 }