github.com/turingchain2020/turingchain@v1.1.21/rpc/types/types_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 types
     6  
     7  import (
     8  	"encoding/json"
     9  	"testing"
    10  
    11  	"github.com/turingchain2020/turingchain/client/mocks"
    12  	"github.com/turingchain2020/turingchain/common"
    13  	"github.com/turingchain2020/turingchain/types"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/mock"
    16  )
    17  
    18  func TestDecodeUserWrite(t *testing.T) {
    19  	payload := []byte("#md#hello#world")
    20  	data := decodeUserWrite(payload)
    21  	assert.Equal(t, data, &types.UserWrite{Topic: "md", Content: "hello#world"})
    22  
    23  	payload = []byte("hello#world")
    24  	data = decodeUserWrite(payload)
    25  	assert.Equal(t, data, &types.UserWrite{Topic: "", Content: "hello#world"})
    26  
    27  	payload = []byte("123#hello#wzw")
    28  	data = decodeUserWrite(payload)
    29  	assert.NotEqual(t, data, &types.UserWrite{Topic: "123", Content: "hello#world"})
    30  }
    31  
    32  func TestDecodeTx(t *testing.T) {
    33  	cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring())
    34  	tx := types.Transaction{
    35  		Execer:  []byte(cfg.ExecName("coin")),
    36  		Payload: []byte("342412abcd"),
    37  		Nonce:   8978167239,
    38  		To:      "1asd234dsf43fds",
    39  		ChainID: cfg.GetChainID(),
    40  	}
    41  
    42  	data, err := DecodeTx(&tx)
    43  	assert.NotNil(t, data)
    44  	assert.Nil(t, err)
    45  
    46  	tx.Execer = []byte(cfg.ExecName("coins"))
    47  	data, err = DecodeTx(&tx)
    48  	assert.NotNil(t, data)
    49  	assert.Nil(t, err)
    50  
    51  	tx = types.Transaction{
    52  		Execer:  []byte(cfg.ExecName("hashlock")),
    53  		Payload: []byte("34"),
    54  		Nonce:   8978167239,
    55  		To:      "1asd234dsf43fds",
    56  		ChainID: cfg.GetChainID(),
    57  	}
    58  
    59  	t.Log(string(tx.Execer))
    60  	data, err = DecodeTx(&tx)
    61  	assert.NotNil(t, data)
    62  	assert.Nil(t, err)
    63  }
    64  
    65  func TestDecodeLog(t *testing.T) {
    66  	execer := []byte("coins")
    67  	log := &ReceiptLog{}
    68  	receipt := &ReceiptData{Ty: 2, Logs: []*ReceiptLog{log}}
    69  	_, err := DecodeLog(execer, receipt)
    70  	assert.NoError(t, err)
    71  }
    72  
    73  func TestConvertWalletTxDetailToJSON(t *testing.T) {
    74  	// 需要先注册执行器类型
    75  	types.NewTuringchainConfig(types.GetDefaultCfgstring())
    76  
    77  	tx := &types.Transaction{Execer: []byte("coins")}
    78  	log := &types.ReceiptLog{Ty: 0, Log: []byte("test")}
    79  	receipt := &types.ReceiptData{Ty: 0, Logs: []*types.ReceiptLog{log}}
    80  	detail := &types.WalletTxDetail{Tx: tx, Receipt: receipt}
    81  	in := &types.WalletTxDetails{TxDetails: []*types.WalletTxDetail{detail}}
    82  	out := &WalletTxDetails{}
    83  	err := ConvertWalletTxDetailToJSON(in, out)
    84  	assert.NoError(t, err)
    85  
    86  	//test withdraw swap from to
    87  	detail.Fromaddr = "from"
    88  	detail.Tx.Payload, err = common.FromHex("0x180322301080c2d72f2205636f696e732a22314761485970576d71414a7371527772706f4e6342385676674b7453776a63487174")
    89  	assert.NoError(t, err)
    90  	tx.To = "to"
    91  	out = &WalletTxDetails{}
    92  	err = ConvertWalletTxDetailToJSON(in, out)
    93  	assert.NoError(t, err)
    94  	assert.Equal(t, "to", out.TxDetails[0].FromAddr)
    95  	assert.Equal(t, "from", detail.Tx.To)
    96  }
    97  
    98  func TestServer(t *testing.T) {
    99  	api := &mocks.QueueProtocolAPI{}
   100  	ch := ChannelClient{QueueProtocolAPI: api}
   101  	cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring())
   102  	api.On("GetConfig", mock.Anything).Return(cfg)
   103  	ch.Init("test", nil, nil, nil)
   104  	db := ch.GetCoinsAccountDB()
   105  	assert.NotNil(t, db)
   106  }
   107  
   108  func TestDecodeTx2(t *testing.T) {
   109  	// 需要先注册执行器类型
   110  	types.NewTuringchainConfig(types.GetDefaultCfgstring())
   111  	bdata, err := common.FromHex("0a05636f696e73121018010a0c108084af5f1a05310a320a3320e8b31b30b9b69483d7f9d3f04c3a22314b67453376617969715a4b6866684d66744e3776743267447639486f4d6b393431")
   112  	assert.Nil(t, err)
   113  	var r types.Transaction
   114  	err = types.Decode(bdata, &r)
   115  	assert.Nil(t, err)
   116  	data, err := DecodeTx(&r)
   117  	assert.Nil(t, err)
   118  	jsondata, err := json.Marshal(data)
   119  	assert.Nil(t, err)
   120  	assert.Equal(t, string(jsondata), `{"execer":"coins","payload":{"transfer":{"cointoken":"","amount":"200000000","note":"1\n2\n3","to":""},"ty":1},"rawPayload":"0x18010a0c108084af5f1a05310a320a33","signature":{"ty":0,"pubkey":"","signature":""},"fee":449000,"feefmt":"0.0045","expire":0,"nonce":5539796760414985017,"from":"1HT7xU2Ngenf7D4yocz2SAcnNLW7rK8d4E","to":"1KgE3vayiqZKhfhMftN7vt2gDv9HoMk941","hash":"0x6f9d543a345f6e17d8c3cc5f846c22570acf3b4b5851f48d0c2be5459d90c410"}`)
   121  }