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.

228 lines
5.7 KiB

  1. package rpctest
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/tendermint/tendermint/config/tendermint_test"
  6. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. func init() {
  10. tendermint_test.ResetConfig("rpc_test_client_test")
  11. initGlobalVariables()
  12. }
  13. //--------------------------------------------------------------------------------
  14. // Test the HTTP client
  15. //--------------------------------------------------------------------------------
  16. //--------------------------------------------------------------------------------
  17. // status
  18. func TestURIStatus(t *testing.T) {
  19. tmResult := new(ctypes.TMResult)
  20. _, err := clientURI.Call("status", map[string]interface{}{}, tmResult)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. testStatus(t, tmResult)
  25. }
  26. func TestJSONStatus(t *testing.T) {
  27. tmResult := new(ctypes.TMResult)
  28. _, err := clientJSON.Call("status", []interface{}{}, tmResult)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. testStatus(t, tmResult)
  33. }
  34. func testStatus(t *testing.T, statusI interface{}) {
  35. tmRes := statusI.(*ctypes.TMResult)
  36. status := (*tmRes).(*ctypes.ResultStatus)
  37. if status.NodeInfo.Network != chainID {
  38. t.Fatal(fmt.Errorf("ChainID mismatch: got %s expected %s",
  39. status.NodeInfo.Network, chainID))
  40. }
  41. }
  42. //--------------------------------------------------------------------------------
  43. // unsafe_set_config
  44. var stringVal = "my string"
  45. var intVal = 987654321
  46. var boolVal = true
  47. // don't change these
  48. var testCasesUnsafeSetConfig = [][]string{
  49. []string{"string", "key1", stringVal},
  50. []string{"int", "key2", fmt.Sprintf("%v", intVal)},
  51. []string{"bool", "key3", fmt.Sprintf("%v", boolVal)},
  52. }
  53. func TestURIUnsafeSetConfig(t *testing.T) {
  54. for _, testCase := range testCasesUnsafeSetConfig {
  55. tmResult := new(ctypes.TMResult)
  56. _, err := clientURI.Call("unsafe_set_config", map[string]interface{}{
  57. "type": testCase[0],
  58. "key": testCase[1],
  59. "value": testCase[2],
  60. }, tmResult)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. }
  65. testUnsafeSetConfig(t)
  66. }
  67. func TestJSONUnsafeSetConfig(t *testing.T) {
  68. for _, testCase := range testCasesUnsafeSetConfig {
  69. tmResult := new(ctypes.TMResult)
  70. _, err := clientJSON.Call("unsafe_set_config", []interface{}{testCase[0], testCase[1], testCase[2]}, tmResult)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. }
  75. testUnsafeSetConfig(t)
  76. }
  77. func testUnsafeSetConfig(t *testing.T) {
  78. s := config.GetString("key1")
  79. if s != stringVal {
  80. t.Fatalf("got %v, expected %v", s, stringVal)
  81. }
  82. i := config.GetInt("key2")
  83. if i != intVal {
  84. t.Fatalf("got %v, expected %v", i, intVal)
  85. }
  86. b := config.GetBool("key3")
  87. if b != boolVal {
  88. t.Fatalf("got %v, expected %v", b, boolVal)
  89. }
  90. }
  91. /*func TestURIBroadcastTx(t *testing.T) {
  92. testBroadcastTx(t, "HTTP")
  93. }*/
  94. /*func TestJSONBroadcastTx(t *testing.T) {
  95. testBroadcastTx(t, "JSONRPC")
  96. }*/
  97. // TODO
  98. /*
  99. func testBroadcastTx(t *testing.T, typ string) {
  100. amt := int64(100)
  101. toAddr := user[1].Address
  102. tx := makeDefaultSendTxSigned(t, typ, toAddr, amt)
  103. receipt := broadcastTx(t, typ, tx)
  104. if receipt.CreatesContract > 0 {
  105. t.Fatal("This tx does not create a contract")
  106. }
  107. if len(receipt.TxHash) == 0 {
  108. t.Fatal("Failed to compute tx hash")
  109. }
  110. pool := node.MempoolReactor().Mempool
  111. txs := pool.GetProposalTxs()
  112. if len(txs) != mempoolCount {
  113. t.Fatalf("The mem pool has %d txs. Expected %d", len(txs), mempoolCount)
  114. }
  115. tx2 := txs[mempoolCount-1].(*types.SendTx)
  116. n, err := new(int64), new(error)
  117. buf1, buf2 := new(bytes.Buffer), new(bytes.Buffer)
  118. tx.WriteSignBytes(chainID, buf1, n, err)
  119. tx2.WriteSignBytes(chainID, buf2, n, err)
  120. if bytes.Compare(buf1.Bytes(), buf2.Bytes()) != 0 {
  121. t.Fatal("inconsistent hashes for mempool tx and sent tx")
  122. }
  123. }*/
  124. //--------------------------------------------------------------------------------
  125. // Test the websocket service
  126. var wsTyp = "JSONRPC"
  127. // make a simple connection to the server
  128. func TestWSConnect(t *testing.T) {
  129. wsc := newWSClient(t)
  130. wsc.Stop()
  131. }
  132. // receive a new block message
  133. func TestWSNewBlock(t *testing.T) {
  134. wsc := newWSClient(t)
  135. eid := types.EventStringNewBlock()
  136. subscribe(t, wsc, eid)
  137. defer func() {
  138. unsubscribe(t, wsc, eid)
  139. wsc.Stop()
  140. }()
  141. waitForEvent(t, wsc, eid, true, func() {}, func(eid string, b interface{}) error {
  142. fmt.Println("Check:", b)
  143. return nil
  144. })
  145. }
  146. // receive a few new block messages in a row, with increasing height
  147. func TestWSBlockchainGrowth(t *testing.T) {
  148. if testing.Short() {
  149. t.Skip("skipping test in short mode.")
  150. }
  151. wsc := newWSClient(t)
  152. eid := types.EventStringNewBlock()
  153. subscribe(t, wsc, eid)
  154. defer func() {
  155. unsubscribe(t, wsc, eid)
  156. wsc.Stop()
  157. }()
  158. // listen for NewBlock, ensure height increases by 1
  159. var initBlockN int
  160. for i := 0; i < 3; i++ {
  161. waitForEvent(t, wsc, eid, true, func() {}, func(eid string, eventData interface{}) error {
  162. block := eventData.(types.EventDataNewBlock).Block
  163. if i == 0 {
  164. initBlockN = block.Header.Height
  165. } else {
  166. if block.Header.Height != initBlockN+i {
  167. return fmt.Errorf("Expected block %d, got block %d", initBlockN+i, block.Header.Height)
  168. }
  169. }
  170. return nil
  171. })
  172. }
  173. }
  174. /* TODO: this with dummy app..
  175. func TestWSDoubleFire(t *testing.T) {
  176. if testing.Short() {
  177. t.Skip("skipping test in short mode.")
  178. }
  179. con := newWSCon(t)
  180. eid := types.EventStringAccInput(user[0].Address)
  181. subscribe(t, con, eid)
  182. defer func() {
  183. unsubscribe(t, con, eid)
  184. con.Close()
  185. }()
  186. amt := int64(100)
  187. toAddr := user[1].Address
  188. // broadcast the transaction, wait to hear about it
  189. waitForEvent(t, con, eid, true, func() {
  190. tx := makeDefaultSendTxSigned(t, wsTyp, toAddr, amt)
  191. broadcastTx(t, wsTyp, tx)
  192. }, func(eid string, b []byte) error {
  193. return nil
  194. })
  195. // but make sure we don't hear about it twice
  196. waitForEvent(t, con, eid, false, func() {
  197. }, func(eid string, b []byte) error {
  198. return nil
  199. })
  200. }*/