Ejemplo n.º 1
0
// TestRpcCommand tests RpcCommand by generating some commands and
// trying to send them off.
func TestRpcCommand(t *testing.T) {
	user := "******"
	pass := "******"
	server := "invalid"
	var msg []byte
	_, err := btcjson.RpcCommand(user, pass, server, msg)
	if err == nil {
		t.Errorf("Should fail.")
	}
	msg, err = btcjson.CreateMessage("getinfo")
	if err != nil {
		t.Errorf("Cannot create valid json message")
	}
	_, err = btcjson.RpcCommand(user, pass, server, msg)
	if err == nil {
		t.Errorf("Should not connect to server.")
	}

	badMsg := []byte("{\"jsonrpc\":\"1.0\",\"id\":\"btcd\",\"method\":\"\"}")
	_, err = btcjson.RpcCommand(user, pass, server, badMsg)
	if err == nil {
		t.Errorf("Cannot have no method in msg..")
	}
	return
}
Ejemplo n.º 2
0
// send sends a JSON-RPC command to the specified RPC server and examines the
// results for various error conditions.  It either returns a valid result or
// an appropriate error.
func send(cfg *config, msg []byte) (interface{}, error) {
	var reply btcjson.Reply
	var err error
	if cfg.NoTLS || (cfg.RPCCert == "" && !cfg.TLSSkipVerify) {
		reply, err = btcjson.RpcCommand(cfg.RPCUser, cfg.RPCPassword,
			cfg.RPCServer, msg)
	} else {
		var pem []byte
		if cfg.RPCCert != "" {
			pem, err = ioutil.ReadFile(cfg.RPCCert)
			if err != nil {
				return nil, err
			}
		}
		reply, err = btcjson.TlsRpcCommand(cfg.RPCUser,
			cfg.RPCPassword, cfg.RPCServer, msg, pem,
			cfg.TLSSkipVerify)
	}
	if err != nil {
		return nil, err
	}

	if reply.Error != nil {
		return nil, reply.Error
	}

	return reply.Result, nil
}