add api test for Put/Get

This commit is contained in:
zelig 2015-05-31 20:48:16 +01:00
parent fe771132e1
commit e7f2234c7f
2 changed files with 107 additions and 11 deletions

View file

@ -62,6 +62,30 @@ func NewApi(datadir, port string) (self *Api, err error) {
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
func (self *Api) Bzz() (p2p.Protocol, error) {
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) {
self.Chunker.Init()
self.dpa.Start()
self.netStore.start(node, connectPeer)
dpaLogger.Infof("Swarm started.")
go startHttpServer(self, self.Port)
if node != nil && self.netStore != nil && connectPeer != nil {
self.netStore.start(node, connectPeer)
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() {
@ -171,7 +201,7 @@ func (self *Api) Upload(lpath string) (string, error) {
dpaLogger.Debugf("uploading '%s'", localpath)
err := filepath.Walk(localpath, func(path string, info os.FileInfo, err error) error {
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 {
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)))
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)
} else {
err = fmt.Errorf("no registry: %v", err)
@ -266,10 +297,12 @@ func (self *Api) Resolve(hostPort string) (contentHash Key, err error) {
var host string
host, _, err = net.SplitHostPort(hostPort)
if err != nil {
if err.Error() == "missing port in address "+hostPort {
porterr := "missing port in address " + hostPort
if err.Error() == porterr {
host = hostPort
err = nil
} else {
err = fmt.Errorf("invalid host '%s': %v", hostPort, err)
err = fmt.Errorf("invalid host '%s': %v (<>'%s')", hostPort, err, porterr)
return
}
}

View file

@ -1,10 +1,73 @@
package bzz
import (
// "net/http"
// "strings"
// "testing"
// "time"
// "net/http"
// "strings"
"testing"
// "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
}