cmd/geth: improve the JS tests

These changes ensure that the JS tests run without networking
and fixes the block chain export and its associated test.
This commit is contained in:
Felix Lange 2015-04-22 10:59:27 +02:00
parent 635b66acdc
commit e1f616fadf
2 changed files with 75 additions and 213 deletions

@ -3,7 +3,6 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"time" "time"
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
@ -318,7 +317,7 @@ func (js *jsre) newAccount(call otto.FunctionCall) otto.Value {
fmt.Printf("Could not create the account: %v", err) fmt.Printf("Could not create the account: %v", err)
return otto.UndefinedValue() return otto.UndefinedValue()
} }
return js.re.ToVal(common.Bytes2Hex(acct.Address)) return js.re.ToVal("0x" + common.Bytes2Hex(acct.Address))
} }
func (js *jsre) nodeInfo(call otto.FunctionCall) otto.Value { func (js *jsre) nodeInfo(call otto.FunctionCall) otto.Value {
@ -334,33 +333,15 @@ func (js *jsre) importChain(call otto.FunctionCall) otto.Value {
fmt.Println("err: require file name") fmt.Println("err: require file name")
return otto.FalseValue() return otto.FalseValue()
} }
fn, err := call.Argument(0).ToString() fn, err := call.Argument(0).ToString()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return otto.FalseValue() return otto.FalseValue()
} }
if err := utils.ImportChain(js.ethereum.ChainManager(), fn); err != nil {
var fh *os.File fmt.Println("Import error: ", err)
fh, err = os.OpenFile(fn, os.O_RDONLY, os.ModePerm)
if err != nil {
fmt.Println(err)
return otto.FalseValue() return otto.FalseValue()
} }
defer fh.Close()
var blocks types.Blocks
if err = rlp.Decode(fh, &blocks); err != nil {
fmt.Println(err)
return otto.FalseValue()
}
js.ethereum.ChainManager().Reset()
if err = js.ethereum.ChainManager().InsertChain(blocks); err != nil {
fmt.Println(err)
return otto.FalseValue()
}
return otto.TrueValue() return otto.TrueValue()
} }

