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.

282 lines
7.1 KiB

  1. package rpctest
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "fmt"
  6. "testing"
  7. . "github.com/tendermint/go-common"
  8. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  9. "github.com/tendermint/tendermint/types"
  10. tmsp "github.com/tendermint/tmsp/types"
  11. )
  12. //--------------------------------------------------------------------------------
  13. // Test the HTTP client
  14. //--------------------------------------------------------------------------------
  15. //--------------------------------------------------------------------------------
  16. // status
  17. func TestURIStatus(t *testing.T) {
  18. tmResult := new(ctypes.TMResult)
  19. _, err := clientURI.Call("status", map[string]interface{}{}, tmResult)
  20. if err != nil {
  21. panic(err)
  22. }
  23. testStatus(t, tmResult)
  24. }
  25. func TestJSONStatus(t *testing.T) {
  26. tmResult := new(ctypes.TMResult)
  27. _, err := clientJSON.Call("status", []interface{}{}, tmResult)
  28. if err != nil {
  29. panic(err)
  30. }
  31. testStatus(t, tmResult)
  32. }
  33. func testStatus(t *testing.T, statusI interface{}) {
  34. tmRes := statusI.(*ctypes.TMResult)
  35. status := (*tmRes).(*ctypes.ResultStatus)
  36. if status.NodeInfo.Network != chainID {
  37. panic(Fmt("ChainID mismatch: got %s expected %s",
  38. status.NodeInfo.Network, chainID))
  39. }
  40. }
  41. //--------------------------------------------------------------------------------
  42. // broadcast tx sync
  43. func testTx() []byte {
  44. buf := make([]byte, 16)
  45. _, err := rand.Read(buf)
  46. if err != nil {
  47. panic(err)
  48. }
  49. return buf
  50. }
  51. func TestURIBroadcastTxSync(t *testing.T) {
  52. config.Set("block_size", 0)
  53. defer config.Set("block_size", -1)
  54. tmResult := new(ctypes.TMResult)
  55. tx := testTx()
  56. _, err := clientURI.Call("broadcast_tx_sync", map[string]interface{}{"tx": tx}, tmResult)
  57. if err != nil {
  58. panic(err)
  59. }
  60. testBroadcastTxSync(t, tmResult, tx)
  61. }
  62. func TestJSONBroadcastTxSync(t *testing.T) {
  63. config.Set("block_size", 0)
  64. defer config.Set("block_size", -1)
  65. tmResult := new(ctypes.TMResult)
  66. tx := testTx()
  67. _, err := clientJSON.Call("broadcast_tx_sync", []interface{}{tx}, tmResult)
  68. if err != nil {
  69. panic(err)
  70. }
  71. testBroadcastTxSync(t, tmResult, tx)
  72. }
  73. func testBroadcastTxSync(t *testing.T, resI interface{}, tx []byte) {
  74. tmRes := resI.(*ctypes.TMResult)
  75. res := (*tmRes).(*ctypes.ResultBroadcastTx)
  76. if res.Code != tmsp.CodeType_OK {
  77. panic(Fmt("BroadcastTxSync got non-zero exit code: %v. %X; %s", res.Code, res.Data, res.Log))
  78. }
  79. mem := node.MempoolReactor().Mempool
  80. if mem.Size() != 1 {
  81. panic(Fmt("Mempool size should have been 1. Got %d", mem.Size()))
  82. }
  83. txs := mem.Reap(1)
  84. if !bytes.Equal(txs[0], tx) {
  85. panic(Fmt("Tx in mempool does not match test tx. Got %X, expected %X", txs[0], testTx))
  86. }
  87. mem.Flush()
  88. }
  89. //--------------------------------------------------------------------------------
  90. // broadcast tx commit
  91. func TestURIBroadcastTxCommit(t *testing.T) {
  92. tmResult := new(ctypes.TMResult)
  93. tx := testTx()
  94. _, err := clientURI.Call("broadcast_tx_commit", map[string]interface{}{"tx": tx}, tmResult)
  95. if err != nil {
  96. panic(err)
  97. }
  98. testBroadcastTxCommit(t, tmResult, tx)
  99. }
  100. func TestJSONBroadcastTxCommit(t *testing.T) {
  101. tmResult := new(ctypes.TMResult)
  102. tx := testTx()
  103. _, err := clientJSON.Call("broadcast_tx_commit", []interface{}{tx}, tmResult)
  104. if err != nil {
  105. panic(err)
  106. }
  107. testBroadcastTxCommit(t, tmResult, tx)
  108. }
  109. func testBroadcastTxCommit(t *testing.T, resI interface{}, tx []byte) {
  110. tmRes := resI.(*ctypes.TMResult)
  111. res := (*tmRes).(*ctypes.ResultBroadcastTx)
  112. if res.Code != tmsp.CodeType_OK {
  113. panic(Fmt("BroadcastTxCommit got non-zero exit code: %v. %X; %s", res.Code, res.Data, res.Log))
  114. }
  115. mem := node.MempoolReactor().Mempool
  116. if mem.Size() != 0 {
  117. panic(Fmt("Mempool size should have been 0. Got %d", mem.Size()))
  118. }
  119. // TODO: find tx in block
  120. }
  121. //--------------------------------------------------------------------------------
  122. // Test the websocket service
  123. var wsTyp = "JSONRPC"
  124. // make a simple connection to the server
  125. func TestWSConnect(t *testing.T) {
  126. wsc := newWSClient(t)
  127. wsc.Stop()
  128. }
  129. // receive a new block message
  130. func TestWSNewBlock(t *testing.T) {
  131. wsc := newWSClient(t)
  132. eid := types.EventStringNewBlock()
  133. subscribe(t, wsc, eid)
  134. defer func() {
  135. unsubscribe(t, wsc, eid)
  136. wsc.Stop()
  137. }()
  138. waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error {
  139. fmt.Println("Check:", b)
  140. return nil
  141. })
  142. }
  143. // receive a few new block messages in a row, with increasing height
  144. func TestWSBlockchainGrowth(t *testing.T) {
  145. if testing.Short() {
  146. t.Skip("skipping test in short mode.")
  147. }
  148. wsc := newWSClient(t)
  149. eid := types.EventStringNewBlock()
  150. subscribe(t, wsc, eid)
  151. defer func() {
  152. unsubscribe(t, wsc, eid)
  153. wsc.Stop()
  154. }()
  155. // listen for NewBlock, ensure height increases by 1
  156. var initBlockN int
  157. for i := 0; i < 3; i++ {
  158. waitForEvent(t, wsc, eid, true, func() {}, func(eid string, eventData interface{}) error {
  159. block := eventData.(types.EventDataNewBlock).Block
  160. if i == 0 {
  161. initBlockN = block.Header.Height
  162. } else {
  163. if block.Header.Height != initBlockN+i {
  164. return fmt.Errorf("Expected block %d, got block %d", initBlockN+i, block.Header.Height)
  165. }
  166. }
  167. return nil
  168. })
  169. }
  170. }
  171. /* TODO: this with dummy app..
  172. func TestWSDoubleFire(t *testing.T) {
  173. if testing.Short() {
  174. t.Skip("skipping test in short mode.")
  175. }
  176. con := newWSCon(t)
  177. eid := types.EventStringAccInput(user[0].Address)
  178. subscribe(t, con, eid)
  179. defer func() {
  180. unsubscribe(t, con, eid)
  181. con.Close()
  182. }()
  183. amt := int64(100)
  184. toAddr := user[1].Address
  185. // broadcast the transaction, wait to hear about it
  186. waitForEvent(t, con, eid, true, func() {
  187. tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
  188. broadcastTx(t, wsTyp, tx)
  189. }, func(eid string, b []byte) error {
  190. return nil
  191. })
  192. // but make sure we don't hear about it twice
  193. waitForEvent(t, con, eid, false, func() {
  194. }, func(eid string, b []byte) error {
  195. return nil
  196. })
  197. }*/
  198. //--------------------------------------------------------------------------------
  199. // unsafe_set_config
  200. var stringVal = "my string"
  201. var intVal = 987654321
  202. var boolVal = true
  203. // don't change these
  204. var testCasesUnsafeSetConfig = [][]string{
  205. []string{"string", "key1", stringVal},
  206. []string{"int", "key2", fmt.Sprintf("%v", intVal)},
  207. []string{"bool", "key3", fmt.Sprintf("%v", boolVal)},
  208. }
  209. func TestURIUnsafeSetConfig(t *testing.T) {
  210. for _, testCase := range testCasesUnsafeSetConfig {
  211. tmResult := new(ctypes.TMResult)
  212. _, err := clientURI.Call("unsafe_set_config", map[string]interface{}{
  213. "type": testCase[0],
  214. "key": testCase[1],
  215. "value": testCase[2],
  216. }, tmResult)
  217. if err != nil {
  218. panic(err)
  219. }
  220. }
  221. testUnsafeSetConfig(t)
  222. }
  223. func TestJSONUnsafeSetConfig(t *testing.T) {
  224. for _, testCase := range testCasesUnsafeSetConfig {
  225. tmResult := new(ctypes.TMResult)
  226. _, err := clientJSON.Call("unsafe_set_config", []interface{}{testCase[0], testCase[1], testCase[2]}, tmResult)
  227. if err != nil {
  228. panic(err)
  229. }
  230. }
  231. testUnsafeSetConfig(t)
  232. }
  233. func testUnsafeSetConfig(t *testing.T) {
  234. s := config.GetString("key1")
  235. if s != stringVal {
  236. panic(Fmt("got %v, expected %v", s, stringVal))
  237. }
  238. i := config.GetInt("key2")
  239. if i != intVal {
  240. panic(Fmt("got %v, expected %v", i, intVal))
  241. }
  242. b := config.GetBool("key3")
  243. if b != boolVal {
  244. panic(Fmt("got %v, expected %v", b, boolVal))
  245. }
  246. }