You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
8.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package rpctest
  2. import (
  3. "bytes"
  4. crand "crypto/rand"
  5. "fmt"
  6. "math/rand"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. abci "github.com/tendermint/abci/types"
  12. . "github.com/tendermint/go-common"
  13. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. //--------------------------------------------------------------------------------
  17. // Test the HTTP client
  18. // These tests assume the dummy app
  19. //--------------------------------------------------------------------------------
  20. //--------------------------------------------------------------------------------
  21. // status
  22. func TestURIStatus(t *testing.T) {
  23. tmResult := new(ctypes.TMResult)
  24. _, err := GetURIClient().Call("status", map[string]interface{}{}, tmResult)
  25. require.Nil(t, err)
  26. testStatus(t, tmResult)
  27. }
  28. func TestJSONStatus(t *testing.T) {
  29. tmResult := new(ctypes.TMResult)
  30. _, err := GetJSONClient().Call("status", []interface{}{}, tmResult)
  31. require.Nil(t, err)
  32. testStatus(t, tmResult)
  33. }
  34. func testStatus(t *testing.T, statusI interface{}) {
  35. chainID := GetConfig().GetString("chain_id")
  36. tmRes := statusI.(*ctypes.TMResult)
  37. status := (*tmRes).(*ctypes.ResultStatus)
  38. assert.Equal(t, chainID, status.NodeInfo.Network)
  39. }
  40. //--------------------------------------------------------------------------------
  41. // broadcast tx sync
  42. // random bytes (excluding byte('='))
  43. func randBytes(t *testing.T) []byte {
  44. n := rand.Intn(10) + 2
  45. buf := make([]byte, n)
  46. _, err := crand.Read(buf)
  47. require.Nil(t, err)
  48. return bytes.Replace(buf, []byte("="), []byte{100}, -1)
  49. }
  50. func TestURIBroadcastTxSync(t *testing.T) {
  51. config.Set("block_size", 0)
  52. defer config.Set("block_size", -1)
  53. tmResult := new(ctypes.TMResult)
  54. tx := randBytes(t)
  55. _, err := GetURIClient().Call("broadcast_tx_sync", map[string]interface{}{"tx": tx}, tmResult)
  56. require.Nil(t, err)
  57. testBroadcastTxSync(t, tmResult, tx)
  58. }
  59. func TestJSONBroadcastTxSync(t *testing.T) {
  60. config.Set("block_size", 0)
  61. defer config.Set("block_size", -1)
  62. tmResult := new(ctypes.TMResult)
  63. tx := randBytes(t)
  64. _, err := GetJSONClient().Call("broadcast_tx_sync", []interface{}{tx}, tmResult)
  65. require.Nil(t, err)
  66. testBroadcastTxSync(t, tmResult, tx)
  67. }
  68. func testBroadcastTxSync(t *testing.T, resI interface{}, tx []byte) {
  69. tmRes := resI.(*ctypes.TMResult)
  70. res := (*tmRes).(*ctypes.ResultBroadcastTx)
  71. require.Equal(t, abci.CodeType_OK, res.Code)
  72. mem := node.MempoolReactor().Mempool
  73. require.Equal(t, 1, mem.Size())
  74. txs := mem.Reap(1)
  75. require.EqualValues(t, tx, txs[0])
  76. mem.Flush()
  77. }
  78. //--------------------------------------------------------------------------------
  79. // query
  80. func testTxKV(t *testing.T) ([]byte, []byte, []byte) {
  81. k := randBytes(t)
  82. v := randBytes(t)
  83. return k, v, []byte(Fmt("%s=%s", k, v))
  84. }
  85. func sendTx(t *testing.T) ([]byte, []byte) {
  86. tmResult := new(ctypes.TMResult)
  87. k, v, tx := testTxKV(t)
  88. _, err := GetJSONClient().Call("broadcast_tx_commit", []interface{}{tx}, tmResult)
  89. require.Nil(t, err)
  90. return k, v
  91. }
  92. func TestURIABCIQuery(t *testing.T) {
  93. k, v := sendTx(t)
  94. time.Sleep(time.Second)
  95. tmResult := new(ctypes.TMResult)
  96. _, err := GetURIClient().Call("abci_query", map[string]interface{}{"path": "", "data": k, "prove": false}, tmResult)
  97. require.Nil(t, err)
  98. testABCIQuery(t, tmResult, v)
  99. }
  100. func TestJSONABCIQuery(t *testing.T) {
  101. k, v := sendTx(t)
  102. tmResult := new(ctypes.TMResult)
  103. _, err := GetJSONClient().Call("abci_query", []interface{}{"", k, false}, tmResult)
  104. require.Nil(t, err)
  105. testABCIQuery(t, tmResult, v)
  106. }
  107. func testABCIQuery(t *testing.T, statusI interface{}, value []byte) {
  108. tmRes := statusI.(*ctypes.TMResult)
  109. resQuery := (*tmRes).(*ctypes.ResultABCIQuery)
  110. require.EqualValues(t, 0, resQuery.Response.Code)
  111. // XXX: specific to value returned by the dummy
  112. require.NotEqual(t, 0, len(resQuery.Response.Value))
  113. }
  114. //--------------------------------------------------------------------------------
  115. // broadcast tx commit
  116. func TestURIBroadcastTxCommit(t *testing.T) {
  117. tmResult := new(ctypes.TMResult)
  118. tx := randBytes(t)
  119. _, err := GetURIClient().Call("broadcast_tx_commit", map[string]interface{}{"tx": tx}, tmResult)
  120. require.Nil(t, err)
  121. testBroadcastTxCommit(t, tmResult, tx)
  122. }
  123. func TestJSONBroadcastTxCommit(t *testing.T) {
  124. tmResult := new(ctypes.TMResult)
  125. tx := randBytes(t)
  126. _, err := GetJSONClient().Call("broadcast_tx_commit", []interface{}{tx}, tmResult)
  127. require.Nil(t, err)
  128. testBroadcastTxCommit(t, tmResult, tx)
  129. }
  130. func testBroadcastTxCommit(t *testing.T, resI interface{}, tx []byte) {
  131. require := require.New(t)
  132. tmRes := resI.(*ctypes.TMResult)
  133. res := (*tmRes).(*ctypes.ResultBroadcastTxCommit)
  134. checkTx := res.CheckTx
  135. require.Equal(abci.CodeType_OK, checkTx.Code)
  136. deliverTx := res.DeliverTx
  137. require.Equal(abci.CodeType_OK, deliverTx.Code)
  138. mem := node.MempoolReactor().Mempool
  139. require.Equal(0, mem.Size())
  140. // TODO: find tx in block
  141. }
  142. //--------------------------------------------------------------------------------
  143. // Test the websocket service
  144. var wsTyp = "JSONRPC"
  145. // make a simple connection to the server
  146. func TestWSConnect(t *testing.T) {
  147. wsc := GetWSClient()
  148. wsc.Stop()
  149. }
  150. // receive a new block message
  151. func TestWSNewBlock(t *testing.T) {
  152. wsc := GetWSClient()
  153. eid := types.EventStringNewBlock()
  154. require.Nil(t, wsc.Subscribe(eid))
  155. defer func() {
  156. require.Nil(t, wsc.Unsubscribe(eid))
  157. wsc.Stop()
  158. }()
  159. waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error {
  160. // fmt.Println("Check:", b)
  161. return nil
  162. })
  163. }
  164. // receive a few new block messages in a row, with increasing height
  165. func TestWSBlockchainGrowth(t *testing.T) {
  166. if testing.Short() {
  167. t.Skip("skipping test in short mode.")
  168. }
  169. wsc := GetWSClient()
  170. eid := types.EventStringNewBlock()
  171. require.Nil(t, wsc.Subscribe(eid))
  172. defer func() {
  173. require.Nil(t, wsc.Unsubscribe(eid))
  174. wsc.Stop()
  175. }()
  176. // listen for NewBlock, ensure height increases by 1
  177. var initBlockN int
  178. for i := 0; i < 3; i++ {
  179. waitForEvent(t, wsc, eid, true, func() {}, func(eid string, eventData interface{}) error {
  180. block := eventData.(types.EventDataNewBlock).Block
  181. if i == 0 {
  182. initBlockN = block.Header.Height
  183. } else {
  184. if block.Header.Height != initBlockN+i {
  185. return fmt.Errorf("Expected block %d, got block %d", initBlockN+i, block.Header.Height)
  186. }
  187. }
  188. return nil
  189. })
  190. }
  191. }
  192. func TestWSTxEvent(t *testing.T) {
  193. require := require.New(t)
  194. wsc := GetWSClient()
  195. tx := randBytes(t)
  196. // listen for the tx I am about to submit
  197. eid := types.EventStringTx(types.Tx(tx))
  198. require.Nil(wsc.Subscribe(eid))
  199. defer func() {
  200. require.Nil(wsc.Unsubscribe(eid))
  201. wsc.Stop()
  202. }()
  203. // send an tx
  204. tmResult := new(ctypes.TMResult)
  205. _, err := GetJSONClient().Call("broadcast_tx_sync", []interface{}{tx}, tmResult)
  206. require.Nil(err)
  207. waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error {
  208. evt, ok := b.(types.EventDataTx)
  209. require.True(ok, "Got wrong event type: %#v", b)
  210. require.Equal(tx, []byte(evt.Tx), "Returned different tx")
  211. require.Equal(abci.CodeType_OK, evt.Code)
  212. return nil
  213. })
  214. }
  215. /* TODO: this with dummy app..
  216. func TestWSDoubleFire(t *testing.T) {
  217. if testing.Short() {
  218. t.Skip("skipping test in short mode.")
  219. }
  220. con := newWSCon(t)
  221. eid := types.EventStringAccInput(user[0].Address)
  222. subscribe(t, con, eid)
  223. defer func() {
  224. unsubscribe(t, con, eid)
  225. con.Close()
  226. }()
  227. amt := int64(100)
  228. toAddr := user[1].Address
  229. // broadcast the transaction, wait to hear about it
  230. waitForEvent(t, con, eid, true, func() {
  231. tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
  232. broadcastTx(t, wsTyp, tx)
  233. }, func(eid string, b []byte) error {
  234. return nil
  235. })
  236. // but make sure we don't hear about it twice
  237. waitForEvent(t, con, eid, false, func() {
  238. }, func(eid string, b []byte) error {
  239. return nil
  240. })
  241. }*/
  242. //--------------------------------------------------------------------------------
  243. // unsafe_set_config
  244. var stringVal = "my string"
  245. var intVal = 987654321
  246. var boolVal = true
  247. // don't change these
  248. var testCasesUnsafeSetConfig = [][]string{
  249. []string{"string", "key1", stringVal},
  250. []string{"int", "key2", fmt.Sprintf("%v", intVal)},
  251. []string{"bool", "key3", fmt.Sprintf("%v", boolVal)},
  252. }
  253. func TestURIUnsafeSetConfig(t *testing.T) {
  254. for _, testCase := range testCasesUnsafeSetConfig {
  255. tmResult := new(ctypes.TMResult)
  256. _, err := GetURIClient().Call("unsafe_set_config", map[string]interface{}{
  257. "type": testCase[0],
  258. "key": testCase[1],
  259. "value": testCase[2],
  260. }, tmResult)
  261. require.Nil(t, err)
  262. }
  263. testUnsafeSetConfig(t)
  264. }
  265. func TestJSONUnsafeSetConfig(t *testing.T) {
  266. for _, testCase := range testCasesUnsafeSetConfig {
  267. tmResult := new(ctypes.TMResult)
  268. _, err := GetJSONClient().Call("unsafe_set_config", []interface{}{testCase[0], testCase[1], testCase[2]}, tmResult)
  269. require.Nil(t, err)
  270. }
  271. testUnsafeSetConfig(t)
  272. }
  273. func testUnsafeSetConfig(t *testing.T) {
  274. require := require.New(t)
  275. s := config.GetString("key1")
  276. require.Equal(stringVal, s)
  277. i := config.GetInt("key2")
  278. require.Equal(intVal, i)
  279. b := config.GetBool("key3")
  280. require.Equal(boolVal, b)
  281. }