From 546211e370fec3caf952591a60c703b86b9fe384 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 2 Jun 2015 11:46:30 +0100 Subject: [PATCH] added first js console tests for bzz --- cmd/geth/admin.go | 6 ++-- cmd/geth/js.go | 8 ++--- cmd/geth/js_bzz_test.go | 68 +++++++++++++++++++++++++++++++++++++++++ cmd/geth/js_test.go | 14 +++++++-- 4 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 cmd/geth/js_bzz_test.go diff --git a/cmd/geth/admin.go b/cmd/geth/admin.go index 20654c0cfd..93a7cdb379 100644 --- a/cmd/geth/admin.go +++ b/cmd/geth/admin.go @@ -220,7 +220,7 @@ func (js *jsre) httpLoadScript(call otto.FunctionCall) otto.Value { fmt.Println(err) return otto.FalseValue() } - script, err := ds.Get(uri, "") + script, err := js.ds.Get(uri, "") if err != nil { fmt.Println(err) return otto.FalseValue() @@ -251,7 +251,7 @@ func (js *jsre) httpGet(call otto.FunctionCall) otto.Value { return otto.UndefinedValue() } } - resp, err := ds.Get(uri, path) + resp, err := js.ds.Get(uri, path) if err != nil { fmt.Println(err) return otto.UndefinedValue() @@ -854,7 +854,7 @@ func (js *jsre) getContractInfo(call otto.FunctionCall) otto.Value { return otto.FalseValue() } - infoDoc, err := natspec.FetchDocsForContract(addr, js.xeth, ds) + infoDoc, err := natspec.FetchDocsForContract(addr, js.xeth, js.ds) if err != nil { fmt.Println(err) return otto.UndefinedValue() diff --git a/cmd/geth/js.go b/cmd/geth/js.go index 216c5e3500..7023610b63 100644 --- a/cmd/geth/js.go +++ b/cmd/geth/js.go @@ -65,6 +65,7 @@ func (r dumbterm) PasswordPrompt(p string) (string, error) { func (r dumbterm) AppendHistory(string) {} type jsre struct { + ds *docserver.DocServer re *re.JSRE ethereum *eth.Ethereum xeth *xeth.XEth @@ -82,6 +83,7 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, bzzEnabled bool if f == nil { f = js } + js.ds = docserver.New("/") js.xeth = xeth.New(ethereum, f) js.wait = js.xeth.UpdateState() js.re = re.New(libPath) @@ -90,7 +92,7 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, bzzEnabled bool js.adminBindings() if bzzEnabled { // register the swarm rountripper with the bzz scheme on the docserver - ds.RegisterProtocol("bzz", &bzz.RoundTripper{Port: bzzPort}) + js.ds.RegisterProtocol("bzz", &bzz.RoundTripper{Port: bzzPort}) // swarm js bindings bzz.NewJSApi(js.re, js.ethereum.Swarm) js.ethereum.Swarm.Resolver = resolver.New(xeth.New(ethereum, f)) @@ -156,11 +158,9 @@ var net = web3.net; js.re.Eval(resolver.GlobalRegistrar + "registrar = GlobalRegistrar.at(\"" + resolver.GlobalRegistrarAddr + "\");") } -var ds = docserver.New("/") - func (self *jsre) ConfirmTransaction(tx string) bool { if self.ethereum.NatSpec { - notice := natspec.GetNotice(self.xeth, tx, ds) + notice := natspec.GetNotice(self.xeth, tx, self.ds) fmt.Println(notice) answer, _ := self.Prompt("Confirm Transaction [y/n]") return strings.HasPrefix(strings.Trim(answer, " "), "y") diff --git a/cmd/geth/js_bzz_test.go b/cmd/geth/js_bzz_test.go new file mode 100644 index 0000000000..f9a1a633e8 --- /dev/null +++ b/cmd/geth/js_bzz_test.go @@ -0,0 +1,68 @@ +package main + +import ( + // "io/ioutil" + "os" + // "path" + // "runtime" + "testing" + + // "github.com/ethereum/go-ethereum/bzz" + // "github.com/ethereum/go-ethereum/common/resolver" + "github.com/ethereum/go-ethereum/eth" +) + +func bzzREPL(t *testing.T, port string) (string, *testjethre, *eth.Ethereum) { + return testREPL(t, func(c *eth.Config) { + c.Bzz = true + c.BzzPort = port + }) +} + +func TestBzzUploadDownload(t *testing.T) { + tmp, repl, ethereum := bzzREPL(t, "") + if err := ethereum.Start(); err != nil { + t.Fatalf("error starting ethereum: %v", err) + } + defer ethereum.Stop() + defer os.RemoveAll(tmp) + _ = repl +} + +func TestBzzPutGet(t *testing.T) { + tmp, repl, ethereum := bzzREPL(t, "") + if err := ethereum.Start(); err != nil { + t.Fatalf("error starting ethereum: %v", err) + } + defer ethereum.Stop() + defer os.RemoveAll(tmp) + if checkEvalJSON(t, repl, `hash = bzz.put("console.log(\"hello from console\")", "application/javascript")`, `"97f1b7c7ea12468fd37c262383b9aa862d0cfbc4fc7218652374679fc5cf40cd"`) != nil { + return + } + want := `{"content":"console.log(\"hello from console\")","contentType":"application/javascript","size":"33","status":"0"}` + if checkEvalJSON(t, repl, `bzz.get(hash)`, want) != nil { + return + } +} + +// the server can be initialized only once per test session ! +// until we implement a stoppable http server +// further http tests will need to make sure the correct server is running +func TestHTTP(t *testing.T) { + tmp, repl, ethereum := bzzREPL(t, "8500") + if err := ethereum.Start(); err != nil { + t.Fatalf("error starting ethereum: %v", err) + } + defer ethereum.Stop() + defer os.RemoveAll(tmp) + if checkEvalJSON(t, repl, `hash = bzz.put("6*7", "application/javascript")`, `"97f1b7c7ea12468fd37c262383b9aa862d0cfbc4fc7218652374679fc5cf40cd"`) != nil { + return + } + if checkEvalJSON(t, repl, `http.get("bzz://"+hash)`, `"6*7"`) != nil { + return + } + + if checkEvalJSON(t, repl, `http.loadScript("bzz://"+hash)`, `42`) != nil { + return + } +} diff --git a/cmd/geth/js_test.go b/cmd/geth/js_test.go index 95a9b97afc..c7d767f5e0 100644 --- a/cmd/geth/js_test.go +++ b/cmd/geth/js_test.go @@ -62,6 +62,10 @@ func (self *testjethre) ConfirmTransaction(tx string) bool { } func testJEthRE(t *testing.T) (string, *testjethre, *eth.Ethereum) { + return testREPL(t, nil) +} + +func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *eth.Ethereum) { tmp, err := ioutil.TempDir("", "geth-test") if err != nil { t.Fatal(err) @@ -72,7 +76,7 @@ func testJEthRE(t *testing.T) (string, *testjethre, *eth.Ethereum) { ks := crypto.NewKeyStorePlain(filepath.Join(tmp, "keystore")) am := accounts.NewManager(ks) - ethereum, err := eth.New(ð.Config{ + conf := ð.Config{ NodeKey: testNodeKey, DataDir: tmp, AccountManager: am, @@ -80,7 +84,11 @@ func testJEthRE(t *testing.T) (string, *testjethre, *eth.Ethereum) { Name: "test", SolcPath: testSolcPath, PowTest: true, - }) + } + if config != nil { + config(conf) + } + ethereum, err := eth.New(conf) if err != nil { t.Fatal("%v", err) } @@ -103,7 +111,7 @@ func testJEthRE(t *testing.T) (string, *testjethre, *eth.Ethereum) { assetPath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") ds := docserver.New("/") tf := &testjethre{ds: ds} - repl := newJSRE(ethereum, assetPath, "", false, "", false, tf) + repl := newJSRE(ethereum, assetPath, "", conf.Bzz, conf.BzzPort, false, tf) tf.jsre = repl return tmp, tf, ethereum }