mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
add api test for Put/Get
This commit is contained in:
parent
fe771132e1
commit
e7f2234c7f
2 changed files with 107 additions and 11 deletions
45
bzz/api.go
45
bzz/api.go
|
|
@ -62,6 +62,30 @@ func NewApi(datadir, port string) (self *Api, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Local swarm without netStore
|
||||||
|
func NewLocalApi(datadir string) (self *Api, err error) {
|
||||||
|
|
||||||
|
self = &Api{
|
||||||
|
Chunker: &TreeChunker{},
|
||||||
|
}
|
||||||
|
dbStore, err := newDbStore(datadir)
|
||||||
|
dbStore.setCapacity(50000)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
memStore := newMemStore(dbStore)
|
||||||
|
localStore := &localStore{
|
||||||
|
memStore,
|
||||||
|
dbStore,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.dpa = &DPA{
|
||||||
|
Chunker: self.Chunker,
|
||||||
|
ChunkStore: localStore,
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Bzz returns the bzz protocol class instances of which run on every peer
|
// Bzz returns the bzz protocol class instances of which run on every peer
|
||||||
func (self *Api) Bzz() (p2p.Protocol, error) {
|
func (self *Api) Bzz() (p2p.Protocol, error) {
|
||||||
return BzzProtocol(self.netStore)
|
return BzzProtocol(self.netStore)
|
||||||
|
|
@ -77,9 +101,15 @@ Start is called when the ethereum stack is started
|
||||||
func (self *Api) Start(node *discover.Node, connectPeer func(string) error) {
|
func (self *Api) Start(node *discover.Node, connectPeer func(string) error) {
|
||||||
self.Chunker.Init()
|
self.Chunker.Init()
|
||||||
self.dpa.Start()
|
self.dpa.Start()
|
||||||
self.netStore.start(node, connectPeer)
|
if node != nil && self.netStore != nil && connectPeer != nil {
|
||||||
dpaLogger.Infof("Swarm started.")
|
self.netStore.start(node, connectPeer)
|
||||||
go startHttpServer(self, self.Port)
|
dpaLogger.Infof("Swarm network started.")
|
||||||
|
} else {
|
||||||
|
dpaLogger.Infof("Local Swarm started without network")
|
||||||
|
}
|
||||||
|
if self.Port != "" {
|
||||||
|
go startHttpServer(self, self.Port)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Api) Stop() {
|
func (self *Api) Stop() {
|
||||||
|
|
@ -171,7 +201,7 @@ func (self *Api) Upload(lpath string) (string, error) {
|
||||||
dpaLogger.Debugf("uploading '%s'", localpath)
|
dpaLogger.Debugf("uploading '%s'", localpath)
|
||||||
err := filepath.Walk(localpath, func(path string, info os.FileInfo, err error) error {
|
err := filepath.Walk(localpath, func(path string, info os.FileInfo, err error) error {
|
||||||
if (err == nil) && !info.IsDir() {
|
if (err == nil) && !info.IsDir() {
|
||||||
//fmt.Printf("lp %s path %s\n", localpath, path)
|
dpaLogger.Debugf("localpath: '%s'; path: '%s'\n", localpath, path)
|
||||||
if len(path) <= start {
|
if len(path) <= start {
|
||||||
return fmt.Errorf("Path is too short")
|
return fmt.Errorf("Path is too short")
|
||||||
}
|
}
|
||||||
|
|
@ -253,6 +283,7 @@ func (self *Api) Register(sender common.Address, hash common.Hash, domain string
|
||||||
domainhash := common.BytesToHash(crypto.Sha3([]byte(domain)))
|
domainhash := common.BytesToHash(crypto.Sha3([]byte(domain)))
|
||||||
|
|
||||||
if self.Resolver != nil {
|
if self.Resolver != nil {
|
||||||
|
dpaLogger.Debugf("Swarm: host '%s' (hash: '%v') to be registered as '%v'", domain, domainhash.Hex(), hash.Hex())
|
||||||
_, err = self.Resolver.RegisterContentHash(sender, domainhash, hash)
|
_, err = self.Resolver.RegisterContentHash(sender, domainhash, hash)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("no registry: %v", err)
|
err = fmt.Errorf("no registry: %v", err)
|
||||||
|
|
@ -266,10 +297,12 @@ func (self *Api) Resolve(hostPort string) (contentHash Key, err error) {
|
||||||
var host string
|
var host string
|
||||||
host, _, err = net.SplitHostPort(hostPort)
|
host, _, err = net.SplitHostPort(hostPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() == "missing port in address "+hostPort {
|
porterr := "missing port in address " + hostPort
|
||||||
|
if err.Error() == porterr {
|
||||||
host = hostPort
|
host = hostPort
|
||||||
|
err = nil
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("invalid host '%s': %v", hostPort, err)
|
err = fmt.Errorf("invalid host '%s': %v (<>'%s')", hostPort, err, porterr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,73 @@
|
||||||
package bzz
|
package bzz
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "net/http"
|
// "net/http"
|
||||||
// "strings"
|
// "strings"
|
||||||
// "testing"
|
"testing"
|
||||||
// "time"
|
// "time"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
// "github.com/ethereum/go-ethereum/common/docserver"
|
// "github.com/ethereum/go-ethereum/common/docserver"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
testDir string
|
||||||
|
datadir = "/tmp/bzz"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
_, filename, _, _ := runtime.Caller(1)
|
||||||
|
testDir = path.Join(path.Dir(filename), "bzztest")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testApi() (api *Api, err error) {
|
||||||
|
os.RemoveAll(datadir)
|
||||||
|
api, err = NewLocalApi(datadir)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
api.Start(nil, nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApiPut(t *testing.T) {
|
||||||
|
api, err := testApi()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
expContent := "hello"
|
||||||
|
expMimeType := "text/plain"
|
||||||
|
expStatus := 0
|
||||||
|
expSize := len(expContent)
|
||||||
|
bzzhash, err := api.Put(expContent, expMimeType)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
bytecontent, mimeType, status, size, err := api.Get(bzzhash)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
content := string(bytecontent)
|
||||||
|
if content != expContent {
|
||||||
|
t.Errorf("incorrect content. expected '%s', got '%s'", expContent, content)
|
||||||
|
}
|
||||||
|
if mimeType != expMimeType {
|
||||||
|
t.Errorf("incorrect mimeType. expected '%s', got '%s'", expMimeType, mimeType)
|
||||||
|
}
|
||||||
|
if status != expStatus {
|
||||||
|
t.Errorf("incorrect status. expected '%s', got '%s'", expStatus, status)
|
||||||
|
}
|
||||||
|
if size != expSize {
|
||||||
|
t.Errorf("incorrect size. expected '%d', got '%d'", expSize, size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestApiUpload(t *testing.T) {
|
||||||
|
api, err := testApi()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
_ = api
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue