diff options
author | obscuren <geffobscura@gmail.com> | 2015-04-01 18:51:44 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-04-01 18:51:44 +0800 |
commit | dba9b83aa07ced653b24c3e34d55ffe13a740243 (patch) | |
tree | 0dc20ee3807e3fe5a076fbfe2ec00bd6601792a6 /rpc/responses_test.go | |
parent | 4e8f8cfab701bb6c4ad2b8cf166d642f408ca398 (diff) | |
parent | b0e09ec82752b48c2384c315dc420e5c39e785b6 (diff) | |
download | go-tangerine-dba9b83aa07ced653b24c3e34d55ffe13a740243.tar.gz go-tangerine-dba9b83aa07ced653b24c3e34d55ffe13a740243.tar.zst go-tangerine-dba9b83aa07ced653b24c3e34d55ffe13a740243.zip |
Merge branch 'tgerring-hexify' into develop
Diffstat (limited to 'rpc/responses_test.go')
-rw-r--r-- | rpc/responses_test.go | 175 |
1 files changed, 103 insertions, 72 deletions
diff --git a/rpc/responses_test.go b/rpc/responses_test.go index 278939830..5c6c6a895 100644 --- a/rpc/responses_test.go +++ b/rpc/responses_test.go @@ -2,7 +2,9 @@ package rpc import ( "encoding/json" + "fmt" "math/big" + "regexp" "testing" "github.com/ethereum/go-ethereum/common" @@ -10,6 +12,15 @@ import ( "github.com/ethereum/go-ethereum/core/types" ) +const ( + reHash = `"0x[0-9a-f]{64}"` // 32 bytes + reHashOpt = `"(0x[0-9a-f]{64})"|null` // 32 bytes or null + reAddress = `"0x[0-9a-f]{40}"` // 20 bytes + reAddressOpt = `"0x[0-9a-f]{40}"|null` // 20 bytes or null + reNum = `"0x([1-9a-f][0-9a-f]{1,15})|0"` // must not have left-padded zeros + reData = `"0x[0-9a-f]*"` // can be "empty" +) + func TestNewBlockRes(t *testing.T) { parentHash := common.HexToHash("0x01") coinbase := common.HexToAddress("0x01") @@ -18,56 +29,35 @@ func TestNewBlockRes(t *testing.T) { nonce := uint64(1) extra := "" block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, extra) - - _ = NewBlockRes(block) -} - -func TestBlockRes(t *testing.T) { - v := &BlockRes{ - BlockNumber: big.NewInt(0), - BlockHash: common.HexToHash("0x0"), - ParentHash: common.HexToHash("0x0"), - Nonce: [8]byte{0, 0, 0, 0, 0, 0, 0, 0}, - Sha3Uncles: common.HexToHash("0x0"), - LogsBloom: types.BytesToBloom([]byte{0}), - TransactionRoot: common.HexToHash("0x0"), - StateRoot: common.HexToHash("0x0"), - Miner: common.HexToAddress("0x0"), - Difficulty: big.NewInt(0), - TotalDifficulty: big.NewInt(0), - Size: big.NewInt(0), - ExtraData: []byte{}, - GasLimit: big.NewInt(0), - MinGasPrice: int64(0), - GasUsed: big.NewInt(0), - UnixTimestamp: int64(0), - // Transactions []*TransactionRes `json:"transactions"` - // Uncles []common.Hash `json:"uncles"` + tests := map[string]string{ + "number": reNum, + "hash": reHash, + "parentHash": reHash, + "nonce": reNum, + "sha3Uncles": reHash, + "logsBloom": reData, + "transactionRoot": reHash, + "stateRoot": reHash, + "miner": reAddress, + "difficulty": `"0x1"`, + "totalDifficulty": reNum, + "size": reNum, + "extraData": reData, + "gasLimit": reNum, + // "minGasPrice": "0x", + "gasUsed": reNum, + "timestamp": reNum, } - _, _ = json.Marshal(v) + v := NewBlockRes(block) + j, _ := json.Marshal(v) - // fmt.Println(string(j)) - -} - -func TestTransactionRes(t *testing.T) { - a := common.HexToAddress("0x0") - v := &TransactionRes{ - Hash: common.HexToHash("0x0"), - Nonce: uint64(0), - BlockHash: common.HexToHash("0x0"), - BlockNumber: int64(0), - TxIndex: int64(0), - From: common.HexToAddress("0x0"), - To: &a, - Value: big.NewInt(0), - Gas: big.NewInt(0), - GasPrice: big.NewInt(0), - Input: []byte{0}, + for k, re := range tests { + match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j)) + if !match { + t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j)) + } } - - _, _ = json.Marshal(v) } func TestNewTransactionRes(t *testing.T) { @@ -78,26 +68,80 @@ func TestNewTransactionRes(t *testing.T) { data := []byte{1, 2, 3} tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data) - _ = NewTransactionRes(tx) + tests := map[string]string{ + "hash": reHash, + "nonce": reNum, + "blockHash": reHash, + "blockNum": reNum, + "transactionIndex": reNum, + "from": reAddress, + "to": reAddressOpt, + "value": reNum, + "gas": reNum, + "gasPrice": reNum, + "input": reData, + } + + v := NewTransactionRes(tx) + v.BlockHash = newHexData(common.HexToHash("0x030201")) + v.BlockNumber = newHexNum(5) + v.TxIndex = newHexNum(0) + j, _ := json.Marshal(v) + for k, re := range tests { + match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j)) + if !match { + t.Error(fmt.Sprintf("`%s` output json does not match format %s. Source %s", k, re, j)) + } + } + } -func TestLogRes(t *testing.T) { - topics := make([]common.Hash, 3) - topics = append(topics, common.HexToHash("0x00")) - topics = append(topics, common.HexToHash("0x10")) - topics = append(topics, common.HexToHash("0x20")) +func TestNewLogRes(t *testing.T) { + log := makeStateLog(0) + tests := map[string]string{ + "address": reAddress, + // "topics": "[.*]" + "data": reData, + "blockNumber": reNum, + // "hash": reHash, + // "logIndex": reNum, + // "blockHash": reHash, + // "transactionHash": reHash, + "transactionIndex": reNum, + } + + v := NewLogRes(log) + j, _ := json.Marshal(v) + + for k, re := range tests { + match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j)) + if !match { + t.Error(fmt.Sprintf("`%s` output json does not match format %s. Got %s", k, re, j)) + } + } + +} - v := &LogRes{ - Topics: topics, - Address: common.HexToAddress("0x0"), - Data: []byte{1, 2, 3}, - Number: uint64(5), +func TestNewLogsRes(t *testing.T) { + logs := make([]state.Log, 3) + logs[0] = makeStateLog(1) + logs[1] = makeStateLog(2) + logs[2] = makeStateLog(3) + tests := map[string]string{} + + v := NewLogsRes(logs) + j, _ := json.Marshal(v) + + for k, re := range tests { + match, _ := regexp.MatchString(fmt.Sprintf(`[{.*"%s":%s.*}]`, k, re), string(j)) + if !match { + t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j)) + } } - _, _ = json.Marshal(v) } -func MakeStateLog(num int) state.Log { +func makeStateLog(num int) state.Log { address := common.HexToAddress("0x0") data := []byte{1, 2, 3} number := uint64(num) @@ -108,16 +152,3 @@ func MakeStateLog(num int) state.Log { log := state.NewLog(address, topics, data, number) return log } - -func TestNewLogRes(t *testing.T) { - log := MakeStateLog(0) - _ = NewLogRes(log) -} - -func TestNewLogsRes(t *testing.T) { - logs := make([]state.Log, 3) - logs[0] = MakeStateLog(1) - logs[1] = MakeStateLog(2) - logs[2] = MakeStateLog(3) - _ = NewLogsRes(logs) -} |