github.com/turingchain2020/turingchain@v1.1.21/rpc/server_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rpc 6 7 import ( 8 "errors" 9 "net/http" 10 "testing" 11 "time" 12 13 "github.com/turingchain2020/turingchain/client/mocks" 14 "github.com/turingchain2020/turingchain/common" 15 qmocks "github.com/turingchain2020/turingchain/queue/mocks" 16 "github.com/turingchain2020/turingchain/rpc/jsonclient" 17 rpctypes "github.com/turingchain2020/turingchain/rpc/types" 18 "github.com/turingchain2020/turingchain/types" 19 "github.com/stretchr/testify/assert" 20 "github.com/stretchr/testify/mock" 21 "golang.org/x/net/context" 22 "google.golang.org/grpc" 23 ) 24 25 func TestCheckIpWhitelist(t *testing.T) { 26 address := "127.0.0.1" 27 assert.True(t, checkIPWhitelist(address)) 28 29 address = "::1" 30 assert.True(t, checkIPWhitelist(address)) 31 32 address = "192.168.3.1" 33 remoteIPWhitelist[address] = true 34 assert.False(t, checkIPWhitelist("192.168.3.2")) 35 36 remoteIPWhitelist["0.0.0.0"] = true 37 assert.True(t, checkIPWhitelist(address)) 38 assert.True(t, checkIPWhitelist("192.168.3.2")) 39 40 } 41 42 func TestCheckBasicAuth(t *testing.T) { 43 rpcCfg = new(types.RPC) 44 var r = &http.Request{Header: make(http.Header)} 45 assert.True(t, checkBasicAuth(r)) 46 r.SetBasicAuth("1212121", "turingchain-mypasswd") 47 assert.True(t, checkBasicAuth(r)) 48 rpcCfg.JrpcUserName = "turingchain-user" 49 rpcCfg.JrpcUserPasswd = "turingchain-mypasswd" 50 r.SetBasicAuth("", "turingchain-mypasswd") 51 assert.False(t, checkBasicAuth(r)) 52 r.SetBasicAuth("", "") 53 assert.False(t, checkBasicAuth(r)) 54 r.SetBasicAuth("turingchain-user", "") 55 assert.False(t, checkBasicAuth(r)) 56 r.SetBasicAuth("turingchain", "1234") 57 assert.False(t, checkBasicAuth(r)) 58 r.SetBasicAuth("turingchain-user", "turingchain-mypasswd") 59 assert.True(t, checkBasicAuth(r)) 60 61 } 62 63 func TestJSONClient_Call(t *testing.T) { 64 rpcCfg = new(types.RPC) 65 rpcCfg.GrpcBindAddr = "127.0.0.1:8101" 66 rpcCfg.JrpcBindAddr = "127.0.0.1:8200" 67 rpcCfg.Whitelist = []string{"127.0.0.1", "0.0.0.0"} 68 rpcCfg.JrpcFuncWhitelist = []string{"*"} 69 rpcCfg.GrpcFuncWhitelist = []string{"*"} 70 InitCfg(rpcCfg) 71 api := new(mocks.QueueProtocolAPI) 72 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 73 api.On("GetConfig", mock.Anything).Return(cfg) 74 qm := &qmocks.Client{} 75 qm.On("GetConfig", mock.Anything).Return(cfg) 76 server := NewJSONRPCServer(qm, api) 77 assert.NotNil(t, server) 78 done := make(chan struct{}, 1) 79 go func() { 80 done <- struct{}{} 81 server.Listen() 82 }() 83 <-done 84 time.Sleep(time.Millisecond) 85 ret := &types.Reply{ 86 IsOk: true, 87 Msg: []byte("123"), 88 } 89 api.On("IsSync").Return(ret, nil) 90 api.On("Close").Return() 91 92 jsonClient, err := jsonclient.NewJSONClient("http://" + rpcCfg.JrpcBindAddr + "/root") 93 assert.Nil(t, err) 94 assert.NotNil(t, jsonClient) 95 96 var result = "" 97 err = jsonClient.Call("Turingchain.Version", nil, &result) 98 assert.NotNil(t, err) 99 assert.Empty(t, result) 100 101 jsonClient, err = jsonclient.NewJSONClient("http://" + rpcCfg.JrpcBindAddr) 102 assert.Nil(t, err) 103 assert.NotNil(t, jsonClient) 104 105 ver := &types.VersionInfo{Turingchain: "6.0.2"} 106 api.On("Version").Return(ver, nil) 107 var nodeVersion types.VersionInfo 108 err = jsonClient.Call("Turingchain.Version", nil, &nodeVersion) 109 assert.Nil(t, err) 110 assert.Equal(t, "6.0.2", nodeVersion.Turingchain) 111 112 var isSnyc bool 113 err = jsonClient.Call("Turingchain.IsSync", &types.ReqNil{}, &isSnyc) 114 assert.Nil(t, err) 115 assert.Equal(t, ret.GetIsOk(), isSnyc) 116 var nodeInfo rpctypes.NodeNetinfo 117 api.On("GetNetInfo", mock.Anything).Return(&types.NodeNetInfo{Externaladdr: "123"}, nil) 118 err = jsonClient.Call("Turingchain.GetNetInfo", &types.ReqNil{}, &nodeInfo) 119 assert.Nil(t, err) 120 assert.Equal(t, "123", nodeInfo.Externaladdr) 121 122 var singRet = "" 123 api.On("ExecWalletFunc", "wallet", "SignRawTx", mock.Anything).Return(&types.ReplySignRawTx{TxHex: "123"}, nil) 124 err = jsonClient.Call("Turingchain.SignRawTx", &types.ReqSignRawTx{}, &singRet) 125 assert.Nil(t, err) 126 assert.Equal(t, "123", singRet) 127 128 var fee types.TotalFee 129 api.On("LocalGet", mock.Anything).Return(nil, errors.New("error value")) 130 err = jsonClient.Call("Turingchain.QueryTotalFee", &types.LocalDBGet{Keys: [][]byte{[]byte("test")}}, &fee) 131 assert.NotNil(t, err) 132 133 var retNtp bool 134 api.On("IsNtpClockSync", mock.Anything).Return(&types.Reply{IsOk: true, Msg: []byte("yes")}, nil) 135 err = jsonClient.Call("Turingchain.IsNtpClockSync", &types.ReqNil{}, &retNtp) 136 assert.Nil(t, err) 137 assert.True(t, retNtp) 138 api.On("GetProperFee", mock.Anything).Return(&types.ReplyProperFee{ProperFee: 2}, nil) 139 testCreateTxCoins(t, cfg, jsonClient) 140 server.Close() 141 mock.AssertExpectationsForObjects(t, api) 142 } 143 144 func testDecodeTxHex(t *testing.T, txHex string) *types.Transaction { 145 txbytes, err := common.FromHex(txHex) 146 assert.Nil(t, err) 147 var tx types.Transaction 148 err = types.Decode(txbytes, &tx) 149 assert.Nil(t, err) 150 return &tx 151 } 152 153 func testCreateTxCoins(t *testing.T, cfg *types.TuringchainConfig, jsonClient *jsonclient.JSONClient) { 154 req := &rpctypes.CreateTx{ 155 To: "184wj4nsgVxKyz2NhM3Yb5RK5Ap6AFRFq2", 156 Amount: 10, 157 Fee: 1, 158 Note: "12312", 159 IsWithdraw: false, 160 IsToken: false, 161 TokenSymbol: "", 162 ExecName: cfg.ExecName("coins"), 163 } 164 var res string 165 err := jsonClient.Call("Turingchain.CreateRawTransaction", req, &res) 166 assert.Nil(t, err) 167 tx := testDecodeTxHex(t, res) 168 assert.Equal(t, "184wj4nsgVxKyz2NhM3Yb5RK5Ap6AFRFq2", tx.To) 169 assert.Equal(t, int64(1), tx.Fee) 170 req.Fee = 0 171 err = jsonClient.Call("Turingchain.CreateRawTransaction", req, &res) 172 assert.Nil(t, err) 173 tx = testDecodeTxHex(t, res) 174 fee, _ := tx.GetRealFee(2) 175 assert.Equal(t, fee, tx.Fee) 176 } 177 178 func TestGrpc_Call(t *testing.T) { 179 rpcCfg := new(types.RPC) 180 rpcCfg.GrpcBindAddr = "127.0.0.1:8101" 181 rpcCfg.JrpcBindAddr = "127.0.0.1:8200" 182 rpcCfg.Whitelist = []string{"127.0.0.1", "0.0.0.0"} 183 rpcCfg.JrpcFuncWhitelist = []string{"*"} 184 rpcCfg.GrpcFuncWhitelist = []string{"*"} 185 InitCfg(rpcCfg) 186 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 187 api := new(mocks.QueueProtocolAPI) 188 api.On("GetConfig", mock.Anything).Return(cfg) 189 _ = NewGrpcServer() 190 qm := &qmocks.Client{} 191 qm.On("GetConfig", mock.Anything).Return(cfg) 192 server := NewGRpcServer(qm, api) 193 assert.NotNil(t, server) 194 go server.Listen() 195 time.Sleep(time.Second) 196 ret := &types.Reply{ 197 IsOk: true, 198 Msg: []byte("123"), 199 } 200 api.On("IsSync").Return(ret, nil) 201 api.On("Close").Return() 202 203 ctx := context.Background() 204 c, err := grpc.DialContext(ctx, rpcCfg.GrpcBindAddr, grpc.WithInsecure()) 205 assert.Nil(t, err) 206 assert.NotNil(t, c) 207 208 client := types.NewTuringchainClient(c) 209 result, err := client.IsSync(ctx, &types.ReqNil{}) 210 211 assert.Nil(t, err) 212 assert.Equal(t, ret.IsOk, result.IsOk) 213 assert.Equal(t, ret.Msg, result.Msg) 214 215 rst, err := client.GetFork(ctx, &types.ReqKey{Key: []byte("ForkBlockHash")}) 216 assert.Nil(t, err) 217 assert.Equal(t, int64(1), rst.Data) 218 219 api.On("GetBlockBySeq", mock.Anything).Return(&types.BlockSeq{}, nil) 220 blockSeq, err := client.GetBlockBySeq(ctx, &types.Int64{Data: 1}) 221 assert.Nil(t, err) 222 assert.Equal(t, &types.BlockSeq{}, blockSeq) 223 224 server.Close() 225 mock.AssertExpectationsForObjects(t, api) 226 } 227 228 func TestRPC(t *testing.T) { 229 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 230 rpcCfg := cfg.GetModuleConfig().RPC 231 rpcCfg.JrpcBindAddr = "9671" 232 rpcCfg.GrpcBindAddr = "9672" 233 rpcCfg.Whitlist = []string{"127.0.0.1"} 234 rpcCfg.JrpcFuncBlacklist = []string{"CloseQueue"} 235 rpcCfg.GrpcFuncBlacklist = []string{"CloseQueue"} 236 rpcCfg.EnableTrace = true 237 InitCfg(rpcCfg) 238 rpc := New(cfg) 239 client := &qmocks.Client{} 240 client.On("GetConfig", mock.Anything).Return(cfg) 241 rpc.SetQueueClient(client) 242 243 assert.Equal(t, client, rpc.GetQueueClient()) 244 assert.NotNil(t, rpc.GRPC()) 245 assert.NotNil(t, rpc.JRPC()) 246 } 247 248 func TestCheckFuncList(t *testing.T) { 249 funcName := "abc" 250 jrpcFuncWhitelist = make(map[string]bool) 251 assert.False(t, checkJrpcFuncWhitelist(funcName)) 252 jrpcFuncWhitelist["*"] = true 253 assert.True(t, checkJrpcFuncWhitelist(funcName)) 254 255 delete(jrpcFuncWhitelist, "*") 256 jrpcFuncWhitelist[funcName] = true 257 assert.True(t, checkJrpcFuncWhitelist(funcName)) 258 259 grpcFuncWhitelist = make(map[string]bool) 260 assert.False(t, checkGrpcFuncWhitelist(funcName)) 261 grpcFuncWhitelist["*"] = true 262 assert.True(t, checkGrpcFuncWhitelist(funcName)) 263 264 delete(grpcFuncWhitelist, "*") 265 grpcFuncWhitelist[funcName] = true 266 assert.True(t, checkGrpcFuncWhitelist(funcName)) 267 268 jrpcFuncBlacklist = make(map[string]bool) 269 assert.False(t, checkJrpcFuncBlacklist(funcName)) 270 jrpcFuncBlacklist[funcName] = true 271 assert.True(t, checkJrpcFuncBlacklist(funcName)) 272 273 grpcFuncBlacklist = make(map[string]bool) 274 assert.False(t, checkGrpcFuncBlacklist(funcName)) 275 grpcFuncBlacklist[funcName] = true 276 assert.True(t, checkGrpcFuncBlacklist(funcName)) 277 278 }