js_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "testing"
  9. "github.com/ethereum/go-ethereum/accounts"
  10. "github.com/ethereum/go-ethereum/crypto"
  11. "github.com/ethereum/go-ethereum/eth"
  12. "regexp"
  13. "runtime"
  14. "strconv"
  15. )
  16. var port = 30300
  17. func testJEthRE(t *testing.T) (*jsre, *eth.Ethereum) {
  18. tmp, err := ioutil.TempDir("", "geth-test")
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. defer os.RemoveAll(tmp)
  23. ks := crypto.NewKeyStorePlain(filepath.Join(tmp, "keys"))
  24. ethereum, err := eth.New(&eth.Config{
  25. DataDir: tmp,
  26. AccountManager: accounts.NewManager(ks),
  27. MaxPeers: 0,
  28. Name: "test",
  29. })
  30. if err != nil {
  31. t.Fatal("%v", err)
  32. }
  33. assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
  34. repl := newJSRE(ethereum, assetPath, false, "")
  35. return repl, ethereum
  36. }
  37. func TestNodeInfo(t *testing.T) {
  38. repl, ethereum := testJEthRE(t)
  39. if err := ethereum.Start(); err != nil {
  40. t.Fatalf("error starting ethereum: %v", err)
  41. }
  42. defer ethereum.Stop()
  43. want := `{"DiscPort":0,"IP":"0.0.0.0","ListenAddr":"","Name":"test","NodeID":"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","NodeUrl":"enode://00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000@0.0.0.0:0","TCPPort":0,"Td":"0"}`
  44. checkEvalJSON(t, repl, `admin.nodeInfo()`, want)
  45. }
  46. func TestAccounts(t *testing.T) {
  47. repl, ethereum := testJEthRE(t)
  48. if err := ethereum.Start(); err != nil {
  49. t.Fatalf("error starting ethereum: %v", err)
  50. }
  51. defer ethereum.Stop()
  52. checkEvalJSON(t, repl, `eth.accounts`, `[]`)
  53. checkEvalJSON(t, repl, `eth.coinbase`, `"0x"`)
  54. val, err := repl.re.Run(`admin.newAccount("password")`)
  55. if err != nil {
  56. t.Errorf("expected no error, got %v", err)
  57. }
  58. addr := val.String()
  59. if !regexp.MustCompile(`0x[0-9a-f]{40}`).MatchString(addr) {
  60. t.Errorf("address not hex: %q", addr)
  61. }
  62. checkEvalJSON(t, repl, `eth.accounts`, `["`+addr+`"]`)
  63. checkEvalJSON(t, repl, `eth.coinbase`, `"`+addr+`"`)
  64. }
  65. func TestBlockChain(t *testing.T) {
  66. repl, ethereum := testJEthRE(t)
  67. if err := ethereum.Start(); err != nil {
  68. t.Fatalf("error starting ethereum: %v", err)
  69. }
  70. defer ethereum.Stop()
  71. // get current block dump before export/import.
  72. val, err := repl.re.Run("JSON.stringify(admin.debug.dumpBlock())")
  73. if err != nil {
  74. t.Errorf("expected no error, got %v", err)
  75. }
  76. beforeExport := val.String()
  77. // do the export
  78. tmp, err := ioutil.TempDir("", "geth-test-export")
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. defer os.RemoveAll(tmp)
  83. tmpfile := filepath.Join(tmp, "export.chain")
  84. tmpfileq := strconv.Quote(tmpfile)
  85. checkEvalJSON(t, repl, `admin.export(`+tmpfileq+`)`, `true`)
  86. if _, err := os.Stat(tmpfile); err != nil {
  87. t.Fatal(err)
  88. }
  89. // check import, verify that dumpBlock gives the same result.
  90. checkEvalJSON(t, repl, `admin.import(`+tmpfileq+`)`, `true`)
  91. checkEvalJSON(t, repl, `admin.debug.dumpBlock()`, beforeExport)
  92. }
  93. func TestMining(t *testing.T) {
  94. repl, ethereum := testJEthRE(t)
  95. if err := ethereum.Start(); err != nil {
  96. t.Fatalf("error starting ethereum: %v", err)
  97. }
  98. defer ethereum.Stop()
  99. checkEvalJSON(t, repl, `eth.mining`, `false`)
  100. }
  101. func TestRPC(t *testing.T) {
  102. repl, ethereum := testJEthRE(t)
  103. if err := ethereum.Start(); err != nil {
  104. t.Errorf("error starting ethereum: %v", err)
  105. return
  106. }
  107. defer ethereum.Stop()
  108. checkEvalJSON(t, repl, `admin.startRPC("127.0.0.1", 5004)`, `true`)
  109. }
  110. func checkEvalJSON(t *testing.T, re *jsre, expr, want string) error {
  111. val, err := re.re.Run("JSON.stringify(" + expr + ")")
  112. if err == nil && val.String() != want {
  113. err = fmt.Errorf("Output mismatch for `%s`:\ngot: %s\nwant: %s", expr, val.String(), want)
  114. }
  115. if err != nil {
  116. _, file, line, _ := runtime.Caller(1)
  117. file = path.Base(file)
  118. fmt.Printf("\t%s:%d: %v\n", file, line, err)
  119. t.Fail()
  120. }
  121. return err
  122. }