2016-02-18 13:07:49 -08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
2017-03-09 01:46:48 -08:00
|
|
|
"bytes"
|
|
|
|
crand "crypto/rand"
|
2017-03-10 00:52:40 -08:00
|
|
|
"fmt"
|
2017-03-09 01:46:48 -08:00
|
|
|
"math/rand"
|
2016-02-18 13:07:49 -08:00
|
|
|
"net/http"
|
2017-03-07 07:09:58 -08:00
|
|
|
"os/exec"
|
2016-02-18 13:07:49 -08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-03-10 00:03:16 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-03-07 07:04:24 -08:00
|
|
|
client "github.com/tendermint/go-rpc/client"
|
|
|
|
server "github.com/tendermint/go-rpc/server"
|
|
|
|
types "github.com/tendermint/go-rpc/types"
|
2017-03-07 06:34:54 -08:00
|
|
|
wire "github.com/tendermint/go-wire"
|
2016-02-18 13:07:49 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client and Server should work over tcp or unix sockets
|
2017-03-07 07:04:24 -08:00
|
|
|
const (
|
2017-03-07 07:09:58 -08:00
|
|
|
tcpAddr = "tcp://0.0.0.0:46657"
|
|
|
|
|
|
|
|
unixSocket = "/tmp/go-rpc.sock"
|
|
|
|
unixAddr = "unix:///tmp/go-rpc.sock"
|
2016-02-18 18:05:24 -08:00
|
|
|
|
|
|
|
websocketEndpoint = "/websocket/endpoint"
|
2016-02-18 13:07:49 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Define a type for results and register concrete versions
|
|
|
|
type Result interface{}
|
|
|
|
|
2017-03-10 00:57:14 -08:00
|
|
|
type ResultEcho struct {
|
2016-02-18 13:07:49 -08:00
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2017-03-10 00:57:14 -08:00
|
|
|
type ResultEchoBytes struct {
|
2017-03-09 01:46:48 -08:00
|
|
|
Value []byte
|
|
|
|
}
|
|
|
|
|
2016-02-18 13:07:49 -08:00
|
|
|
var _ = wire.RegisterInterface(
|
|
|
|
struct{ Result }{},
|
2017-03-10 00:57:14 -08:00
|
|
|
wire.ConcreteType{&ResultEcho{}, 0x1},
|
|
|
|
wire.ConcreteType{&ResultEchoBytes{}, 0x2},
|
2016-02-18 13:07:49 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Define some routes
|
2017-03-07 07:04:24 -08:00
|
|
|
var Routes = map[string]*server.RPCFunc{
|
2017-03-10 00:57:14 -08:00
|
|
|
"echo": server.NewRPCFunc(EchoResult, "arg"),
|
|
|
|
"echo_ws": server.NewWSRPCFunc(EchoWSResult, "arg"),
|
|
|
|
"echo_bytes": server.NewRPCFunc(EchoBytesResult, "arg"),
|
2016-02-18 13:07:49 -08:00
|
|
|
}
|
|
|
|
|
2017-03-10 00:57:14 -08:00
|
|
|
func EchoResult(v string) (Result, error) {
|
|
|
|
return &ResultEcho{v}, nil
|
2016-02-18 13:07:49 -08:00
|
|
|
}
|
|
|
|
|
2017-03-10 00:57:14 -08:00
|
|
|
func EchoWSResult(wsCtx types.WSRPCContext, v string) (Result, error) {
|
|
|
|
return &ResultEcho{v}, nil
|
2017-03-09 06:30:55 -08:00
|
|
|
}
|
|
|
|
|
2017-03-10 00:57:14 -08:00
|
|
|
func EchoBytesResult(v []byte) (Result, error) {
|
|
|
|
return &ResultEchoBytes{v}, nil
|
2017-03-09 01:46:48 -08:00
|
|
|
}
|
|
|
|
|
2016-02-18 13:07:49 -08:00
|
|
|
// launch unix and tcp servers
|
|
|
|
func init() {
|
2017-03-07 07:09:58 -08:00
|
|
|
cmd := exec.Command("rm", "-f", unixSocket)
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = cmd.Wait()
|
2017-03-07 07:04:24 -08:00
|
|
|
|
2016-02-18 13:07:49 -08:00
|
|
|
mux := http.NewServeMux()
|
2017-03-07 07:04:24 -08:00
|
|
|
server.RegisterRPCFuncs(mux, Routes)
|
|
|
|
wm := server.NewWebsocketManager(Routes, nil)
|
2016-02-18 18:05:24 -08:00
|
|
|
mux.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
|
2016-02-18 13:07:49 -08:00
|
|
|
go func() {
|
2017-03-07 07:04:24 -08:00
|
|
|
_, err := server.StartHTTPServer(tcpAddr, mux)
|
2016-02-18 13:07:49 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-06-21 12:19:56 -07:00
|
|
|
mux2 := http.NewServeMux()
|
2017-03-07 07:04:24 -08:00
|
|
|
server.RegisterRPCFuncs(mux2, Routes)
|
|
|
|
wm = server.NewWebsocketManager(Routes, nil)
|
2016-06-21 12:19:56 -07:00
|
|
|
mux2.HandleFunc(websocketEndpoint, wm.WebsocketHandler)
|
2016-02-18 13:07:49 -08:00
|
|
|
go func() {
|
2017-03-07 07:04:24 -08:00
|
|
|
_, err := server.StartHTTPServer(unixAddr, mux2)
|
2016-02-18 13:07:49 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait for servers to start
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:56:04 -08:00
|
|
|
func echoViaHTTP(cl client.HTTPClient, val string) (string, error) {
|
2016-02-18 13:07:49 -08:00
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": val,
|
|
|
|
}
|
|
|
|
var result Result
|
2017-03-10 00:57:14 -08:00
|
|
|
if _, err := cl.Call("echo", params, &result); err != nil {
|
2017-03-10 00:52:40 -08:00
|
|
|
return "", err
|
|
|
|
}
|
2017-03-10 00:57:14 -08:00
|
|
|
return result.(*ResultEcho).Value, nil
|
2016-02-18 13:07:49 -08:00
|
|
|
}
|
|
|
|
|
2017-03-10 02:56:04 -08:00
|
|
|
func echoBytesViaHTTP(cl client.HTTPClient, bytes []byte) ([]byte, error) {
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": bytes,
|
|
|
|
}
|
|
|
|
var result Result
|
|
|
|
if _, err := cl.Call("echo_bytes", params, &result); err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return result.(*ResultEchoBytes).Value, nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 00:52:40 -08:00
|
|
|
func testWithHTTPClient(t *testing.T, cl client.HTTPClient) {
|
2016-02-18 13:07:49 -08:00
|
|
|
val := "acbd"
|
2017-03-10 02:56:04 -08:00
|
|
|
got, err := echoViaHTTP(cl, val)
|
2017-03-10 00:03:16 -08:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got, val)
|
2017-03-10 02:56:04 -08:00
|
|
|
|
|
|
|
val2 := randBytes(t)
|
|
|
|
got2, err := echoBytesViaHTTP(cl, val2)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got2, val2)
|
2016-02-18 13:07:49 -08:00
|
|
|
}
|
|
|
|
|
2017-03-10 02:56:04 -08:00
|
|
|
func echoViaWS(cl *client.WSClient, val string) (string, error) {
|
2017-03-07 06:34:54 -08:00
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": val,
|
|
|
|
}
|
2017-03-07 07:04:24 -08:00
|
|
|
err := cl.WriteJSON(types.RPCRequest{
|
2016-02-18 18:05:24 -08:00
|
|
|
JSONRPC: "2.0",
|
|
|
|
ID: "",
|
2017-03-10 00:57:14 -08:00
|
|
|
Method: "echo",
|
2016-02-18 18:05:24 -08:00
|
|
|
Params: params,
|
|
|
|
})
|
2017-03-10 02:56:04 -08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-02-18 18:05:24 -08:00
|
|
|
|
2017-03-09 05:44:00 -08:00
|
|
|
select {
|
|
|
|
case msg := <-cl.ResultsCh:
|
|
|
|
result := new(Result)
|
|
|
|
wire.ReadJSONPtr(result, msg, &err)
|
2017-03-10 02:56:04 -08:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return (*result).(*ResultEcho).Value, nil
|
2017-03-09 05:44:00 -08:00
|
|
|
case err := <-cl.ErrorsCh:
|
2017-03-10 02:56:04 -08:00
|
|
|
return "", err
|
2016-02-18 18:05:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:56:04 -08:00
|
|
|
func echoBytesViaWS(cl *client.WSClient, bytes []byte) ([]byte, error) {
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": bytes,
|
|
|
|
}
|
|
|
|
err := cl.WriteJSON(types.RPCRequest{
|
|
|
|
JSONRPC: "2.0",
|
|
|
|
ID: "",
|
|
|
|
Method: "echo_bytes",
|
|
|
|
Params: params,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case msg := <-cl.ResultsCh:
|
|
|
|
result := new(Result)
|
|
|
|
wire.ReadJSONPtr(result, msg, &err)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
return (*result).(*ResultEchoBytes).Value, nil
|
|
|
|
case err := <-cl.ErrorsCh:
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWithWSClient(t *testing.T, cl *client.WSClient) {
|
|
|
|
val := "acbd"
|
|
|
|
got, err := echoViaWS(cl, val)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got, val)
|
|
|
|
|
|
|
|
val2 := randBytes(t)
|
|
|
|
got2, err := echoBytesViaWS(cl, val2)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got2, val2)
|
|
|
|
}
|
|
|
|
|
2016-02-18 18:05:24 -08:00
|
|
|
//-------------
|
|
|
|
|
2017-03-10 00:52:40 -08:00
|
|
|
func TestServersAndClientsBasic(t *testing.T) {
|
|
|
|
serverAddrs := [...]string{tcpAddr, unixAddr}
|
|
|
|
for _, addr := range serverAddrs {
|
|
|
|
cl1 := client.NewURIClient(addr)
|
|
|
|
fmt.Printf("=== testing server on %s using %v client", addr, cl1)
|
|
|
|
testWithHTTPClient(t, cl1)
|
2016-02-18 13:07:49 -08:00
|
|
|
|
2017-03-10 00:52:40 -08:00
|
|
|
cl2 := client.NewJSONRPCClient(tcpAddr)
|
|
|
|
fmt.Printf("=== testing server on %s using %v client", addr, cl2)
|
|
|
|
testWithHTTPClient(t, cl2)
|
2016-02-18 18:05:24 -08:00
|
|
|
|
2017-03-10 00:52:40 -08:00
|
|
|
cl3 := client.NewWSClient(tcpAddr, websocketEndpoint)
|
|
|
|
_, err := cl3.Start()
|
|
|
|
require.Nil(t, err)
|
|
|
|
fmt.Printf("=== testing server on %s using %v client", addr, cl3)
|
|
|
|
testWithWSClient(t, cl3)
|
|
|
|
cl3.Stop()
|
|
|
|
}
|
2016-02-18 18:05:24 -08:00
|
|
|
}
|
2017-01-02 09:50:20 -08:00
|
|
|
|
|
|
|
func TestHexStringArg(t *testing.T) {
|
2017-03-07 13:22:35 -08:00
|
|
|
cl := client.NewURIClient(tcpAddr)
|
2017-01-02 09:50:20 -08:00
|
|
|
// should NOT be handled as hex
|
|
|
|
val := "0xabc"
|
2017-03-10 02:56:04 -08:00
|
|
|
got, err := echoViaHTTP(cl, val)
|
2017-03-10 00:03:16 -08:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got, val)
|
2017-01-02 09:50:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuotedStringArg(t *testing.T) {
|
2017-03-07 13:22:35 -08:00
|
|
|
cl := client.NewURIClient(tcpAddr)
|
2017-01-02 09:50:20 -08:00
|
|
|
// should NOT be unquoted
|
|
|
|
val := "\"abc\""
|
2017-03-10 02:56:04 -08:00
|
|
|
got, err := echoViaHTTP(cl, val)
|
2017-03-10 00:03:16 -08:00
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, got, val)
|
2017-01-02 09:50:20 -08:00
|
|
|
}
|
2017-03-09 01:46:48 -08:00
|
|
|
|
2017-03-09 06:30:55 -08:00
|
|
|
func TestWSNewWSRPCFunc(t *testing.T) {
|
2017-03-10 02:56:04 -08:00
|
|
|
cl := client.NewWSClient(tcpAddr, websocketEndpoint)
|
2017-03-09 06:30:55 -08:00
|
|
|
_, err := cl.Start()
|
2017-03-10 00:03:16 -08:00
|
|
|
require.Nil(t, err)
|
2017-03-09 06:30:55 -08:00
|
|
|
defer cl.Stop()
|
|
|
|
|
|
|
|
val := "acbd"
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"arg": val,
|
|
|
|
}
|
|
|
|
err = cl.WriteJSON(types.RPCRequest{
|
|
|
|
JSONRPC: "2.0",
|
|
|
|
ID: "",
|
2017-03-10 00:57:14 -08:00
|
|
|
Method: "echo_ws",
|
2017-03-09 06:30:55 -08:00
|
|
|
Params: params,
|
|
|
|
})
|
2017-03-10 00:03:16 -08:00
|
|
|
require.Nil(t, err)
|
2017-03-09 06:30:55 -08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case msg := <-cl.ResultsCh:
|
|
|
|
result := new(Result)
|
|
|
|
wire.ReadJSONPtr(result, msg, &err)
|
2017-03-10 00:03:16 -08:00
|
|
|
require.Nil(t, err)
|
2017-03-10 00:57:14 -08:00
|
|
|
got := (*result).(*ResultEcho).Value
|
2017-03-10 00:03:16 -08:00
|
|
|
assert.Equal(t, got, val)
|
2017-03-09 06:30:55 -08:00
|
|
|
case err := <-cl.ErrorsCh:
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 02:56:04 -08:00
|
|
|
|
|
|
|
func randBytes(t *testing.T) []byte {
|
|
|
|
n := rand.Intn(10) + 2
|
|
|
|
buf := make([]byte, n)
|
|
|
|
_, err := crand.Read(buf)
|
|
|
|
require.Nil(t, err)
|
|
|
|
return bytes.Replace(buf, []byte("="), []byte{100}, -1)
|
|
|
|
}
|