@ -3,260 +3,141 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path/filepath"
"os" "os"
"path" "path"
"testing" "testing"
"github.com/robertkrimen/otto"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
"runtime"
"regexp"
"strconv"
) )
var port = 30300 var port = 30300
func testJEthRE(t *testing.T) (repl *jsre, ethereum *eth.Ethereum, err error) { func testJEthRE(t *testing.T) (*jsre, *eth.Ethereum) {
os.RemoveAll("/tmp/eth/") tmp, err := ioutil.TempDir("", "geth-test")
err = os.MkdirAll("/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/", os.ModePerm)
if err != nil { if err != nil {
t.Errorf("%v", err) t.Fatal(err)
return
} }
err = os.MkdirAll("/tmp/eth/data", os.ModePerm) defer os.RemoveAll(tmp)
if err != nil {
t.Errorf("%v", err)
return
}
// FIXME: this does not work ATM
ks := crypto.NewKeyStorePlain("/tmp/eth/keys")
ioutil.WriteFile("/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/e273f01c99144c438695e10f24926dc1f9fbf62d",
[]byte(`{"Id":"RhRXD+fNRKS4jx+7ZfEsNA==","Address":"4nPwHJkUTEOGleEPJJJtwfn79i0=","PrivateKey":"h4ACVpe74uIvi5Cg/2tX/Yrm2xdr3J7QoMbMtNX2CNc="}`), os.ModePerm)
port++ ks := crypto.NewKeyStorePlain(filepath.Join(tmp, "keys"))
ethereum, err = eth.New(&eth.Config{ ethereum, err := eth.New(&eth.Config{
DataDir: "/tmp/eth", DataDir: tmp,
AccountManager: accounts.NewManager(ks), AccountManager: accounts.NewManager(ks),
Port: fmt.Sprintf("%d", port), MaxPeers: 0,
MaxPeers: 10,
Name: "test", Name: "test",
}) })
if err != nil { if err != nil {
t.Errorf("%v", err) t.Fatal("%v", err)
return
} }
assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
repl = newJSRE(ethereum, assetPath, false) repl := newJSRE(ethereum, assetPath, false)
return return repl, ethereum
} }
func TestNodeInfo(t *testing.T) { func TestNodeInfo(t *testing.T) {
repl, ethereum, err := testJEthRE(t) repl, ethereum := testJEthRE(t)
if err != nil { if err := ethereum.Start(); err != nil {
t.Errorf("error creating jsre, got %v", err) t.Fatalf("error starting ethereum: %v", err)
return
}
err = ethereum.Start()
if err != nil {
t.Errorf("error starting ethereum: %v", err)
return
} }
defer ethereum.Stop() defer ethereum.Stop()
val, err := repl.re.Run("admin.nodeInfo()") 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"}`
if err != nil { checkEvalJSON(t, repl, `admin.nodeInfo()`, want)
t.Errorf("expected no error, got %v", err)
}
exp, err := val.Export()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
nodeInfo, ok := exp.(*eth.NodeInfo)
if !ok {
t.Errorf("expected nodeInfo, got %v", err)
}
exp = "test"
got := nodeInfo.Name
if exp != got {
t.Errorf("expected %v, got %v", exp, got)
}
exp = 30301
port := nodeInfo.DiscPort
if exp != port {
t.Errorf("expected %v, got %v", exp, port)
}
exp = 30301
port = nodeInfo.TCPPort
if exp != port {
t.Errorf("expected %v, got %v", exp, port)
}
} }
func TestAccounts(t *testing.T) { func TestAccounts(t *testing.T) {
repl, ethereum, err := testJEthRE(t) repl, ethereum := testJEthRE(t)
if err != nil { if err := ethereum.Start(); err != nil {
t.Errorf("error creating jsre, got %v", err) t.Fatalf("error starting ethereum: %v", err)
return
}
err = ethereum.Start()
if err != nil {
t.Errorf("error starting ethereum: %v", err)
return
} }
defer ethereum.Stop() defer ethereum.Stop()
val, err := repl.re.Run("eth.coinbase") checkEvalJSON(t, repl, `eth.accounts`, `[]`)
checkEvalJSON(t, repl, `eth.coinbase`, `"0x"`)
val, err := repl.re.Run(`admin.newAccount("password")`)
if err != nil { if err != nil {
t.Errorf("expected no error, got %v", err) t.Errorf("expected no error, got %v", err)
} }
addr := val.String()
pp, err := repl.re.PrettyPrint(val) if !regexp.MustCompile(`0x[0-9a-f]{40}`).MatchString(addr) {
if err != nil { t.Errorf("address not hex: %q", addr)
t.Errorf("%v", err)
}
if !val.IsString() {
t.Errorf("incorrect type, expected string, got %v: %v", val, pp)
}
strVal, _ := val.ToString()
expected := "0xe273f01c99144c438695e10f24926dc1f9fbf62d"
if strVal != expected {
t.Errorf("incorrect result, expected %s, got %v", expected, strVal)
}
val, err = repl.re.Run(`admin.newAccount("password")`)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
addr, err := val.ToString()
if err != nil {
t.Errorf("expected string, got %v", err)
}
val, err = repl.re.Run("eth.accounts")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
exp, err := val.Export()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
interfaceAddr, ok := exp.([]interface{})
if !ok {
t.Errorf("expected []string, got %T", exp)
}
addrs := make([]string, len(interfaceAddr))
for i, addr := range interfaceAddr {
var ok bool
if addrs[i], ok = addr.(string); !ok {
t.Errorf("expected addrs[%d] to be string. Got %T instead", i, addr)
}
}
if len(addrs) != 2 || (addr != addrs[0][2:] && addr != addrs[1][2:]) {
t.Errorf("expected addrs == [<default>, <new>], got %v (%v)", addrs, addr)
} }
checkEvalJSON(t, repl, `eth.accounts`, `["` + addr + `"]`)
checkEvalJSON(t, repl, `eth.coinbase`, `"` + addr + `"`)
} }
func TestBlockChain(t *testing.T) { func TestBlockChain(t *testing.T) {
repl, ethereum, err := testJEthRE(t) repl, ethereum := testJEthRE(t)
if err != nil { if err := ethereum.Start(); err != nil {
t.Errorf("error creating jsre, got %v", err) t.Fatalf("error starting ethereum: %v", err)
return
}
err = ethereum.Start()
if err != nil {
t.Errorf("error starting ethereum: %v", err)
return
} }
defer ethereum.Stop() defer ethereum.Stop()
// should get current block // get current block dump before export/import.
val0, err := repl.re.Run("admin.debug.dumpBlock()") val, err := repl.re.Run("JSON.stringify(admin.debug.dumpBlock())")
if err != nil { if err != nil {
t.Errorf("expected no error, got %v", err) t.Errorf("expected no error, got %v", err)
} }
beforeExport := val.String()
fn := "/tmp/eth/data/blockchain.0" // do the export
_, err = repl.re.Run("admin.export(\"" + fn + "\")") tmp, err := ioutil.TempDir("", "geth-test-export")
if err != nil { if err != nil {
t.Errorf("expected no error, got %v", err) t.Fatal(err)
} }
if _, err = os.Stat(fn); err != nil { defer os.RemoveAll(tmp)
t.Errorf("expected no error on file, got %v", err) tmpfile := filepath.Join(tmp, "export.chain")
tmpfileq := strconv.Quote(tmpfile)
checkEvalJSON(t, repl, `admin.export(` + tmpfileq + `)`, `true`)
if _, err := os.Stat(tmpfile); err != nil {
t.Fatal(err)
} }
_, err = repl.re.Run("admin.import(\"" + fn + "\")") // check import, verify that dumpBlock gives the same result.
if err != nil { checkEvalJSON(t, repl, `admin.import(` + tmpfileq + `)`, `true`)
t.Errorf("expected no error, got %v", err) checkEvalJSON(t, repl, `admin.debug.dumpBlock()`, beforeExport)
}
var val1 otto.Value
// should get current block
val1, err = repl.re.Run("admin.debug.dumpBlock()")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
// FIXME: neither != , nor reflect.DeepEqual works, doing string comparison
v0 := fmt.Sprintf("%v", val0)
v1 := fmt.Sprintf("%v", val1)
if v0 != v1 {
t.Errorf("expected same head after export-import, got %v (!=%v)", v1, v0)
}
} }
func TestMining(t *testing.T) { func TestMining(t *testing.T) {
repl, ethereum, err := testJEthRE(t) repl, ethereum := testJEthRE(t)
if err != nil { if err := ethereum.Start(); err != nil {
t.Errorf("error creating jsre, got %v", err) t.Fatalf("error starting ethereum: %v", err)
return
}
err = ethereum.Start()
if err != nil {
t.Errorf("error starting ethereum: %v", err)
return
} }
defer ethereum.Stop() defer ethereum.Stop()
val, err := repl.re.Run("eth.mining") checkEvalJSON(t, repl, `eth.mining`, `false`)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
var mining bool
mining, err = val.ToBoolean()
if err != nil {
t.Errorf("expected boolean, got %v", err)
}
if mining {
t.Errorf("expected false (not mining), got true")
}
} }
func TestRPC(t *testing.T) { func TestRPC(t *testing.T) {
repl, ethereum, err := testJEthRE(t) repl, ethereum := testJEthRE(t)
if err != nil { if err := ethereum.Start(); err != nil {
t.Errorf("error creating jsre, got %v", err)
return
}
err = ethereum.Start()
if err != nil {
t.Errorf("error starting ethereum: %v", err) t.Errorf("error starting ethereum: %v", err)
return return
} }
defer ethereum.Stop() defer ethereum.Stop()
val, err := repl.re.Run(`admin.startRPC("127.0.0.1", 5004)`) checkEvalJSON(t, repl, `admin.startRPC("127.0.0.1", 5004)`, `true`)
if err != nil { }
t.Errorf("expected no error, got %v", err)
} func checkEvalJSON(t *testing.T, re *jsre, expr, want string) error {
success, _ := val.ToBoolean() val, err := re.re.Run("JSON.stringify("+ expr + ")")
if !success { if err == nil && val.String() != want {
t.Errorf("expected true (started), got false") err = fmt.Errorf("Output mismatch for `%s`:\ngot: %s\nwant: %s", expr, val.String(), want)
} }
if err != nil {
_, file, line, _ := runtime.Caller(1)
file = path.Base(file)
fmt.Printf("\t%s:%d: %v\n", file, line, err)
t.Fail()
}
return err
} }