rpc: tests cleanup, use client lib for JSONRPC testing too

This commit is contained in:
Ethan Buchman 2015-04-02 18:11:37 -07:00
parent 474bf31400
commit 3bedcbf94d
5 changed files with 98 additions and 240 deletions

View File

@ -149,7 +149,7 @@ func (c *ClientHTTP) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error)
}
func (c *ClientHTTP) GenPrivAccount() (*core.ResponseGenPrivAccount, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
@ -269,7 +269,7 @@ func (c *ClientHTTP) GetStorage(address []byte, storage []byte) (*core.ResponseG
}
func (c *ClientHTTP) ListAccounts() (*core.ResponseListAccounts, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
@ -299,7 +299,7 @@ func (c *ClientHTTP) ListAccounts() (*core.ResponseListAccounts, error) {
}
func (c *ClientHTTP) ListValidators() (*core.ResponseListValidators, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
@ -329,7 +329,7 @@ func (c *ClientHTTP) ListValidators() (*core.ResponseListValidators, error) {
}
func (c *ClientHTTP) NetInfo() (*core.ResponseNetInfo, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
@ -389,7 +389,7 @@ func (c *ClientHTTP) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*
}
func (c *ClientHTTP) Status() (*core.ResponseStatus, error) {
values, err := argsToURLValues(nil, nil)
values, err := argsToURLValues(nil)
if err != nil {
return nil, err
}
@ -530,7 +530,7 @@ func (c *ClientJSON) GenPrivAccount() (*core.ResponseGenPrivAccount, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["GenPrivAccount"],
Params: []interface{}{nil},
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -638,7 +638,7 @@ func (c *ClientJSON) ListAccounts() (*core.ResponseListAccounts, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["ListAccounts"],
Params: []interface{}{nil},
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -665,7 +665,7 @@ func (c *ClientJSON) ListValidators() (*core.ResponseListValidators, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["ListValidators"],
Params: []interface{}{nil},
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -692,7 +692,7 @@ func (c *ClientJSON) NetInfo() (*core.ResponseNetInfo, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["NetInfo"],
Params: []interface{}{nil},
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)
@ -746,7 +746,7 @@ func (c *ClientJSON) Status() (*core.ResponseStatus, error) {
request := RPCRequest{
JSONRPC: "2.0",
Method: reverseFuncMap["Status"],
Params: []interface{}{nil},
Params: []interface{}{},
Id: 0,
}
body, err := c.RequestResponse(request)

View File

@ -0,0 +1,59 @@
package rpc
import (
"testing"
)
//--------------------------------------------------------------------------------
// Test the HTTP client
func TestHTTPStatus(t *testing.T) {
testStatus(t, "HTTP")
}
func TestHTTPGenPriv(t *testing.T) {
testGenPriv(t, "HTTP")
}
func TestHTTPGetAccount(t *testing.T) {
testGetAccount(t, "HTTP")
}
func TestHTTPSignedTx(t *testing.T) {
testSignedTx(t, "HTTP")
}
func TestHTTPBroadcastTx(t *testing.T) {
testBroadcastTx(t, "HTTP")
}
func TestHTTPGetStorage(t *testing.T) {
testGetStorage(t, "HTTP")
}
//--------------------------------------------------------------------------------
// Test the JSONRPC client
func TestJSONStatus(t *testing.T) {
testStatus(t, "JSONRPC")
}
func TestJSONGenPriv(t *testing.T) {
testGenPriv(t, "JSONRPC")
}
func TestJSONGetAccount(t *testing.T) {
testGetAccount(t, "JSONRPC")
}
func TestJSONSignedTx(t *testing.T) {
testSignedTx(t, "JSONRPC")
}
func TestJSONBroadcastTx(t *testing.T) {
testBroadcastTx(t, "JSONRPC")
}
func TestJSONGetStorage(t *testing.T) {
testGetStorage(t, "JSONRPC")
}

View File

@ -4,7 +4,6 @@ import (
"bytes"
"encoding/hex"
"github.com/tendermint/tendermint/account"
"github.com/tendermint/tendermint/binary"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/daemon"
"github.com/tendermint/tendermint/logger"
@ -13,12 +12,10 @@ import (
"github.com/tendermint/tendermint/rpc/core"
"github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
"io/ioutil"
"net/http"
"net/url"
"testing"
)
// global variables for use across all tests
var (
rpcAddr = "127.0.0.1:8089"
requestAddr = "http://" + rpcAddr + "/"
@ -209,17 +206,6 @@ func dumpStorage(t *testing.T, addr []byte) core.ResponseDumpStorage {
t.Fatal(err)
}
return *resp
/*addrString := "\"" + hex.EncodeToString(addr) + "\""
var response struct {
Result core.ResponseDumpStorage `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
requestResponse(t, "dump_storage", url.Values{"address": {addrString}}, &response)
if response.Error != "" {
t.Fatal(response.Error)
}*/
}
func getStorage(t *testing.T, typ string, addr, slot []byte) []byte {
@ -252,21 +238,3 @@ func checkTx(t *testing.T, fromAddr []byte, priv *account.PrivAccount, tx *types
t.Fatal(types.ErrTxInvalidSignature)
}
}
//--------------------------------------------------------------------------------
func requestResponse(t *testing.T, method string, values url.Values, response interface{}) {
resp, err := http.PostForm(requestAddr+method, values)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
binary.ReadJSON(response, body, &err)
if err != nil {
t.Fatal(err)
}
}

View File

@ -1,177 +0,0 @@
package rpc
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/rpc"
"github.com/tendermint/tendermint/rpc/core"
"github.com/tendermint/tendermint/types"
"io/ioutil"
"net/http"
"net/url"
"testing"
)
func TestJSONStatus(t *testing.T) {
s := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "status",
Params: []interface{}{},
Id: 0,
}
b, err := json.Marshal(s)
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(b)
resp, err := http.Post(requestAddr, "text/json", buf)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
var response struct {
Result core.ResponseStatus `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
t.Fatal(err)
}
if response.Result.Network != config.App().GetString("Network") {
t.Fatal(fmt.Errorf("Network mismatch: got %s expected %s",
response.Result.Network, config.App().Get("Network")))
}
}
func TestJSONGenPriv(t *testing.T) {
s := rpc.RPCRequest{
JSONRPC: "2.0",
Method: "unsafe/gen_priv_account",
Params: []interface{}{},
Id: 0,
}
b, err := json.Marshal(s)
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(b)
resp, err := http.Post(requestAddr, "text/json", buf)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatal(resp)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
var response struct {
Result core.ResponseGenPrivAccount `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
binary.ReadJSON(&response, body, &err)
if err != nil {
t.Fatal(err)
}
if len(response.Result.PrivAccount.Address) == 0 {
t.Fatal("Failed to generate an address")
}
}
func TestJSONGetAccount(t *testing.T) {
byteAddr, _ := hex.DecodeString(userAddr)
acc := getAccount(t, "JSONRPC", byteAddr)
if bytes.Compare(acc.Address, byteAddr) != 0 {
t.Fatalf("Failed to get correct account. Got %x, expected %x", acc.Address, byteAddr)
}
}
func TestJSONSignedTx(t *testing.T) {
byteAddr, _ := hex.DecodeString(userAddr)
var byteKey [64]byte
oh, _ := hex.DecodeString(userPriv)
copy(byteKey[:], oh)
amt := uint64(100)
toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
tx, priv := signTx(t, "JSONRPC", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
checkTx(t, byteAddr, priv, tx.(*types.SendTx))
toAddr = []byte{20, 143, 24, 63, 16, 17, 83, 29, 90, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
tx, priv = signTx(t, "JSONRPC", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
checkTx(t, byteAddr, priv, tx.(*types.SendTx))
toAddr = []byte{0, 0, 4, 0, 0, 4, 0, 0, 4, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
tx, priv = signTx(t, "JSONRPC", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
checkTx(t, byteAddr, priv, tx.(*types.SendTx))
}
func TestJSONBroadcastTx(t *testing.T) {
byteAddr, _ := hex.DecodeString(userAddr)
var byteKey [64]byte
oh, _ := hex.DecodeString(userPriv)
copy(byteKey[:], oh)
amt := uint64(100)
toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
tx, priv := signTx(t, "JSONRPC", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
checkTx(t, byteAddr, priv, tx.(*types.SendTx))
n, w := new(int64), new(bytes.Buffer)
var err error
binary.WriteJSON(tx, w, n, &err)
if err != nil {
t.Fatal(err)
}
b := w.Bytes()
var response struct {
Result core.ResponseBroadcastTx `json:"result"`
Error string `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
requestResponse(t, "broadcast_tx", url.Values{"tx": {string(b)}}, &response)
if response.Error != "" {
t.Fatal(response.Error)
}
receipt := response.Result.Receipt
if receipt.CreatesContract > 0 {
t.Fatal("This tx does not create a contract")
}
if len(receipt.TxHash) == 0 {
t.Fatal("Failed to compute tx hash")
}
pool := node.MempoolReactor().Mempool
txs := pool.GetProposalTxs()
if len(txs) != mempoolCount+1 {
t.Fatalf("The mem pool has %d txs. Expected %d", len(txs), mempoolCount+1)
}
tx2 := txs[mempoolCount].(*types.SendTx)
mempoolCount += 1
if bytes.Compare(types.TxId(tx), types.TxId(tx2)) != 0 {
t.Fatal(Fmt("inconsistent hashes for mempool tx and sent tx: %v vs %v", tx, tx2))
}
}
/*tx.Inputs[0].Signature = mint.priv.PrivKey.Sign(account.SignBytes(tx))
err = mint.MempoolReactor.BroadcastTx(tx)
return hex.EncodeToString(merkle.HashFromBinary(tx)), err*/

View File

@ -5,45 +5,50 @@ import (
"encoding/hex"
"fmt"
. "github.com/tendermint/tendermint/common"
"github.com/tendermint/tendermint/merkle"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/types"
"testing"
"time"
)
func TestHTTPStatus(t *testing.T) {
client := clients["HTTP"]
func testStatus(t *testing.T, typ string) {
client := clients[typ]
resp, err := client.Status()
if err != nil {
t.Fatal(err)
}
fmt.Println(">>>", resp)
return
if resp.Network != config.App().GetString("Network") {
t.Fatal(fmt.Errorf("Network mismatch: got %s expected %s",
resp.Network, config.App().Get("Network")))
}
}
func TestHTTPGenPriv(t *testing.T) {
client := clients["HTTP"]
func testGenPriv(t *testing.T, typ string) {
client := clients[typ]
resp, err := client.GenPrivAccount()
if err != nil {
t.Fatal(err)
}
fmt.Println(">>>", resp)
if len(resp.PrivAccount.Address) == 0 {
t.Fatal("Failed to generate an address")
}
}
func TestHTTPGetAccount(t *testing.T) {
func testGetAccount(t *testing.T, typ string) {
byteAddr, _ := hex.DecodeString(userAddr)
acc := getAccount(t, "HTTP", byteAddr)
acc := getAccount(t, typ, byteAddr)
if acc == nil {
t.Fatalf("Account was nil")
}
if bytes.Compare(acc.Address, byteAddr) != 0 {
t.Fatalf("Failed to get correct account. Got %x, expected %x", acc.Address, byteAddr)
}
}
func TestHTTPSignedTx(t *testing.T) {
func testSignedTx(t *testing.T, typ string) {
byteAddr, _ := hex.DecodeString(userAddr)
var byteKey [64]byte
oh, _ := hex.DecodeString(userPriv)
@ -51,19 +56,19 @@ func TestHTTPSignedTx(t *testing.T) {
amt := uint64(100)
toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
tx, priv := signTx(t, "HTTP", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
tx, priv := signTx(t, typ, byteAddr, toAddr, nil, byteKey, amt, 0, 0)
checkTx(t, byteAddr, priv, tx.(*types.SendTx))
toAddr = []byte{20, 143, 24, 63, 16, 17, 83, 29, 90, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
tx, priv = signTx(t, "HTTP", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
tx, priv = signTx(t, typ, byteAddr, toAddr, nil, byteKey, amt, 0, 0)
checkTx(t, byteAddr, priv, tx.(*types.SendTx))
toAddr = []byte{0, 0, 4, 0, 0, 4, 0, 0, 4, 91, 52, 2, 0, 41, 190, 121, 122, 34, 86, 54}
tx, priv = signTx(t, "HTTP", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
tx, priv = signTx(t, typ, byteAddr, toAddr, nil, byteKey, amt, 0, 0)
checkTx(t, byteAddr, priv, tx.(*types.SendTx))
}
func TestHTTPBroadcastTx(t *testing.T) {
func testBroadcastTx(t *testing.T, typ string) {
byteAddr, _ := hex.DecodeString(userAddr)
var byteKey [64]byte
oh, _ := hex.DecodeString(userPriv)
@ -71,7 +76,7 @@ func TestHTTPBroadcastTx(t *testing.T) {
amt := uint64(100)
toAddr := []byte{20, 143, 25, 63, 16, 177, 83, 29, 91, 91, 54, 23, 233, 46, 190, 121, 122, 34, 86, 54}
tx, receipt := broadcastTx(t, "HTTP", byteAddr, toAddr, nil, byteKey, amt, 0, 0)
tx, receipt := broadcastTx(t, typ, byteAddr, toAddr, nil, byteKey, amt, 0, 0)
if receipt.CreatesContract > 0 {
t.Fatal("This tx does not create a contract")
}
@ -85,13 +90,16 @@ func TestHTTPBroadcastTx(t *testing.T) {
}
tx2 := txs[mempoolCount].(*types.SendTx)
mempoolCount += 1
if bytes.Compare(merkle.HashFromBinary(tx), merkle.HashFromBinary(tx2)) != 0 {
n, err := new(int64), new(error)
buf1, buf2 := new(bytes.Buffer), new(bytes.Buffer)
tx.WriteSignBytes(buf1, n, err)
tx2.WriteSignBytes(buf2, n, err)
if bytes.Compare(buf1.Bytes(), buf2.Bytes()) != 0 {
t.Fatal("inconsistent hashes for mempool tx and sent tx")
}
}
func TestHTTPGetStorage(t *testing.T) {
func testGetStorage(t *testing.T, typ string) {
priv := state.LoadPrivValidator(".tendermint/priv_validator.json")
_ = priv
//core.SetPrivValidator(priv)
@ -103,7 +111,7 @@ func TestHTTPGetStorage(t *testing.T) {
amt := uint64(1100)
code := []byte{0x60, 0x5, 0x60, 0x1, 0x55}
_, receipt := broadcastTx(t, "HTTP", byteAddr, nil, code, byteKey, amt, 1000, 1000)
_, receipt := broadcastTx(t, typ, byteAddr, nil, code, byteKey, amt, 1000, 1000)
if receipt.CreatesContract == 0 {
t.Fatal("This tx creates a contract")
}
@ -117,7 +125,7 @@ func TestHTTPGetStorage(t *testing.T) {
time.Sleep(time.Second * 20)
mempoolCount -= 1
v := getStorage(t, "HTTP", contractAddr, []byte{0x1})
v := getStorage(t, typ, contractAddr, []byte{0x1})
got := RightPadWord256(v)
expected := RightPadWord256([]byte{0x5})
if got.Compare(expected) != 0 {