added first js console tests for bzz

This commit is contained in:
zelig 2015-06-02 11:46:30 +01:00
parent b2330c4e2e
commit 546211e370
4 changed files with 86 additions and 10 deletions

View file

@ -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()

View file

@ -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")

68
cmd/geth/js_bzz_test.go Normal file
View file

@ -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
}
}

View file

@ -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(&eth.Config{
conf := &eth.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
}