github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/rpc/server_test.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rpc
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"net"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/ethereumproject/go-ethereum/logger/glog"
    27  )
    28  
    29  type Service struct{}
    30  
    31  type Args struct {
    32  	S string
    33  }
    34  
    35  func init() {
    36  	glog.SetD(0)
    37  	glog.SetV(0)
    38  }
    39  
    40  func (s *Service) NoArgsRets() {
    41  }
    42  
    43  type Result struct {
    44  	String string
    45  	Int    int
    46  	Args   *Args
    47  }
    48  
    49  func (s *Service) Echo(str string, i int, args *Args) Result {
    50  	return Result{str, i, args}
    51  }
    52  
    53  func (s *Service) EchoWithCtx(ctx context.Context, str string, i int, args *Args) Result {
    54  	return Result{str, i, args}
    55  }
    56  
    57  func (s *Service) Rets() (string, error) {
    58  	return "", nil
    59  }
    60  
    61  func (s *Service) InvalidRets1() (error, string) {
    62  	return nil, ""
    63  }
    64  
    65  func (s *Service) InvalidRets2() (string, string) {
    66  	return "", ""
    67  }
    68  
    69  func (s *Service) InvalidRets3() (string, string, error) {
    70  	return "", "", nil
    71  }
    72  
    73  func (s *Service) Subscription(ctx context.Context) (Subscription, error) {
    74  	return nil, nil
    75  }
    76  
    77  func TestServerRegisterName(t *testing.T) {
    78  	server := NewServer()
    79  	service := new(Service)
    80  
    81  	if err := server.RegisterName("calc", service); err != nil {
    82  		t.Fatalf("%v", err)
    83  	}
    84  
    85  	if len(server.services) != 2 {
    86  		t.Fatalf("Expected 2 service entries, got %d", len(server.services))
    87  	}
    88  
    89  	svc, ok := server.services["calc"]
    90  	if !ok {
    91  		t.Fatalf("Expected service calc to be registered")
    92  	}
    93  
    94  	if len(svc.callbacks) != 4 {
    95  		t.Errorf("Expected 4 callbacks for service 'calc', got %d", len(svc.callbacks))
    96  	}
    97  
    98  	if len(svc.subscriptions) != 1 {
    99  		t.Errorf("Expected 1 subscription for service 'calc', got %d", len(svc.subscriptions))
   100  	}
   101  }
   102  
   103  func testServerMethodExecution(t *testing.T, method string) {
   104  	server := NewServer()
   105  	service := new(Service)
   106  
   107  	if err := server.RegisterName("test", service); err != nil {
   108  		t.Fatalf("%v", err)
   109  	}
   110  
   111  	stringArg := "string arg"
   112  	intArg := 1122
   113  	argsArg := &Args{"abcde"}
   114  	params := []interface{}{stringArg, intArg, argsArg}
   115  
   116  	request := map[string]interface{}{
   117  		"id":      12345,
   118  		"method":  "test_" + method,
   119  		"version": "2.0",
   120  		"params":  params,
   121  	}
   122  
   123  	clientConn, serverConn := net.Pipe()
   124  	defer clientConn.Close()
   125  
   126  	go server.ServeCodec(NewJSONCodec(serverConn), OptionMethodInvocation)
   127  
   128  	out := json.NewEncoder(clientConn)
   129  	in := json.NewDecoder(clientConn)
   130  
   131  	if err := out.Encode(request); err != nil {
   132  		t.Fatal(err)
   133  	}
   134  
   135  	response := JSONResponse{Result: &Result{}}
   136  	if err := in.Decode(&response); err != nil {
   137  		t.Fatal(err)
   138  	}
   139  
   140  	if result, ok := response.Result.(*Result); ok {
   141  		if result.String != stringArg {
   142  			t.Errorf("expected %s, got : %s\n", stringArg, result.String)
   143  		}
   144  		if result.Int != intArg {
   145  			t.Errorf("expected %d, got %d\n", intArg, result.Int)
   146  		}
   147  		if !reflect.DeepEqual(result.Args, argsArg) {
   148  			t.Errorf("expected %v, got %v\n", argsArg, result)
   149  		}
   150  	} else {
   151  		t.Fatalf("invalid response: expected *Result - got: %T", response.Result)
   152  	}
   153  }
   154  
   155  func TestServerMethodExecution(t *testing.T) {
   156  	testServerMethodExecution(t, "echo")
   157  }
   158  
   159  func TestServerMethodWithCtx(t *testing.T) {
   160  	testServerMethodExecution(t, "echoWithCtx")
   161  }