swarm/network: fix filesystem API - upload/Modify tests

This commit is contained in:
zelig 2016-06-26 12:09:07 +02:00
parent 2371ac7265
commit 58397953bc
4 changed files with 27 additions and 12 deletions

View file

@ -1,7 +1,7 @@
package api
import (
// "bytes"
"io"
"io/ioutil"
"os"
"testing"
@ -51,10 +51,8 @@ func checkResponse(t *testing.T, resp *testResponse, exp *Response) {
resp.Content = string(content)
}
if resp.Content != exp.Content {
// if !bytes.Equal(resp.Content, exp.Content) {
t.Errorf("incorrect content. expected '%s...', got '%s...'", string(exp.Content), string(resp.Content))
}
}
// if !bytes.Equal(resp.Content, exp.Content)
t.Errorf("incorrect content. expected '%s...', got '%s...'", string(exp.Content), string(resp.Con}
// func expResponse(content []byte, mimeType string, status int) *Response {
func expResponse(content string, mimeType string, status int) *Response {
@ -67,7 +65,13 @@ func testGet(t *testing.T, api *Api, bzzhash string) *testResponse {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
return &testResponse{reader, &Response{mimeType, status, reader.Size(), ""}}
s := make([]byte, reader.Size())
_, err = reader.Read(s)
if err != io.EOF {
t.Fatalf("unexpected error: %v", err)
}
return &testResponse{reader, &Response{mimeType, status, reader.Size(), string(s)}}
// return &testResponse{reader, &Response{mimeType, status, reader.Size(), nil}}
}

View file

@ -19,7 +19,7 @@ var (
"CacheCapacity": 5000,
"Radius": 0,
"Branches": 128,
"Hash": "SHA256",
"Hash": "SHA3",
"CallInterval": 3000000000,
"KadDbPath": "` + filepath.Join("TMPDIR", "0d2f62485607cf38d9d795d93682a517661e513e", "bzz-peers.json") + `",
"MaxProx": 8,

View file

@ -1,12 +1,16 @@
package api
import (
"io"
"io/ioutil"
"os"
"path"
"runtime"
"testing"
)
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
)
var (
testDir string
@ -17,8 +21,8 @@ func init() {
_, filename, _, _ := runtime.Caller(1)
testDir = path.Join(path.Dir(filename), "../test")
testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test")
}
glog.SetV(logger.Detail)
glog.SetV = nil
func testFileSystem(t *testing.T, f func(*FileSystem)) {
testApi(t, func(api *Api) {
f(NewFileSystem(api))
@ -29,6 +33,7 @@ func readPath(t *testing.T, parts ...string) string {
// func readPath(t *testing.T, parts ...string) []byte {
file := path.Join(parts...)
content, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("unexpected error reading '%v': %v", file, err)
}
@ -45,13 +50,17 @@ func TestApiDirUpload0(t *testing.T) {
}
content := readPath(t, testDir, "test0", "index.html")
t.Logf("content (%v): %v ", len(content), content)
resp := testGet(t, api, bzzhash+"/index.html")
exp := expResponse(content, "text/html; charset=utf-8", 0)
exp := expRes ponse(content, "text/html; charset=utf-8", 0)
t.Logf("index.html (size%v=?=%v)", resp.Size, exp.Size)
checkResponse(t, resp, exp)
t.FailNow()
content = readPath(t, testDir, "test0", "index.css")
resp = testGet(t, api, bzzhash+"/index.css")
exp = expResponse(content, "text/css", 0)
t.Logf("index.css (size%v=?=%v)", resp.Size, exp.Size)
checkResponse(t, resp, exp)
content = readPath(t, testDir, "test0", "img", "logo.png")
@ -163,7 +172,7 @@ func TestApiFileUploadWithRootFile(t *testing.T) {
testFileSystem(t, func(fs *FileSystem) {
api := fs.api
bzzhash, err := fs.Upload(path.Join(testDir, "test0", "index.html"), "index.html")
if err != nil {
if err != io.EOF {
t.Errorf("unexpected error: %v", err)
return
}

View file

@ -43,6 +43,8 @@ func (self *Storage) Get(bzzpath string) (*Response, error) {
return &Response{mimeType, status, expsize, string(body[:size])}, err
}
// Modify(rootHash, path, contentHash, contentType) takes th e manifest trie rooted in rootHash,
// and merge on to it. creating an entry w conentType (mime)
func (self *Storage) Modify(rootHash, path, contentHash, contentType string) (newRootHash string, err error) {
return self.api.Modify(rootHash+"/"+path, contentHash, contentType, true)
}